This commit is contained in:
2025-10-17 10:33:11 +08:00
commit ad9924cce2
3 changed files with 61 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
__pycache__
.venv
venv
.vscode
.idea
upload

49
main.py Normal file
View File

@@ -0,0 +1,49 @@
from pathlib import Path
from loguru import logger
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import FileResponse
import aiofiles
def mk_upload_dir():
import os
os.makedirs("upload", exist_ok=True)
mk_upload_dir()
app = FastAPI()
@app.get("/{file:path}")
def download(file: str) -> FileResponse:
file = "/"+file
logger.info(f"Downloading {file}")
file_path = Path("upload") / file
if not file_path.exists():
raise HTTPException(status_code=404, detail="File not found")
return FileResponse(path=file, filename=file)
@app.post("/{file:path}")
async def upload(file: str, request: Request) -> dict:
logger.info(f"Uploading {file}")
# 确保目录存在
full_path = Path("upload") / file
full_path.parent.mkdir(parents=True, exist_ok=True)
# 流式写入文件
total_size = 0
async with aiofiles.open(full_path, 'wb') as f:
async for chunk in request.stream():
await f.write(chunk)
total_size += len(chunk)
return {
"status": "success",
"file": file,
"size": total_size,
"path": full_path
}

4
requirements.txt Normal file
View File

@@ -0,0 +1,4 @@
fastapi
uvicorn[standard]
loguru
aiofiles