library.py 88 KB

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