Hamza4100 commited on
Commit
86f4817
·
verified ·
1 Parent(s): 29041f7

Update app/routes/upload.py

Browse files
Files changed (1) hide show
  1. app/routes/upload.py +71 -0
app/routes/upload.py CHANGED
@@ -135,6 +135,77 @@ async def upload_file(
135
  )
136
 
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  @router.get("/upload/list", response_model=dict)
139
  async def list_files(current_user=Depends(require_role("doctor", "lab_tech", "admin"))):
140
  """List all uploaded files
 
135
  )
136
 
137
 
138
+ @router.post("/upload/batch")
139
+ async def upload_batch(
140
+ files: list[UploadFile] = File(...),
141
+ db: Session = Depends(get_db),
142
+ current_user=Depends(require_role("doctor", "lab_tech", "admin")),
143
+ ):
144
+ """Upload up to 5 pathology reports in batch"""
145
+ if len(files) > 5:
146
+ raise HTTPException(
147
+ status_code=400,
148
+ detail="Batch limit exceeded: Maximum 5 files allowed per batch.",
149
+ )
150
+
151
+ results = []
152
+ for file in files:
153
+ try:
154
+ file_id = str(uuid.uuid4())
155
+ file_extension = os.path.splitext(file.filename)[1].lower()
156
+ if file_extension not in [".pdf", ".png", ".jpg", ".jpeg", ".tif", ".tiff"]:
157
+ results.append({
158
+ "file_id": "",
159
+ "filename": file.filename,
160
+ "status": "error",
161
+ "message": f"Unsupported format {file_extension}",
162
+ })
163
+ continue
164
+
165
+ contents = await file.read()
166
+ file_size = len(contents)
167
+
168
+ # Save to temporary local cache
169
+ saved_filename = f"{file_id}_{file.filename}"
170
+ local_path = os.path.join(UPLOAD_FOLDER, saved_filename)
171
+ with open(local_path, "wb") as f:
172
+ f.write(contents)
173
+
174
+ # Save to PostgreSQL binary vault
175
+ file_type = (file.content_type or "").strip() or file_extension.lstrip(".")
176
+ await asyncio.to_thread(
177
+ DatabaseService.save_document,
178
+ db,
179
+ file_id,
180
+ file.filename,
181
+ file_size,
182
+ file_type,
183
+ contents,
184
+ )
185
+
186
+ results.append({
187
+ "file_id": file_id,
188
+ "filename": file.filename,
189
+ "file_size": file_size,
190
+ "status": "success",
191
+ "message": "File uploaded successfully",
192
+ })
193
+ except Exception as e:
194
+ results.append({
195
+ "file_id": "",
196
+ "filename": file.filename,
197
+ "status": "error",
198
+ "message": str(e),
199
+ })
200
+
201
+ return {
202
+ "status": "success",
203
+ "total": len(files),
204
+ "successful": len([r for r in results if r["status"] == "success"]),
205
+ "files": results,
206
+ }
207
+
208
+
209
  @router.get("/upload/list", response_model=dict)
210
  async def list_files(current_user=Depends(require_role("doctor", "lab_tech", "admin"))):
211
  """List all uploaded files