Factor Studios commited on
Commit
40571d3
·
verified ·
1 Parent(s): abc001f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File, HTTPException
2
+ from fastapi.responses import FileResponse, JSONResponse
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ import os
5
+ import uuid
6
+
7
+ app = FastAPI()
8
+
9
+ # Configure CORS
10
+ app.add_middleware(
11
+ CORSMiddleware,
12
+ allow_origins=["*"], # Allows all origins
13
+ allow_credentials=True,
14
+ allow_methods=["*"], # Allows all methods
15
+ allow_headers=["*"], # Allows all headers
16
+ )
17
+
18
+ UPLOAD_DIRECTORY = "./uploads"
19
+
20
+ os.makedirs(UPLOAD_DIRECTORY, exist_ok=True)
21
+
22
+ @app.post("/upload")
23
+ async def upload_mp4(file: UploadFile = File(...)):
24
+ if not file.filename.endswith(".mp4"):
25
+ raise HTTPException(status_code=400, detail="Invalid file type. Only MP4 files are allowed.")
26
+
27
+ file_id = str(uuid.uuid4()) + ".mp4"
28
+ file_path = os.path.join(UPLOAD_DIRECTORY, file_id)
29
+
30
+ try:
31
+ with open(file_path, "wb") as buffer:
32
+ while True:
33
+ chunk = await file.read(1024 * 1024) # Read in 1MB chunks
34
+ if not chunk:
35
+ break
36
+ buffer.write(chunk)
37
+ return JSONResponse(content={"id": file_id, "message": "File uploaded successfully"}, status_code=200)
38
+ except Exception as e:
39
+ raise HTTPException(status_code=500, detail=f"Failed to upload file: {str(e)}")
40
+
41
+ @app.get("/download/{mp4_id}")
42
+ async def download_mp4(mp4_id: str):
43
+ file_path = os.path.join(UPLOAD_DIRECTORY, mp4_id)
44
+ if not os.path.exists(file_path):
45
+ raise HTTPException(status_code=404, detail="File not found")
46
+ return FileResponse(file_path, media_type="video/mp4", filename=mp4_id)
47
+
48
+ @app.delete("/delete/{mp4_id}")
49
+ async def delete_mp4(mp4_id: str):
50
+ file_path = os.path.join(UPLOAD_DIRECTORY, mp4_id)
51
+ if not os.path.exists(file_path):
52
+ raise HTTPException(status_code=404, detail="File not found")
53
+ try:
54
+ os.remove(file_path)
55
+ return JSONResponse(content={"message": f"File {mp4_id} deleted successfully"}, status_code=200)
56
+ except Exception as e:
57
+ raise HTTPException(status_code=500, detail=f"Failed to delete file: {str(e)}")