diff --git a/Dockerfile.fastapi b/Dockerfile.fastapi index f435100..59bc6c1 100644 --- a/Dockerfile.fastapi +++ b/Dockerfile.fastapi @@ -15,6 +15,9 @@ ENV PORT=8000 ENV HOST=0.0.0.0 ENV DOWNLOAD_FOLDER=/tmp/downloads +# Ammount (in secs) before file autodeletes +ENV FILE_AUTODELETE_DELAY_SECS=300 + VOLUME ["/tmp/downloads"] CMD ["python3", "main.py"] diff --git a/server/py/fastapi_server/main.py b/server/py/fastapi_server/main.py index 3b9e358..3ac499d 100644 --- a/server/py/fastapi_server/main.py +++ b/server/py/fastapi_server/main.py @@ -112,10 +112,17 @@ def set_metadata_and_rename(mp3_file: str, name: str, artist: str = None, album: DOWNLOAD_FOLDER = os.environ.get("DOWNLOAD_FOLDER","downloads") # Where the converted files will be saved if not os.path.exists(DOWNLOAD_FOLDER): os.makedirs(DOWNLOAD_FOLDER) - -async def delay_delete_file(delay:float, file:str): - await asyncio.sleep(delay) - if os.path.exists(file): os.unlink(file) + +# Throw error if the env is incorrect +try: + AUTODELETE_DELAY_SECS = int(os.environ.get("FILE_AUTODELETE_DELAY_SECS", 0)) +except ValueError: + raise EnvironmentError("You must set AUTODELETE_DELAY_SECS to a valid number (int) of seconds! To disable auto delete (not recommended), set it to 0") + +async def delay_delete_file(file:str): + if (AUTODELETE_DELAY_SECS > 0): + await asyncio.sleep(AUTODELETE_DELAY_SECS) + if os.path.exists(file): os.unlink(file) @app.post("/convert") async def create_conversion(req: Request): @@ -138,7 +145,7 @@ async def create_conversion(req: Request): file_name = final_file.split("/")[-1] print("File served: " + final_file) - asyncio.create_task(delay_delete_file(300, final_file)) + asyncio.create_task(delay_delete_file(final_file)) return { "file" : file_name }