library.py 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  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, Response, 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. BatchThumbnailRequest,
  26. BatchThumbnailResponse,
  27. BatchThumbnailResult,
  28. BulkDeleteRequest,
  29. BulkDeleteResponse,
  30. FileDuplicate,
  31. FileListResponse,
  32. FileMoveRequest,
  33. FilePrintRequest,
  34. FileResponse as FileResponseSchema,
  35. FileUpdate,
  36. FileUploadResponse,
  37. FolderCreate,
  38. FolderResponse,
  39. FolderTreeItem,
  40. FolderUpdate,
  41. ZipExtractError,
  42. ZipExtractResponse,
  43. ZipExtractResult,
  44. )
  45. from backend.app.services.archive import ArchiveService, ThreeMFParser
  46. logger = logging.getLogger(__name__)
  47. router = APIRouter(prefix="/library", tags=["library"])
  48. def get_library_dir() -> Path:
  49. """Get the library storage directory."""
  50. base_dir = Path(app_settings.archive_dir)
  51. library_dir = base_dir / "library"
  52. library_dir.mkdir(parents=True, exist_ok=True)
  53. return library_dir
  54. def get_library_files_dir() -> Path:
  55. """Get the directory for library files."""
  56. files_dir = get_library_dir() / "files"
  57. files_dir.mkdir(parents=True, exist_ok=True)
  58. return files_dir
  59. def get_library_thumbnails_dir() -> Path:
  60. """Get the directory for library thumbnails."""
  61. thumbnails_dir = get_library_dir() / "thumbnails"
  62. thumbnails_dir.mkdir(parents=True, exist_ok=True)
  63. return thumbnails_dir
  64. def calculate_file_hash(file_path: Path) -> str:
  65. """Calculate SHA256 hash of a file."""
  66. sha256_hash = hashlib.sha256()
  67. with open(file_path, "rb") as f:
  68. for byte_block in iter(lambda: f.read(4096), b""):
  69. sha256_hash.update(byte_block)
  70. return sha256_hash.hexdigest()
  71. def extract_gcode_thumbnail(file_path: Path) -> bytes | None:
  72. """Extract embedded thumbnail from gcode file.
  73. Supports PrusaSlicer/BambuStudio format:
  74. ; thumbnail begin WxH SIZE
  75. ; base64data...
  76. ; thumbnail end
  77. """
  78. try:
  79. thumbnail_data = None
  80. in_thumbnail = False
  81. thumbnail_lines = []
  82. best_size = 0
  83. with open(file_path, errors="ignore") as f:
  84. # Only read first 50KB for performance (thumbnails are at the start)
  85. content = f.read(50000)
  86. for line in content.split("\n"):
  87. line = line.strip()
  88. # Check for thumbnail start
  89. if line.startswith("; thumbnail begin"):
  90. in_thumbnail = True
  91. thumbnail_lines = []
  92. # Parse dimensions: "; thumbnail begin 300x300 12345"
  93. match = re.search(r"(\d+)x(\d+)", line)
  94. if match:
  95. width = int(match.group(1))
  96. # Prefer larger thumbnails (up to 300px)
  97. if width > best_size and width <= 300:
  98. best_size = width
  99. continue
  100. # Check for thumbnail end
  101. if line.startswith("; thumbnail end"):
  102. if in_thumbnail and thumbnail_lines:
  103. try:
  104. # Decode the base64 data
  105. b64_data = "".join(thumbnail_lines)
  106. decoded = base64.b64decode(b64_data)
  107. # Only keep if this is the best size or first valid thumbnail
  108. if thumbnail_data is None or best_size > 0:
  109. thumbnail_data = decoded
  110. except Exception:
  111. pass
  112. in_thumbnail = False
  113. thumbnail_lines = []
  114. continue
  115. # Collect thumbnail data
  116. if in_thumbnail and line.startswith(";"):
  117. # Remove the leading "; " or ";"
  118. data_line = line[1:].strip()
  119. if data_line:
  120. thumbnail_lines.append(data_line)
  121. return thumbnail_data
  122. except Exception as e:
  123. logger.warning(f"Failed to extract gcode thumbnail: {e}")
  124. return None
  125. def create_image_thumbnail(file_path: Path, thumbnails_dir: Path, max_size: int = 256) -> str | None:
  126. """Create a thumbnail from an image file.
  127. For small images, copies directly. For larger images, resizes.
  128. Returns the thumbnail path or None on failure.
  129. """
  130. try:
  131. from PIL import Image
  132. thumb_filename = f"{uuid.uuid4().hex}.png"
  133. thumb_path = thumbnails_dir / thumb_filename
  134. with Image.open(file_path) as img:
  135. # Convert to RGB if necessary (for PNG with transparency, etc.)
  136. if img.mode in ("RGBA", "LA", "P"):
  137. # Create white background for transparency
  138. background = Image.new("RGB", img.size, (255, 255, 255))
  139. if img.mode == "P":
  140. img = img.convert("RGBA")
  141. background.paste(img, mask=img.split()[-1] if img.mode == "RGBA" else None)
  142. img = background
  143. elif img.mode != "RGB":
  144. img = img.convert("RGB")
  145. # Resize if larger than max_size
  146. if img.width > max_size or img.height > max_size:
  147. img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
  148. img.save(thumb_path, "PNG", optimize=True)
  149. return str(thumb_path)
  150. except ImportError:
  151. # PIL not installed, just copy the file if it's small enough
  152. logger.warning("PIL not installed, copying image as thumbnail")
  153. try:
  154. file_size = file_path.stat().st_size
  155. if file_size < 500000: # Less than 500KB
  156. thumb_filename = f"{uuid.uuid4().hex}{file_path.suffix}"
  157. thumb_path = thumbnails_dir / thumb_filename
  158. shutil.copy2(file_path, thumb_path)
  159. return str(thumb_path)
  160. except Exception:
  161. pass
  162. return None
  163. except Exception as e:
  164. logger.warning(f"Failed to create image thumbnail: {e}")
  165. return None
  166. # Supported image extensions for thumbnails
  167. IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".tiff", ".tif"}
  168. # ============ Folder Endpoints ============
  169. @router.get("/folders", response_model=list[FolderTreeItem])
  170. @router.get("/folders/", response_model=list[FolderTreeItem])
  171. async def list_folders(response: Response, db: AsyncSession = Depends(get_db)):
  172. """Get all folders as a tree structure."""
  173. # Prevent browser caching of folder list
  174. response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
  175. # Get all folders with project and archive joins
  176. result = await db.execute(
  177. select(LibraryFolder, Project.name, PrintArchive.print_name)
  178. .outerjoin(Project, LibraryFolder.project_id == Project.id)
  179. .outerjoin(PrintArchive, LibraryFolder.archive_id == PrintArchive.id)
  180. .order_by(LibraryFolder.name)
  181. )
  182. rows = result.all()
  183. # Get file counts per folder
  184. file_counts_result = await db.execute(
  185. select(LibraryFile.folder_id, func.count(LibraryFile.id))
  186. .where(LibraryFile.folder_id.isnot(None))
  187. .group_by(LibraryFile.folder_id)
  188. )
  189. file_counts = dict(file_counts_result.all())
  190. # Build tree structure
  191. folder_map = {}
  192. root_folders = []
  193. for folder, project_name, archive_name in rows:
  194. folder_item = FolderTreeItem(
  195. id=folder.id,
  196. name=folder.name,
  197. parent_id=folder.parent_id,
  198. project_id=folder.project_id,
  199. archive_id=folder.archive_id,
  200. project_name=project_name,
  201. archive_name=archive_name,
  202. file_count=file_counts.get(folder.id, 0),
  203. children=[],
  204. )
  205. folder_map[folder.id] = folder_item
  206. # Link children to parents
  207. for folder, _, _ in rows:
  208. folder_item = folder_map[folder.id]
  209. if folder.parent_id is None:
  210. root_folders.append(folder_item)
  211. elif folder.parent_id in folder_map:
  212. folder_map[folder.parent_id].children.append(folder_item)
  213. return root_folders
  214. @router.get("/folders/by-project/{project_id}", response_model=list[FolderResponse])
  215. async def get_folders_by_project(project_id: int, db: AsyncSession = Depends(get_db)):
  216. """Get all folders linked to a specific project."""
  217. result = await db.execute(
  218. select(LibraryFolder, Project.name)
  219. .outerjoin(Project, LibraryFolder.project_id == Project.id)
  220. .where(LibraryFolder.project_id == project_id)
  221. .order_by(LibraryFolder.name)
  222. )
  223. rows = result.all()
  224. folders = []
  225. for folder, project_name in rows:
  226. # Get file count
  227. file_count_result = await db.execute(
  228. select(func.count(LibraryFile.id)).where(LibraryFile.folder_id == folder.id)
  229. )
  230. file_count = file_count_result.scalar() or 0
  231. folders.append(
  232. FolderResponse(
  233. id=folder.id,
  234. name=folder.name,
  235. parent_id=folder.parent_id,
  236. project_id=folder.project_id,
  237. archive_id=folder.archive_id,
  238. project_name=project_name,
  239. archive_name=None,
  240. file_count=file_count,
  241. created_at=folder.created_at,
  242. updated_at=folder.updated_at,
  243. )
  244. )
  245. return folders
  246. @router.get("/folders/by-archive/{archive_id}", response_model=list[FolderResponse])
  247. async def get_folders_by_archive(archive_id: int, db: AsyncSession = Depends(get_db)):
  248. """Get all folders linked to a specific archive."""
  249. result = await db.execute(
  250. select(LibraryFolder, PrintArchive.print_name)
  251. .outerjoin(PrintArchive, LibraryFolder.archive_id == PrintArchive.id)
  252. .where(LibraryFolder.archive_id == archive_id)
  253. .order_by(LibraryFolder.name)
  254. )
  255. rows = result.all()
  256. folders = []
  257. for folder, archive_name in rows:
  258. # Get file count
  259. file_count_result = await db.execute(
  260. select(func.count(LibraryFile.id)).where(LibraryFile.folder_id == folder.id)
  261. )
  262. file_count = file_count_result.scalar() or 0
  263. folders.append(
  264. FolderResponse(
  265. id=folder.id,
  266. name=folder.name,
  267. parent_id=folder.parent_id,
  268. project_id=folder.project_id,
  269. archive_id=folder.archive_id,
  270. project_name=None,
  271. archive_name=archive_name,
  272. file_count=file_count,
  273. created_at=folder.created_at,
  274. updated_at=folder.updated_at,
  275. )
  276. )
  277. return folders
  278. @router.post("/folders", response_model=FolderResponse)
  279. @router.post("/folders/", response_model=FolderResponse)
  280. async def create_folder(data: FolderCreate, db: AsyncSession = Depends(get_db)):
  281. """Create a new folder."""
  282. # Verify parent exists if specified
  283. if data.parent_id is not None:
  284. parent_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.parent_id))
  285. if not parent_result.scalar_one_or_none():
  286. raise HTTPException(status_code=404, detail="Parent folder not found")
  287. # Verify project exists if specified
  288. project_name = None
  289. if data.project_id is not None:
  290. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  291. project = project_result.scalar_one_or_none()
  292. if not project:
  293. raise HTTPException(status_code=404, detail="Project not found")
  294. project_name = project.name
  295. # Verify archive exists if specified
  296. archive_name = None
  297. if data.archive_id is not None:
  298. archive_result = await db.execute(select(PrintArchive).where(PrintArchive.id == data.archive_id))
  299. archive = archive_result.scalar_one_or_none()
  300. if not archive:
  301. raise HTTPException(status_code=404, detail="Archive not found")
  302. archive_name = archive.print_name
  303. folder = LibraryFolder(
  304. name=data.name,
  305. parent_id=data.parent_id,
  306. project_id=data.project_id,
  307. archive_id=data.archive_id,
  308. )
  309. db.add(folder)
  310. await db.flush()
  311. await db.refresh(folder)
  312. return FolderResponse(
  313. id=folder.id,
  314. name=folder.name,
  315. parent_id=folder.parent_id,
  316. project_id=folder.project_id,
  317. archive_id=folder.archive_id,
  318. project_name=project_name,
  319. archive_name=archive_name,
  320. file_count=0,
  321. created_at=folder.created_at,
  322. updated_at=folder.updated_at,
  323. )
  324. @router.get("/folders/{folder_id}", response_model=FolderResponse)
  325. async def get_folder(folder_id: int, db: AsyncSession = Depends(get_db)):
  326. """Get a folder by ID."""
  327. result = await db.execute(
  328. select(LibraryFolder, Project.name, PrintArchive.print_name)
  329. .outerjoin(Project, LibraryFolder.project_id == Project.id)
  330. .outerjoin(PrintArchive, LibraryFolder.archive_id == PrintArchive.id)
  331. .where(LibraryFolder.id == folder_id)
  332. )
  333. row = result.one_or_none()
  334. if not row:
  335. raise HTTPException(status_code=404, detail="Folder not found")
  336. folder, project_name, archive_name = row
  337. # Get file count
  338. file_count_result = await db.execute(select(func.count(LibraryFile.id)).where(LibraryFile.folder_id == folder_id))
  339. file_count = file_count_result.scalar() or 0
  340. return FolderResponse(
  341. id=folder.id,
  342. name=folder.name,
  343. parent_id=folder.parent_id,
  344. project_id=folder.project_id,
  345. archive_id=folder.archive_id,
  346. project_name=project_name,
  347. archive_name=archive_name,
  348. file_count=file_count,
  349. created_at=folder.created_at,
  350. updated_at=folder.updated_at,
  351. )
  352. @router.put("/folders/{folder_id}", response_model=FolderResponse)
  353. async def update_folder(folder_id: int, data: FolderUpdate, db: AsyncSession = Depends(get_db)):
  354. """Update a folder."""
  355. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  356. folder = result.scalar_one_or_none()
  357. if not folder:
  358. raise HTTPException(status_code=404, detail="Folder not found")
  359. if data.name is not None:
  360. folder.name = data.name
  361. if data.parent_id is not None:
  362. # Prevent circular reference
  363. if data.parent_id == folder_id:
  364. raise HTTPException(status_code=400, detail="Folder cannot be its own parent")
  365. # Check for circular reference in ancestors
  366. if data.parent_id != 0: # 0 means move to root
  367. current_id = data.parent_id
  368. while current_id is not None:
  369. if current_id == folder_id:
  370. raise HTTPException(status_code=400, detail="Cannot move folder into its own subtree")
  371. parent_result = await db.execute(select(LibraryFolder.parent_id).where(LibraryFolder.id == current_id))
  372. current_id = parent_result.scalar()
  373. folder.parent_id = data.parent_id
  374. else:
  375. folder.parent_id = None
  376. # Update project_id (0 to unlink)
  377. if data.project_id is not None:
  378. if data.project_id == 0:
  379. folder.project_id = None
  380. else:
  381. # Verify project exists
  382. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  383. if not project_result.scalar_one_or_none():
  384. raise HTTPException(status_code=404, detail="Project not found")
  385. folder.project_id = data.project_id
  386. # Update archive_id (0 to unlink)
  387. if data.archive_id is not None:
  388. if data.archive_id == 0:
  389. folder.archive_id = None
  390. else:
  391. # Verify archive exists
  392. archive_result = await db.execute(select(PrintArchive).where(PrintArchive.id == data.archive_id))
  393. if not archive_result.scalar_one_or_none():
  394. raise HTTPException(status_code=404, detail="Archive not found")
  395. folder.archive_id = data.archive_id
  396. await db.flush()
  397. await db.refresh(folder)
  398. # Get file count and names
  399. file_count_result = await db.execute(select(func.count(LibraryFile.id)).where(LibraryFile.folder_id == folder_id))
  400. file_count = file_count_result.scalar() or 0
  401. # Get project and archive names
  402. project_name = None
  403. archive_name = None
  404. if folder.project_id:
  405. project_result = await db.execute(select(Project.name).where(Project.id == folder.project_id))
  406. project_name = project_result.scalar()
  407. if folder.archive_id:
  408. archive_result = await db.execute(select(PrintArchive.print_name).where(PrintArchive.id == folder.archive_id))
  409. archive_name = archive_result.scalar()
  410. return FolderResponse(
  411. id=folder.id,
  412. name=folder.name,
  413. parent_id=folder.parent_id,
  414. project_id=folder.project_id,
  415. archive_id=folder.archive_id,
  416. project_name=project_name,
  417. archive_name=archive_name,
  418. file_count=file_count,
  419. created_at=folder.created_at,
  420. updated_at=folder.updated_at,
  421. )
  422. @router.delete("/folders/{folder_id}")
  423. async def delete_folder(folder_id: int, db: AsyncSession = Depends(get_db)):
  424. """Delete a folder and all its contents (cascade)."""
  425. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  426. folder = result.scalar_one_or_none()
  427. if not folder:
  428. raise HTTPException(status_code=404, detail="Folder not found")
  429. # Get all files in this folder and subfolders to delete from disk
  430. async def get_all_file_ids(fid: int) -> list[int]:
  431. """Recursively get all file IDs in a folder tree."""
  432. file_ids = []
  433. # Get files in this folder
  434. files_result = await db.execute(
  435. select(LibraryFile.id, LibraryFile.file_path, LibraryFile.thumbnail_path).where(
  436. LibraryFile.folder_id == fid
  437. )
  438. )
  439. for file_id, file_path, thumb_path in files_result.all():
  440. file_ids.append(file_id)
  441. # Delete actual files
  442. try:
  443. if file_path and os.path.exists(file_path):
  444. os.remove(file_path)
  445. if thumb_path and os.path.exists(thumb_path):
  446. os.remove(thumb_path)
  447. except Exception as e:
  448. logger.warning(f"Failed to delete file: {e}")
  449. # Get child folders and recurse
  450. children_result = await db.execute(select(LibraryFolder.id).where(LibraryFolder.parent_id == fid))
  451. for (child_id,) in children_result.all():
  452. file_ids.extend(await get_all_file_ids(child_id))
  453. return file_ids
  454. await get_all_file_ids(folder_id)
  455. # Delete folder (cascade will handle files and subfolders)
  456. await db.delete(folder)
  457. return {"status": "success", "message": "Folder deleted"}
  458. # ============ File Endpoints ============
  459. @router.get("/files", response_model=list[FileListResponse])
  460. @router.get("/files/", response_model=list[FileListResponse])
  461. async def list_files(
  462. response: Response,
  463. folder_id: int | None = None,
  464. include_root: bool = True,
  465. db: AsyncSession = Depends(get_db),
  466. ):
  467. """List files, optionally filtered by folder.
  468. Args:
  469. folder_id: Filter by folder ID. If None and include_root=True, returns root files.
  470. include_root: If True and folder_id is None, returns files at root level.
  471. If False and folder_id is None, returns all files.
  472. """
  473. query = select(LibraryFile)
  474. if folder_id is not None:
  475. query = query.where(LibraryFile.folder_id == folder_id)
  476. elif include_root:
  477. query = query.where(LibraryFile.folder_id.is_(None))
  478. query = query.order_by(LibraryFile.filename)
  479. result = await db.execute(query)
  480. files = result.scalars().all()
  481. # Get duplicate counts
  482. hash_counts = {}
  483. if files:
  484. hashes = [f.file_hash for f in files if f.file_hash]
  485. if hashes:
  486. dup_result = await db.execute(
  487. select(LibraryFile.file_hash, func.count(LibraryFile.id))
  488. .where(LibraryFile.file_hash.in_(hashes))
  489. .group_by(LibraryFile.file_hash)
  490. )
  491. hash_counts = {h: c - 1 for h, c in dup_result.all()} # -1 to exclude self
  492. # Prevent browser caching of file list
  493. response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
  494. file_list = []
  495. for f in files:
  496. # Extract key metadata for display
  497. print_name = None
  498. print_time = None
  499. filament_grams = None
  500. if f.file_metadata:
  501. print_name = f.file_metadata.get("print_name")
  502. print_time = f.file_metadata.get("print_time_seconds")
  503. filament_grams = f.file_metadata.get("filament_used_grams")
  504. file_list.append(
  505. FileListResponse(
  506. id=f.id,
  507. folder_id=f.folder_id,
  508. filename=f.filename,
  509. file_type=f.file_type,
  510. file_size=f.file_size,
  511. thumbnail_path=f.thumbnail_path,
  512. print_count=f.print_count,
  513. duplicate_count=hash_counts.get(f.file_hash, 0) if f.file_hash else 0,
  514. created_at=f.created_at,
  515. print_name=print_name,
  516. print_time_seconds=print_time,
  517. filament_used_grams=filament_grams,
  518. )
  519. )
  520. return file_list
  521. @router.post("/files", response_model=FileUploadResponse)
  522. @router.post("/files/", response_model=FileUploadResponse)
  523. async def upload_file(
  524. file: UploadFile = File(...),
  525. folder_id: int | None = None,
  526. db: AsyncSession = Depends(get_db),
  527. ):
  528. """Upload a file to the library."""
  529. try:
  530. if not file.filename:
  531. raise HTTPException(status_code=400, detail="Filename is required")
  532. filename = file.filename
  533. ext = os.path.splitext(filename)[1].lower()
  534. # Handle files without extension
  535. file_type = ext[1:] if ext else "unknown"
  536. # Verify folder exists if specified
  537. if folder_id is not None:
  538. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  539. if not folder_result.scalar_one_or_none():
  540. raise HTTPException(status_code=404, detail="Folder not found")
  541. # Generate unique filename for storage
  542. unique_filename = f"{uuid.uuid4().hex}{ext}"
  543. file_path = get_library_files_dir() / unique_filename
  544. # Save file
  545. content = await file.read()
  546. with open(file_path, "wb") as f:
  547. f.write(content)
  548. # Calculate hash
  549. file_hash = calculate_file_hash(file_path)
  550. # Check for duplicates
  551. dup_result = await db.execute(select(LibraryFile.id).where(LibraryFile.file_hash == file_hash).limit(1))
  552. duplicate_of = dup_result.scalar()
  553. # Extract metadata and thumbnail
  554. metadata = {}
  555. thumbnail_path = None
  556. thumbnails_dir = get_library_thumbnails_dir()
  557. if ext == ".3mf":
  558. try:
  559. parser = ThreeMFParser(str(file_path))
  560. raw_metadata = parser.parse()
  561. # Extract thumbnail before cleaning metadata
  562. thumbnail_data = raw_metadata.get("_thumbnail_data")
  563. thumbnail_ext = raw_metadata.get("_thumbnail_ext", ".png")
  564. # Save thumbnail if extracted
  565. if thumbnail_data:
  566. thumb_filename = f"{uuid.uuid4().hex}{thumbnail_ext}"
  567. thumb_path = thumbnails_dir / thumb_filename
  568. with open(thumb_path, "wb") as f:
  569. f.write(thumbnail_data)
  570. thumbnail_path = str(thumb_path)
  571. # Clean metadata - remove non-JSON-serializable data (bytes, etc.)
  572. def clean_metadata(obj):
  573. if isinstance(obj, dict):
  574. return {
  575. k: clean_metadata(v)
  576. for k, v in obj.items()
  577. if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext")
  578. }
  579. elif isinstance(obj, list):
  580. return [clean_metadata(i) for i in obj if not isinstance(i, bytes)]
  581. elif isinstance(obj, bytes):
  582. return None
  583. return obj
  584. metadata = clean_metadata(raw_metadata)
  585. except Exception as e:
  586. logger.warning(f"Failed to parse 3MF: {e}")
  587. elif ext == ".gcode":
  588. # Extract embedded thumbnail from gcode
  589. try:
  590. thumbnail_data = extract_gcode_thumbnail(file_path)
  591. if thumbnail_data:
  592. thumb_filename = f"{uuid.uuid4().hex}.png"
  593. thumb_path = thumbnails_dir / thumb_filename
  594. with open(thumb_path, "wb") as f:
  595. f.write(thumbnail_data)
  596. thumbnail_path = str(thumb_path)
  597. except Exception as e:
  598. logger.warning(f"Failed to extract gcode thumbnail: {e}")
  599. elif ext == ".stl":
  600. # Generate thumbnail from STL file
  601. try:
  602. from backend.app.services.stl_thumbnail import generate_stl_thumbnail
  603. thumb_filename = f"{uuid.uuid4().hex}.png"
  604. thumb_path = thumbnails_dir / thumb_filename
  605. if generate_stl_thumbnail(file_path, thumb_path):
  606. thumbnail_path = str(thumb_path)
  607. except Exception as e:
  608. logger.warning(f"Failed to generate STL thumbnail: {e}")
  609. elif ext.lower() in IMAGE_EXTENSIONS:
  610. # For image files, create a thumbnail from the image itself
  611. thumbnail_path = create_image_thumbnail(file_path, thumbnails_dir)
  612. # Create database entry
  613. library_file = LibraryFile(
  614. folder_id=folder_id,
  615. filename=filename,
  616. file_path=str(file_path),
  617. file_type=file_type,
  618. file_size=len(content),
  619. file_hash=file_hash,
  620. thumbnail_path=thumbnail_path,
  621. file_metadata=metadata if metadata else None,
  622. )
  623. db.add(library_file)
  624. await db.flush()
  625. await db.refresh(library_file)
  626. return FileUploadResponse(
  627. id=library_file.id,
  628. filename=library_file.filename,
  629. file_type=library_file.file_type,
  630. file_size=library_file.file_size,
  631. thumbnail_path=library_file.thumbnail_path,
  632. duplicate_of=duplicate_of,
  633. metadata=library_file.file_metadata,
  634. )
  635. except HTTPException:
  636. raise
  637. except Exception as e:
  638. logger.error(f"Upload failed for {file.filename}: {e}", exc_info=True)
  639. raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)}")
  640. @router.post("/files/extract-zip", response_model=ZipExtractResponse)
  641. async def extract_zip_file(
  642. file: UploadFile = File(...),
  643. folder_id: int | None = None,
  644. preserve_structure: bool = True,
  645. db: AsyncSession = Depends(get_db),
  646. ):
  647. """Upload and extract a ZIP file to the library.
  648. Args:
  649. file: The ZIP file to extract
  650. folder_id: Target folder ID (None = root)
  651. preserve_structure: If True, recreate folder structure from ZIP; if False, extract all files flat
  652. """
  653. import tempfile
  654. import zipfile
  655. if not file.filename or not file.filename.lower().endswith(".zip"):
  656. raise HTTPException(status_code=400, detail="Only ZIP files are supported")
  657. # Verify target folder exists if specified
  658. if folder_id is not None:
  659. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  660. if not folder_result.scalar_one_or_none():
  661. raise HTTPException(status_code=404, detail="Target folder not found")
  662. # Save ZIP to temp file
  663. try:
  664. with tempfile.NamedTemporaryFile(delete=False, suffix=".zip") as tmp:
  665. content = await file.read()
  666. tmp.write(content)
  667. tmp_path = tmp.name
  668. except Exception as e:
  669. raise HTTPException(status_code=500, detail=f"Failed to save ZIP file: {str(e)}")
  670. extracted_files: list[ZipExtractResult] = []
  671. errors: list[ZipExtractError] = []
  672. folders_created = 0
  673. folder_cache: dict[str, int] = {} # path -> folder_id
  674. try:
  675. with zipfile.ZipFile(tmp_path, "r") as zf:
  676. # Filter out directories and hidden/system files
  677. file_list = [
  678. name
  679. for name in zf.namelist()
  680. if not name.endswith("/")
  681. and not name.startswith("__MACOSX")
  682. and not os.path.basename(name).startswith(".")
  683. ]
  684. for zip_path in file_list:
  685. try:
  686. # Determine target folder
  687. target_folder_id = folder_id
  688. if preserve_structure:
  689. # Get directory path from ZIP
  690. dir_path = os.path.dirname(zip_path)
  691. if dir_path:
  692. # Create folder structure
  693. parts = dir_path.split("/")
  694. current_parent = folder_id
  695. current_path = ""
  696. for part in parts:
  697. if not part:
  698. continue
  699. current_path = f"{current_path}/{part}" if current_path else part
  700. if current_path in folder_cache:
  701. current_parent = folder_cache[current_path]
  702. else:
  703. # Check if folder exists
  704. existing = await db.execute(
  705. select(LibraryFolder).where(
  706. LibraryFolder.name == part,
  707. LibraryFolder.parent_id == current_parent
  708. if current_parent
  709. else LibraryFolder.parent_id.is_(None),
  710. )
  711. )
  712. existing_folder = existing.scalar_one_or_none()
  713. if existing_folder:
  714. current_parent = existing_folder.id
  715. else:
  716. # Create folder
  717. new_folder = LibraryFolder(name=part, parent_id=current_parent)
  718. db.add(new_folder)
  719. await db.flush()
  720. current_parent = new_folder.id
  721. folders_created += 1
  722. folder_cache[current_path] = current_parent
  723. target_folder_id = current_parent
  724. # Extract file
  725. filename = os.path.basename(zip_path)
  726. ext = os.path.splitext(filename)[1].lower()
  727. file_type = ext[1:] if ext else "unknown"
  728. # Generate unique filename for storage
  729. unique_filename = f"{uuid.uuid4().hex}{ext}"
  730. file_path = get_library_files_dir() / unique_filename
  731. # Extract and save file
  732. file_content = zf.read(zip_path)
  733. with open(file_path, "wb") as f:
  734. f.write(file_content)
  735. # Calculate hash
  736. file_hash = calculate_file_hash(file_path)
  737. # Extract metadata and thumbnail for 3MF files
  738. metadata = {}
  739. thumbnail_path = None
  740. thumbnails_dir = get_library_thumbnails_dir()
  741. if ext == ".3mf":
  742. try:
  743. parser = ThreeMFParser(str(file_path))
  744. raw_metadata = parser.parse()
  745. thumbnail_data = raw_metadata.get("_thumbnail_data")
  746. thumbnail_ext = raw_metadata.get("_thumbnail_ext", ".png")
  747. if thumbnail_data:
  748. thumb_filename = f"{uuid.uuid4().hex}{thumbnail_ext}"
  749. thumb_path = thumbnails_dir / thumb_filename
  750. with open(thumb_path, "wb") as f:
  751. f.write(thumbnail_data)
  752. thumbnail_path = str(thumb_path)
  753. def clean_metadata(obj):
  754. if isinstance(obj, dict):
  755. return {
  756. k: clean_metadata(v)
  757. for k, v in obj.items()
  758. if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext")
  759. }
  760. elif isinstance(obj, list):
  761. return [clean_metadata(i) for i in obj if not isinstance(i, bytes)]
  762. elif isinstance(obj, bytes):
  763. return None
  764. return obj
  765. metadata = clean_metadata(raw_metadata)
  766. except Exception as e:
  767. logger.warning(f"Failed to parse 3MF from ZIP: {e}")
  768. elif ext == ".gcode":
  769. try:
  770. thumbnail_data = extract_gcode_thumbnail(file_path)
  771. if thumbnail_data:
  772. thumb_filename = f"{uuid.uuid4().hex}.png"
  773. thumb_path = thumbnails_dir / thumb_filename
  774. with open(thumb_path, "wb") as f:
  775. f.write(thumbnail_data)
  776. thumbnail_path = str(thumb_path)
  777. except Exception as e:
  778. logger.warning(f"Failed to extract gcode thumbnail from ZIP: {e}")
  779. elif ext == ".stl":
  780. try:
  781. from backend.app.services.stl_thumbnail import generate_stl_thumbnail
  782. thumb_filename = f"{uuid.uuid4().hex}.png"
  783. thumb_path = thumbnails_dir / thumb_filename
  784. if generate_stl_thumbnail(file_path, thumb_path):
  785. thumbnail_path = str(thumb_path)
  786. except Exception as e:
  787. logger.warning(f"Failed to generate STL thumbnail from ZIP: {e}")
  788. elif ext.lower() in IMAGE_EXTENSIONS:
  789. thumbnail_path = create_image_thumbnail(file_path, thumbnails_dir)
  790. # Create database entry
  791. library_file = LibraryFile(
  792. folder_id=target_folder_id,
  793. filename=filename,
  794. file_path=str(file_path),
  795. file_type=file_type,
  796. file_size=len(file_content),
  797. file_hash=file_hash,
  798. thumbnail_path=thumbnail_path,
  799. file_metadata=metadata if metadata else None,
  800. )
  801. db.add(library_file)
  802. await db.flush()
  803. await db.refresh(library_file)
  804. extracted_files.append(
  805. ZipExtractResult(
  806. filename=filename,
  807. file_id=library_file.id,
  808. folder_id=target_folder_id,
  809. )
  810. )
  811. # Commit after each file to release database lock
  812. # This prevents long-running transactions from blocking other requests
  813. await db.commit()
  814. except Exception as e:
  815. logger.error(f"Failed to extract {zip_path}: {e}")
  816. errors.append(ZipExtractError(filename=os.path.basename(zip_path), error=str(e)))
  817. # Rollback the failed file but continue with others
  818. await db.rollback()
  819. return ZipExtractResponse(
  820. extracted=len(extracted_files),
  821. folders_created=folders_created,
  822. files=extracted_files,
  823. errors=errors,
  824. )
  825. except zipfile.BadZipFile:
  826. raise HTTPException(status_code=400, detail="Invalid or corrupted ZIP file")
  827. except Exception as e:
  828. logger.error(f"ZIP extraction failed: {e}", exc_info=True)
  829. raise HTTPException(status_code=500, detail=f"ZIP extraction failed: {str(e)}")
  830. finally:
  831. # Clean up temp file
  832. try:
  833. os.unlink(tmp_path)
  834. except Exception:
  835. pass
  836. # ============ Queue Operations ============
  837. # NOTE: These routes must be defined BEFORE /files/{file_id} to avoid path parameter conflicts
  838. def is_sliced_file(filename: str) -> bool:
  839. """Check if a file is a sliced (printable) file.
  840. Sliced files are:
  841. - .gcode files
  842. - .3mf files that contain '.gcode.' in the name (e.g., filename.gcode.3mf)
  843. """
  844. lower = filename.lower()
  845. return lower.endswith(".gcode") or ".gcode." in lower
  846. @router.post("/files/add-to-queue", response_model=AddToQueueResponse)
  847. async def add_files_to_queue(
  848. request: AddToQueueRequest,
  849. db: AsyncSession = Depends(get_db),
  850. ):
  851. """Add library files to the print queue.
  852. Only sliced files (.gcode or .gcode.3mf) can be added to the queue.
  853. The archive will be created automatically when the print starts.
  854. """
  855. added: list[AddToQueueResult] = []
  856. errors: list[AddToQueueError] = []
  857. # Get all requested files
  858. result = await db.execute(select(LibraryFile).where(LibraryFile.id.in_(request.file_ids)))
  859. files = {f.id: f for f in result.scalars().all()}
  860. # Get max position for queue ordering
  861. pos_result = await db.execute(select(func.coalesce(func.max(PrintQueueItem.position), 0)))
  862. max_position = pos_result.scalar() or 0
  863. for file_id in request.file_ids:
  864. lib_file = files.get(file_id)
  865. if not lib_file:
  866. errors.append(AddToQueueError(file_id=file_id, filename="(not found)", error="File not found"))
  867. continue
  868. # Validate file is sliced
  869. if not is_sliced_file(lib_file.filename):
  870. errors.append(
  871. AddToQueueError(
  872. file_id=file_id,
  873. filename=lib_file.filename,
  874. error="Not a sliced file. Only .gcode or .gcode.3mf files can be printed.",
  875. )
  876. )
  877. continue
  878. try:
  879. # Verify file exists on disk
  880. file_path = Path(app_settings.base_dir) / lib_file.file_path
  881. if not file_path.exists():
  882. errors.append(
  883. AddToQueueError(file_id=file_id, filename=lib_file.filename, error="File not found on disk")
  884. )
  885. continue
  886. # Create queue item referencing library file (archive created at print start)
  887. max_position += 1
  888. queue_item = PrintQueueItem(
  889. printer_id=None, # Unassigned
  890. library_file_id=file_id,
  891. position=max_position,
  892. status="pending",
  893. )
  894. db.add(queue_item)
  895. await db.flush() # Get queue_item.id
  896. added.append(
  897. AddToQueueResult(
  898. file_id=file_id,
  899. filename=lib_file.filename,
  900. queue_item_id=queue_item.id,
  901. )
  902. )
  903. except Exception as e:
  904. logger.exception(f"Error adding file {file_id} to queue")
  905. errors.append(AddToQueueError(file_id=file_id, filename=lib_file.filename, error=str(e)))
  906. await db.commit()
  907. return AddToQueueResponse(added=added, errors=errors)
  908. @router.get("/files/{file_id}/plates")
  909. async def get_library_file_plates(
  910. file_id: int,
  911. db: AsyncSession = Depends(get_db),
  912. ):
  913. """Get available plates from a multi-plate 3MF library file.
  914. Returns a list of plates with their index, name, thumbnail availability,
  915. and filament requirements. For single-plate exports, returns a single plate.
  916. """
  917. import xml.etree.ElementTree as ET
  918. import zipfile
  919. # Get the library file
  920. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  921. lib_file = result.scalar_one_or_none()
  922. if not lib_file:
  923. raise HTTPException(status_code=404, detail="File not found")
  924. file_path = Path(app_settings.base_dir) / lib_file.file_path
  925. if not file_path.exists():
  926. raise HTTPException(status_code=404, detail="File not found on disk")
  927. # Only 3MF files have plates
  928. if not lib_file.filename.lower().endswith(".3mf"):
  929. return {"file_id": file_id, "filename": lib_file.filename, "plates": [], "is_multi_plate": False}
  930. plates = []
  931. try:
  932. with zipfile.ZipFile(file_path, "r") as zf:
  933. namelist = zf.namelist()
  934. # Find all plate gcode files to determine available plates
  935. gcode_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".gcode")]
  936. if not gcode_files:
  937. # No sliced plates found
  938. return {"file_id": file_id, "filename": lib_file.filename, "plates": [], "is_multi_plate": False}
  939. # Extract plate indices from gcode filenames
  940. plate_indices = []
  941. for gf in gcode_files:
  942. try:
  943. plate_str = gf[15:-6] # Remove "Metadata/plate_" and ".gcode"
  944. plate_indices.append(int(plate_str))
  945. except ValueError:
  946. pass
  947. plate_indices.sort()
  948. # Parse model_settings.config for plate names
  949. plate_names = {}
  950. if "Metadata/model_settings.config" in namelist:
  951. try:
  952. model_content = zf.read("Metadata/model_settings.config").decode()
  953. model_root = ET.fromstring(model_content)
  954. for plate_elem in model_root.findall(".//plate"):
  955. plater_id = None
  956. plater_name = None
  957. for meta in plate_elem.findall("metadata"):
  958. key = meta.get("key")
  959. value = meta.get("value")
  960. if key == "plater_id" and value:
  961. try:
  962. plater_id = int(value)
  963. except ValueError:
  964. pass
  965. elif key == "plater_name" and value:
  966. plater_name = value.strip()
  967. if plater_id is not None and plater_name:
  968. plate_names[plater_id] = plater_name
  969. except Exception:
  970. pass
  971. # Parse slice_info.config for plate metadata
  972. plate_metadata = {}
  973. if "Metadata/slice_info.config" in namelist:
  974. content = zf.read("Metadata/slice_info.config").decode()
  975. root = ET.fromstring(content)
  976. for plate_elem in root.findall(".//plate"):
  977. plate_info = {"filaments": [], "prediction": None, "weight": None, "name": None, "objects": []}
  978. plate_index = None
  979. for meta in plate_elem.findall("metadata"):
  980. key = meta.get("key")
  981. value = meta.get("value")
  982. if key == "index" and value:
  983. try:
  984. plate_index = int(value)
  985. except ValueError:
  986. pass
  987. elif key == "prediction" and value:
  988. try:
  989. plate_info["prediction"] = int(value)
  990. except ValueError:
  991. pass
  992. elif key == "weight" and value:
  993. try:
  994. plate_info["weight"] = float(value)
  995. except ValueError:
  996. pass
  997. # Get filaments used in this plate
  998. for filament_elem in plate_elem.findall("filament"):
  999. filament_id = filament_elem.get("id")
  1000. filament_type = filament_elem.get("type", "")
  1001. filament_color = filament_elem.get("color", "")
  1002. used_g = filament_elem.get("used_g", "0")
  1003. used_m = filament_elem.get("used_m", "0")
  1004. try:
  1005. used_grams = float(used_g)
  1006. except (ValueError, TypeError):
  1007. used_grams = 0
  1008. if used_grams > 0 and filament_id:
  1009. plate_info["filaments"].append(
  1010. {
  1011. "slot_id": int(filament_id),
  1012. "type": filament_type,
  1013. "color": filament_color,
  1014. "used_grams": round(used_grams, 1),
  1015. "used_meters": float(used_m) if used_m else 0,
  1016. }
  1017. )
  1018. plate_info["filaments"].sort(key=lambda x: x["slot_id"])
  1019. # Collect object names
  1020. for obj_elem in plate_elem.findall("object"):
  1021. obj_name = obj_elem.get("name")
  1022. if obj_name and obj_name not in plate_info["objects"]:
  1023. plate_info["objects"].append(obj_name)
  1024. # Set plate name
  1025. if plate_index is not None:
  1026. custom_name = plate_names.get(plate_index)
  1027. if custom_name:
  1028. plate_info["name"] = custom_name
  1029. elif plate_info["objects"]:
  1030. plate_info["name"] = plate_info["objects"][0]
  1031. plate_metadata[plate_index] = plate_info
  1032. # Build plate list
  1033. for idx in plate_indices:
  1034. meta = plate_metadata.get(idx, {})
  1035. has_thumbnail = f"Metadata/plate_{idx}.png" in namelist
  1036. plates.append(
  1037. {
  1038. "index": idx,
  1039. "name": meta.get("name"),
  1040. "objects": meta.get("objects", []),
  1041. "has_thumbnail": has_thumbnail,
  1042. "thumbnail_url": f"/api/v1/library/files/{file_id}/plate-thumbnail/{idx}"
  1043. if has_thumbnail
  1044. else None,
  1045. "print_time_seconds": meta.get("prediction"),
  1046. "filament_used_grams": meta.get("weight"),
  1047. "filaments": meta.get("filaments", []),
  1048. }
  1049. )
  1050. except Exception as e:
  1051. logger.warning(f"Failed to parse plates from library file {file_id}: {e}")
  1052. return {
  1053. "file_id": file_id,
  1054. "filename": lib_file.filename,
  1055. "plates": plates,
  1056. "is_multi_plate": len(plates) > 1,
  1057. }
  1058. @router.get("/files/{file_id}/plate-thumbnail/{plate_index}")
  1059. async def get_library_file_plate_thumbnail(
  1060. file_id: int,
  1061. plate_index: int,
  1062. db: AsyncSession = Depends(get_db),
  1063. ):
  1064. """Get the thumbnail image for a specific plate from a library file."""
  1065. import zipfile
  1066. from starlette.responses import Response
  1067. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  1068. lib_file = result.scalar_one_or_none()
  1069. if not lib_file:
  1070. raise HTTPException(status_code=404, detail="File not found")
  1071. file_path = Path(app_settings.base_dir) / lib_file.file_path
  1072. if not file_path.exists():
  1073. raise HTTPException(status_code=404, detail="File not found on disk")
  1074. try:
  1075. with zipfile.ZipFile(file_path, "r") as zf:
  1076. thumb_path = f"Metadata/plate_{plate_index}.png"
  1077. if thumb_path in zf.namelist():
  1078. data = zf.read(thumb_path)
  1079. return Response(content=data, media_type="image/png")
  1080. except Exception:
  1081. pass
  1082. raise HTTPException(status_code=404, detail=f"Thumbnail for plate {plate_index} not found")
  1083. @router.get("/files/{file_id}/filament-requirements")
  1084. async def get_library_file_filament_requirements(
  1085. file_id: int,
  1086. plate_id: int | None = None,
  1087. db: AsyncSession = Depends(get_db),
  1088. ):
  1089. """Get filament requirements from a library file.
  1090. Parses the 3MF file to extract filament slot IDs, types, colors, and usage.
  1091. This enables AMS slot assignment when printing from the file manager.
  1092. Args:
  1093. file_id: The library file ID
  1094. plate_id: Optional plate index to get filaments for a specific plate
  1095. """
  1096. import xml.etree.ElementTree as ET
  1097. import zipfile
  1098. # Get the library file
  1099. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  1100. lib_file = result.scalar_one_or_none()
  1101. if not lib_file:
  1102. raise HTTPException(status_code=404, detail="File not found")
  1103. # Get the full file path
  1104. file_path = Path(app_settings.base_dir) / lib_file.file_path
  1105. if not file_path.exists():
  1106. raise HTTPException(status_code=404, detail="File not found on disk")
  1107. # Only 3MF files have parseable filament info
  1108. if not lib_file.filename.lower().endswith(".3mf"):
  1109. return {"file_id": file_id, "filename": lib_file.filename, "plate_id": plate_id, "filaments": []}
  1110. filaments = []
  1111. try:
  1112. with zipfile.ZipFile(file_path, "r") as zf:
  1113. # Parse slice_info.config for filament requirements
  1114. if "Metadata/slice_info.config" in zf.namelist():
  1115. content = zf.read("Metadata/slice_info.config").decode()
  1116. root = ET.fromstring(content)
  1117. if plate_id is not None:
  1118. # Find filaments for specific plate
  1119. for plate_elem in root.findall(".//plate"):
  1120. # Check if this is the requested plate
  1121. plate_index = None
  1122. for meta in plate_elem.findall("metadata"):
  1123. if meta.get("key") == "index":
  1124. try:
  1125. plate_index = int(meta.get("value", ""))
  1126. except ValueError:
  1127. pass
  1128. break
  1129. if plate_index == plate_id:
  1130. # Extract filaments from this plate
  1131. for filament_elem in plate_elem.findall("filament"):
  1132. filament_id = filament_elem.get("id")
  1133. filament_type = filament_elem.get("type", "")
  1134. filament_color = filament_elem.get("color", "")
  1135. used_g = filament_elem.get("used_g", "0")
  1136. used_m = filament_elem.get("used_m", "0")
  1137. try:
  1138. used_grams = float(used_g)
  1139. except (ValueError, TypeError):
  1140. used_grams = 0
  1141. if used_grams > 0 and filament_id:
  1142. filaments.append(
  1143. {
  1144. "slot_id": int(filament_id),
  1145. "type": filament_type,
  1146. "color": filament_color,
  1147. "used_grams": round(used_grams, 1),
  1148. "used_meters": float(used_m) if used_m else 0,
  1149. }
  1150. )
  1151. break
  1152. else:
  1153. # Extract all filaments with used_g > 0 (for single-plate or overview)
  1154. for filament_elem in root.findall(".//filament"):
  1155. filament_id = filament_elem.get("id")
  1156. filament_type = filament_elem.get("type", "")
  1157. filament_color = filament_elem.get("color", "")
  1158. used_g = filament_elem.get("used_g", "0")
  1159. used_m = filament_elem.get("used_m", "0")
  1160. try:
  1161. used_grams = float(used_g)
  1162. except (ValueError, TypeError):
  1163. used_grams = 0
  1164. if used_grams > 0 and filament_id:
  1165. filaments.append(
  1166. {
  1167. "slot_id": int(filament_id),
  1168. "type": filament_type,
  1169. "color": filament_color,
  1170. "used_grams": round(used_grams, 1),
  1171. "used_meters": float(used_m) if used_m else 0,
  1172. }
  1173. )
  1174. # Sort by slot ID
  1175. filaments.sort(key=lambda x: x["slot_id"])
  1176. except Exception as e:
  1177. logger.warning(f"Failed to parse filament requirements from library file {file_id}: {e}")
  1178. return {
  1179. "file_id": file_id,
  1180. "filename": lib_file.filename,
  1181. "plate_id": plate_id,
  1182. "filaments": filaments,
  1183. }
  1184. @router.post("/files/{file_id}/print")
  1185. async def print_library_file(
  1186. file_id: int,
  1187. printer_id: int,
  1188. body: FilePrintRequest | None = None,
  1189. db: AsyncSession = Depends(get_db),
  1190. ):
  1191. """Print a library file directly.
  1192. This endpoint:
  1193. 1. Creates an archive from the library file
  1194. 2. Uploads the file to the printer
  1195. 3. Starts the print
  1196. Only sliced files (.gcode or .gcode.3mf) can be printed.
  1197. """
  1198. import zipfile
  1199. from backend.app.main import register_expected_print
  1200. from backend.app.models.printer import Printer
  1201. from backend.app.services.bambu_ftp import (
  1202. delete_file_async,
  1203. get_ftp_retry_settings,
  1204. upload_file_async,
  1205. with_ftp_retry,
  1206. )
  1207. from backend.app.services.printer_manager import printer_manager
  1208. # Use defaults if no body provided
  1209. if body is None:
  1210. body = FilePrintRequest()
  1211. # Get the library file
  1212. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  1213. lib_file = result.scalar_one_or_none()
  1214. if not lib_file:
  1215. raise HTTPException(status_code=404, detail="File not found")
  1216. # Validate file is sliced
  1217. if not is_sliced_file(lib_file.filename):
  1218. raise HTTPException(
  1219. status_code=400,
  1220. detail="Not a sliced file. Only .gcode or .gcode.3mf files can be printed.",
  1221. )
  1222. # Get the full file path
  1223. file_path = Path(app_settings.base_dir) / lib_file.file_path
  1224. if not file_path.exists():
  1225. raise HTTPException(status_code=404, detail="File not found on disk")
  1226. # Get printer
  1227. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1228. printer = result.scalar_one_or_none()
  1229. if not printer:
  1230. raise HTTPException(status_code=404, detail="Printer not found")
  1231. # Check printer is connected
  1232. if not printer_manager.is_connected(printer_id):
  1233. raise HTTPException(status_code=400, detail="Printer is not connected")
  1234. # Create archive from the library file
  1235. archive_service = ArchiveService(db)
  1236. archive = await archive_service.archive_print(
  1237. printer_id=printer_id,
  1238. source_file=file_path,
  1239. )
  1240. if not archive:
  1241. raise HTTPException(status_code=500, detail="Failed to create archive")
  1242. await db.flush()
  1243. # Prepare remote filename
  1244. base_name = lib_file.filename
  1245. if base_name.endswith(".gcode.3mf"):
  1246. base_name = base_name[:-10]
  1247. elif base_name.endswith(".3mf"):
  1248. base_name = base_name[:-4]
  1249. remote_filename = f"{base_name}.3mf"
  1250. remote_path = f"/{remote_filename}"
  1251. # Get FTP retry settings
  1252. ftp_retry_enabled, ftp_retry_count, ftp_retry_delay, ftp_timeout = await get_ftp_retry_settings()
  1253. # Delete existing file if present (avoids 553 error)
  1254. await delete_file_async(
  1255. printer.ip_address,
  1256. printer.access_code,
  1257. remote_path,
  1258. socket_timeout=ftp_timeout,
  1259. printer_model=printer.model,
  1260. )
  1261. # Upload file to printer
  1262. if ftp_retry_enabled:
  1263. uploaded = await with_ftp_retry(
  1264. upload_file_async,
  1265. printer.ip_address,
  1266. printer.access_code,
  1267. file_path,
  1268. remote_path,
  1269. socket_timeout=ftp_timeout,
  1270. printer_model=printer.model,
  1271. max_retries=ftp_retry_count,
  1272. retry_delay=ftp_retry_delay,
  1273. operation_name=f"Upload for print to {printer.name}",
  1274. )
  1275. else:
  1276. uploaded = await upload_file_async(
  1277. printer.ip_address,
  1278. printer.access_code,
  1279. file_path,
  1280. remote_path,
  1281. socket_timeout=ftp_timeout,
  1282. printer_model=printer.model,
  1283. )
  1284. if not uploaded:
  1285. raise HTTPException(status_code=500, detail="Failed to upload file to printer")
  1286. # Register this as an expected print so we don't create a duplicate archive
  1287. register_expected_print(printer_id, remote_filename, archive.id)
  1288. # Determine plate ID
  1289. if body.plate_id is not None:
  1290. plate_id = body.plate_id
  1291. else:
  1292. plate_id = 1
  1293. try:
  1294. with zipfile.ZipFile(file_path, "r") as zf:
  1295. for name in zf.namelist():
  1296. if name.startswith("Metadata/plate_") and name.endswith(".gcode"):
  1297. plate_str = name[15:-6]
  1298. plate_id = int(plate_str)
  1299. break
  1300. except Exception:
  1301. pass
  1302. logger.info(
  1303. f"Print library file {file_id}: archive_id={archive.id}, plate_id={plate_id}, "
  1304. f"ams_mapping={body.ams_mapping}, bed_levelling={body.bed_levelling}"
  1305. )
  1306. # Start the print
  1307. started = printer_manager.start_print(
  1308. printer_id,
  1309. remote_filename,
  1310. plate_id,
  1311. ams_mapping=body.ams_mapping,
  1312. timelapse=body.timelapse,
  1313. bed_levelling=body.bed_levelling,
  1314. flow_cali=body.flow_cali,
  1315. vibration_cali=body.vibration_cali,
  1316. layer_inspect=body.layer_inspect,
  1317. use_ams=body.use_ams,
  1318. )
  1319. if not started:
  1320. raise HTTPException(status_code=500, detail="Failed to start print")
  1321. await db.commit()
  1322. return {
  1323. "status": "printing",
  1324. "printer_id": printer_id,
  1325. "archive_id": archive.id,
  1326. "filename": lib_file.filename,
  1327. }
  1328. # ============ File Detail Endpoints ============
  1329. @router.get("/files/{file_id}", response_model=FileResponseSchema)
  1330. async def get_file(file_id: int, db: AsyncSession = Depends(get_db)):
  1331. """Get a file by ID with full details."""
  1332. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  1333. file = result.scalar_one_or_none()
  1334. if not file:
  1335. raise HTTPException(status_code=404, detail="File not found")
  1336. # Get folder name
  1337. folder_name = None
  1338. if file.folder_id:
  1339. folder_result = await db.execute(select(LibraryFolder.name).where(LibraryFolder.id == file.folder_id))
  1340. folder_name = folder_result.scalar()
  1341. # Get project name
  1342. project_name = None
  1343. if file.project_id:
  1344. project_result = await db.execute(select(Project.name).where(Project.id == file.project_id))
  1345. project_name = project_result.scalar()
  1346. # Get duplicates
  1347. duplicates = []
  1348. duplicate_count = 0
  1349. if file.file_hash:
  1350. dup_result = await db.execute(
  1351. select(LibraryFile, LibraryFolder.name)
  1352. .outerjoin(LibraryFolder, LibraryFile.folder_id == LibraryFolder.id)
  1353. .where(LibraryFile.file_hash == file.file_hash, LibraryFile.id != file.id)
  1354. )
  1355. for dup_file, dup_folder_name in dup_result.all():
  1356. duplicates.append(
  1357. FileDuplicate(
  1358. id=dup_file.id,
  1359. filename=dup_file.filename,
  1360. folder_id=dup_file.folder_id,
  1361. folder_name=dup_folder_name,
  1362. created_at=dup_file.created_at,
  1363. )
  1364. )
  1365. duplicate_count = len(duplicates)
  1366. return FileResponseSchema(
  1367. id=file.id,
  1368. folder_id=file.folder_id,
  1369. folder_name=folder_name,
  1370. project_id=file.project_id,
  1371. project_name=project_name,
  1372. filename=file.filename,
  1373. file_path=file.file_path,
  1374. file_type=file.file_type,
  1375. file_size=file.file_size,
  1376. file_hash=file.file_hash,
  1377. thumbnail_path=file.thumbnail_path,
  1378. metadata=file.file_metadata,
  1379. print_count=file.print_count,
  1380. last_printed_at=file.last_printed_at,
  1381. notes=file.notes,
  1382. duplicates=duplicates if duplicates else None,
  1383. duplicate_count=duplicate_count,
  1384. created_at=file.created_at,
  1385. updated_at=file.updated_at,
  1386. )
  1387. @router.put("/files/{file_id}", response_model=FileResponseSchema)
  1388. async def update_file(file_id: int, data: FileUpdate, db: AsyncSession = Depends(get_db)):
  1389. """Update a file's metadata."""
  1390. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  1391. file = result.scalar_one_or_none()
  1392. if not file:
  1393. raise HTTPException(status_code=404, detail="File not found")
  1394. if data.filename is not None:
  1395. # Validate filename doesn't contain path separators
  1396. if "/" in data.filename or "\\" in data.filename:
  1397. raise HTTPException(status_code=400, detail="Filename cannot contain path separators")
  1398. file.filename = data.filename
  1399. if data.folder_id is not None:
  1400. if data.folder_id == 0:
  1401. file.folder_id = None
  1402. else:
  1403. # Verify folder exists
  1404. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.folder_id))
  1405. if not folder_result.scalar_one_or_none():
  1406. raise HTTPException(status_code=404, detail="Folder not found")
  1407. file.folder_id = data.folder_id
  1408. if data.project_id is not None:
  1409. if data.project_id == 0:
  1410. file.project_id = None
  1411. else:
  1412. # Verify project exists
  1413. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  1414. if not project_result.scalar_one_or_none():
  1415. raise HTTPException(status_code=404, detail="Project not found")
  1416. file.project_id = data.project_id
  1417. if data.notes is not None:
  1418. file.notes = data.notes if data.notes else None
  1419. await db.flush()
  1420. await db.refresh(file)
  1421. # Return full response (reuse get_file logic)
  1422. return await get_file(file_id, db)
  1423. @router.delete("/files/{file_id}")
  1424. async def delete_file(file_id: int, db: AsyncSession = Depends(get_db)):
  1425. """Delete a file."""
  1426. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  1427. file = result.scalar_one_or_none()
  1428. if not file:
  1429. raise HTTPException(status_code=404, detail="File not found")
  1430. # Delete actual files
  1431. try:
  1432. if file.file_path and os.path.exists(file.file_path):
  1433. os.remove(file.file_path)
  1434. if file.thumbnail_path and os.path.exists(file.thumbnail_path):
  1435. os.remove(file.thumbnail_path)
  1436. except Exception as e:
  1437. logger.warning(f"Failed to delete file from disk: {e}")
  1438. await db.delete(file)
  1439. return {"status": "success", "message": "File deleted"}
  1440. # ============ File Content Endpoints ============
  1441. @router.get("/files/{file_id}/download")
  1442. async def download_file(file_id: int, db: AsyncSession = Depends(get_db)):
  1443. """Download a file."""
  1444. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  1445. file = result.scalar_one_or_none()
  1446. if not file:
  1447. raise HTTPException(status_code=404, detail="File not found")
  1448. if not file.file_path or not os.path.exists(file.file_path):
  1449. raise HTTPException(status_code=404, detail="File not found on disk")
  1450. return FastAPIFileResponse(
  1451. file.file_path,
  1452. filename=file.filename,
  1453. media_type="application/octet-stream",
  1454. )
  1455. @router.get("/files/{file_id}/thumbnail")
  1456. async def get_thumbnail(file_id: int, db: AsyncSession = Depends(get_db)):
  1457. """Get a file's thumbnail."""
  1458. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  1459. file = result.scalar_one_or_none()
  1460. if not file:
  1461. raise HTTPException(status_code=404, detail="File not found")
  1462. if not file.thumbnail_path or not os.path.exists(file.thumbnail_path):
  1463. raise HTTPException(status_code=404, detail="Thumbnail not found")
  1464. # Detect media type from extension
  1465. thumb_ext = os.path.splitext(file.thumbnail_path)[1].lower()
  1466. media_types = {
  1467. ".png": "image/png",
  1468. ".jpg": "image/jpeg",
  1469. ".jpeg": "image/jpeg",
  1470. ".gif": "image/gif",
  1471. ".webp": "image/webp",
  1472. }
  1473. media_type = media_types.get(thumb_ext, "image/png")
  1474. return FastAPIFileResponse(file.thumbnail_path, media_type=media_type)
  1475. @router.get("/files/{file_id}/gcode")
  1476. async def get_gcode(file_id: int, db: AsyncSession = Depends(get_db)):
  1477. """Get gcode for a file (for preview)."""
  1478. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  1479. file = result.scalar_one_or_none()
  1480. if not file:
  1481. raise HTTPException(status_code=404, detail="File not found")
  1482. if not file.file_path or not os.path.exists(file.file_path):
  1483. raise HTTPException(status_code=404, detail="File not found on disk")
  1484. if file.file_type == "gcode":
  1485. return FastAPIFileResponse(file.file_path, media_type="text/plain")
  1486. elif file.file_type == "3mf":
  1487. # Extract gcode from 3mf
  1488. import zipfile
  1489. try:
  1490. with zipfile.ZipFile(file.file_path, "r") as zf:
  1491. # Find gcode file
  1492. gcode_files = [n for n in zf.namelist() if n.endswith(".gcode")]
  1493. if not gcode_files:
  1494. raise HTTPException(status_code=404, detail="No gcode found in 3MF file")
  1495. gcode_content = zf.read(gcode_files[0])
  1496. from fastapi.responses import Response
  1497. return Response(content=gcode_content, media_type="text/plain")
  1498. except zipfile.BadZipFile:
  1499. raise HTTPException(status_code=400, detail="Invalid 3MF file")
  1500. else:
  1501. raise HTTPException(status_code=400, detail="Unsupported file type")
  1502. # ============ Bulk Operations ============
  1503. @router.post("/files/move")
  1504. async def move_files(data: FileMoveRequest, db: AsyncSession = Depends(get_db)):
  1505. """Move multiple files to a folder."""
  1506. # Verify folder exists if specified
  1507. if data.folder_id is not None:
  1508. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.folder_id))
  1509. if not folder_result.scalar_one_or_none():
  1510. raise HTTPException(status_code=404, detail="Folder not found")
  1511. # Update files
  1512. moved = 0
  1513. for file_id in data.file_ids:
  1514. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  1515. file = result.scalar_one_or_none()
  1516. if file:
  1517. file.folder_id = data.folder_id
  1518. moved += 1
  1519. return {"status": "success", "moved": moved}
  1520. @router.post("/bulk-delete", response_model=BulkDeleteResponse)
  1521. async def bulk_delete(data: BulkDeleteRequest, db: AsyncSession = Depends(get_db)):
  1522. """Delete multiple files and/or folders."""
  1523. deleted_files = 0
  1524. deleted_folders = 0
  1525. # Delete files first
  1526. for file_id in data.file_ids:
  1527. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  1528. file = result.scalar_one_or_none()
  1529. if file:
  1530. try:
  1531. if file.file_path and os.path.exists(file.file_path):
  1532. os.remove(file.file_path)
  1533. if file.thumbnail_path and os.path.exists(file.thumbnail_path):
  1534. os.remove(file.thumbnail_path)
  1535. except Exception as e:
  1536. logger.warning(f"Failed to delete file from disk: {e}")
  1537. await db.delete(file)
  1538. deleted_files += 1
  1539. # Delete folders (cascade will handle contents)
  1540. for folder_id in data.folder_ids:
  1541. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  1542. folder = result.scalar_one_or_none()
  1543. if folder:
  1544. # Count files that will be deleted
  1545. file_count_result = await db.execute(
  1546. select(func.count(LibraryFile.id)).where(LibraryFile.folder_id == folder_id)
  1547. )
  1548. deleted_files += file_count_result.scalar() or 0
  1549. await db.delete(folder)
  1550. deleted_folders += 1
  1551. return BulkDeleteResponse(deleted_files=deleted_files, deleted_folders=deleted_folders)
  1552. # ============ Stats Endpoint ============
  1553. @router.get("/stats")
  1554. async def get_library_stats(db: AsyncSession = Depends(get_db)):
  1555. """Get library statistics."""
  1556. # Total files
  1557. total_files_result = await db.execute(select(func.count(LibraryFile.id)))
  1558. total_files = total_files_result.scalar() or 0
  1559. # Total folders
  1560. total_folders_result = await db.execute(select(func.count(LibraryFolder.id)))
  1561. total_folders = total_folders_result.scalar() or 0
  1562. # Total size
  1563. total_size_result = await db.execute(select(func.sum(LibraryFile.file_size)))
  1564. total_size = total_size_result.scalar() or 0
  1565. # Files by type
  1566. type_result = await db.execute(
  1567. select(LibraryFile.file_type, func.count(LibraryFile.id)).group_by(LibraryFile.file_type)
  1568. )
  1569. files_by_type = dict(type_result.all())
  1570. # Total prints
  1571. total_prints_result = await db.execute(select(func.sum(LibraryFile.print_count)))
  1572. total_prints = total_prints_result.scalar() or 0
  1573. # Disk space info
  1574. library_dir = get_library_dir()
  1575. try:
  1576. disk_stat = shutil.disk_usage(library_dir)
  1577. disk_free_bytes = disk_stat.free
  1578. disk_total_bytes = disk_stat.total
  1579. disk_used_bytes = disk_stat.used
  1580. except Exception:
  1581. disk_free_bytes = 0
  1582. disk_total_bytes = 0
  1583. disk_used_bytes = 0
  1584. return {
  1585. "total_files": total_files,
  1586. "total_folders": total_folders,
  1587. "total_size_bytes": total_size,
  1588. "files_by_type": files_by_type,
  1589. "total_prints": total_prints,
  1590. "disk_free_bytes": disk_free_bytes,
  1591. "disk_total_bytes": disk_total_bytes,
  1592. "disk_used_bytes": disk_used_bytes,
  1593. }
  1594. # ============ Thumbnail Generation Endpoints ============
  1595. @router.post("/files/{file_id}/regenerate-thumbnail", response_model=FileResponseSchema)
  1596. async def regenerate_thumbnail(file_id: int, db: AsyncSession = Depends(get_db)):
  1597. """Regenerate thumbnail for a specific file.
  1598. Works for STL, 3MF, gcode, and image files.
  1599. """
  1600. result = await db.execute(select(LibraryFile).where(LibraryFile.id == file_id))
  1601. file = result.scalar_one_or_none()
  1602. if not file:
  1603. raise HTTPException(status_code=404, detail="File not found")
  1604. if not file.file_path or not os.path.exists(file.file_path):
  1605. raise HTTPException(status_code=404, detail="File not found on disk")
  1606. ext = os.path.splitext(file.filename)[1].lower()
  1607. thumbnails_dir = get_library_thumbnails_dir()
  1608. file_path = Path(file.file_path)
  1609. # Delete old thumbnail if exists
  1610. if file.thumbnail_path and os.path.exists(file.thumbnail_path):
  1611. try:
  1612. os.remove(file.thumbnail_path)
  1613. except Exception as e:
  1614. logger.warning(f"Failed to delete old thumbnail: {e}")
  1615. thumbnail_path = None
  1616. if ext == ".3mf":
  1617. try:
  1618. parser = ThreeMFParser(str(file_path))
  1619. raw_metadata = parser.parse()
  1620. thumbnail_data = raw_metadata.get("_thumbnail_data")
  1621. thumbnail_ext = raw_metadata.get("_thumbnail_ext", ".png")
  1622. if thumbnail_data:
  1623. thumb_filename = f"{uuid.uuid4().hex}{thumbnail_ext}"
  1624. thumb_path = thumbnails_dir / thumb_filename
  1625. with open(thumb_path, "wb") as f:
  1626. f.write(thumbnail_data)
  1627. thumbnail_path = str(thumb_path)
  1628. except Exception as e:
  1629. logger.warning(f"Failed to extract 3MF thumbnail: {e}")
  1630. elif ext == ".gcode":
  1631. try:
  1632. thumbnail_data = extract_gcode_thumbnail(file_path)
  1633. if thumbnail_data:
  1634. thumb_filename = f"{uuid.uuid4().hex}.png"
  1635. thumb_path = thumbnails_dir / thumb_filename
  1636. with open(thumb_path, "wb") as f:
  1637. f.write(thumbnail_data)
  1638. thumbnail_path = str(thumb_path)
  1639. except Exception as e:
  1640. logger.warning(f"Failed to extract gcode thumbnail: {e}")
  1641. elif ext == ".stl":
  1642. try:
  1643. from backend.app.services.stl_thumbnail import generate_stl_thumbnail
  1644. thumb_filename = f"{uuid.uuid4().hex}.png"
  1645. thumb_path = thumbnails_dir / thumb_filename
  1646. if generate_stl_thumbnail(file_path, thumb_path):
  1647. thumbnail_path = str(thumb_path)
  1648. except Exception as e:
  1649. logger.warning(f"Failed to generate STL thumbnail: {e}")
  1650. elif ext.lower() in IMAGE_EXTENSIONS:
  1651. thumbnail_path = create_image_thumbnail(file_path, thumbnails_dir)
  1652. # Update database
  1653. file.thumbnail_path = thumbnail_path
  1654. await db.flush()
  1655. await db.refresh(file)
  1656. # Return full response
  1657. return await get_file(file_id, db)
  1658. @router.post("/generate-stl-thumbnails", response_model=BatchThumbnailResponse)
  1659. async def batch_generate_stl_thumbnails(
  1660. request: BatchThumbnailRequest,
  1661. db: AsyncSession = Depends(get_db),
  1662. ):
  1663. """Generate thumbnails for existing STL files.
  1664. Can target specific files, a folder, or all STL files missing thumbnails.
  1665. Args:
  1666. request: Batch request specifying which files to process
  1667. - file_ids: List of specific file IDs to process
  1668. - folder_id: Process all STL files in this folder
  1669. - all_missing: Process all STL files without thumbnails
  1670. """
  1671. from backend.app.services.stl_thumbnail import generate_stl_thumbnail
  1672. results: list[BatchThumbnailResult] = []
  1673. thumbnails_dir = get_library_thumbnails_dir()
  1674. # Build query based on request parameters
  1675. query = select(LibraryFile).where(LibraryFile.file_type == "stl")
  1676. if request.file_ids:
  1677. # Specific files requested
  1678. query = query.where(LibraryFile.id.in_(request.file_ids))
  1679. elif request.folder_id is not None:
  1680. # All STL files in a folder
  1681. query = query.where(LibraryFile.folder_id == request.folder_id)
  1682. elif request.all_missing:
  1683. # All STL files without thumbnails
  1684. query = query.where(LibraryFile.thumbnail_path.is_(None))
  1685. else:
  1686. # No valid filter specified
  1687. raise HTTPException(
  1688. status_code=400,
  1689. detail="Must specify file_ids, folder_id, or all_missing=true",
  1690. )
  1691. result = await db.execute(query)
  1692. files = result.scalars().all()
  1693. succeeded = 0
  1694. failed = 0
  1695. for file in files:
  1696. if not file.file_path or not os.path.exists(file.file_path):
  1697. results.append(
  1698. BatchThumbnailResult(
  1699. file_id=file.id,
  1700. filename=file.filename,
  1701. success=False,
  1702. error="File not found on disk",
  1703. )
  1704. )
  1705. failed += 1
  1706. continue
  1707. try:
  1708. # Delete old thumbnail if exists
  1709. if file.thumbnail_path and os.path.exists(file.thumbnail_path):
  1710. try:
  1711. os.remove(file.thumbnail_path)
  1712. except Exception:
  1713. pass
  1714. # Generate new thumbnail
  1715. thumb_filename = f"{uuid.uuid4().hex}.png"
  1716. thumb_path = thumbnails_dir / thumb_filename
  1717. if generate_stl_thumbnail(file.file_path, thumb_path):
  1718. file.thumbnail_path = str(thumb_path)
  1719. results.append(
  1720. BatchThumbnailResult(
  1721. file_id=file.id,
  1722. filename=file.filename,
  1723. success=True,
  1724. )
  1725. )
  1726. succeeded += 1
  1727. else:
  1728. results.append(
  1729. BatchThumbnailResult(
  1730. file_id=file.id,
  1731. filename=file.filename,
  1732. success=False,
  1733. error="Thumbnail generation failed",
  1734. )
  1735. )
  1736. failed += 1
  1737. except Exception as e:
  1738. results.append(
  1739. BatchThumbnailResult(
  1740. file_id=file.id,
  1741. filename=file.filename,
  1742. success=False,
  1743. error=str(e),
  1744. )
  1745. )
  1746. failed += 1
  1747. await db.commit()
  1748. return BatchThumbnailResponse(
  1749. processed=len(results),
  1750. succeeded=succeeded,
  1751. failed=failed,
  1752. results=results,
  1753. )