Merge pull request #358 from ablazejuk/patch-1

Refactor: extract directory creation into helper function
This commit is contained in:
Badr B. 2024-10-20 15:37:01 +02:00 committed by GitHub
commit aaffdee935
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,6 +8,11 @@ from fastapi import HTTPException
from config.config import get_learnhouse_config 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( async def upload_content(
directory: str, directory: str,
type_of_dir: Literal["orgs", "users"], type_of_dir: Literal["orgs", "users"],
@ -32,11 +37,9 @@ async def upload_content(
detail=f"File format {file_format} not allowed", detail=f"File format {file_format} not allowed",
) )
ensure_directory_exists(f"content/{type_of_dir}/{uuid}/{directory}")
if content_delivery == "filesystem": 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 # upload file to server
with open( with open(
f"content/{type_of_dir}/{uuid}/{directory}/{file_and_format}", 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, 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 # Upload file to server
with open( with open(
f"content/{type_of_dir}/{uuid}/{directory}/{file_and_format}", f"content/{type_of_dir}/{uuid}/{directory}/{file_and_format}",