pending_uploads.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. """API routes for pending uploads (virtual printer queue mode)."""
  2. from datetime import datetime, timezone
  3. from pathlib import Path
  4. from fastapi import APIRouter, Depends, HTTPException
  5. from pydantic import BaseModel
  6. from sqlalchemy import select
  7. from sqlalchemy.ext.asyncio import AsyncSession
  8. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  9. from backend.app.core.database import get_db
  10. from backend.app.core.permissions import Permission
  11. from backend.app.models.pending_upload import PendingUpload
  12. from backend.app.models.user import User
  13. from backend.app.services.archive import ArchiveService, resolve_display_stem
  14. router = APIRouter(prefix="/pending-uploads", tags=["pending-uploads"])
  15. class ArchiveRequest(BaseModel):
  16. """Request to archive a pending upload."""
  17. tags: str | None = None
  18. notes: str | None = None
  19. project_id: int | None = None
  20. class PendingUploadResponse(BaseModel):
  21. """Response model for pending upload."""
  22. id: int
  23. filename: str
  24. display_name: str # Resolved name that mirrors the eventual archive's print_name (#1152 follow-up)
  25. file_size: int
  26. source_ip: str | None
  27. status: str
  28. tags: str | None
  29. notes: str | None
  30. project_id: int | None
  31. uploaded_at: datetime
  32. class Config:
  33. from_attributes = True
  34. def _resolve_display_name(pending: PendingUpload, prefer_filename: bool) -> str:
  35. """Compute the name the review card should show, matching what archive_print
  36. will eventually write to ``PrintArchive.print_name`` so the user sees the
  37. same name in both places (#1152 follow-up).
  38. Mirrors ``ArchiveService.archive_print``:
  39. - ``prefer_filename=True`` → stripped filename stem.
  40. - ``prefer_filename=False`` → ``metadata_print_name`` if set, else stem.
  41. """
  42. stem = resolve_display_stem(pending.filename)
  43. if prefer_filename:
  44. return stem
  45. return (pending.metadata_print_name or "").strip() or stem
  46. async def _augment_with_display_name(
  47. db: AsyncSession,
  48. pendings: list[PendingUpload],
  49. ) -> list[PendingUploadResponse]:
  50. """Build response objects with display_name resolved against the toggle.
  51. Reads the ``virtual_printer_archive_name_source`` setting once per request
  52. rather than per row.
  53. """
  54. from backend.app.api.routes.settings import get_setting
  55. prefer_filename = (await get_setting(db, "virtual_printer_archive_name_source")) == "filename"
  56. return [
  57. PendingUploadResponse(
  58. id=p.id,
  59. filename=p.filename,
  60. display_name=_resolve_display_name(p, prefer_filename),
  61. file_size=p.file_size,
  62. source_ip=p.source_ip,
  63. status=p.status,
  64. tags=p.tags,
  65. notes=p.notes,
  66. project_id=p.project_id,
  67. uploaded_at=p.uploaded_at,
  68. )
  69. for p in pendings
  70. ]
  71. @router.get("/", response_model=list[PendingUploadResponse])
  72. async def list_pending_uploads(
  73. db: AsyncSession = Depends(get_db),
  74. _: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_READ),
  75. ):
  76. """List all pending uploads."""
  77. result = await db.execute(
  78. select(PendingUpload).where(PendingUpload.status == "pending").order_by(PendingUpload.uploaded_at.desc())
  79. )
  80. return await _augment_with_display_name(db, list(result.scalars().all()))
  81. @router.get("/count")
  82. async def get_pending_count(
  83. db: AsyncSession = Depends(get_db),
  84. _: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_READ),
  85. ):
  86. """Get count of pending uploads."""
  87. result = await db.execute(select(PendingUpload).where(PendingUpload.status == "pending"))
  88. count = len(result.scalars().all())
  89. return {"count": count}
  90. # Note: Bulk operations must be defined BEFORE parameterized routes
  91. # to prevent FastAPI from matching /archive-all as /{upload_id}
  92. @router.post("/archive-all")
  93. async def archive_all_pending(
  94. db: AsyncSession = Depends(get_db),
  95. _: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_CREATE),
  96. ):
  97. """Archive all pending uploads."""
  98. from backend.app.api.routes.settings import get_setting
  99. result = await db.execute(select(PendingUpload).where(PendingUpload.status == "pending"))
  100. pending_uploads = result.scalars().all()
  101. archived = 0
  102. failed = 0
  103. service = ArchiveService(db)
  104. prefer_filename = (await get_setting(db, "virtual_printer_archive_name_source")) == "filename"
  105. for pending in pending_uploads:
  106. file_path = Path(pending.file_path)
  107. if not file_path.exists():
  108. pending.status = "discarded"
  109. failed += 1
  110. continue
  111. try:
  112. archive = await service.archive_print(
  113. printer_id=None,
  114. source_file=file_path,
  115. print_data={
  116. "status": "archived",
  117. "source": "virtual_printer",
  118. "source_ip": pending.source_ip,
  119. },
  120. prefer_filename_for_name=prefer_filename,
  121. )
  122. if archive:
  123. pending.status = "archived"
  124. pending.archived_id = archive.id
  125. pending.archived_at = datetime.now(timezone.utc)
  126. archived += 1
  127. # Clean up temp file
  128. try:
  129. file_path.unlink()
  130. except OSError:
  131. pass # Best-effort temp file cleanup after archiving
  132. else:
  133. failed += 1
  134. except Exception: # Mixed async DB + archive operations
  135. failed += 1
  136. await db.commit()
  137. return {
  138. "archived": archived,
  139. "failed": failed,
  140. }
  141. @router.delete("/discard-all")
  142. async def discard_all_pending(
  143. db: AsyncSession = Depends(get_db),
  144. _: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_DELETE_ALL),
  145. ):
  146. """Discard all pending uploads."""
  147. result = await db.execute(select(PendingUpload).where(PendingUpload.status == "pending"))
  148. pending_uploads = result.scalars().all()
  149. discarded = 0
  150. for pending in pending_uploads:
  151. # Delete file from disk
  152. try:
  153. file_path = Path(pending.file_path)
  154. file_path.unlink(missing_ok=True)
  155. except OSError:
  156. pass # Best-effort file deletion; record is still marked discarded
  157. pending.status = "discarded"
  158. discarded += 1
  159. await db.commit()
  160. return {"discarded": discarded}
  161. @router.get("/{upload_id}", response_model=PendingUploadResponse)
  162. async def get_pending_upload(
  163. upload_id: int,
  164. db: AsyncSession = Depends(get_db),
  165. _: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_READ),
  166. ):
  167. """Get a specific pending upload."""
  168. result = await db.execute(select(PendingUpload).where(PendingUpload.id == upload_id))
  169. pending = result.scalar_one_or_none()
  170. if not pending:
  171. raise HTTPException(status_code=404, detail="Upload not found")
  172. return (await _augment_with_display_name(db, [pending]))[0]
  173. @router.post("/{upload_id}/archive")
  174. async def archive_pending_upload(
  175. upload_id: int,
  176. request: ArchiveRequest = None,
  177. db: AsyncSession = Depends(get_db),
  178. _: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_CREATE),
  179. ):
  180. """Archive a pending upload."""
  181. result = await db.execute(select(PendingUpload).where(PendingUpload.id == upload_id))
  182. pending = result.scalar_one_or_none()
  183. if not pending:
  184. raise HTTPException(status_code=404, detail="Upload not found")
  185. if pending.status != "pending":
  186. raise HTTPException(status_code=400, detail="Upload already processed")
  187. # Check file exists
  188. file_path = Path(pending.file_path)
  189. if not file_path.exists():
  190. raise HTTPException(status_code=404, detail="Upload file not found on disk")
  191. # Archive the file
  192. from backend.app.api.routes.settings import get_setting
  193. prefer_filename = (await get_setting(db, "virtual_printer_archive_name_source")) == "filename"
  194. service = ArchiveService(db)
  195. archive = await service.archive_print(
  196. printer_id=None,
  197. source_file=file_path,
  198. print_data={
  199. "status": "archived",
  200. "source": "virtual_printer",
  201. "source_ip": pending.source_ip,
  202. },
  203. prefer_filename_for_name=prefer_filename,
  204. )
  205. if not archive:
  206. raise HTTPException(status_code=500, detail="Failed to archive file")
  207. # Apply tags/notes/project from request
  208. if request:
  209. if request.tags:
  210. archive.tags = request.tags
  211. if request.notes:
  212. archive.notes = request.notes
  213. if request.project_id:
  214. archive.project_id = request.project_id
  215. # Update pending record
  216. pending.status = "archived"
  217. pending.archived_id = archive.id
  218. pending.archived_at = datetime.now(timezone.utc)
  219. if request:
  220. pending.tags = request.tags
  221. pending.notes = request.notes
  222. pending.project_id = request.project_id
  223. await db.commit()
  224. # Clean up temp file
  225. try:
  226. file_path.unlink()
  227. except OSError:
  228. pass # Best-effort temp file cleanup after successful archive
  229. return {
  230. "id": archive.id,
  231. "print_name": archive.print_name,
  232. "filename": archive.filename,
  233. }
  234. @router.delete("/{upload_id}")
  235. async def discard_pending_upload(
  236. upload_id: int,
  237. db: AsyncSession = Depends(get_db),
  238. _: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_DELETE_ALL),
  239. ):
  240. """Discard a pending upload without archiving."""
  241. result = await db.execute(select(PendingUpload).where(PendingUpload.id == upload_id))
  242. pending = result.scalar_one_or_none()
  243. if not pending:
  244. raise HTTPException(status_code=404, detail="Upload not found")
  245. # Delete file from disk
  246. file_path = Path(pending.file_path)
  247. try:
  248. file_path.unlink(missing_ok=True)
  249. except OSError:
  250. pass # Best-effort file deletion on discard
  251. # Update status
  252. pending.status = "discarded"
  253. await db.commit()
  254. return {"success": True}