From e526f691ef883b6df150126c727f3abe7fc37993 Mon Sep 17 00:00:00 2001 From: Christian Krakau-Louis Date: Mon, 24 Mar 2025 16:52:47 +0100 Subject: [PATCH] added first draft of upload interface --- app/frontend.py | 19 ++++++++++ app/main.py | 3 ++ app/routes_ui.py | 26 +++++++++++++ frontend/index.html | 89 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 app/frontend.py create mode 100644 app/routes_ui.py create mode 100644 frontend/index.html diff --git a/app/frontend.py b/app/frontend.py new file mode 100644 index 00000000..8fef9bd7 --- /dev/null +++ b/app/frontend.py @@ -0,0 +1,19 @@ +# app/frontend.py (new file or inline in main.py) +from fastapi import APIRouter +from fastapi.responses import FileResponse +from fastapi.staticfiles import StaticFiles +import os + +router = APIRouter() + +# 1) Serve the folder that contains index.html, etc. +# e.g. "frontend" is relative to your project root +frontend_folder = os.path.join(os.path.dirname(__file__), "..", "frontend") + +# If you just want to serve the entire folder as static: +router.mount("/static", StaticFiles(directory=frontend_folder), name="static") + +# 2) For the root route ("/"), return the index.html +@router.get("/ui", response_class=FileResponse) +def serve_ui(): + return os.path.join(frontend_folder, "index.html") diff --git a/app/main.py b/app/main.py index 4a236c5c..9739c1f5 100644 --- a/app/main.py +++ b/app/main.py @@ -8,6 +8,7 @@ from app.tasks.upload_to_dropbox import upload_to_dropbox from app.tasks.upload_to_paperless import upload_to_paperless from app.tasks.upload_to_nextcloud import upload_to_nextcloud from app.tasks.send_to_all import send_to_all_destinations +from app.frontend import router as frontend_router app = FastAPI(title="Document Processing API") @@ -111,3 +112,5 @@ def process_all_pdfs_in_workdir(): "pdf_files": pdf_files, "task_ids": task_ids } + +app.include_router(frontend_router) \ No newline at end of file diff --git a/app/routes_ui.py b/app/routes_ui.py new file mode 100644 index 00000000..2ddd949d --- /dev/null +++ b/app/routes_ui.py @@ -0,0 +1,26 @@ +# In main.py or a new dedicated file, e.g. app/routes_ui.py +from fastapi import APIRouter, UploadFile, File, HTTPException +from app.tasks.upload_to_s3 import upload_to_s3 +import os + +router = APIRouter() + +@router.post("/ui-upload") +async def ui_upload(file: UploadFile = File(...)): + # You can store this file in your 'workdir' (like how /process does) or a tmp dir + workdir = "/workdir" + target_path = os.path.join(workdir, file.filename) + + try: + with open(target_path, "wb") as f: + content = await file.read() + f.write(content) + except Exception as e: + raise HTTPException(status_code=500, detail=f"Failed to save file: {e}") + + # Now you can call your existing Celery flow: + task = upload_to_s3.delay(target_path) + return {"task_id": task.id, "status": "queued"} + +# Then include this in your main app as well: +# app.include_router(router) diff --git a/frontend/index.html b/frontend/index.html new file mode 100644 index 00000000..3140069e --- /dev/null +++ b/frontend/index.html @@ -0,0 +1,89 @@ + + + + + + Document Processor - Upload + + + + + +

Upload a File

+ +
+

+ Drag & drop a file here, or click to select a file. +

+ +
+ +
+ + + +