curfox_test_api / main.py
Arafath10's picture
Update main.py
7fc821e
raw
history blame
1.14 kB
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import StreamingResponse
import os
import io
app = FastAPI()
# Define the directory to store uploaded files
UPLOAD_DIR = "/code/content/"
@app.on_event("startup")
async def startup_event():
# Check and create the directory if it doesn't exist
os.makedirs(UPLOAD_DIR, exist_ok=True)
@app.get("/")
def read_root():
return {"message": "Hello World"}
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
try:
# Save the file with a specific name
file_path = os.path.join(UPLOAD_DIR, "inputvoice.mp3")
with open(file_path, "wb") as f:
f.write(file.file.read())
# Read the content of the saved file
with open(file_path, "rb") as f:
file_content = f.read()
# Return the content as a streaming response
return StreamingResponse(io.BytesIO(file_content), media_type="audio/mpeg", headers={"Content-Disposition": "inline; filename=inputvoice.mp3"})
except PermissionError as e:
return {"error": f"PermissionError: {str(e)}"}