feat: add file upload endpoint for remote cache
This commit is contained in:
@@ -3,5 +3,6 @@ redis>=5.0.0
|
|||||||
requests>=2.31.0
|
requests>=2.31.0
|
||||||
fastapi>=0.109.0
|
fastapi>=0.109.0
|
||||||
uvicorn>=0.27.0
|
uvicorn>=0.27.0
|
||||||
|
python-multipart>=0.0.6
|
||||||
# Core artdag from GitHub
|
# Core artdag from GitHub
|
||||||
git+https://github.com/gilesbradshaw/art-dag.git
|
git+https://github.com/gilesbradshaw/art-dag.git
|
||||||
|
|||||||
25
server.py
25
server.py
@@ -16,7 +16,7 @@ from datetime import datetime, timezone
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from fastapi import FastAPI, HTTPException
|
from fastapi import FastAPI, HTTPException, UploadFile, File
|
||||||
from fastapi.responses import FileResponse
|
from fastapi.responses import FileResponse
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
@@ -232,6 +232,29 @@ async def import_to_cache(path: str):
|
|||||||
return {"content_hash": content_hash, "cached": True}
|
return {"content_hash": content_hash, "cached": True}
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/cache/upload")
|
||||||
|
async def upload_to_cache(file: UploadFile = File(...)):
|
||||||
|
"""Upload a file to cache."""
|
||||||
|
# Write to temp file first
|
||||||
|
import tempfile
|
||||||
|
with tempfile.NamedTemporaryFile(delete=False) as tmp:
|
||||||
|
content = await file.read()
|
||||||
|
tmp.write(content)
|
||||||
|
tmp_path = Path(tmp.name)
|
||||||
|
|
||||||
|
# Hash and move to cache
|
||||||
|
content_hash = file_hash(tmp_path)
|
||||||
|
cache_path = CACHE_DIR / content_hash
|
||||||
|
|
||||||
|
if not cache_path.exists():
|
||||||
|
import shutil
|
||||||
|
shutil.move(str(tmp_path), cache_path)
|
||||||
|
else:
|
||||||
|
tmp_path.unlink()
|
||||||
|
|
||||||
|
return {"content_hash": content_hash, "filename": file.filename, "size": len(content)}
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import uvicorn
|
import uvicorn
|
||||||
uvicorn.run(app, host="0.0.0.0", port=8100)
|
uvicorn.run(app, host="0.0.0.0", port=8100)
|
||||||
|
|||||||
Reference in New Issue
Block a user