From b558b02711fd80823fe720e76017ce558710f7e3 Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Wed, 11 Sep 2024 04:15:09 +0200 Subject: [PATCH 1/5] [+] FetchDirContent --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index 4640025..ad45b9c 100644 --- a/app.py +++ b/app.py @@ -90,9 +90,9 @@ route_handlers = { 'SetSubdomain': generic_executor.set_subdomain, 'FetchServers': generic_executor.fetch_servers, 'FetchLogs': generic_executor.fetch_logs, - 'FetchFilenames': generic_executor.fetch_players_status, 'FetchHistory': generic_executor.fetch_history, 'FetchPlayersStatus': generic_executor.fetch_players_status, + 'FetchDirContent': generic_executor.fetch_dir_content, 'AccountCreate': generic_executor.account_create, 'AccountDelete': generic_executor.account_delete, 'ServerCreate': generic_executor.server_create, From 6832b052306517a2b196c219e0b8ff2d4ccbb217 Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Wed, 11 Sep 2024 04:15:40 +0200 Subject: [PATCH 2/5] [+] filter_directory_content Allows a user to extract the files having a specific extension --- file_manager.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/file_manager.py b/file_manager.py index f31af0c..9b0f400 100644 --- a/file_manager.py +++ b/file_manager.py @@ -2,8 +2,8 @@ import datetime import json import logging import os -import shutil import re +import shutil supported_versions = ["bukkit", "paper", "spigot"] @@ -102,6 +102,15 @@ def get_path_from_extension(filename) -> str or None: return None +def filter_directory_contents(desired_extension: str, directory_contents: list[str]): + filtered_list = [] + for item in directory_contents: + _, ext = os.path.splitext(item) + if ext.lower() == desired_extension: + filtered_list.append(item) + return filtered_list + + def kebab_to_camel_case(s: str) -> str: parts = s.split('-') return parts[0] + ''.join(part.title() for part in parts[1:]) From 0fc0c1cdd2d72e0f7228404c1c6d748e493ce3a3 Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Wed, 11 Sep 2024 04:15:56 +0200 Subject: [PATCH 3/5] [-] Stopped async testing --- unit_test.py | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/unit_test.py b/unit_test.py index bc91385..7c25236 100644 --- a/unit_test.py +++ b/unit_test.py @@ -1,5 +1,7 @@ import asyncio +import os +import file_manager import firebase_manager @@ -11,34 +13,10 @@ def ban_user(user_id: str): print("Error banning user " + user_id, "|", str(e), type(e).__name__) -async def one() -> int: - return 1 - -async def two() -> int: - await asyncio.sleep(2) - return 2 - -async def three() -> int: - await asyncio.sleep(3) - return 3 - -coroutines = [ - one(), - two(), - three() -] -async def main() -> None: - results = await asyncio.gather(*coroutines) - - #for result in results: - # print(result) - - print(f"0: {results[0]}") - print(f"1: {results[1]}") - print(f"2: {results[2]}") - if __name__ == '__main__': #ban_user("MpkbDMOO8PQddQgB5VgBQdTMWF53") #file_manager.log_action("gqZN3eCHF3V2er3Py3rlgk8u2t83", "test", "DeleteServer") #firebase_manager.set_servers_not_running() - asyncio.run(main()) + current_dir_content: list[str] = os.listdir(".") + parsed_extension: str = '.py' + print(file_manager.filter_directory_contents(parsed_extension, current_dir_content)) From 984a2a9acda856f84291f0245a68205aa249223e Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Wed, 11 Sep 2024 04:16:14 +0200 Subject: [PATCH 4/5] [+] Implemented new FetchDirContent API call :) --- generic_executor.py | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/generic_executor.py b/generic_executor.py index 0cfdb67..80003c3 100644 --- a/generic_executor.py +++ b/generic_executor.py @@ -120,36 +120,25 @@ def fetch_players_status(user: UserRecord, name: str) -> tuple[HTTPStatus, Union file_manager.log_error(type(e).__name__, str(e)) 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 - server_path: str = f"users/{user_id}/{name}" - plugins_dir: str = f"{server_path}/plugins" - datapacks_dir: str = f"{server_path}/world/datapacks" - - file_names = { - "plugins": [], - "datapacks": [] + server_path: str = f"users/{user_id}/{name}/" + dirs: dict[str, [str, str]] = { + 'plugins': ['plugins', '.jar'], + 'datapack' : ['world/datapacks', '.zip'], } - + files: list[dict[str, list[str]]] = [] try: - #plugins - if os.path.exists(plugins_dir): - file_names["plugins"] = os.listdir(plugins_dir) - else: - return HTTPStatus.NOT_FOUND, f"Plugins directory not found for server '{name}'." - - #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 - + for key, (path, extension) in dirs.items(): + final_path = f"{server_path}/{path}" + if os.path.exists(final_path): + parsed_content = file_manager.filter_directory_contents( extension, os.listdir(final_path)) + files.append({key: parsed_content}) + return HTTPStatus.OK, files except Exception as 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]]: From 142dff547f6af93142b442bf7abb1a02e546b72b Mon Sep 17 00:00:00 2001 From: Charles Le Maux Date: Wed, 11 Sep 2024 04:16:26 +0200 Subject: [PATCH 5/5] [+] Local testing successful --- api_sender.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/api_sender.html b/api_sender.html index 8a02693..08d2c91 100644 --- a/api_sender.html +++ b/api_sender.html @@ -81,6 +81,7 @@ +

Update Property

@@ -105,10 +106,6 @@