From d77ba5376e594e2376aa8a8d254c8b64f9f7b716 Mon Sep 17 00:00:00 2001 From: Andrey Blazejuk Date: Sun, 20 Oct 2024 08:07:51 -0300 Subject: [PATCH] Refactor: extract directory creation into helper function --- apps/api/src/services/utils/upload_content.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/apps/api/src/services/utils/upload_content.py b/apps/api/src/services/utils/upload_content.py index d32a787b..b4b1d774 100644 --- a/apps/api/src/services/utils/upload_content.py +++ b/apps/api/src/services/utils/upload_content.py @@ -8,6 +8,11 @@ from fastapi import HTTPException from config.config import get_learnhouse_config +def ensure_directory_exists(directory: str): + if not os.path.exists(directory): + os.makedirs(directory) + + async def upload_content( directory: str, type_of_dir: Literal["orgs", "users"], @@ -32,11 +37,9 @@ async def upload_content( detail=f"File format {file_format} not allowed", ) + ensure_directory_exists(f"content/{type_of_dir}/{uuid}/{directory}") + if content_delivery == "filesystem": - # create folder for activity - if not os.path.exists(f"content/{type_of_dir}/{uuid}/{directory}"): - # create folder for activity - os.makedirs(f"content/{type_of_dir}/{uuid}/{directory}") # upload file to server with open( f"content/{type_of_dir}/{uuid}/{directory}/{file_and_format}", @@ -54,11 +57,6 @@ async def upload_content( endpoint_url=learnhouse_config.hosting_config.content_delivery.s3api.endpoint_url, ) - # Create folder for activity - if not os.path.exists(f"content/{type_of_dir}/{uuid}/{directory}"): - # create folder for activity - os.makedirs(f"content/{type_of_dir}/{uuid}/{directory}") - # Upload file to server with open( f"content/{type_of_dir}/{uuid}/{directory}/{file_and_format}",