Added functionality to upload files and process them using a Celery task that deletes temporary files after processing.

This commit is contained in:
Timothy Farrell 2024-07-15 11:24:11 -05:00 committed by Timothy Farrell (aider)
parent 80cab76f4a
commit 5268696f39

21
app.py
View File

@ -1,9 +1,28 @@
from fastapi import FastAPI, File, UploadFile
import tempfile
import os
# Assuming Celery is already set up and imported correctly in the project.
from celery import shared_task # Import your Celery instance here.
app = FastAPI()
@app.post("/uploadfile/")
async def upload_file(file: UploadFile = File(...)):
contents = await file.read()
# Here you can process the WAV file data stored in 'contents'
# Save the file to a temporary directory
temp_dir = tempfile.gettempdir()
temp_file_path = os.path.join(temp_dir, file.filename)
with open(temp_file_path, 'wb') as f:
f.write(contents)
# Call a Celery task that deletes the file after processing.
delete_temp_file.delay(temp_file_path) # Assuming this is your Celery task name.
return {"filename": file.filename}
# Define the Celery task to delete the temporary file
@shared_task
def delete_temp_file(file_path):
os.remove(file_path)