Spaces:
Sleeping
Sleeping
Commit
·
0643755
1
Parent(s):
324bb3c
comeon
Browse files
main.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile
|
2 |
from fastapi.responses import HTMLResponse, FileResponse
|
3 |
import shutil
|
4 |
import os
|
|
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
@@ -32,7 +33,7 @@ async def read_root():
|
|
32 |
return html_content.format(file_list=file_list)
|
33 |
|
34 |
@app.post("/uploadfile/")
|
35 |
-
async def upload_file(file: UploadFile = File(...)):
|
36 |
# Save the uploaded file to the uploads directory
|
37 |
upload_directory = "uploads"
|
38 |
os.makedirs(upload_directory, exist_ok=True) # Create the directory if it doesn't exist
|
@@ -41,6 +42,9 @@ async def upload_file(file: UploadFile = File(...)):
|
|
41 |
with open(file_location, "wb") as buffer:
|
42 |
shutil.copyfileobj(file.file, buffer)
|
43 |
|
|
|
|
|
|
|
44 |
return {"filename": file.filename}
|
45 |
|
46 |
def get_uploaded_files():
|
@@ -54,3 +58,9 @@ def get_uploaded_files():
|
|
54 |
@app.get("/files/{filename}")
|
55 |
async def get_file(filename: str):
|
56 |
return FileResponse(f"uploads/{filename}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, BackgroundTasks
|
2 |
from fastapi.responses import HTMLResponse, FileResponse
|
3 |
import shutil
|
4 |
import os
|
5 |
+
import time
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
|
|
33 |
return html_content.format(file_list=file_list)
|
34 |
|
35 |
@app.post("/uploadfile/")
|
36 |
+
async def upload_file(file: UploadFile = File(...), background_tasks: BackgroundTasks):
|
37 |
# Save the uploaded file to the uploads directory
|
38 |
upload_directory = "uploads"
|
39 |
os.makedirs(upload_directory, exist_ok=True) # Create the directory if it doesn't exist
|
|
|
42 |
with open(file_location, "wb") as buffer:
|
43 |
shutil.copyfileobj(file.file, buffer)
|
44 |
|
45 |
+
# Schedule the file deletion after 10 minutes
|
46 |
+
background_tasks.add_task(delete_file_after_delay, file_location, 600) # 600 seconds = 10 minutes
|
47 |
+
|
48 |
return {"filename": file.filename}
|
49 |
|
50 |
def get_uploaded_files():
|
|
|
58 |
@app.get("/files/{filename}")
|
59 |
async def get_file(filename: str):
|
60 |
return FileResponse(f"uploads/{filename}")
|
61 |
+
|
62 |
+
def delete_file_after_delay(file_path: str, delay: int):
|
63 |
+
time.sleep(delay) # Wait for the specified delay
|
64 |
+
if os.path.exists(file_path):
|
65 |
+
os.remove(file_path) # Delete the file
|
66 |
+
print(f"Deleted file: {file_path}") # Log the deletion (optional)
|