Avinashnalla7 commited on
Commit
8974c6e
·
1 Parent(s): c509456

Restore POST /api/pdf/{pdf_id} upload endpoint

Browse files
Files changed (1) hide show
  1. backend/api.py +17 -1
backend/api.py CHANGED
@@ -14,7 +14,7 @@ from pathlib import Path
14
  from typing import Any, Dict
15
 
16
  from dotenv import load_dotenv
17
- from fastapi import FastAPI, HTTPException, Request, UploadFile, File, Form
18
 
19
  def _env(*keys: str, default: str = "") -> str:
20
  import os
@@ -96,6 +96,22 @@ def _get_env_required(key: str) -> str:
96
  def health():
97
  return {"ok": True}
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  @app.get("/api/pdf/{pdf_id}")
100
  def get_pdf(pdf_id: str):
101
  pdf_path = _pdf_path(pdf_id)
 
14
  from typing import Any, Dict
15
 
16
  from dotenv import load_dotenv
17
+ from fastapi import FastAPI, HTTPException, Request, UploadFile, File, Form, UploadFile, File, Form
18
 
19
  def _env(*keys: str, default: str = "") -> str:
20
  import os
 
96
  def health():
97
  return {"ok": True}
98
 
99
+ @app.post("/api/pdf/{pdf_id}")
100
+ async def put_pdf(pdf_id: str, file: UploadFile = File(...), pdf_name: str = Form("")):
101
+ base = Path("/data/uploads")
102
+ pdf_dir = base / "pdfs"
103
+ pdf_dir.mkdir(parents=True, exist_ok=True)
104
+
105
+ pdf_path = pdf_dir / f"{pdf_id}.pdf"
106
+ name_path = pdf_dir / f"{pdf_id}.name.txt"
107
+
108
+ data = await file.read()
109
+ pdf_path.write_bytes(data)
110
+ if pdf_name:
111
+ name_path.write_text(pdf_name, encoding="utf-8")
112
+
113
+ return {"ok": True}
114
+
115
  @app.get("/api/pdf/{pdf_id}")
116
  def get_pdf(pdf_id: str):
117
  pdf_path = _pdf_path(pdf_id)