[+] Implemented new FetchDirContent API call :)

This commit is contained in:
Charles Le Maux 2024-09-11 04:16:14 +02:00
parent 0fc0c1cdd2
commit 984a2a9acd

View File

@ -120,36 +120,25 @@ def fetch_players_status(user: UserRecord, name: str) -> tuple[HTTPStatus, Union
file_manager.log_error(type(e).__name__, str(e)) file_manager.log_error(type(e).__name__, str(e))
return HTTPStatus.INTERNAL_SERVER_ERROR, "Unknown error." return HTTPStatus.INTERNAL_SERVER_ERROR, "Unknown error."
def fetch_file_names(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[dict[str, list[str]], str]]:
def fetch_dir_content(user: UserRecord, name: str) -> tuple[HTTPStatus, Union[str, list]]:
user_id: str = user.uid user_id: str = user.uid
server_path: str = f"users/{user_id}/{name}" server_path: str = f"users/{user_id}/{name}/"
plugins_dir: str = f"{server_path}/plugins" dirs: dict[str, [str, str]] = {
datapacks_dir: str = f"{server_path}/world/datapacks" 'plugins': ['plugins', '.jar'],
'datapack' : ['world/datapacks', '.zip'],
file_names = {
"plugins": [],
"datapacks": []
} }
files: list[dict[str, list[str]]] = []
try: try:
#plugins for key, (path, extension) in dirs.items():
if os.path.exists(plugins_dir): final_path = f"{server_path}/{path}"
file_names["plugins"] = os.listdir(plugins_dir) if os.path.exists(final_path):
else: parsed_content = file_manager.filter_directory_contents( extension, os.listdir(final_path))
return HTTPStatus.NOT_FOUND, f"Plugins directory not found for server '{name}'." files.append({key: parsed_content})
return HTTPStatus.OK, files
#datapacks
if os.path.exists(datapacks_dir):
file_names["datapacks"] = os.listdir(datapacks_dir)
else:
return HTTPStatus.NOT_FOUND, f"Datapacks directory not found for server '{name}'."
return HTTPStatus.OK, file_names
except Exception as e: except Exception as e:
file_manager.log_error(type(e).__name__, str(e)) file_manager.log_error(type(e).__name__, str(e))
return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error fetching file names: {str(e)}" return HTTPStatus.INTERNAL_SERVER_ERROR, f"Error fetching files for server {name}"
def account_create(user: UserRecord) -> tuple[HTTPStatus, Union[str, None]]: def account_create(user: UserRecord) -> tuple[HTTPStatus, Union[str, None]]: