1 Star 0 Fork 0

iStop/difyOSS

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
server.py 2.13 KB
一键复制 编辑 原始数据 按行查看 历史
iStop 提交于 2024-09-26 01:06 +08:00 . demo
# app.py
import os
from fastapi import FastAPI, File, UploadFile, HTTPException, Response,Form
from fastapi.responses import StreamingResponse
import shutil
app = FastAPI()
@app.post("/upload/")
async def upload_file(
file: UploadFile = File(...),
custom_param: str = Form(...)
):
folder = "uploads/"
file_content = await file.read()
filename = os.path.join(folder, file.filename)
os.makedirs(folder, exist_ok=True)
with open(filename, "wb") as f:
f.write(file_content)
return {"filename": file.filename, "custom_param": custom_param, "message": "File uploaded successfully!"}
@app.get("/download/{filename}")
async def download_file(filename: str):
folder = "uploads/"
filepath = os.path.join(folder, filename)
if not os.path.exists(filepath):
raise HTTPException(status_code=404, detail="File not found")
with open(filepath, "rb") as f:
content = f.read()
return Response(content=content, media_type="application/octet-stream")
@app.get("/download/stream/{filename}")
async def download_file_stream(filename: str):
folder = "uploads/"
filepath = os.path.join(folder, filename)
if not os.path.exists(filepath):
raise HTTPException(status_code=404, detail="File not found")
return StreamingResponse(open(filepath, "rb"), media_type="application/octet-stream")
@app.get("/exists/{filename}")
async def file_exists(filename: str):
folder = "uploads/"
filepath = os.path.join(folder, filename)
return {"exists": os.path.exists(filepath)}
@app.delete("/delete/{filename}")
async def delete_file(filename: str):
folder = "uploads/"
filepath = os.path.join(folder, filename)
target_folder = 'deleted'
if os.path.exists(filepath):
os.makedirs(target_folder, exist_ok=True)
target_filepath = os.path.join(target_folder, filename)
shutil.move(filepath, target_filepath)
return {"message": f"File {filename} deleted successfully."}
else:
raise HTTPException(status_code=404, detail="File not found")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/istop/dify-oss.git
git@gitee.com:istop/dify-oss.git
istop
dify-oss
difyOSS
master

搜索帮助