From 9fdf8971e3750ab1bc5e909bb422821f3da43226 Mon Sep 17 00:00:00 2001 From: Felipe Cardoso Date: Wed, 12 Mar 2025 21:16:42 +0100 Subject: [PATCH] Add function to relocate theme files in storage Introduce `_relocate_theme_file` to handle moving files to theme-specific directories in the storage system. This ensures better organization of uploaded files by associating them with a theme ID and file type, improving maintainability and structure. --- backend/app/core/storage.py | 2 +- backend/app/utils/files.py | 45 ++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/backend/app/core/storage.py b/backend/app/core/storage.py index eb30cd6..97b842b 100644 --- a/backend/app/core/storage.py +++ b/backend/app/core/storage.py @@ -11,7 +11,7 @@ from app.core.config import settings class StorageProvider(ABC): """Base abstract class for storage providers.""" - + upload_folder: Path @abstractmethod async def save_file(self, file: UploadFile, destination: str) -> str: """Save a file to storage and return the relative path.""" diff --git a/backend/app/utils/files.py b/backend/app/utils/files.py index cba8d4e..970fe9a 100644 --- a/backend/app/utils/files.py +++ b/backend/app/utils/files.py @@ -1,8 +1,10 @@ import os +import shutil import uuid from datetime import datetime from app.core.config import settings +from app.core.storage import StorageProvider def generate_unique_filename(original_filename: str) -> str: @@ -25,4 +27,45 @@ def get_relative_storage_path(folder: str, filename: str) -> str: def validate_image_content_type(content_type: str) -> bool: """Check if the content type is an allowed image type.""" - return content_type in settings.ALLOWED_IMAGE_TYPES \ No newline at end of file + return content_type in settings.ALLOWED_IMAGE_TYPES + +def _relocate_theme_file(theme_id: uuid.UUID, current_url: str, file_type: str, storage: StorageProvider) -> str: + """ + Move an uploaded file to a theme-specific folder. + + Args: + theme_id: The theme ID + current_url: The current file URL + file_type: Type of file (preview, background, etc.) + storage: The storage provider + + Returns: + The new URL if successful, otherwise None + """ + try: + # Extract the filename from the URL + current_file = current_url.split('/files/')[1] if '/files/' in current_url else current_url + filename = os.path.basename(current_file) + + # Define source and destination paths + source_path = os.path.join(storage.upload_folder, current_file) + theme_folder = os.path.join(storage.upload_folder, f'event-themes/{theme_id}/{file_type}') + + # Create the destination folder if it doesn't exist + os.makedirs(theme_folder, exist_ok=True) + + # Define destination path + dest_path = os.path.join(theme_folder, filename) + + # Move the file + if os.path.exists(source_path): + shutil.move(source_path, dest_path) + + # Return the new URL + relative_path = os.path.relpath(dest_path, storage.upload_folder) + return f"/files/{relative_path}" + + return None + except Exception as e: + print(f"Error relocating file: {str(e)}") + return None \ No newline at end of file