parthmax commited on
Commit
e595855
·
verified ·
1 Parent(s): d3cf927

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile
2
+ from fastapi.responses import FileResponse
3
+ import os
4
+ import uuid
5
+
6
+ app = FastAPI()
7
+
8
+ UPLOAD_DIR = "uploads"
9
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
10
+
11
+ @app.post("/upload")
12
+ async def upload(file: UploadFile):
13
+ file_id = str(uuid.uuid4())[:8]
14
+ file_path = f"{UPLOAD_DIR}/{file_id}_{file.filename}"
15
+
16
+ ```
17
+ with open(file_path, "wb") as f:
18
+ f.write(await file.read())
19
+
20
+ return {
21
+ "link": f"/file/{file_id}_{file.filename}"
22
+ }
23
+ ```
24
+
25
+ @app.get("/file/{filename}")
26
+ def get_file(filename: str):
27
+ file_path = f"{UPLOAD_DIR}/{filename}"
28
+ return FileResponse(file_path)