From 7adb63faceede216b693bd3607d711a303790a16 Mon Sep 17 00:00:00 2001 From: gilesb Date: Wed, 7 Jan 2026 12:57:09 +0000 Subject: [PATCH] feat: add file upload endpoint for remote cache --- requirements.txt | 1 + server.py | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 6db191d..880bacf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,5 +3,6 @@ redis>=5.0.0 requests>=2.31.0 fastapi>=0.109.0 uvicorn>=0.27.0 +python-multipart>=0.0.6 # Core artdag from GitHub git+https://github.com/gilesbradshaw/art-dag.git diff --git a/server.py b/server.py index febce99..b9ac13d 100644 --- a/server.py +++ b/server.py @@ -16,7 +16,7 @@ from datetime import datetime, timezone from pathlib import Path from typing import Optional -from fastapi import FastAPI, HTTPException +from fastapi import FastAPI, HTTPException, UploadFile, File from fastapi.responses import FileResponse from pydantic import BaseModel @@ -232,6 +232,29 @@ async def import_to_cache(path: str): 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__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8100)