From e228a2e59c13e4a24c8482df7546d950ca479dc7 Mon Sep 17 00:00:00 2001 From: retoor Date: Sun, 9 Nov 2025 00:27:13 +0100 Subject: [PATCH] Update. --- requirements.txt | 1 + retoors/services/config_service.py | 7 +++++-- retoors/services/file_service.py | 11 +++++++---- retoors/services/user_service.py | 7 +++++-- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/requirements.txt b/requirements.txt index 8eaecc6..f057cfc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,4 @@ pytest pytest-aiohttp aiohttp-test-utils pytest-mock +pillow diff --git a/retoors/services/config_service.py b/retoors/services/config_service.py index 5eea3a2..6e0466d 100644 --- a/retoors/services/config_service.py +++ b/retoors/services/config_service.py @@ -12,8 +12,11 @@ class ConfigService: def _load_config(self): config_from_file = {} if self._config_path.exists(): - with open(self._config_path, "r") as f: - config_from_file = json.load(f) + try: + with open(self._config_path, "r") as f: + config_from_file = json.load(f) + except json.JSONDecodeError: + config_from_file = {} # Override with environment variables config_from_env = { diff --git a/retoors/services/file_service.py b/retoors/services/file_service.py index 217e225..c1b6e1b 100644 --- a/retoors/services/file_service.py +++ b/retoors/services/file_service.py @@ -14,10 +14,13 @@ class FileService: async def _load_users_data(self): """Loads user data from the JSON file.""" if not self.users_data_path.exists(): - return {} + return [] async with aiofiles.open(self.users_data_path, mode="r") as f: content = await f.read() - return json.loads(content) if content else {} + try: + return json.loads(content) if content else [] + except json.JSONDecodeError: + return [] async def _save_users_data(self, data): """Saves user data to the JSON file.""" @@ -89,7 +92,7 @@ class FileService: async def generate_share_link(self, user_email: str, item_path: str) -> str | None: """Generates a shareable link for a file or folder.""" users_data = await self._load_users_data() - user = users_data.get(user_email) + user = next((u for u in users_data if u.get("email") == user_email), None) if not user: return None @@ -112,7 +115,7 @@ class FileService: async def get_shared_item(self, share_id: str) -> dict | None: """Retrieves information about a shared item.""" users_data = await self._load_users_data() - for user_email, user_info in users_data.items(): + for user_info in users_data: if "shared_items" in user_info and share_id in user_info["shared_items"]: shared_item = user_info["shared_items"][share_id] expiry_time = datetime.datetime.fromisoformat(shared_item["expires_at"]) diff --git a/retoors/services/user_service.py b/retoors/services/user_service.py index efae4d9..9924b8f 100644 --- a/retoors/services/user_service.py +++ b/retoors/services/user_service.py @@ -14,8 +14,11 @@ class UserService: def _load_users(self) -> List[Dict[str, Any]]: if not self._users_path.exists(): return [] - with open(self._users_path, "r") as f: - return json.load(f) + try: + with open(self._users_path, "r") as f: + return json.load(f) + except json.JSONDecodeError: + return [] def _save_users(self): with open(self._users_path, "w") as f: