file autodele on
This commit is contained in:
@@ -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"]
|
||||
|
||||
@@ -113,8 +113,15 @@ DOWNLOAD_FOLDER = os.environ.get("DOWNLOAD_FOLDER","downloads") # Where the con
|
||||
if not os.path.exists(DOWNLOAD_FOLDER):
|
||||
os.makedirs(DOWNLOAD_FOLDER)
|
||||
|
||||
async def delay_delete_file(delay:float, file:str):
|
||||
await asyncio.sleep(delay)
|
||||
# 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")
|
||||
@@ -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 }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user