-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
61 lines (53 loc) · 1.91 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from fastapi import FastAPI, File, UploadFile, Request, HTTPException
from starlette.responses import FileResponse, RedirectResponse
from fastapi.templating import Jinja2Templates
import os
import shutil
import socket
import uvicorn
app = FastAPI()
templates = Jinja2Templates(directory="templates")
@app.get("/")
async def home(request: Request):
files = os.listdir("uploads/")
return templates.TemplateResponse("index.html", {"request": request, "files": files})
@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
file_path = f"uploads/{file.filename}"
with open(file_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
return RedirectResponse(url="/", status_code=303)
@app.get("/downloadfile/{filename}")
async def download_file(filename: str):
file_path = f"uploads/{filename}"
if os.path.exists(file_path):
return FileResponse(file_path)
else:
raise HTTPException(status_code=404, detail="File not found")
@app.post("/deletefile/{filename}")
async def delete_file(filename: str):
file_path = f"uploads/{filename}"
if os.path.exists(file_path):
os.remove(file_path)
return RedirectResponse(url="/", status_code=303)
else:
raise HTTPException(status_code=404, detail="File not found")
def get_local_ip():
# This function retrieves the local IP address
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't have to be reachable
s.connect(('10.254.254.254', 1))
local_ip = s.getsockname()[0]
except Exception:
local_ip = '127.0.0.1'
finally:
s.close()
return local_ip
def create_app():
os.makedirs("uploads", exist_ok=True)
local_ip = get_local_ip()
print(f"Server is running on http://{local_ip}:8000")
return app
if __name__ == '__main__':
uvicorn.run("app:create_app", host="0.0.0.0", port=8000, reload=True)