Refactored some stuff

This commit is contained in:
2023-01-19 13:47:18 +01:00
parent aa8be6006c
commit 08d060e160
3 changed files with 23 additions and 64 deletions

View File

@@ -1,6 +1,7 @@
from os import makedirs, path
from typing import Any, Tuple, Union
from uuid import uuid4
from zipfile import ZIP_DEFLATED, ZipFile
from ujson import loads, dumps, JSONDecodeError
from traceback import print_exc
@@ -62,20 +63,24 @@ def configGet(key: str, *args: str) -> Any:
this_key = this_key[dict_key]
return this_key[key]
def saveFile(filebytes: bytes, filename: Union[str, None] = None, dirname: Union[str, None] = None) -> Tuple[str, str]:
"""Save some bytedata into random file and return its ID
def zip_saves(save_filename: str, save_bytes: bytes, saveinfo_bytes: bytes) -> Tuple[str, str]:
"""Save files of the SV save into archive and return uuid and path
### Args:
* filebytes (`bytes`): Bytes to write into file
* save_filename (`str`): Filename of the save file
vsave_bytes (`bytes`): Bytes of the save file
* saveinfo_bytes (`bytes`): Bytes of the save info file
### Returns:
* `Tuple[str, str]`: Tuple where first item is an ID and the second is an absolute path to file
* `Tuple[str, str]`: First element is an UUID and the second is a filepath
"""
pathlist = [configGet("data", "locations"), "files"]
if dirname is not None:
pathlist.append(dirname)
makedirs(path.join(pathlist), exist_ok=True)
filename = str(uuid4()) if filename is None else filename
with open(path.join(pathlist+[filename], "wb")) as file:
file.write(filebytes)
return filename, path.join(pathlist+[filename])
save_uuid = str(uuid4())
makedirs(path.join(configGet("data", "locations"), "files", save_uuid))
with ZipFile(path.join(configGet("data", "locations"), "files", save_uuid, save_filename+".svsave"), 'w', ZIP_DEFLATED, compresslevel=configGet("compression")) as ziph:
ziph.writestr("SaveGameInfo", saveinfo_bytes)
ziph.writestr(save_filename, save_bytes)
return save_uuid, path.join(configGet("data", "locations"), "files", save_uuid, save_filename+".svsave")