library.py 106 KB

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