Skip to content

Commit

Permalink
fix data backup
Browse files Browse the repository at this point in the history
  • Loading branch information
greeeen-dev committed Dec 6, 2024
1 parent 745eece commit b7f7974
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
26 changes: 18 additions & 8 deletions unifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""

import nextcord
import ujson
from nextcord import Interaction, ApplicationError
from nextcord.ext import commands
import aiohttp
Expand Down Expand Up @@ -245,17 +246,22 @@ def __init__(self, *args, **kwargs):
self.__encrypted = kwargs.get('encrypt', False)

# Ensure necessary keys exist
self.update({'rooms': {}, 'emojis': [], 'nicknames': {}, 'blocked': {}, 'banned': {},
'moderators': [], 'avatars': {}, 'experiments': {}, 'experiments_info': {}, 'colors': {},
'external_bridge': [], 'modlogs': {}, 'trusted': [], 'report_threads': {}, 'fullbanned': [],
'exp': {}, 'appealban': [], 'languages': {}, 'settings': {}, 'invites': {}, 'underattack': [],
'rooms_count': {}, 'connections_count': {}, 'allocations_override': {}, 'filters': {}}
)
self.update(self.base)
self.threads = []

# Load data
self.load_data()

@property
def base(self):
return {
'rooms': {}, 'emojis': [], 'nicknames': {}, 'blocked': {}, 'banned': {},
'moderators': [], 'avatars': {}, 'experiments': {}, 'experiments_info': {}, 'colors': {},
'external_bridge': [], 'modlogs': {}, 'trusted': [], 'report_threads': {}, 'fullbanned': [],
'exp': {}, 'appealban': [], 'languages': {}, 'settings': {}, 'invites': {}, 'underattack': [],
'rooms_count': {}, 'connections_count': {}, 'allocations_override': {}, 'filters': {}
}

@property
def save_lock(self):
return self.__save_lock
Expand All @@ -281,20 +287,24 @@ def load_data(self):
self.update(data)
except FileNotFoundError:
pass # If the file is not found, initialize an empty dictionary
except json.JSONDecodeError:
pass # If the file is corrupted, initialize an empty dictionary

def save(self):
tosave = {}
for key in self.keys():
if not key in self.base.keys():
continue
tosave.update({key: self[key]})
if self.__save_lock:
return
if self.__encrypted:
data = jsontools.dumps_bytes(tosave)
data = jsontools.dumps_bytes(dict(tosave))
self.__secure_storage.save(data, self.file_path)
else:
with open(self.file_path, 'w') as file:
# noinspection PyTypeChecker
json.dump(tosave, file, indent=4)
json.dump(dict(tosave), file, indent=4)

return

Expand Down
5 changes: 4 additions & 1 deletion utils/compressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ def compress(data: bytes, filename: Optional[str], chunk_size: int, level: int =
with compressor.stream_writer(target) as f:
for i in range(0, len(data), chunk_size):
f.write(data[i:i + chunk_size])
f.flush()
target.seek(0)
result = target.read()

# return bytes if filename is None
if not filename:
return target.getvalue()
return result

def decompress(file: Union[bytes, str], chunk_size: int) -> bytes:
"""Decompresses a file compressed using Zstandard."""
Expand Down
4 changes: 2 additions & 2 deletions utils/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def __init__(self, password):
def encrypt(self, data):
data, tag, nonce, salt = self.__encryptor.encrypt(data, self.__password)
return {
'data': data,
'data': base64.b64encode(data).decode('ascii'),
'tag': tag,
'nonce': nonce,
'salt': salt
Expand Down Expand Up @@ -469,4 +469,4 @@ def load(self, filename):
with open(filename, 'r') as file:
data = json.load(file)

return self.__rawencryptor.decrypt(data['data'], data['tag'], data['nonce'], data['salt'])
return self.__rawencryptor.decrypt(base64.b64decode(data['data']), data['tag'], data['nonce'], data['salt'])

0 comments on commit b7f7974

Please sign in to comment.