file autodele on

This commit is contained in:
suricatingss
2026-01-19 01:55:23 +00:00
parent 9a842cd756
commit b3dcd3058a
2 changed files with 15 additions and 5 deletions

View File

@@ -15,6 +15,9 @@ ENV PORT=8000
ENV HOST=0.0.0.0 ENV HOST=0.0.0.0
ENV DOWNLOAD_FOLDER=/tmp/downloads ENV DOWNLOAD_FOLDER=/tmp/downloads
# Ammount (in secs) before file autodeletes
ENV FILE_AUTODELETE_DELAY_SECS=300
VOLUME ["/tmp/downloads"] VOLUME ["/tmp/downloads"]
CMD ["python3", "main.py"] CMD ["python3", "main.py"]

View File

@@ -113,9 +113,16 @@ DOWNLOAD_FOLDER = os.environ.get("DOWNLOAD_FOLDER","downloads") # Where the con
if not os.path.exists(DOWNLOAD_FOLDER): if not os.path.exists(DOWNLOAD_FOLDER):
os.makedirs(DOWNLOAD_FOLDER) os.makedirs(DOWNLOAD_FOLDER)
async def delay_delete_file(delay:float, file:str): # Throw error if the env is incorrect
await asyncio.sleep(delay) try:
if os.path.exists(file): os.unlink(file) 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") @app.post("/convert")
async def create_conversion(req: Request): async def create_conversion(req: Request):
@@ -138,7 +145,7 @@ async def create_conversion(req: Request):
file_name = final_file.split("/")[-1] file_name = final_file.split("/")[-1]
print("File served: " + final_file) 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 } return { "file" : file_name }