mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
Improve unzip_file function. Reduce cognitive complexity.
Removed all unspecified exceptions. Removed unneeded calls to other functions. Reduced size of try catch blocks to just parts of code that could throw an error.
This commit is contained in:
parent
890e84cd5e
commit
bd021e0d43
@ -13,6 +13,7 @@ import urllib.request
|
|||||||
import ssl
|
import ssl
|
||||||
import time
|
import time
|
||||||
import certifi
|
import certifi
|
||||||
|
from jsonschema.exceptions import ValidationError
|
||||||
|
|
||||||
from app.classes.helpers.helpers import Helpers
|
from app.classes.helpers.helpers import Helpers
|
||||||
from app.classes.shared.console import Console
|
from app.classes.shared.console import Console
|
||||||
@ -394,12 +395,26 @@ class FileHelpers:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def unzip_file(zip_path, server_update=False):
|
def unzip_file(zip_path, server_update: bool = False) -> None:
|
||||||
ignored_names = ["server.properties", "permissions.json", "allowlist.json"]
|
"""
|
||||||
|
Unzips zip file at zip_path to location generated at new_dir based on zip
|
||||||
|
contents.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
zip_path: Path to zip file to unzip.
|
||||||
|
server_update: Will skip ignored items list if not set to true. Used for
|
||||||
|
updating bedrock servers.
|
||||||
|
|
||||||
|
Returns: None
|
||||||
|
|
||||||
|
"""
|
||||||
|
ignored_names: list = [
|
||||||
|
"server.properties",
|
||||||
|
"permissions.json",
|
||||||
|
"allowlist.json",
|
||||||
|
]
|
||||||
# Get directory without zipfile name
|
# Get directory without zipfile name
|
||||||
new_dir = pathlib.Path(zip_path).parents[0]
|
new_dir = pathlib.Path(zip_path).parents[0]
|
||||||
# make sure we're able to access the zip file
|
|
||||||
if Helpers.check_file_perms(zip_path) and os.path.isfile(zip_path):
|
|
||||||
# make sure the directory we're unzipping this to exists
|
# make sure the directory we're unzipping this to exists
|
||||||
Helpers.ensure_dir_exists(new_dir)
|
Helpers.ensure_dir_exists(new_dir)
|
||||||
# we'll make a temporary directory to unzip this to.
|
# we'll make a temporary directory to unzip this to.
|
||||||
@ -408,12 +423,24 @@ class FileHelpers:
|
|||||||
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
||||||
# we'll extract this to the temp dir using zipfile module
|
# we'll extract this to the temp dir using zipfile module
|
||||||
zip_ref.extractall(temp_dir)
|
zip_ref.extractall(temp_dir)
|
||||||
|
# Catch zipfile extract all error or file open errors.
|
||||||
|
except ValueError as why:
|
||||||
|
Console.error(f"Unzip failed with information: {why}")
|
||||||
|
raise RuntimeError(f"Unzip failed for path: {zip_path}") from why
|
||||||
|
except FileNotFoundError as why:
|
||||||
|
Console.error(f"Unzip failed file not found: {zip_path}")
|
||||||
|
raise FileNotFoundError(f"Unable to find file at path: {zip_path}") from why
|
||||||
|
except PermissionError as why:
|
||||||
|
Console.error(f"Bad permissions for file at: {zip_path}")
|
||||||
|
raise PermissionError(f"Bad permissions for file at: {zip_path}") from why
|
||||||
|
|
||||||
# we'll iterate through the top level directory moving everything
|
# we'll iterate through the top level directory moving everything
|
||||||
# out of the temp directory and into it's final home.
|
# out of the temp directory and into it's final home.
|
||||||
for item in os.listdir(temp_dir):
|
for item in os.listdir(temp_dir):
|
||||||
# if the file is one of our ignored names we'll skip it
|
# if the file is one of our ignored names we'll skip it
|
||||||
if item in ignored_names and server_update:
|
if item in ignored_names and server_update:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# we handle files and dirs differently or we'll crash out.
|
# we handle files and dirs differently or we'll crash out.
|
||||||
if os.path.isdir(os.path.join(temp_dir, item)):
|
if os.path.isdir(os.path.join(temp_dir, item)):
|
||||||
try:
|
try:
|
||||||
@ -421,7 +448,7 @@ class FileHelpers:
|
|||||||
os.path.join(temp_dir, item),
|
os.path.join(temp_dir, item),
|
||||||
os.path.join(new_dir, item),
|
os.path.join(new_dir, item),
|
||||||
)
|
)
|
||||||
except Exception as ex:
|
except shutil.Error as ex:
|
||||||
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
|
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
@ -429,13 +456,8 @@ class FileHelpers:
|
|||||||
os.path.join(temp_dir, item),
|
os.path.join(temp_dir, item),
|
||||||
os.path.join(new_dir, item),
|
os.path.join(new_dir, item),
|
||||||
)
|
)
|
||||||
except Exception as ex:
|
except shutil.Error as ex:
|
||||||
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
|
logger.error(f"ERROR IN ZIP IMPORT: {ex}")
|
||||||
except Exception as ex:
|
|
||||||
Console.error(ex)
|
|
||||||
else:
|
|
||||||
return "false"
|
|
||||||
return
|
|
||||||
|
|
||||||
def unzip_server(self, zip_path, user_id):
|
def unzip_server(self, zip_path, user_id):
|
||||||
if Helpers.check_file_perms(zip_path):
|
if Helpers.check_file_perms(zip_path):
|
||||||
|
@ -565,7 +565,7 @@ class ApiServersServerFilesZipHandler(BaseApiHandler):
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
if Helpers.check_file_exists(folder):
|
if Helpers.check_file_exists(folder):
|
||||||
folder = self.file_helper.unzip_file(folder, user_id)
|
self.file_helper.unzip_file(folder, user_id)
|
||||||
else:
|
else:
|
||||||
if user_id:
|
if user_id:
|
||||||
return self.finish_json(
|
return self.finish_json(
|
||||||
|
Loading…
Reference in New Issue
Block a user