library.py 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. """API routes for File Manager (Library) functionality."""
  2. import base64
  3. import hashlib
  4. import logging
  5. import os
  6. import re
  7. import shutil
  8. import uuid
  9. from pathlib import Path
  10. from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
  11. from fastapi.responses import FileResponse as FastAPIFileResponse
  12. from sqlalchemy import func, select
  13. from sqlalchemy.ext.asyncio import AsyncSession
  14. from backend.app.core.config import settings as app_settings
  15. from backend.app.core.database import get_db
  16. from backend.app.models.archive import PrintArchive
  17. from backend.app.models.library import LibraryFile, LibraryFolder
  18. from backend.app.models.print_queue import PrintQueueItem
  19. from backend.app.models.project import Project
  20. from backend.app.schemas.library import (
  21. AddToQueueError,
  22. AddToQueueRequest,
  23. AddToQueueResponse,
  24. AddToQueueResult,
  25. BulkDeleteRequest,
  26. BulkDeleteResponse,
  27. FileDuplicate,
  28. FileListResponse,
  29. FileMoveRequest,
  30. FileResponse as FileResponseSchema,
  31. FileUpdate,
  32. FileUploadResponse,
  33. FolderCreate,
  34. FolderResponse,
  35. FolderTreeItem,
  36. FolderUpdate,
  37. )
  38. from backend.app.services.archive import ArchiveService, ThreeMFParser
  39. logger = logging.getLogger(__name__)
  40. router = APIRouter(prefix="/library", tags=["library"])
  41. def get_library_dir() -> Path:
  42. """Get the library storage directory."""
  43. base_dir = Path(app_settings.archive_dir)
  44. library_dir = base_dir / "library"
  45. library_dir.mkdir(parents=True, exist_ok=True)
  46. return library_dir
  47. def get_library_files_dir() -> Path:
  48. """Get the directory for library files."""
  49. files_dir = get_library_dir() / "files"
  50. files_dir.mkdir(parents=True, exist_ok=True)
  51. return files_dir
  52. def get_library_thumbnails_dir() -> Path:
  53. """Get the directory for library thumbnails."""
  54. thumbnails_dir = get_library_dir() / "thumbnails"
  55. thumbnails_dir.mkdir(parents=True, exist_ok=True)
  56. return thumbnails_dir
  57. def calculate_file_hash(file_path: Path) -> str:
  58. """Calculate SHA256 hash of a file."""
  59. sha256_hash = hashlib.sha256()
  60. with open(file_path, "rb") as f:
  61. for byte_block in iter(lambda: f.read(4096), b""):
  62. sha256_hash.update(byte_block)
  63. return sha256_hash.hexdigest()
  64. def extract_gcode_thumbnail(file_path: Path) -> bytes | None:
  65. """Extract embedded thumbnail from gcode file.
  66. Supports PrusaSlicer/BambuStudio format:
  67. ; thumbnail begin WxH SIZE
  68. ; base64data...
  69. ; thumbnail end
  70. """
  71. try:
  72. thumbnail_data = None
  73. in_thumbnail = False
  74. thumbnail_lines = []
  75. best_size = 0
  76. with open(file_path, errors="ignore") as f:
  77. # Only read first 50KB for performance (thumbnails are at the start)
  78. content = f.read(50000)
  79. for line in content.split("\n"):
  80. line = line.strip()
  81. # Check for thumbnail start
  82. if line.startswith("; thumbnail begin"):
  83. in_thumbnail = True
  84. thumbnail_lines = []
  85. # Parse dimensions: "; thumbnail begin 300x300 12345"
  86. match = re.search(r"(\d+)x(\d+)", line)
  87. if match:
  88. width = int(match.group(1))
  89. # Prefer larger thumbnails (up to 300px)
  90. if width > best_size and width <= 300:
  91. best_size = width
  92. continue
  93. # Check for thumbnail end
  94. if line.startswith("; thumbnail end"):
  95. if in_thumbnail and thumbnail_lines:
  96. try:
  97. # Decode the base64 data
  98. b64_data = "".join(thumbnail_lines)
  99. decoded = base64.b64decode(b64_data)
  100. # Only keep if this is the best size or first valid thumbnail
  101. if thumbnail_data is None or best_size > 0:
  102. thumbnail_data = decoded
  103. except Exception:
  104. pass
  105. in_thumbnail = False
  106. thumbnail_lines = []
  107. continue
  108. # Collect thumbnail data
  109. if in_thumbnail and line.startswith(";"):
  110. # Remove the leading "; " or ";"
  111. data_line = line[1:].strip()
  112. if data_line:
  113. thumbnail_lines.append(data_line)
  114. return thumbnail_data
  115. except Exception as e:
  116. logger.warning(f"Failed to extract gcode thumbnail: {e}")
  117. return None
  118. def create_image_thumbnail(file_path: Path, thumbnails_dir: Path, max_size: int = 256) -> str | None:
  119. """Create a thumbnail from an image file.
  120. For small images, copies directly. For larger images, resizes.
  121. Returns the thumbnail path or None on failure.
  122. """
  123. try:
  124. from PIL import Image
  125. thumb_filename = f"{uuid.uuid4().hex}.png"
  126. thumb_path = thumbnails_dir / thumb_filename
  127. with Image.open(file_path) as img:
  128. # Convert to RGB if necessary (for PNG with transparency, etc.)
  129. if img.mode in ("RGBA", "LA", "P"):
  130. # Create white background for transparency
  131. background = Image.new("RGB", img.size, (255, 255, 255))
  132. if img.mode == "P":
  133. img = img.convert("RGBA")
  134. background.paste(img, mask=img.split()[-1] if img.mode == "RGBA" else None)
  135. img = background
  136. elif img.mode != "RGB":
  137. img = img.convert("RGB")
  138. # Resize if larger than max_size
  139. if img.width > max_size or img.height > max_size:
  140. img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
  141. img.save(thumb_path, "PNG", optimize=True)
  142. return str(thumb_path)
  143. except ImportError:
  144. # PIL not installed, just copy the file if it's small enough
  145. logger.warning("PIL not installed, copying image as thumbnail")
  146. try:
  147. file_size = file_path.stat().st_size
  148. if file_size < 500000: # Less than 500KB
  149. thumb_filename = f"{uuid.uuid4().hex}{file_path.suffix}"
  150. thumb_path = thumbnails_dir / thumb_filename
  151. shutil.copy2(file_path, thumb_path)
  152. return str(thumb_path)
  153. except Exception:
  154. pass
  155. return None
  156. except Exception as e:
  157. logger.warning(f"Failed to create image thumbnail: {e}")
  158. return None
  159. # Supported image extensions for thumbnails
  160. IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".tiff", ".tif"}
  161. # ============ Folder Endpoints ============
  162. @router.get("/folders", response_model=list[FolderTreeItem])
  163. @router.get("/folders/", response_model=list[FolderTreeItem])
  164. async def list_folders(db: AsyncSession = Depends(get_db)):
  165. """Get all folders as a tree structure."""
  166. # Get all folders with project and archive joins
  167. result = await db.execute(
  168. select(LibraryFolder, Project.name, PrintArchive.print_name)
  169. .outerjoin(Project, LibraryFolder.project_id == Project.id)
  170. .outerjoin(PrintArchive, LibraryFolder.archive_id == PrintArchive.id)
  171. .order_by(LibraryFolder.name)
  172. )
  173. rows = result.all()
  174. # Get file counts per folder
  175. file_counts_result = await db.execute(
  176. select(LibraryFile.folder_id, func.count(LibraryFile.id))
  177. .where(LibraryFile.folder_id.isnot(None))
  178. .group_by(LibraryFile.folder_id)
  179. )
  180. file_counts = dict(file_counts_result.all())
  181. # Build tree structure
  182. folder_map = {}
  183. root_folders = []
  184. for folder, project_name, archive_name in rows:
  185. folder_item = FolderTreeItem(
  186. id=folder.id,
  187. name=folder.name,
  188. parent_id=folder.parent_id,
  189. project_id=folder.project_id,
  190. archive_id=folder.archive_id,
  191. project_name=project_name,
  192. archive_name=archive_name,
  193. file_count=file_counts.get(folder.id, 0),
  194. children=[],
  195. )
  196. folder_map[folder.id] = folder_item
  197. # Link children to parents
  198. for folder, _, _ in rows:
  199. folder_item = folder_map[folder.id]
  200. if folder.parent_id is None:
  201. root_folders.append(folder_item)
  202. elif folder.parent_id in folder_map:
  203. folder_map[folder.parent_id].children.append(folder_item)
  204. return root_folders
  205. @router.get("/folders/by-project/{project_id}", response_model=list[FolderResponse])
  206. async def get_folders_by_project(project_id: int, db: AsyncSession = Depends(get_db)):
  207. """Get all folders linked to a specific project."""
  208. result = await db.execute(
  209. select(LibraryFolder, Project.name)
  210. .outerjoin(Project, LibraryFolder.project_id == Project.id)
  211. .where(LibraryFolder.project_id == project_id)
  212. .order_by(LibraryFolder.name)
  213. )
  214. rows = result.all()
  215. folders = []
  216. for folder, project_name in rows:
  217. # Get file count
  218. file_count_result = await db.execute(
  219. select(func.count(LibraryFile.id)).where(LibraryFile.folder_id == folder.id)
  220. )
  221. file_count = file_count_result.scalar() or 0
  222. folders.append(
  223. FolderResponse(
  224. id=folder.id,
  225. name=folder.name,
  226. parent_id=folder.parent_id,
  227. project_id=folder.project_id,
  228. archive_id=folder.archive_id,
  229. project_name=project_name,
  230. archive_name=None,
  231. file_count=file_count,
  232. created_at=folder.created_at,
  233. updated_at=folder.updated_at,
  234. )
  235. )
  236. return folders
  237. @router.get("/folders/by-archive/{archive_id}", response_model=list[FolderResponse])
  238. async def get_folders_by_archive(archive_id: int, db: AsyncSession = Depends(get_db)):
  239. """Get all folders linked to a specific archive."""
  240. result = await db.execute(
  241. select(LibraryFolder, PrintArchive.print_name)
  242. .outerjoin(PrintArchive, LibraryFolder.archive_id == PrintArchive.id)
  243. .where(LibraryFolder.archive_id == archive_id)
  244. .order_by(LibraryFolder.name)
  245. )
  246. rows = result.all()
  247. folders = []
  248. for folder, archive_name in rows:
  249. # Get file count
  250. file_count_result = await db.execute(
  251. select(func.count(LibraryFile.id)).where(LibraryFile.folder_id == folder.id)
  252. )
  253. file_count = file_count_result.scalar() or 0
  254. folders.append(
  255. FolderResponse(
  256. id=folder.id,
  257. name=folder.name,
  258. parent_id=folder.parent_id,
  259. project_id=folder.project_id,
  260. archive_id=folder.archive_id,
  261. project_name=None,
  262. archive_name=archive_name,
  263. file_count=file_count,
  264. created_at=folder.created_at,
  265. updated_at=folder.updated_at,
  266. )
  267. )
  268. return folders
  269. @router.post("/folders", response_model=FolderResponse)
  270. @router.post("/folders/", response_model=FolderResponse)
  271. async def create_folder(data: FolderCreate, db: AsyncSession = Depends(get_db)):
  272. """Create a new folder."""
  273. # Verify parent exists if specified
  274. if data.parent_id is not None:
  275. parent_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.parent_id))
  276. if not parent_result.scalar_one_or_none():
  277. raise HTTPException(status_code=404, detail="Parent folder not found")
  278. # Verify project exists if specified
  279. project_name = None
  280. if data.project_id is not None:
  281. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  282. project = project_result.scalar_one_or_none()
  283. if not project:
  284. raise HTTPException(status_code=404, detail="Project not found")
  285. project_name = project.name
  286. # Verify archive exists if specified
  287. archive_name = None
  288. if data.archive_id is not None:
  289. archive_result = await db.execute(select(PrintArchive).where(PrintArchive.id == data.archive_id))
  290. archive = archive_result.scalar_one_or_none()
  291. if not archive:
  292. raise HTTPException(status_code=404, detail="Archive not found")
  293. archive_name = archive.print_name
  294. folder = LibraryFolder(
  295. name=data.name,
  296. parent_id=data.parent_id,
  297. project_id=data.project_id,
  298. archive_id=data.archive_id,
  299. )
  300. db.add(folder)
  301. await db.flush()
  302. await db.refresh(folder)
  303. return FolderResponse(
  304. id=folder.id,
  305. name=folder.name,
  306. parent_id=folder.parent_id,
  307. project_id=folder.project_id,
  308. archive_id=folder.archive_id,
  309. project_name=project_name,
  310. archive_name=archive_name,
  311. file_count=0,
  312. created_at=folder.created_at,
  313. updated_at=folder.updated_at,
  314. )
  315. @router.get("/folders/{folder_id}", response_model=FolderResponse)
  316. async def get_folder(folder_id: int, db: AsyncSession = Depends(get_db)):
  317. """Get a folder by ID."""
  318. result = await db.execute(
  319. select(LibraryFolder, Project.name, PrintArchive.print_name)
  320. .outerjoin(Project, LibraryFolder.project_id == Project.id)
  321. .outerjoin(PrintArchive, LibraryFolder.archive_id == PrintArchive.id)
  322. .where(LibraryFolder.id == folder_id)
  323. )
  324. row = result.one_or_none()
  325. if not row:
  326. raise HTTPException(status_code=404, detail="Folder not found")
  327. folder, project_name, archive_name = row
  328. # Get file count
  329. file_count_result = await db.execute(select(func.count(LibraryFile.id)).where(LibraryFile.folder_id == folder_id))
  330. file_count = file_count_result.scalar() or 0
  331. return FolderResponse(
  332. id=folder.id,
  333. name=folder.name,
  334. parent_id=folder.parent_id,
  335. project_id=folder.project_id,
  336. archive_id=folder.archive_id,
  337. project_name=project_name,
  338. archive_name=archive_name,
  339. file_count=file_count,
  340. created_at=folder.created_at,
  341. updated_at=folder.updated_at,
  342. )
  343. @router.put("/folders/{folder_id}", response_model=FolderResponse)
  344. async def update_folder(folder_id: int, data: FolderUpdate, db: AsyncSession = Depends(get_db)):
  345. """Update a folder."""
  346. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  347. folder = result.scalar_one_or_none()
  348. if not folder:
  349. raise HTTPException(status_code=404, detail="Folder not found")
  350. if data.name is not None:
  351. folder.name = data.name
  352. if data.parent_id is not None:
  353. # Prevent circular reference
  354. if data.parent_id == folder_id:
  355. raise HTTPException(status_code=400, detail="Folder cannot be its own parent")
  356. # Check for circular reference in ancestors
  357. if data.parent_id != 0: # 0 means move to root
  358. current_id = data.parent_id
  359. while current_id is not None:
  360. if current_id == folder_id:
  361. raise HTTPException(status_code=400, detail="Cannot move folder into its own subtree")
  362. parent_result = await db.execute(select(LibraryFolder.parent_id).where(LibraryFolder.id == current_id))
  363. current_id = parent_result.scalar()
  364. folder.parent_id = data.parent_id
  365. else:
  366. folder.parent_id = None
  367. # Update project_id (0 to unlink)
  368. if data.project_id is not None:
  369. if data.project_id == 0:
  370. folder.project_id = None
  371. else:
  372. # Verify project exists
  373. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  374. if not project_result.scalar_one_or_none():
  375. raise HTTPException(status_code=404, detail="Project not found")
  376. folder.project_id = data.project_id
  377. # Update archive_id (0 to unlink)
  378. if data.archive_id is not None:
  379. if data.archive_id == 0:
  380. folder.archive_id = None
  381. else:
  382. # Verify archive exists
  383. archive_result = await db.execute(select(PrintArchive).where(PrintArchive.id == data.archive_id))
  384. if not archive_result.scalar_one_or_none():
  385. raise HTTPException(status_code=404, detail="Archive not found")
  386. folder.archive_id = data.archive_id
  387. await db.flush()
  388. await db.refresh(folder)
  389. # Get file count and names
  390. file_count_result = await db.execute(select(func.count(LibraryFile.id)).where(LibraryFile.folder_id == folder_id))
  391. file_count = file_count_result.scalar() or 0
  392. # Get project and archive names
  393. project_name = None
  394. archive_name = None
  395. if folder.project_id:
  396. project_result = await db.execute(select(Project.name).where(Project.id == folder.project_id))
  397. project_name = project_result.scalar()
  398. if folder.archive_id:
  399. archive_result = await db.execute(select(PrintArchive.print_name).where(PrintArchive.id == folder.archive_id))
  400. archive_name = archive_result.scalar()
  401. return FolderResponse(
  402. id=folder.id,
  403. name=folder.name,
  404. parent_id=folder.parent_id,
  405. project_id=folder.project_id,
  406. archive_id=folder.archive_id,
  407. project_name=project_name,
  408. archive_name=archive_name,
  409. file_count=file_count,
  410. created_at=folder.created_at,
  411. updated_at=folder.updated_at,
  412. )
  413. @router.delete("/folders/{folder_id}")
  414. async def delete_folder(folder_id: int, db: AsyncSession = Depends(get_db)):
  415. """Delete a folder and all its contents (cascade)."""
  416. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  417. folder = result.scalar_one_or_none()
  418. if not folder:
  419. raise HTTPException(status_code=404, detail="Folder not found")
  420. # Get all files in this folder and subfolders to delete from disk
  421. async def get_all_file_ids(fid: int) -> list[int]:
  422. """Recursively get all file IDs in a folder tree."""
  423. file_ids = []
  424. # Get files in this folder
  425. files_result = await db.execute(
  426. select(LibraryFile.id, LibraryFile.file_path, LibraryFile.thumbnail_path).where(
  427. LibraryFile.folder_id == fid
  428. )
  429. )
  430. for file_id, file_path, thumb_path in files_result.all():
  431. file_ids.append(file_id)
  432. # Delete actual files
  433. try:
  434. if file_path and os.path.exists(file_path):
  435. os.remove(file_path)
  436. if thumb_path and os.path.exists(thumb_path):
  437. os.remove(thumb_path)
  438. except Exception as e:
  439. logger.warning(f"Failed to delete file: {e}")
  440. # Get child folders and recurse
  441. children_result = await db.execute(select(LibraryFolder.id).where(LibraryFolder.parent_id == fid))
  442. for (child_id,) in children_result.all():
  443. file_ids.extend(await get_all_file_ids(child_id))
  444. return file_ids
  445. await get_all_file_ids(folder_id)
  446. # Delete folder (cascade will handle files and subfolders)
  447. await db.delete(folder)
  448. return {"status": "success", "message": "Folder deleted"}
  449. # ============ File Endpoints ============
  450. @router.get("/files", response_model=list[FileListResponse])
  451. @router.get("/files/", response_model=list[FileListResponse])
  452. async def list_files(
  453. folder_id: int | None = None,
  454. include_root: bool = True,
  455. db: AsyncSession = Depends(get_db),
  456. ):
  457. """List files, optionally filtered by folder.
  458. Args:
  459. folder_id: Filter by folder ID. If None and include_root=True, returns root files.
  460. include_root: If True and folder_id is None, returns files at root level.
  461. If False and folder_id is None, returns all files.
  462. """
  463. query = select(LibraryFile)
  464. if folder_id is not None:
  465. query = query.where(LibraryFile.folder_id == folder_id)
  466. elif include_root:
  467. query = query.where(LibraryFile.folder_id.is_(None))
  468. query = query.order_by(LibraryFile.filename)
  469. result = await db.execute(query)
  470. files = result.scalars().all()
  471. # Get duplicate counts
  472. hash_counts = {}
  473. if files:
  474. hashes = [f.file_hash for f in files if f.file_hash]
  475. if hashes:
  476. dup_result = await db.execute(
  477. select(LibraryFile.file_hash, func.count(LibraryFile.id))
  478. .where(LibraryFile.file_hash.in_(hashes))
  479. .group_by(LibraryFile.file_hash)
  480. )
  481. hash_counts = {h: c - 1 for h, c in dup_result.all()} # -1 to exclude self
  482. response = []
  483. for f in files:
  484. # Extract key metadata for display
  485. print_name = None
  486. print_time = None
  487. filament_grams = None
  488. if f.file_metadata:
  489. print_name = f.file_metadata.get("print_name")
  490. print_time = f.file_metadata.get("print_time_seconds")
  491. filament_grams = f.file_metadata.get("filament_used_grams")
  492. response.append(
  493. FileListResponse(
  494. id=f.id,
  495. folder_id=f.folder_id,
  496. filename=f.filename,
  497. file_type=f.file_type,
  498. file_size=f.file_size,
  499. thumbnail_path=f.thumbnail_path,
  500. print_count=f.print_count,
  501. duplicate_count=hash_counts.get(f.file_hash, 0) if f.file_hash else 0,
  502. created_at=f.created_at,
  503. print_name=print_name,
  504. print_time_seconds=print_time,
  505. filament_used_grams=filament_grams,
  506. )
  507. )
  508. return response
  509. @router.post("/files", response_model=FileUploadResponse)
  510. @router.post("/files/", response_model=FileUploadResponse)
  511. async def upload_file(
  512. file: UploadFile = File(...),
  513. folder_id: int | None = None,
  514. db: AsyncSession = Depends(get_db),
  515. ):
  516. """Upload a file to the library."""
  517. try:
  518. if not file.filename:
  519. raise HTTPException(status_code=400, detail="Filename is required")
  520. filename = file.filename
  521. ext = os.path.splitext(filename)[1].lower()
  522. # Handle files without extension
  523. file_type = ext[1:] if ext else "unknown"
  524. # Verify folder exists if specified
  525. if folder_id is not None:
  526. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  527. if not folder_result.scalar_one_or_none():
  528. raise HTTPException(status_code=404, detail="Folder not found")
  529. # Generate unique filename for storage
  530. unique_filename = f"{uuid.uuid4().hex}{ext}"
  531. file_path = get_library_files_dir() / unique_filename
  532. # Save file
  533. content = await file.read()
  534. with open(file_path, "wb") as f:
  535. f.write(content)
  536. # Calculate hash
  537. file_hash = calculate_file_hash(file_path)
  538. # Check for duplicates
  539. dup_result = await db.execute(select(LibraryFile.id).where(LibraryFile.file_hash == file_hash).limit(1))
  540. duplicate_of = dup_result.scalar()
  541. # Extract metadata and thumbnail
  542. metadata = {}
  543. thumbnail_path = None
  544. thumbnails_dir = get_library_thumbnails_dir()
  545. if ext == ".3mf":
  546. try:
  547. parser = ThreeMFParser(str(file_path))
  548. raw_metadata = parser.parse()
  549. # Extract thumbnail before cleaning metadata
  550. thumbnail_data = raw_metadata.get("_thumbnail_data")
  551. thumbnail_ext = raw_metadata.get("_thumbnail_ext", ".png")
  552. # Save thumbnail if extracted
  553. if thumbnail_data:
  554. thumb_filename = f"{uuid.uuid4().hex}{thumbnail_ext}"
  555. thumb_path = thumbnails_dir / thumb_filename
  556. with open(thumb_path, "wb") as f:
  557. f.write(thumbnail_data)
  558. thumbnail_path = str(thumb_path)
  559. # Clean metadata - remove non-JSON-serializable data (bytes, etc.)
  560. def clean_metadata(obj):
  561. if isinstance(obj, dict):
  562. return {
  563. k: clean_metadata(v)
  564. for k, v in obj.items()
  565. if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext")
  566. }
  567. elif isinstance(obj, list):
  568. return [clean_metadata(i) for i in obj if not isinstance(i, bytes)]
  569. elif isinstance(obj, bytes):
  570. return None
  571. return obj
  572. metadata = clean_metadata(raw_metadata)
  573. except Exception as e:
  574. logger.warning(f"Failed to parse 3MF: {e}")
  575. elif ext == ".gcode":
  576. # Extract embedded thumbnail from gcode
  577. try:
  578. thumbnail_data = extract_gcode_thumbnail(file_path)
  579. if thumbnail_data:
  580. thumb_filename = f"{uuid.uuid4().hex}.png"
  581. thumb_path = thumbnails_dir / thumb_filename
  582. with open(thumb_path, "wb") as f:
  583. f.write(thumbnail_data)
  584. thumbnail_path = str(thumb_path)
  585. except Exception as e:
  586. logger.warning(f"Failed to extract gcode thumbnail: {e}")
  587. elif ext.lower() in IMAGE_EXTENSIONS:
  588. # For image files, create a thumbnail from the image itself
  589. thumbnail_path = create_image_thumbnail(file_path, thumbnails_dir)
  590. # Create database entry
  591. library_file = LibraryFile(
  592. folder_id=folder_id,
  593. filename=filename,
  594. file_path=str(file_path),
  595. file_type=file_type,
  596. file_size=len(content),
  597. file_hash=file_hash,
  598. thumbnail_path=thumbnail_path,
  599. file_metadata=metadata if metadata else None,
  600. )
  601. db.add(library_file)
  602. await db.flush()
  603. await db.refresh(library_file)
  604. return FileUploadResponse(
  605. id=library_file.id,
  606. filename=library_file.filename,
  607. file_type=library_file.file_type,
  608. file_size=library_file.file_size,
  609. thumbnail_path=library_file.thumbnail_path,
  610. duplicate_of=duplicate_of,
  611. metadata=library_file.file_metadata,
  612. )
  613. except HTTPException:
  614. raise
  615. except Exception as e:
  616. logger.error(f"Upload failed for {file.filename}: {e}", exc_info=True)
  617. raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)}")
  618. # ============ Queue Operations ============
  619. # NOTE: These routes must be defined BEFORE /files/{file_id} to avoid path parameter conflicts
  620. def is_sliced_file(filename: str) -> bool:
  621. """Check if a file is a sliced (printable) file.
  622. Sliced files are:
  623. - .gcode files
  624. - .3mf files that contain '.gcode.' in the name (e.g., filename.gcode.3mf)
  625. """
  626. lower = filename.lower()
  627. return lower.endswith(".gcode") or ".gcode." in lower
  628. @router.post("/files/add-to-queue", response_model=AddToQueueResponse)
  629. async def add_files_to_queue(
  630. request: AddToQueueRequest,
  631. db: AsyncSession = Depends(get_db),
  632. ):
  633. """Add library files to the print queue.
  634. Only sliced files (.gcode or .gcode.3mf) can be added to the queue.
  635. For each file:
  636. 1. Validates it's a sliced file
  637. 2. Creates an archive from the library file
  638. 3. Creates a queue item pointing to that archive
  639. """
  640. added: list[AddToQueueResult] = []
  641. errors: list[AddToQueueError] = []
  642. # Get all requested files
  643. result = await db.execute(select(LibraryFile).where(LibraryFile.id.in_(request.file_ids)))
  644. files = {f.id: f for f in result.scalars().all()}
  645. # Get max position for queue ordering
  646. pos_result = await db.execute(select(func.coalesce(func.max(PrintQueueItem.position), 0)))
  647. max_position = pos_result.scalar() or 0
  648. archive_service = ArchiveService(db)
  649. for file_id in request.file_ids:
  650. lib_file = files.get(file_id)
  651. if not lib_file:
  652. errors.append(AddToQueueError(file_id=file_id, filename="(not found)", error="File not found"))
  653. continue
  654. # Validate file is sliced
  655. if not is_sliced_file(lib_file.filename):
  656. errors.append(
  657. AddToQueueError(
  658. file_id=file_id,
  659. filename=lib_file.filename,
  660. error="Not a sliced file. Only .gcode or .gcode.3mf files can be printed.",
  661. )
  662. )
  663. continue
  664. try:
  665. # Get the full file path
  666. file_path = Path(app_settings.base_dir) / lib_file.file_path
  667. if not file_path.exists():
  668. errors.append(
  669. AddToQueueError(file_id=file_id, filename=lib_file.filename, error="File not found on disk")
  670. )
  671. continue
  672. # Create archive from the library file
  673. archive = await archive_service.archive_print(
  674. printer_id=None, # Unassigned
  675. source_file=file_path,
  676. )
  677. if not archive:
  678. errors.append(
  679. AddToQueueError(file_id=file_id, filename=lib_file.filename, error="Failed to create archive")
  680. )
  681. continue
  682. # Create queue item
  683. max_position += 1
  684. queue_item = PrintQueueItem(
  685. printer_id=None, # Unassigned
  686. archive_id=archive.id,
  687. position=max_position,
  688. status="pending",
  689. )
  690. db.add(queue_item)
  691. await db.flush() # Get queue_item.id
  692. added.append(
  693. AddToQueueResult(
  694. file_id=file_id,
  695. filename=lib_file.filename,
  696. queue_item_id=queue_item.id,
  697. archive_id=archive.id,
  698. )
  699. )
  700. except Exception as e:
  701. logger.exception(f"Error adding file {file_id} to queue")
  702. errors.append(AddToQueueError(file_id=file_id, filename=lib_file.filename, error=str(e)))
  703. await db.commit()
  704. return AddToQueueResponse(added=added, errors=errors)
  705. # ============ File Detail Endpoints ============
  706. @router.get("/files/{file_id}", response_model=FileResponseSchema)
  707. async def get_file(file_id: int, db: AsyncSession = Depends(get_db)):
  708. """Get a file by ID with full details."""
  709. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  710. file = result.scalar_one_or_none()
  711. if not file:
  712. raise HTTPException(status_code=404, detail="File not found")
  713. # Get folder name
  714. folder_name = None
  715. if file.folder_id:
  716. folder_result = await db.execute(select(LibraryFolder.name).where(LibraryFolder.id == file.folder_id))
  717. folder_name = folder_result.scalar()
  718. # Get project name
  719. project_name = None
  720. if file.project_id:
  721. project_result = await db.execute(select(Project.name).where(Project.id == file.project_id))
  722. project_name = project_result.scalar()
  723. # Get duplicates
  724. duplicates = []
  725. duplicate_count = 0
  726. if file.file_hash:
  727. dup_result = await db.execute(
  728. select(LibraryFile, LibraryFolder.name)
  729. .outerjoin(LibraryFolder, LibraryFile.folder_id == LibraryFolder.id)
  730. .where(LibraryFile.file_hash == file.file_hash, LibraryFile.id != file.id)
  731. )
  732. for dup_file, dup_folder_name in dup_result.all():
  733. duplicates.append(
  734. FileDuplicate(
  735. id=dup_file.id,
  736. filename=dup_file.filename,
  737. folder_id=dup_file.folder_id,
  738. folder_name=dup_folder_name,
  739. created_at=dup_file.created_at,
  740. )
  741. )
  742. duplicate_count = len(duplicates)
  743. return FileResponseSchema(
  744. id=file.id,
  745. folder_id=file.folder_id,
  746. folder_name=folder_name,
  747. project_id=file.project_id,
  748. project_name=project_name,
  749. filename=file.filename,
  750. file_path=file.file_path,
  751. file_type=file.file_type,
  752. file_size=file.file_size,
  753. file_hash=file.file_hash,
  754. thumbnail_path=file.thumbnail_path,
  755. metadata=file.file_metadata,
  756. print_count=file.print_count,
  757. last_printed_at=file.last_printed_at,
  758. notes=file.notes,
  759. duplicates=duplicates if duplicates else None,
  760. duplicate_count=duplicate_count,
  761. created_at=file.created_at,
  762. updated_at=file.updated_at,
  763. )
  764. @router.put("/files/{file_id}", response_model=FileResponseSchema)
  765. async def update_file(file_id: int, data: FileUpdate, db: AsyncSession = Depends(get_db)):
  766. """Update a file's metadata."""
  767. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  768. file = result.scalar_one_or_none()
  769. if not file:
  770. raise HTTPException(status_code=404, detail="File not found")
  771. if data.folder_id is not None:
  772. if data.folder_id == 0:
  773. file.folder_id = None
  774. else:
  775. # Verify folder exists
  776. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.folder_id))
  777. if not folder_result.scalar_one_or_none():
  778. raise HTTPException(status_code=404, detail="Folder not found")
  779. file.folder_id = data.folder_id
  780. if data.project_id is not None:
  781. if data.project_id == 0:
  782. file.project_id = None
  783. else:
  784. # Verify project exists
  785. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  786. if not project_result.scalar_one_or_none():
  787. raise HTTPException(status_code=404, detail="Project not found")
  788. file.project_id = data.project_id
  789. if data.notes is not None:
  790. file.notes = data.notes if data.notes else None
  791. await db.flush()
  792. await db.refresh(file)
  793. # Return full response (reuse get_file logic)
  794. return await get_file(file_id, db)
  795. @router.delete("/files/{file_id}")
  796. async def delete_file(file_id: int, db: AsyncSession = Depends(get_db)):
  797. """Delete a file."""
  798. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  799. file = result.scalar_one_or_none()
  800. if not file:
  801. raise HTTPException(status_code=404, detail="File not found")
  802. # Delete actual files
  803. try:
  804. if file.file_path and os.path.exists(file.file_path):
  805. os.remove(file.file_path)
  806. if file.thumbnail_path and os.path.exists(file.thumbnail_path):
  807. os.remove(file.thumbnail_path)
  808. except Exception as e:
  809. logger.warning(f"Failed to delete file from disk: {e}")
  810. await db.delete(file)
  811. return {"status": "success", "message": "File deleted"}
  812. # ============ File Content Endpoints ============
  813. @router.get("/files/{file_id}/download")
  814. async def download_file(file_id: int, db: AsyncSession = Depends(get_db)):
  815. """Download a file."""
  816. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  817. file = result.scalar_one_or_none()
  818. if not file:
  819. raise HTTPException(status_code=404, detail="File not found")
  820. if not file.file_path or not os.path.exists(file.file_path):
  821. raise HTTPException(status_code=404, detail="File not found on disk")
  822. return FastAPIFileResponse(
  823. file.file_path,
  824. filename=file.filename,
  825. media_type="application/octet-stream",
  826. )
  827. @router.get("/files/{file_id}/thumbnail")
  828. async def get_thumbnail(file_id: int, db: AsyncSession = Depends(get_db)):
  829. """Get a file's thumbnail."""
  830. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  831. file = result.scalar_one_or_none()
  832. if not file:
  833. raise HTTPException(status_code=404, detail="File not found")
  834. if not file.thumbnail_path or not os.path.exists(file.thumbnail_path):
  835. raise HTTPException(status_code=404, detail="Thumbnail not found")
  836. # Detect media type from extension
  837. thumb_ext = os.path.splitext(file.thumbnail_path)[1].lower()
  838. media_types = {
  839. ".png": "image/png",
  840. ".jpg": "image/jpeg",
  841. ".jpeg": "image/jpeg",
  842. ".gif": "image/gif",
  843. ".webp": "image/webp",
  844. }
  845. media_type = media_types.get(thumb_ext, "image/png")
  846. return FastAPIFileResponse(file.thumbnail_path, media_type=media_type)
  847. @router.get("/files/{file_id}/gcode")
  848. async def get_gcode(file_id: int, db: AsyncSession = Depends(get_db)):
  849. """Get gcode for a file (for preview)."""
  850. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  851. file = result.scalar_one_or_none()
  852. if not file:
  853. raise HTTPException(status_code=404, detail="File not found")
  854. if not file.file_path or not os.path.exists(file.file_path):
  855. raise HTTPException(status_code=404, detail="File not found on disk")
  856. if file.file_type == "gcode":
  857. return FastAPIFileResponse(file.file_path, media_type="text/plain")
  858. elif file.file_type == "3mf":
  859. # Extract gcode from 3mf
  860. import zipfile
  861. try:
  862. with zipfile.ZipFile(file.file_path, "r") as zf:
  863. # Find gcode file
  864. gcode_files = [n for n in zf.namelist() if n.endswith(".gcode")]
  865. if not gcode_files:
  866. raise HTTPException(status_code=404, detail="No gcode found in 3MF file")
  867. gcode_content = zf.read(gcode_files[0])
  868. from fastapi.responses import Response
  869. return Response(content=gcode_content, media_type="text/plain")
  870. except zipfile.BadZipFile:
  871. raise HTTPException(status_code=400, detail="Invalid 3MF file")
  872. else:
  873. raise HTTPException(status_code=400, detail="Unsupported file type")
  874. # ============ Bulk Operations ============
  875. @router.post("/files/move")
  876. async def move_files(data: FileMoveRequest, db: AsyncSession = Depends(get_db)):
  877. """Move multiple files to a folder."""
  878. # Verify folder exists if specified
  879. if data.folder_id is not None:
  880. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.folder_id))
  881. if not folder_result.scalar_one_or_none():
  882. raise HTTPException(status_code=404, detail="Folder not found")
  883. # Update files
  884. moved = 0
  885. for file_id in data.file_ids:
  886. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  887. file = result.scalar_one_or_none()
  888. if file:
  889. file.folder_id = data.folder_id
  890. moved += 1
  891. return {"status": "success", "moved": moved}
  892. @router.post("/bulk-delete", response_model=BulkDeleteResponse)
  893. async def bulk_delete(data: BulkDeleteRequest, db: AsyncSession = Depends(get_db)):
  894. """Delete multiple files and/or folders."""
  895. deleted_files = 0
  896. deleted_folders = 0
  897. # Delete files first
  898. for file_id in data.file_ids:
  899. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  900. file = result.scalar_one_or_none()
  901. if file:
  902. try:
  903. if file.file_path and os.path.exists(file.file_path):
  904. os.remove(file.file_path)
  905. if file.thumbnail_path and os.path.exists(file.thumbnail_path):
  906. os.remove(file.thumbnail_path)
  907. except Exception as e:
  908. logger.warning(f"Failed to delete file from disk: {e}")
  909. await db.delete(file)
  910. deleted_files += 1
  911. # Delete folders (cascade will handle contents)
  912. for folder_id in data.folder_ids:
  913. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  914. folder = result.scalar_one_or_none()
  915. if folder:
  916. # Count files that will be deleted
  917. file_count_result = await db.execute(
  918. select(func.count(LibraryFile.id)).where(LibraryFile.folder_id == folder_id)
  919. )
  920. deleted_files += file_count_result.scalar() or 0
  921. await db.delete(folder)
  922. deleted_folders += 1
  923. return BulkDeleteResponse(deleted_files=deleted_files, deleted_folders=deleted_folders)
  924. # ============ Stats Endpoint ============
  925. @router.get("/stats")
  926. async def get_library_stats(db: AsyncSession = Depends(get_db)):
  927. """Get library statistics."""
  928. # Total files
  929. total_files_result = await db.execute(select(func.count(LibraryFile.id)))
  930. total_files = total_files_result.scalar() or 0
  931. # Total folders
  932. total_folders_result = await db.execute(select(func.count(LibraryFolder.id)))
  933. total_folders = total_folders_result.scalar() or 0
  934. # Total size
  935. total_size_result = await db.execute(select(func.sum(LibraryFile.file_size)))
  936. total_size = total_size_result.scalar() or 0
  937. # Files by type
  938. type_result = await db.execute(
  939. select(LibraryFile.file_type, func.count(LibraryFile.id)).group_by(LibraryFile.file_type)
  940. )
  941. files_by_type = dict(type_result.all())
  942. # Total prints
  943. total_prints_result = await db.execute(select(func.sum(LibraryFile.print_count)))
  944. total_prints = total_prints_result.scalar() or 0
  945. # Disk space info
  946. library_dir = get_library_dir()
  947. try:
  948. disk_stat = shutil.disk_usage(library_dir)
  949. disk_free_bytes = disk_stat.free
  950. disk_total_bytes = disk_stat.total
  951. disk_used_bytes = disk_stat.used
  952. except Exception:
  953. disk_free_bytes = 0
  954. disk_total_bytes = 0
  955. disk_used_bytes = 0
  956. return {
  957. "total_files": total_files,
  958. "total_folders": total_folders,
  959. "total_size_bytes": total_size,
  960. "files_by_type": files_by_type,
  961. "total_prints": total_prints,
  962. "disk_free_bytes": disk_free_bytes,
  963. "disk_total_bytes": disk_total_bytes,
  964. "disk_used_bytes": disk_used_bytes,
  965. }