library.py 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428
  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 datetime import datetime, timezone
  12. from pathlib import Path
  13. from fastapi import APIRouter, Depends, File, HTTPException, Query, Response, UploadFile
  14. from fastapi.responses import FileResponse as FastAPIFileResponse
  15. from sqlalchemy import func, select
  16. from sqlalchemy.ext.asyncio import AsyncSession
  17. from sqlalchemy.orm import selectinload
  18. from backend.app.core.auth import (
  19. RequireCameraStreamTokenIfAuthEnabled,
  20. require_ownership_permission,
  21. require_permission_if_auth_enabled,
  22. )
  23. from backend.app.core.config import settings as app_settings
  24. from backend.app.core.database import get_db
  25. from backend.app.core.permissions import Permission
  26. from backend.app.models.archive import PrintArchive
  27. from backend.app.models.library import LibraryFile, LibraryFolder
  28. from backend.app.models.print_queue import PrintQueueItem
  29. from backend.app.models.project import Project
  30. from backend.app.models.user import User
  31. from backend.app.schemas.library import (
  32. AddToQueueError,
  33. AddToQueueRequest,
  34. AddToQueueResponse,
  35. AddToQueueResult,
  36. BatchThumbnailRequest,
  37. BatchThumbnailResponse,
  38. BatchThumbnailResult,
  39. BulkDeleteRequest,
  40. BulkDeleteResponse,
  41. ExternalFolderCreate,
  42. FileDuplicate,
  43. FileListResponse,
  44. FileMoveRequest,
  45. FilePrintRequest,
  46. FileResponse as FileResponseSchema,
  47. FileUpdate,
  48. FileUploadResponse,
  49. FolderCreate,
  50. FolderResponse,
  51. FolderTreeItem,
  52. FolderUpdate,
  53. ZipExtractError,
  54. ZipExtractResponse,
  55. ZipExtractResult,
  56. )
  57. from backend.app.schemas.slicer import SliceRequest, SliceResponse
  58. from backend.app.services.archive import ThreeMFParser
  59. from backend.app.services.stl_thumbnail import generate_stl_thumbnail
  60. from backend.app.utils.threemf_tools import extract_nozzle_mapping_from_3mf
  61. logger = logging.getLogger(__name__)
  62. router = APIRouter(prefix="/library", tags=["library"])
  63. def get_library_dir() -> Path:
  64. """Get the library storage directory."""
  65. base_dir = Path(app_settings.archive_dir)
  66. library_dir = base_dir / "library"
  67. library_dir.mkdir(parents=True, exist_ok=True)
  68. return library_dir
  69. def get_library_files_dir() -> Path:
  70. """Get the directory for library files."""
  71. files_dir = get_library_dir() / "files"
  72. files_dir.mkdir(parents=True, exist_ok=True)
  73. return files_dir
  74. def get_library_thumbnails_dir() -> Path:
  75. """Get the directory for library thumbnails."""
  76. thumbnails_dir = get_library_dir() / "thumbnails"
  77. thumbnails_dir.mkdir(parents=True, exist_ok=True)
  78. return thumbnails_dir
  79. def to_relative_path(absolute_path: Path | str) -> str:
  80. """Convert an absolute path to a path relative to base_dir for storage."""
  81. if not absolute_path:
  82. return ""
  83. abs_path = Path(absolute_path)
  84. base_dir = Path(app_settings.base_dir)
  85. try:
  86. return str(abs_path.relative_to(base_dir))
  87. except ValueError:
  88. # Path is not under base_dir, return as-is (shouldn't happen normally)
  89. return str(abs_path)
  90. def to_absolute_path(relative_path: str | None) -> Path | None:
  91. """Convert a relative path (from database) to an absolute path for file operations."""
  92. if not relative_path:
  93. return None
  94. path = Path(relative_path)
  95. # Handle already-absolute paths verbatim (backwards compatibility during migration).
  96. # Legacy DB rows may store absolute paths that predate the base_dir layout; the
  97. # traversal guard below only applies to relative paths coming from user input.
  98. if path.is_absolute():
  99. return path.resolve()
  100. base = Path(app_settings.base_dir).resolve()
  101. resolved = (base / relative_path).resolve()
  102. # Guard against path traversal — resolved path must stay inside base_dir.
  103. # Use is_relative_to() to avoid the /data/app vs /data/app_evil prefix confusion
  104. # that a plain startswith(str(base)) check would miss.
  105. if not resolved.is_relative_to(base):
  106. raise ValueError(f"Path escapes base directory: {relative_path!r}")
  107. return resolved
  108. def calculate_file_hash(file_path: Path) -> str:
  109. """Calculate SHA256 hash of a file."""
  110. sha256_hash = hashlib.sha256()
  111. with open(file_path, "rb") as f:
  112. for byte_block in iter(lambda: f.read(4096), b""):
  113. sha256_hash.update(byte_block)
  114. return sha256_hash.hexdigest()
  115. def _resolve_upload_destination(target_folder: LibraryFolder | None, filename: str) -> tuple[Path, bool]:
  116. """Resolve the on-disk destination for an uploaded file.
  117. Non-external target: returns ``(<library_files_dir>/<uuid><ext>, False)``.
  118. Writable external target: writes to ``<external_path>/<filename>``
  119. (preserves the real filename so the file is recognisable on the mount);
  120. returns ``(dest, True)``. Raises ``HTTPException`` for read-only external
  121. folders (403), missing/inaccessible/non-writable external paths (400), and
  122. filename collisions on the external mount (409). See #1112 — previously
  123. uploads to writable external folders were silently misrouted to the
  124. internal library dir.
  125. """
  126. if target_folder is not None and target_folder.is_external:
  127. if target_folder.external_readonly:
  128. raise HTTPException(status_code=403, detail="Cannot upload to a read-only external folder")
  129. if not target_folder.external_path:
  130. raise HTTPException(status_code=400, detail="External folder has no configured path")
  131. ext_dir = Path(target_folder.external_path)
  132. if not ext_dir.exists() or not ext_dir.is_dir():
  133. raise HTTPException(
  134. status_code=400,
  135. detail=f"External path is not accessible: {target_folder.external_path}",
  136. )
  137. if not os.access(ext_dir, os.W_OK):
  138. raise HTTPException(
  139. status_code=400,
  140. detail=f"External path is not writable: {target_folder.external_path}",
  141. )
  142. # Guard against path-traversal via a pathological filename — join then
  143. # verify the resolved destination is still inside the external dir.
  144. dest = (ext_dir / filename).resolve()
  145. try:
  146. dest.relative_to(ext_dir.resolve())
  147. except ValueError:
  148. raise HTTPException(status_code=400, detail="Invalid filename")
  149. if dest.exists():
  150. raise HTTPException(
  151. status_code=409,
  152. detail=f"A file named {filename!r} already exists in the external folder",
  153. )
  154. return dest, True
  155. ext = os.path.splitext(filename)[1].lower()
  156. return get_library_files_dir() / f"{uuid.uuid4().hex}{ext}", False
  157. def _stored_file_path(abs_path: Path, is_external: bool) -> str:
  158. """Produce the value to persist in ``LibraryFile.file_path``.
  159. External files store the absolute mount path directly (same as scan does),
  160. so ``to_absolute_path`` round-trips through its ``is_absolute()`` fast
  161. path. Managed files store a path relative to ``base_dir`` for portability.
  162. """
  163. return str(abs_path) if is_external else to_relative_path(abs_path)
  164. def _clean_3mf_metadata(obj):
  165. """Strip bytes and thumbnail-carrier keys so the payload is JSON-storable.
  166. Shared by ``upload_file`` and :func:`save_3mf_bytes_to_library` — the
  167. ``ThreeMFParser`` output embeds the thumbnail bytes under
  168. ``_thumbnail_data``/``_thumbnail_ext`` and may also include raw bytes in
  169. other fields, none of which can be JSON-encoded.
  170. """
  171. if isinstance(obj, dict):
  172. return {
  173. k: _clean_3mf_metadata(v)
  174. for k, v in obj.items()
  175. if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext")
  176. }
  177. if isinstance(obj, list):
  178. return [_clean_3mf_metadata(i) for i in obj if not isinstance(i, bytes)]
  179. if isinstance(obj, bytes):
  180. return None
  181. return obj
  182. async def save_3mf_bytes_to_library(
  183. db: AsyncSession,
  184. *,
  185. file_bytes: bytes,
  186. filename: str,
  187. folder_id: int | None = None,
  188. source_type: str | None = None,
  189. source_url: str | None = None,
  190. owner_id: int | None = None,
  191. ) -> tuple[LibraryFile, bool]:
  192. """Save a 3MF blob into the library and return ``(library_file, was_existing)``.
  193. Used by routes that receive a 3MF in-process rather than as a multipart
  194. upload (currently: MakerWorld import; reusable for any future source that
  195. fetches bytes server-side). Deduplicates by ``source_url`` when provided —
  196. if a LibraryFile with the same source_url already exists, the existing
  197. row is returned and the bytes are NOT re-saved (MakerWorld signed URLs
  198. change each download, so hash-based dedupe alone would miss re-imports).
  199. Parses 3MF metadata + thumbnail the same way the multipart upload route
  200. does, via :class:`ThreeMFParser`. Paths are stored as relative so the
  201. library is portable across installs.
  202. """
  203. # Source-URL-based dedupe: return the existing row untouched.
  204. if source_url:
  205. existing = await db.execute(LibraryFile.active().where(LibraryFile.source_url == source_url).limit(1))
  206. existing_row = existing.scalar_one_or_none()
  207. if existing_row is not None:
  208. return existing_row, True
  209. # Persist bytes to disk under a UUID-scoped filename; keep the original
  210. # extension so downstream logic (ThreeMFParser, thumbnail viewer) works.
  211. ext = os.path.splitext(filename)[1].lower() or ".3mf"
  212. unique_filename = f"{uuid.uuid4().hex}{ext}"
  213. file_path = get_library_files_dir() / unique_filename
  214. with open(file_path, "wb") as fh:
  215. fh.write(file_bytes)
  216. file_hash = calculate_file_hash(file_path)
  217. # Extract metadata + thumbnail from the 3MF.
  218. metadata: dict | None = None
  219. thumbnail_path: str | None = None
  220. if ext == ".3mf":
  221. try:
  222. parser = ThreeMFParser(str(file_path))
  223. raw_metadata = parser.parse()
  224. thumb_data = raw_metadata.get("_thumbnail_data")
  225. thumb_ext = raw_metadata.get("_thumbnail_ext", ".png")
  226. if thumb_data:
  227. thumbs_dir = get_library_thumbnails_dir()
  228. thumb_filename = f"{uuid.uuid4().hex}{thumb_ext}"
  229. thumb_path = thumbs_dir / thumb_filename
  230. with open(thumb_path, "wb") as fh:
  231. fh.write(thumb_data)
  232. thumbnail_path = str(thumb_path)
  233. metadata = _clean_3mf_metadata(raw_metadata) or None
  234. except Exception as exc:
  235. # Matches the multipart upload route's behaviour — a bad 3MF should
  236. # still land in the library so the user can see / delete it rather
  237. # than failing the whole request.
  238. logger.warning("Failed to parse 3MF %s: %s", filename, exc)
  239. library_file = LibraryFile(
  240. folder_id=folder_id,
  241. filename=filename,
  242. file_path=to_relative_path(file_path),
  243. file_type=ext[1:] if ext else "unknown",
  244. file_size=len(file_bytes),
  245. file_hash=file_hash,
  246. thumbnail_path=to_relative_path(thumbnail_path) if thumbnail_path else None,
  247. file_metadata=metadata,
  248. source_type=source_type,
  249. source_url=source_url,
  250. created_by_id=owner_id,
  251. )
  252. db.add(library_file)
  253. await db.commit()
  254. await db.refresh(library_file)
  255. return library_file, False
  256. def extract_gcode_thumbnail(file_path: Path) -> bytes | None:
  257. """Extract embedded thumbnail from gcode file.
  258. Supports PrusaSlicer/BambuStudio format:
  259. ; thumbnail begin WxH SIZE
  260. ; base64data...
  261. ; thumbnail end
  262. """
  263. try:
  264. thumbnail_data = None
  265. in_thumbnail = False
  266. thumbnail_lines = []
  267. best_size = 0
  268. with open(file_path, errors="ignore") as f:
  269. # Only read first 50KB for performance (thumbnails are at the start)
  270. content = f.read(50000)
  271. for line in content.split("\n"):
  272. line = line.strip()
  273. # Check for thumbnail start
  274. if line.startswith("; thumbnail begin"):
  275. in_thumbnail = True
  276. thumbnail_lines = []
  277. # Parse dimensions: "; thumbnail begin 300x300 12345"
  278. match = re.search(r"(\d+)x(\d+)", line)
  279. if match:
  280. width = int(match.group(1))
  281. # Prefer larger thumbnails (up to 300px)
  282. if width > best_size and width <= 300:
  283. best_size = width
  284. continue
  285. # Check for thumbnail end
  286. if line.startswith("; thumbnail end"):
  287. if in_thumbnail and thumbnail_lines:
  288. try:
  289. # Decode the base64 data
  290. b64_data = "".join(thumbnail_lines)
  291. decoded = base64.b64decode(b64_data)
  292. # Only keep if this is the best size or first valid thumbnail
  293. if thumbnail_data is None or best_size > 0:
  294. thumbnail_data = decoded
  295. except (binascii.Error, ValueError):
  296. pass # Skip thumbnail with invalid base64 data
  297. in_thumbnail = False
  298. thumbnail_lines = []
  299. continue
  300. # Collect thumbnail data
  301. if in_thumbnail and line.startswith(";"):
  302. # Remove the leading "; " or ";"
  303. data_line = line[1:].strip()
  304. if data_line:
  305. thumbnail_lines.append(data_line)
  306. return thumbnail_data
  307. except Exception as e:
  308. logger.warning("Failed to extract gcode thumbnail: %s", e)
  309. return None
  310. def create_image_thumbnail(file_path: Path, thumbnails_dir: Path, max_size: int = 256) -> str | None:
  311. """Create a thumbnail from an image file.
  312. For small images, copies directly. For larger images, resizes.
  313. Returns the thumbnail path or None on failure.
  314. """
  315. try:
  316. from PIL import Image
  317. thumb_filename = f"{uuid.uuid4().hex}.png"
  318. thumb_path = thumbnails_dir / thumb_filename
  319. with Image.open(file_path) as img:
  320. # Convert to RGB if necessary (for PNG with transparency, etc.)
  321. if img.mode in ("RGBA", "LA", "P"):
  322. # Create white background for transparency
  323. background = Image.new("RGB", img.size, (255, 255, 255))
  324. if img.mode == "P":
  325. img = img.convert("RGBA")
  326. background.paste(img, mask=img.split()[-1] if img.mode == "RGBA" else None)
  327. img = background
  328. elif img.mode != "RGB":
  329. img = img.convert("RGB")
  330. # Resize if larger than max_size
  331. if img.width > max_size or img.height > max_size:
  332. img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
  333. img.save(thumb_path, "PNG", optimize=True)
  334. return str(thumb_path)
  335. except ImportError:
  336. # PIL not installed, just copy the file if it's small enough
  337. logger.warning("PIL not installed, copying image as thumbnail")
  338. try:
  339. file_size = file_path.stat().st_size
  340. if file_size < 500000: # Less than 500KB
  341. thumb_filename = f"{uuid.uuid4().hex}{file_path.suffix}"
  342. thumb_path = thumbnails_dir / thumb_filename
  343. shutil.copy2(file_path, thumb_path)
  344. return str(thumb_path)
  345. except OSError:
  346. pass # File inaccessible; fall through to return None
  347. return None
  348. except Exception as e:
  349. logger.warning("Failed to create image thumbnail: %s", e)
  350. return None
  351. # Supported image extensions for thumbnails
  352. IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".tiff", ".tif"}
  353. # ============ Folder Endpoints ============
  354. @router.get("/folders", response_model=list[FolderTreeItem])
  355. @router.get("/folders/", response_model=list[FolderTreeItem])
  356. async def list_folders(
  357. response: Response,
  358. db: AsyncSession = Depends(get_db),
  359. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  360. ):
  361. """Get all folders as a tree structure."""
  362. # Prevent browser caching of folder list
  363. response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
  364. # Get all folders with project and archive joins
  365. result = await db.execute(
  366. select(LibraryFolder, Project.name, PrintArchive.print_name)
  367. .outerjoin(Project, LibraryFolder.project_id == Project.id)
  368. .outerjoin(PrintArchive, LibraryFolder.archive_id == PrintArchive.id)
  369. .order_by(LibraryFolder.name)
  370. )
  371. rows = result.all()
  372. # Get file counts per folder
  373. file_counts_result = await db.execute(
  374. select(LibraryFile.folder_id, func.count(LibraryFile.id))
  375. .where(LibraryFile.folder_id.isnot(None), LibraryFile.deleted_at.is_(None))
  376. .group_by(LibraryFile.folder_id)
  377. )
  378. file_counts = dict(file_counts_result.all())
  379. # Build tree structure
  380. folder_map = {}
  381. root_folders = []
  382. for folder, project_name, archive_name in rows:
  383. folder_item = FolderTreeItem(
  384. id=folder.id,
  385. name=folder.name,
  386. parent_id=folder.parent_id,
  387. project_id=folder.project_id,
  388. archive_id=folder.archive_id,
  389. project_name=project_name,
  390. archive_name=archive_name,
  391. is_external=folder.is_external,
  392. external_path=folder.external_path,
  393. external_readonly=folder.external_readonly,
  394. file_count=file_counts.get(folder.id, 0),
  395. children=[],
  396. )
  397. folder_map[folder.id] = folder_item
  398. # Link children to parents
  399. for folder, _, _ in rows:
  400. folder_item = folder_map[folder.id]
  401. if folder.parent_id is None:
  402. root_folders.append(folder_item)
  403. elif folder.parent_id in folder_map:
  404. folder_map[folder.parent_id].children.append(folder_item)
  405. return root_folders
  406. @router.get("/folders/by-project/{project_id}", response_model=list[FolderResponse])
  407. async def get_folders_by_project(
  408. project_id: int,
  409. db: AsyncSession = Depends(get_db),
  410. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  411. ):
  412. """Get all folders linked to a specific project."""
  413. result = await db.execute(
  414. select(LibraryFolder, Project.name)
  415. .outerjoin(Project, LibraryFolder.project_id == Project.id)
  416. .where(LibraryFolder.project_id == project_id)
  417. .order_by(LibraryFolder.name)
  418. )
  419. rows = result.all()
  420. folders = []
  421. for folder, project_name in rows:
  422. # Get file count
  423. file_count_result = await db.execute(
  424. select(func.count(LibraryFile.id)).where(
  425. LibraryFile.folder_id == folder.id,
  426. LibraryFile.deleted_at.is_(None),
  427. )
  428. )
  429. file_count = file_count_result.scalar() or 0
  430. folders.append(
  431. FolderResponse(
  432. id=folder.id,
  433. name=folder.name,
  434. parent_id=folder.parent_id,
  435. project_id=folder.project_id,
  436. archive_id=folder.archive_id,
  437. project_name=project_name,
  438. archive_name=None,
  439. is_external=folder.is_external,
  440. external_path=folder.external_path,
  441. external_readonly=folder.external_readonly,
  442. external_show_hidden=folder.external_show_hidden,
  443. file_count=file_count,
  444. created_at=folder.created_at,
  445. updated_at=folder.updated_at,
  446. )
  447. )
  448. return folders
  449. @router.get("/folders/by-archive/{archive_id}", response_model=list[FolderResponse])
  450. async def get_folders_by_archive(
  451. archive_id: int,
  452. db: AsyncSession = Depends(get_db),
  453. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  454. ):
  455. """Get all folders linked to a specific archive."""
  456. result = await db.execute(
  457. select(LibraryFolder, PrintArchive.print_name)
  458. .outerjoin(PrintArchive, LibraryFolder.archive_id == PrintArchive.id)
  459. .where(LibraryFolder.archive_id == archive_id)
  460. .order_by(LibraryFolder.name)
  461. )
  462. rows = result.all()
  463. folders = []
  464. for folder, archive_name in rows:
  465. # Get file count
  466. file_count_result = await db.execute(
  467. select(func.count(LibraryFile.id)).where(
  468. LibraryFile.folder_id == folder.id,
  469. LibraryFile.deleted_at.is_(None),
  470. )
  471. )
  472. file_count = file_count_result.scalar() or 0
  473. folders.append(
  474. FolderResponse(
  475. id=folder.id,
  476. name=folder.name,
  477. parent_id=folder.parent_id,
  478. project_id=folder.project_id,
  479. archive_id=folder.archive_id,
  480. project_name=None,
  481. archive_name=archive_name,
  482. is_external=folder.is_external,
  483. external_path=folder.external_path,
  484. external_readonly=folder.external_readonly,
  485. external_show_hidden=folder.external_show_hidden,
  486. file_count=file_count,
  487. created_at=folder.created_at,
  488. updated_at=folder.updated_at,
  489. )
  490. )
  491. return folders
  492. @router.post("/folders", response_model=FolderResponse)
  493. @router.post("/folders/", response_model=FolderResponse)
  494. async def create_folder(
  495. data: FolderCreate,
  496. db: AsyncSession = Depends(get_db),
  497. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  498. ):
  499. """Create a new folder."""
  500. # Verify parent exists if specified
  501. if data.parent_id is not None:
  502. parent_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.parent_id))
  503. if not parent_result.scalar_one_or_none():
  504. raise HTTPException(status_code=404, detail="Parent folder not found")
  505. # Verify project exists if specified
  506. project_name = None
  507. if data.project_id is not None:
  508. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  509. project = project_result.scalar_one_or_none()
  510. if not project:
  511. raise HTTPException(status_code=404, detail="Project not found")
  512. project_name = project.name
  513. # Verify archive exists if specified
  514. archive_name = None
  515. if data.archive_id is not None:
  516. archive_result = await db.execute(select(PrintArchive).where(PrintArchive.id == data.archive_id))
  517. archive = archive_result.scalar_one_or_none()
  518. if not archive:
  519. raise HTTPException(status_code=404, detail="Archive not found")
  520. archive_name = archive.print_name
  521. folder = LibraryFolder(
  522. name=data.name,
  523. parent_id=data.parent_id,
  524. project_id=data.project_id,
  525. archive_id=data.archive_id,
  526. )
  527. db.add(folder)
  528. await db.commit()
  529. await db.refresh(folder)
  530. return FolderResponse(
  531. id=folder.id,
  532. name=folder.name,
  533. parent_id=folder.parent_id,
  534. project_id=folder.project_id,
  535. archive_id=folder.archive_id,
  536. project_name=project_name,
  537. archive_name=archive_name,
  538. is_external=folder.is_external,
  539. external_path=folder.external_path,
  540. external_readonly=folder.external_readonly,
  541. external_show_hidden=folder.external_show_hidden,
  542. file_count=0,
  543. created_at=folder.created_at,
  544. updated_at=folder.updated_at,
  545. )
  546. @router.get("/folders/{folder_id}", response_model=FolderResponse)
  547. async def get_folder(
  548. folder_id: int,
  549. db: AsyncSession = Depends(get_db),
  550. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  551. ):
  552. """Get a folder by ID."""
  553. result = await db.execute(
  554. select(LibraryFolder, Project.name, PrintArchive.print_name)
  555. .outerjoin(Project, LibraryFolder.project_id == Project.id)
  556. .outerjoin(PrintArchive, LibraryFolder.archive_id == PrintArchive.id)
  557. .where(LibraryFolder.id == folder_id)
  558. )
  559. row = result.one_or_none()
  560. if not row:
  561. raise HTTPException(status_code=404, detail="Folder not found")
  562. folder, project_name, archive_name = row
  563. # Get file count
  564. file_count_result = await db.execute(
  565. select(func.count(LibraryFile.id)).where(
  566. LibraryFile.folder_id == folder_id,
  567. LibraryFile.deleted_at.is_(None),
  568. )
  569. )
  570. file_count = file_count_result.scalar() or 0
  571. return FolderResponse(
  572. id=folder.id,
  573. name=folder.name,
  574. parent_id=folder.parent_id,
  575. project_id=folder.project_id,
  576. archive_id=folder.archive_id,
  577. project_name=project_name,
  578. archive_name=archive_name,
  579. is_external=folder.is_external,
  580. external_path=folder.external_path,
  581. external_readonly=folder.external_readonly,
  582. external_show_hidden=folder.external_show_hidden,
  583. file_count=file_count,
  584. created_at=folder.created_at,
  585. updated_at=folder.updated_at,
  586. )
  587. @router.put("/folders/{folder_id}", response_model=FolderResponse)
  588. async def update_folder(
  589. folder_id: int,
  590. data: FolderUpdate,
  591. db: AsyncSession = Depends(get_db),
  592. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPDATE_ALL)),
  593. ):
  594. """Update a folder.
  595. Note: Folders require library:update_all permission since they don't have
  596. ownership tracking.
  597. """
  598. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  599. folder = result.scalar_one_or_none()
  600. if not folder:
  601. raise HTTPException(status_code=404, detail="Folder not found")
  602. if data.name is not None:
  603. folder.name = data.name
  604. if data.parent_id is not None:
  605. # Prevent circular reference
  606. if data.parent_id == folder_id:
  607. raise HTTPException(status_code=400, detail="Folder cannot be its own parent")
  608. # Check for circular reference in ancestors
  609. if data.parent_id != 0: # 0 means move to root
  610. current_id = data.parent_id
  611. while current_id is not None:
  612. if current_id == folder_id:
  613. raise HTTPException(status_code=400, detail="Cannot move folder into its own subtree")
  614. parent_result = await db.execute(select(LibraryFolder.parent_id).where(LibraryFolder.id == current_id))
  615. current_id = parent_result.scalar()
  616. folder.parent_id = data.parent_id
  617. else:
  618. folder.parent_id = None
  619. # Update project_id (0 to unlink)
  620. if data.project_id is not None:
  621. if data.project_id == 0:
  622. folder.project_id = None
  623. else:
  624. # Verify project exists
  625. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  626. if not project_result.scalar_one_or_none():
  627. raise HTTPException(status_code=404, detail="Project not found")
  628. folder.project_id = data.project_id
  629. # Update archive_id (0 to unlink)
  630. if data.archive_id is not None:
  631. if data.archive_id == 0:
  632. folder.archive_id = None
  633. else:
  634. # Verify archive exists
  635. archive_result = await db.execute(select(PrintArchive).where(PrintArchive.id == data.archive_id))
  636. if not archive_result.scalar_one_or_none():
  637. raise HTTPException(status_code=404, detail="Archive not found")
  638. folder.archive_id = data.archive_id
  639. await db.commit()
  640. await db.refresh(folder)
  641. # Get file count and names
  642. file_count_result = await db.execute(
  643. select(func.count(LibraryFile.id)).where(
  644. LibraryFile.folder_id == folder_id,
  645. LibraryFile.deleted_at.is_(None),
  646. )
  647. )
  648. file_count = file_count_result.scalar() or 0
  649. # Get project and archive names
  650. project_name = None
  651. archive_name = None
  652. if folder.project_id:
  653. project_result = await db.execute(select(Project.name).where(Project.id == folder.project_id))
  654. project_name = project_result.scalar()
  655. if folder.archive_id:
  656. archive_result = await db.execute(select(PrintArchive.print_name).where(PrintArchive.id == folder.archive_id))
  657. archive_name = archive_result.scalar()
  658. return FolderResponse(
  659. id=folder.id,
  660. name=folder.name,
  661. parent_id=folder.parent_id,
  662. project_id=folder.project_id,
  663. archive_id=folder.archive_id,
  664. project_name=project_name,
  665. archive_name=archive_name,
  666. is_external=folder.is_external,
  667. external_path=folder.external_path,
  668. external_readonly=folder.external_readonly,
  669. external_show_hidden=folder.external_show_hidden,
  670. file_count=file_count,
  671. created_at=folder.created_at,
  672. updated_at=folder.updated_at,
  673. )
  674. @router.delete("/folders/{folder_id}")
  675. async def delete_folder(
  676. folder_id: int,
  677. db: AsyncSession = Depends(get_db),
  678. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_DELETE_ALL)),
  679. ):
  680. """Delete a folder and all its contents (cascade).
  681. Note: Folders require library:delete_all permission since they don't have
  682. ownership tracking.
  683. """
  684. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  685. folder = result.scalar_one_or_none()
  686. if not folder:
  687. raise HTTPException(status_code=404, detail="Folder not found")
  688. # External folders: only remove DB records, never delete files from external path
  689. is_ext = folder.is_external
  690. # Get all files in this folder and subfolders to delete from disk
  691. async def get_all_file_ids(fid: int) -> list[int]:
  692. """Recursively get all file IDs in a folder tree."""
  693. file_ids = []
  694. # Get files in this folder
  695. files_result = await db.execute(
  696. select(LibraryFile.id, LibraryFile.file_path, LibraryFile.thumbnail_path, LibraryFile.is_external).where(
  697. LibraryFile.folder_id == fid
  698. )
  699. )
  700. for fid_val, file_path, thumb_path, file_is_ext in files_result.all():
  701. file_ids.append(fid_val)
  702. # Only delete non-external files from disk
  703. if not is_ext and not file_is_ext:
  704. try:
  705. if file_path and os.path.exists(file_path):
  706. os.remove(file_path)
  707. if thumb_path and os.path.exists(thumb_path):
  708. os.remove(thumb_path)
  709. except OSError as e:
  710. logger.warning("Failed to delete file: %s", e)
  711. # Get child folders and recurse
  712. children_result = await db.execute(select(LibraryFolder.id).where(LibraryFolder.parent_id == fid))
  713. for (child_id,) in children_result.all():
  714. file_ids.extend(await get_all_file_ids(child_id))
  715. return file_ids
  716. await get_all_file_ids(folder_id)
  717. # Delete folder (cascade will handle files and subfolders)
  718. await db.delete(folder)
  719. await db.commit()
  720. return {"status": "success", "message": "Folder deleted"}
  721. # ============ External Folder Endpoints ============
  722. # Blocked system directories that cannot be mounted
  723. _BLOCKED_PREFIXES = (
  724. "/proc",
  725. "/sys",
  726. "/dev",
  727. "/run",
  728. "/boot",
  729. "/sbin",
  730. "/bin",
  731. "/usr/sbin",
  732. "/usr/bin",
  733. "/lib",
  734. "/etc",
  735. )
  736. # Supported file extensions for external folder scanning
  737. _SCANNABLE_EXTENSIONS = {
  738. ".3mf",
  739. ".gcode",
  740. ".gcode.3mf",
  741. ".stl",
  742. ".obj",
  743. ".step",
  744. ".stp",
  745. ".png",
  746. ".jpg",
  747. ".jpeg",
  748. ".gif",
  749. ".webp",
  750. ".svg",
  751. }
  752. def _validate_external_path(path_str: str) -> Path:
  753. """Validate an external path is safe to mount."""
  754. path = Path(path_str).resolve()
  755. if not path.is_absolute():
  756. raise HTTPException(status_code=400, detail="Path must be absolute")
  757. for prefix in _BLOCKED_PREFIXES:
  758. if str(path).startswith(prefix):
  759. raise HTTPException(status_code=400, detail=f"Cannot mount system directory: {prefix}")
  760. if not path.exists():
  761. raise HTTPException(status_code=400, detail=f"Path does not exist: {path}")
  762. if not path.is_dir():
  763. raise HTTPException(status_code=400, detail=f"Path is not a directory: {path}")
  764. # Check readability
  765. if not os.access(path, os.R_OK):
  766. raise HTTPException(status_code=400, detail=f"Path is not readable: {path}")
  767. return path
  768. @router.post("/folders/external", response_model=FolderResponse)
  769. async def create_external_folder(
  770. data: ExternalFolderCreate,
  771. db: AsyncSession = Depends(get_db),
  772. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  773. ):
  774. """Create an external folder that points to a host directory."""
  775. resolved = _validate_external_path(data.external_path)
  776. # Check no other external folder already points to this path
  777. existing = await db.execute(
  778. select(LibraryFolder).where(
  779. LibraryFolder.is_external.is_(True),
  780. LibraryFolder.external_path == str(resolved),
  781. )
  782. )
  783. if existing.scalar_one_or_none():
  784. raise HTTPException(status_code=409, detail="An external folder already exists for this path")
  785. # Verify parent exists if specified
  786. if data.parent_id is not None:
  787. parent_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.parent_id))
  788. if not parent_result.scalar_one_or_none():
  789. raise HTTPException(status_code=404, detail="Parent folder not found")
  790. folder = LibraryFolder(
  791. name=data.name,
  792. parent_id=data.parent_id,
  793. is_external=True,
  794. external_path=str(resolved),
  795. external_readonly=data.readonly,
  796. external_show_hidden=data.show_hidden,
  797. )
  798. db.add(folder)
  799. await db.commit()
  800. await db.refresh(folder)
  801. return FolderResponse(
  802. id=folder.id,
  803. name=folder.name,
  804. parent_id=folder.parent_id,
  805. project_id=None,
  806. archive_id=None,
  807. is_external=True,
  808. external_path=folder.external_path,
  809. external_readonly=folder.external_readonly,
  810. external_show_hidden=folder.external_show_hidden,
  811. file_count=0,
  812. created_at=folder.created_at,
  813. updated_at=folder.updated_at,
  814. )
  815. @router.post("/folders/{folder_id}/scan")
  816. async def scan_external_folder(
  817. folder_id: int,
  818. db: AsyncSession = Depends(get_db),
  819. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  820. ):
  821. """Scan an external folder and sync files to the database.
  822. Discovers new files, removes DB entries for deleted files.
  823. Does not copy files — stores the external path directly.
  824. """
  825. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  826. folder = result.scalar_one_or_none()
  827. if not folder:
  828. raise HTTPException(status_code=404, detail="Folder not found")
  829. if not folder.is_external or not folder.external_path:
  830. raise HTTPException(status_code=400, detail="Not an external folder")
  831. ext_path = Path(folder.external_path)
  832. if not ext_path.exists() or not ext_path.is_dir():
  833. raise HTTPException(status_code=400, detail=f"External path is not accessible: {folder.external_path}")
  834. # Collect all existing child external subfolder IDs (single query)
  835. all_folder_ids = [folder_id]
  836. child_result = await db.execute(
  837. select(LibraryFolder).where(
  838. LibraryFolder.is_external.is_(True),
  839. LibraryFolder.parent_id.isnot(None),
  840. )
  841. )
  842. all_child_folders = child_result.scalars().all()
  843. # Walk the parent chain to find all descendants of folder_id
  844. parent_to_children: dict[int, list] = {}
  845. for cf in all_child_folders:
  846. parent_to_children.setdefault(cf.parent_id, []).append(cf)
  847. queue = [folder_id]
  848. while queue:
  849. pid = queue.pop()
  850. for child in parent_to_children.get(pid, []):
  851. all_folder_ids.append(child.id)
  852. queue.append(child.id)
  853. # Get existing DB files across root and all subfolders
  854. existing_result = await db.execute(
  855. LibraryFile.active().where(
  856. LibraryFile.folder_id.in_(all_folder_ids),
  857. LibraryFile.is_external.is_(True),
  858. )
  859. )
  860. existing_files = {f.file_path: f for f in existing_result.scalars().all()}
  861. # Build folder cache: relative path -> folder_id (for resolving subfolders)
  862. # Pre-populate with existing child folders keyed by their external_path
  863. folder_cache: dict[str, int] = {"": folder_id}
  864. for fid in all_folder_ids:
  865. if fid == folder_id:
  866. continue
  867. # Find the child folder object
  868. for cf in all_child_folders:
  869. if cf.id == fid and cf.external_path:
  870. try:
  871. rel = str(Path(cf.external_path).relative_to(ext_path))
  872. if rel != ".":
  873. folder_cache[rel] = cf.id
  874. except ValueError:
  875. pass
  876. # Scan the directory
  877. added = 0
  878. removed = 0
  879. found_paths: set[str] = set()
  880. seen_rel_dirs: set[str] = set()
  881. for dirpath, dirnames, filenames in os.walk(ext_path):
  882. # Filter hidden directories unless configured
  883. if not folder.external_show_hidden:
  884. dirnames[:] = [d for d in dirnames if not d.startswith(".")]
  885. rel_dir = str(Path(dirpath).relative_to(ext_path))
  886. if rel_dir == ".":
  887. rel_dir = ""
  888. seen_rel_dirs.add(rel_dir)
  889. # Resolve or create subfolder chain for this directory
  890. if rel_dir and rel_dir not in folder_cache:
  891. parts = Path(rel_dir).parts
  892. current_path = ""
  893. current_parent = folder_id
  894. for part in parts:
  895. current_path = f"{current_path}/{part}".lstrip("/")
  896. if current_path in folder_cache:
  897. current_parent = folder_cache[current_path]
  898. else:
  899. existing_sub = await db.execute(
  900. select(LibraryFolder).where(
  901. LibraryFolder.name == part,
  902. LibraryFolder.parent_id == current_parent,
  903. LibraryFolder.is_external.is_(True),
  904. )
  905. )
  906. existing_folder = existing_sub.scalar_one_or_none()
  907. if existing_folder:
  908. current_parent = existing_folder.id
  909. else:
  910. new_folder = LibraryFolder(
  911. name=part,
  912. parent_id=current_parent,
  913. is_external=True,
  914. external_path=str(ext_path / current_path),
  915. external_readonly=folder.external_readonly,
  916. external_show_hidden=folder.external_show_hidden,
  917. )
  918. db.add(new_folder)
  919. await db.flush()
  920. current_parent = new_folder.id
  921. folder_cache[current_path] = current_parent
  922. target_folder_id = folder_cache.get(rel_dir, folder_id)
  923. for filename in filenames:
  924. # Skip hidden files unless configured
  925. if not folder.external_show_hidden and filename.startswith("."):
  926. continue
  927. filepath = Path(dirpath) / filename
  928. ext = filepath.suffix.lower()
  929. # Check for compound extensions like .gcode.3mf
  930. if ext not in _SCANNABLE_EXTENSIONS:
  931. # Check compound
  932. compound = "".join(filepath.suffixes[-2:]).lower() if len(filepath.suffixes) >= 2 else ""
  933. if compound not in _SCANNABLE_EXTENSIONS:
  934. continue
  935. # Resolve symlinks and ensure still under external_path
  936. try:
  937. real_path = filepath.resolve()
  938. real_path.relative_to(ext_path.resolve())
  939. except (ValueError, OSError):
  940. continue # Symlink escapes the external dir
  941. file_path_str = str(filepath)
  942. found_paths.add(file_path_str)
  943. if file_path_str in existing_files:
  944. continue # Already tracked
  945. # Get file info
  946. try:
  947. stat = filepath.stat()
  948. except OSError:
  949. continue
  950. file_type = ext[1:] if ext else "unknown"
  951. # For compound extensions, use the meaningful part
  952. if file_type in ("3mf",) and len(filepath.suffixes) >= 2:
  953. inner = filepath.suffixes[-2].lower()
  954. if inner == ".gcode":
  955. file_type = "gcode.3mf"
  956. # Extract thumbnail for 3mf files
  957. thumbnail_path = None
  958. file_metadata = None
  959. if file_type == "3mf":
  960. try:
  961. parser = ThreeMFParser(str(filepath))
  962. raw_metadata = parser.parse()
  963. if raw_metadata:
  964. # Extract thumbnail before cleaning metadata
  965. thumb_data = raw_metadata.get("_thumbnail_data")
  966. thumbnail_ext = raw_metadata.get("_thumbnail_ext", ".png")
  967. if thumb_data:
  968. thumb_dir = get_library_thumbnails_dir()
  969. thumb_filename = f"{uuid.uuid4().hex}{thumbnail_ext}"
  970. thumb_full = thumb_dir / thumb_filename
  971. thumb_full.write_bytes(thumb_data)
  972. thumbnail_path = to_relative_path(thumb_full)
  973. # Clean metadata - remove non-JSON-serializable data (bytes, etc.)
  974. def clean_metadata(obj):
  975. if isinstance(obj, dict):
  976. return {
  977. k: clean_metadata(v)
  978. for k, v in obj.items()
  979. if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext")
  980. }
  981. elif isinstance(obj, list):
  982. return [clean_metadata(i) for i in obj if not isinstance(i, bytes)]
  983. elif isinstance(obj, bytes):
  984. return None
  985. return obj
  986. file_metadata = clean_metadata(raw_metadata)
  987. except Exception as e:
  988. logger.debug("Failed to extract metadata from external 3mf %s: %s", filepath, e)
  989. # Generate thumbnail for STL files
  990. if file_type == "stl" and thumbnail_path is None:
  991. try:
  992. thumb_dir = get_library_thumbnails_dir()
  993. thumb_result = generate_stl_thumbnail(str(filepath), str(thumb_dir))
  994. if thumb_result:
  995. thumbnail_path = to_relative_path(Path(thumb_result))
  996. except Exception as e:
  997. logger.debug("Failed to generate STL thumbnail for external %s: %s", filepath, e)
  998. # Extract gcode thumbnail
  999. if file_type == "gcode" and thumbnail_path is None:
  1000. thumb_data = extract_gcode_thumbnail(filepath)
  1001. if thumb_data:
  1002. thumb_dir = get_library_thumbnails_dir()
  1003. thumb_filename = f"{uuid.uuid4().hex}.png"
  1004. thumb_full = thumb_dir / thumb_filename
  1005. thumb_full.write_bytes(thumb_data)
  1006. thumbnail_path = to_relative_path(thumb_full)
  1007. # Create thumbnail for image files
  1008. if ext.lower() in IMAGE_EXTENSIONS and thumbnail_path is None:
  1009. thumbnail_path_str = create_image_thumbnail(filepath, get_library_thumbnails_dir())
  1010. if thumbnail_path_str:
  1011. thumbnail_path = to_relative_path(Path(thumbnail_path_str))
  1012. db_file = LibraryFile(
  1013. folder_id=target_folder_id,
  1014. is_external=True,
  1015. filename=filename,
  1016. file_path=file_path_str,
  1017. file_type=file_type,
  1018. file_size=stat.st_size,
  1019. file_hash=None, # Skip hashing external files for performance
  1020. thumbnail_path=thumbnail_path,
  1021. file_metadata=file_metadata,
  1022. )
  1023. db.add(db_file)
  1024. added += 1
  1025. # Remove DB entries for files that no longer exist on disk
  1026. for path_str, db_file in existing_files.items():
  1027. if path_str not in found_paths:
  1028. # Clean up thumbnail if we generated one
  1029. if db_file.thumbnail_path:
  1030. try:
  1031. abs_thumb = to_absolute_path(db_file.thumbnail_path)
  1032. if abs_thumb and abs_thumb.exists():
  1033. abs_thumb.unlink()
  1034. except OSError:
  1035. pass
  1036. await db.delete(db_file)
  1037. removed += 1
  1038. # Remove empty subfolders whose directories no longer exist on disk
  1039. # Process deepest-first by sorting on path depth (descending)
  1040. subfolder_entries = [(rel, fid) for rel, fid in folder_cache.items() if rel and fid != folder_id]
  1041. subfolder_entries.sort(key=lambda x: x[0].count("/"), reverse=True)
  1042. for rel_path, sub_fid in subfolder_entries:
  1043. if rel_path in seen_rel_dirs:
  1044. continue # Directory still exists on disk
  1045. # Check if subfolder has any remaining files
  1046. file_count_result = await db.execute(
  1047. select(func.count(LibraryFile.id)).where(
  1048. LibraryFile.folder_id == sub_fid,
  1049. LibraryFile.deleted_at.is_(None),
  1050. )
  1051. )
  1052. if (file_count_result.scalar() or 0) == 0:
  1053. # Check if it has any remaining child folders
  1054. child_count_result = await db.execute(
  1055. select(func.count(LibraryFolder.id)).where(LibraryFolder.parent_id == sub_fid)
  1056. )
  1057. if (child_count_result.scalar() or 0) == 0:
  1058. sub_folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == sub_fid))
  1059. sub_folder_obj = sub_folder_result.scalar_one_or_none()
  1060. if sub_folder_obj:
  1061. await db.delete(sub_folder_obj)
  1062. await db.commit()
  1063. return {"status": "success", "added": added, "removed": removed}
  1064. # ============ File Endpoints ============
  1065. @router.get("/files", response_model=list[FileListResponse])
  1066. @router.get("/files/", response_model=list[FileListResponse])
  1067. async def list_files(
  1068. response: Response,
  1069. folder_id: int | None = None,
  1070. project_id: int | None = None,
  1071. include_root: bool = True,
  1072. db: AsyncSession = Depends(get_db),
  1073. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  1074. ):
  1075. """List files, optionally filtered by folder or project.
  1076. Args:
  1077. folder_id: Filter by folder ID. If None and include_root=True, returns root files.
  1078. project_id: Return all files across folders linked to this project (bulk fetch, avoids N+1).
  1079. include_root: If True and folder_id is None, returns files at root level.
  1080. If False and folder_id is None, returns all files.
  1081. """
  1082. query = LibraryFile.active().options(selectinload(LibraryFile.created_by))
  1083. if folder_id is not None:
  1084. query = query.where(LibraryFile.folder_id == folder_id)
  1085. elif project_id is not None:
  1086. # Single join instead of one query per folder (avoids N+1 pattern)
  1087. query = query.join(LibraryFolder, LibraryFile.folder_id == LibraryFolder.id)
  1088. query = query.where(LibraryFolder.project_id == project_id)
  1089. elif include_root:
  1090. query = query.where(LibraryFile.folder_id.is_(None))
  1091. query = query.order_by(LibraryFile.filename)
  1092. result = await db.execute(query)
  1093. files = result.scalars().all()
  1094. # Get duplicate counts
  1095. hash_counts = {}
  1096. if files:
  1097. hashes = [f.file_hash for f in files if f.file_hash]
  1098. if hashes:
  1099. dup_result = await db.execute(
  1100. select(LibraryFile.file_hash, func.count(LibraryFile.id))
  1101. .where(LibraryFile.file_hash.in_(hashes), LibraryFile.deleted_at.is_(None))
  1102. .group_by(LibraryFile.file_hash)
  1103. )
  1104. hash_counts = {h: c - 1 for h, c in dup_result.all()} # -1 to exclude self
  1105. # Prevent browser caching of file list
  1106. response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
  1107. file_list = []
  1108. for f in files:
  1109. # Extract key metadata for display
  1110. print_name = None
  1111. print_time = None
  1112. filament_grams = None
  1113. sliced_for_model = None
  1114. if f.file_metadata:
  1115. print_name = f.file_metadata.get("print_name")
  1116. print_time = f.file_metadata.get("print_time_seconds")
  1117. filament_grams = f.file_metadata.get("filament_used_grams")
  1118. sliced_for_model = f.file_metadata.get("sliced_for_model")
  1119. file_list.append(
  1120. FileListResponse(
  1121. id=f.id,
  1122. folder_id=f.folder_id,
  1123. is_external=f.is_external,
  1124. filename=f.filename,
  1125. file_type=f.file_type,
  1126. file_size=f.file_size,
  1127. thumbnail_path=f.thumbnail_path,
  1128. print_count=f.print_count,
  1129. duplicate_count=hash_counts.get(f.file_hash, 0) if f.file_hash else 0,
  1130. created_by_id=f.created_by_id,
  1131. created_by_username=f.created_by.username if f.created_by else None,
  1132. created_at=f.created_at,
  1133. print_name=print_name,
  1134. print_time_seconds=print_time,
  1135. filament_used_grams=filament_grams,
  1136. sliced_for_model=sliced_for_model,
  1137. )
  1138. )
  1139. return file_list
  1140. @router.post("/files", response_model=FileUploadResponse)
  1141. @router.post("/files/", response_model=FileUploadResponse)
  1142. async def upload_file(
  1143. file: UploadFile = File(...),
  1144. folder_id: int | None = None,
  1145. generate_stl_thumbnails: bool = Query(default=True),
  1146. db: AsyncSession = Depends(get_db),
  1147. current_user: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  1148. ):
  1149. """Upload a file to the library."""
  1150. try:
  1151. if not file.filename:
  1152. raise HTTPException(status_code=400, detail="Filename is required")
  1153. filename = file.filename
  1154. ext = os.path.splitext(filename)[1].lower()
  1155. # Handle files without extension
  1156. file_type = ext[1:] if ext else "unknown"
  1157. # Verify folder exists if specified
  1158. target_folder = None
  1159. if folder_id is not None:
  1160. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  1161. target_folder = folder_result.scalar_one_or_none()
  1162. if not target_folder:
  1163. raise HTTPException(status_code=404, detail="Folder not found")
  1164. # Writable external folders write through to the mount so the file is
  1165. # visible outside Bambuddy (#1112); everything else lands under the
  1166. # internal library dir with a UUID-scoped filename.
  1167. file_path, is_external_upload = _resolve_upload_destination(target_folder, filename)
  1168. # Save file
  1169. content = await file.read()
  1170. with open(file_path, "wb") as f:
  1171. f.write(content)
  1172. # Calculate hash
  1173. file_hash = calculate_file_hash(file_path)
  1174. # Check for duplicates
  1175. dup_result = await db.execute(
  1176. select(LibraryFile.id).where(LibraryFile.file_hash == file_hash, LibraryFile.deleted_at.is_(None)).limit(1)
  1177. )
  1178. duplicate_of = dup_result.scalar()
  1179. # Extract metadata and thumbnail
  1180. metadata = {}
  1181. thumbnail_path = None
  1182. thumbnails_dir = get_library_thumbnails_dir()
  1183. if ext == ".3mf":
  1184. try:
  1185. parser = ThreeMFParser(str(file_path))
  1186. raw_metadata = parser.parse()
  1187. # Extract thumbnail before cleaning metadata
  1188. thumbnail_data = raw_metadata.get("_thumbnail_data")
  1189. thumbnail_ext = raw_metadata.get("_thumbnail_ext", ".png")
  1190. # Save thumbnail if extracted
  1191. if thumbnail_data:
  1192. thumb_filename = f"{uuid.uuid4().hex}{thumbnail_ext}"
  1193. thumb_path = thumbnails_dir / thumb_filename
  1194. with open(thumb_path, "wb") as f:
  1195. f.write(thumbnail_data)
  1196. thumbnail_path = str(thumb_path)
  1197. # Clean metadata - remove non-JSON-serializable data (bytes, etc.)
  1198. def clean_metadata(obj):
  1199. if isinstance(obj, dict):
  1200. return {
  1201. k: clean_metadata(v)
  1202. for k, v in obj.items()
  1203. if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext")
  1204. }
  1205. elif isinstance(obj, list):
  1206. return [clean_metadata(i) for i in obj if not isinstance(i, bytes)]
  1207. elif isinstance(obj, bytes):
  1208. return None
  1209. return obj
  1210. metadata = clean_metadata(raw_metadata)
  1211. except Exception as e:
  1212. logger.warning("Failed to parse 3MF: %s", e)
  1213. elif ext == ".gcode":
  1214. # Extract embedded thumbnail from gcode
  1215. try:
  1216. thumbnail_data = extract_gcode_thumbnail(file_path)
  1217. if thumbnail_data:
  1218. thumb_filename = f"{uuid.uuid4().hex}.png"
  1219. thumb_path = thumbnails_dir / thumb_filename
  1220. with open(thumb_path, "wb") as f:
  1221. f.write(thumbnail_data)
  1222. thumbnail_path = str(thumb_path)
  1223. except Exception as e:
  1224. logger.warning("Failed to extract gcode thumbnail: %s", e)
  1225. elif ext.lower() in IMAGE_EXTENSIONS:
  1226. # For image files, create a thumbnail from the image itself
  1227. thumbnail_path = create_image_thumbnail(file_path, thumbnails_dir)
  1228. elif ext == ".stl":
  1229. # Generate STL thumbnail if enabled
  1230. if generate_stl_thumbnails:
  1231. thumbnail_path = generate_stl_thumbnail(file_path, thumbnails_dir)
  1232. # Create database entry (managed files store relative paths for portability;
  1233. # external files store the absolute mount path — same shape as scan produces)
  1234. library_file = LibraryFile(
  1235. folder_id=folder_id,
  1236. is_external=is_external_upload,
  1237. filename=filename,
  1238. file_path=_stored_file_path(file_path, is_external_upload),
  1239. file_type=file_type,
  1240. file_size=len(content),
  1241. file_hash=file_hash,
  1242. thumbnail_path=to_relative_path(thumbnail_path) if thumbnail_path else None,
  1243. file_metadata=metadata if metadata else None,
  1244. created_by_id=current_user.id if current_user else None,
  1245. )
  1246. db.add(library_file)
  1247. await db.commit()
  1248. await db.refresh(library_file)
  1249. return FileUploadResponse(
  1250. id=library_file.id,
  1251. filename=library_file.filename,
  1252. file_type=library_file.file_type,
  1253. file_size=library_file.file_size,
  1254. thumbnail_path=library_file.thumbnail_path,
  1255. duplicate_of=duplicate_of,
  1256. metadata=library_file.file_metadata,
  1257. )
  1258. except HTTPException:
  1259. raise
  1260. except Exception as e:
  1261. logger.error("Upload failed for %s: %s", file.filename, e, exc_info=True)
  1262. raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)}")
  1263. @router.post("/files/extract-zip", response_model=ZipExtractResponse)
  1264. async def extract_zip_file(
  1265. file: UploadFile = File(...),
  1266. folder_id: int | None = Query(default=None),
  1267. preserve_structure: bool = Query(default=True),
  1268. create_folder_from_zip: bool = Query(default=False),
  1269. generate_stl_thumbnails: bool = Query(default=True),
  1270. db: AsyncSession = Depends(get_db),
  1271. current_user: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  1272. ):
  1273. """Upload and extract a ZIP file to the library.
  1274. Args:
  1275. file: The ZIP file to extract
  1276. folder_id: Target folder ID (None = root)
  1277. preserve_structure: If True, recreate folder structure from ZIP; if False, extract all files flat
  1278. create_folder_from_zip: If True, create a folder named after the ZIP file and extract into it
  1279. generate_stl_thumbnails: If True, generate thumbnails for STL files
  1280. """
  1281. import tempfile
  1282. if not file.filename or not file.filename.lower().endswith(".zip"):
  1283. raise HTTPException(status_code=400, detail="Only ZIP files are supported")
  1284. # Verify target folder exists if specified
  1285. if folder_id is not None:
  1286. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  1287. target_folder = folder_result.scalar_one_or_none()
  1288. if not target_folder:
  1289. raise HTTPException(status_code=404, detail="Target folder not found")
  1290. if target_folder.is_external and target_folder.external_readonly:
  1291. raise HTTPException(status_code=403, detail="Cannot extract ZIP to a read-only external folder")
  1292. if target_folder.is_external:
  1293. # Writable external folders aren't supported by extract-zip because the
  1294. # nested-subfolder creation path would need to mkdir on the mount and
  1295. # create matching is_external=True LibraryFolder rows — a separate
  1296. # design. Direct the user at Scan, which already handles that shape
  1297. # (#1112).
  1298. raise HTTPException(
  1299. status_code=400,
  1300. detail=(
  1301. "Cannot extract ZIP directly into an external folder. "
  1302. "Extract the ZIP on the external mount and run 'Scan External Folder' instead."
  1303. ),
  1304. )
  1305. # Save ZIP to temp file
  1306. try:
  1307. with tempfile.NamedTemporaryFile(delete=False, suffix=".zip") as tmp:
  1308. content = await file.read()
  1309. tmp.write(content)
  1310. tmp_path = tmp.name
  1311. except Exception as e:
  1312. raise HTTPException(status_code=500, detail=f"Failed to save ZIP file: {str(e)}")
  1313. extracted_files: list[ZipExtractResult] = []
  1314. errors: list[ZipExtractError] = []
  1315. folders_created = 0
  1316. folder_cache: dict[str, int] = {} # path -> folder_id
  1317. # If create_folder_from_zip is True, create a folder named after the ZIP file
  1318. zip_folder_id = folder_id
  1319. logger.info(
  1320. f"ZIP extraction: create_folder_from_zip={create_folder_from_zip}, folder_id={folder_id}, filename={file.filename}"
  1321. )
  1322. if create_folder_from_zip and file.filename:
  1323. # Remove .zip extension to get folder name
  1324. zip_folder_name = file.filename[:-4] if file.filename.lower().endswith(".zip") else file.filename
  1325. # Check if folder already exists
  1326. existing = await db.execute(
  1327. select(LibraryFolder).where(
  1328. LibraryFolder.name == zip_folder_name,
  1329. LibraryFolder.parent_id == folder_id if folder_id else LibraryFolder.parent_id.is_(None),
  1330. )
  1331. )
  1332. existing_folder = existing.scalar_one_or_none()
  1333. if existing_folder:
  1334. zip_folder_id = existing_folder.id
  1335. logger.info("Reusing existing folder '%s' with id=%s", zip_folder_name, zip_folder_id)
  1336. else:
  1337. # Create folder
  1338. new_folder = LibraryFolder(name=zip_folder_name, parent_id=folder_id)
  1339. db.add(new_folder)
  1340. await db.flush()
  1341. await db.commit() # Commit folder creation immediately
  1342. zip_folder_id = new_folder.id
  1343. folders_created += 1
  1344. logger.info("Created new folder '%s' with id=%s", zip_folder_name, zip_folder_id)
  1345. try:
  1346. with zipfile.ZipFile(tmp_path, "r") as zf:
  1347. # Filter out directories and hidden/system files
  1348. file_list = [
  1349. name
  1350. for name in zf.namelist()
  1351. if not name.endswith("/")
  1352. and not name.startswith("__MACOSX")
  1353. and not os.path.basename(name).startswith(".")
  1354. ]
  1355. for zip_path in file_list:
  1356. try:
  1357. # Determine target folder (use zip_folder_id as base if create_folder_from_zip was used)
  1358. target_folder_id = zip_folder_id
  1359. if preserve_structure:
  1360. # Get directory path from ZIP
  1361. dir_path = os.path.dirname(zip_path)
  1362. if dir_path:
  1363. # Create folder structure
  1364. parts = dir_path.split("/")
  1365. current_parent = zip_folder_id
  1366. current_path = ""
  1367. for part in parts:
  1368. if not part:
  1369. continue
  1370. current_path = f"{current_path}/{part}" if current_path else part
  1371. if current_path in folder_cache:
  1372. current_parent = folder_cache[current_path]
  1373. else:
  1374. # Check if folder exists
  1375. existing = await db.execute(
  1376. select(LibraryFolder).where(
  1377. LibraryFolder.name == part,
  1378. LibraryFolder.parent_id == current_parent
  1379. if current_parent
  1380. else LibraryFolder.parent_id.is_(None),
  1381. )
  1382. )
  1383. existing_folder = existing.scalar_one_or_none()
  1384. if existing_folder:
  1385. current_parent = existing_folder.id
  1386. else:
  1387. # Create folder
  1388. new_folder = LibraryFolder(name=part, parent_id=current_parent)
  1389. db.add(new_folder)
  1390. await db.flush()
  1391. current_parent = new_folder.id
  1392. folders_created += 1
  1393. folder_cache[current_path] = current_parent
  1394. target_folder_id = current_parent
  1395. # Extract file
  1396. filename = os.path.basename(zip_path)
  1397. ext = os.path.splitext(filename)[1].lower()
  1398. file_type = ext[1:] if ext else "unknown"
  1399. # Generate unique filename for storage
  1400. unique_filename = f"{uuid.uuid4().hex}{ext}"
  1401. file_path = get_library_files_dir() / unique_filename
  1402. # Extract and save file
  1403. file_content = zf.read(zip_path)
  1404. with open(file_path, "wb") as f:
  1405. f.write(file_content)
  1406. # Calculate hash
  1407. file_hash = calculate_file_hash(file_path)
  1408. # Extract metadata and thumbnail for 3MF files
  1409. metadata = {}
  1410. thumbnail_path = None
  1411. thumbnails_dir = get_library_thumbnails_dir()
  1412. if ext == ".3mf":
  1413. try:
  1414. parser = ThreeMFParser(str(file_path))
  1415. raw_metadata = parser.parse()
  1416. thumbnail_data = raw_metadata.get("_thumbnail_data")
  1417. thumbnail_ext = raw_metadata.get("_thumbnail_ext", ".png")
  1418. if thumbnail_data:
  1419. thumb_filename = f"{uuid.uuid4().hex}{thumbnail_ext}"
  1420. thumb_path = thumbnails_dir / thumb_filename
  1421. with open(thumb_path, "wb") as f:
  1422. f.write(thumbnail_data)
  1423. thumbnail_path = str(thumb_path)
  1424. def clean_metadata(obj):
  1425. if isinstance(obj, dict):
  1426. return {
  1427. k: clean_metadata(v)
  1428. for k, v in obj.items()
  1429. if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext")
  1430. }
  1431. elif isinstance(obj, list):
  1432. return [clean_metadata(i) for i in obj if not isinstance(i, bytes)]
  1433. elif isinstance(obj, bytes):
  1434. return None
  1435. return obj
  1436. metadata = clean_metadata(raw_metadata)
  1437. except Exception as e:
  1438. logger.warning("Failed to parse 3MF from ZIP: %s", e)
  1439. elif ext == ".gcode":
  1440. try:
  1441. thumbnail_data = extract_gcode_thumbnail(file_path)
  1442. if thumbnail_data:
  1443. thumb_filename = f"{uuid.uuid4().hex}.png"
  1444. thumb_path = thumbnails_dir / thumb_filename
  1445. with open(thumb_path, "wb") as f:
  1446. f.write(thumbnail_data)
  1447. thumbnail_path = str(thumb_path)
  1448. except Exception as e:
  1449. logger.warning("Failed to extract gcode thumbnail from ZIP: %s", e)
  1450. elif ext.lower() in IMAGE_EXTENSIONS:
  1451. thumbnail_path = create_image_thumbnail(file_path, thumbnails_dir)
  1452. elif ext == ".stl":
  1453. # Generate STL thumbnail if enabled
  1454. if generate_stl_thumbnails:
  1455. thumbnail_path = generate_stl_thumbnail(file_path, thumbnails_dir)
  1456. # Create database entry (store relative paths for portability)
  1457. library_file = LibraryFile(
  1458. folder_id=target_folder_id,
  1459. filename=filename,
  1460. file_path=to_relative_path(file_path),
  1461. file_type=file_type,
  1462. file_size=len(file_content),
  1463. file_hash=file_hash,
  1464. thumbnail_path=to_relative_path(thumbnail_path) if thumbnail_path else None,
  1465. file_metadata=metadata if metadata else None,
  1466. created_by_id=current_user.id if current_user else None,
  1467. )
  1468. db.add(library_file)
  1469. await db.flush()
  1470. await db.refresh(library_file)
  1471. extracted_files.append(
  1472. ZipExtractResult(
  1473. filename=filename,
  1474. file_id=library_file.id,
  1475. folder_id=target_folder_id,
  1476. )
  1477. )
  1478. # Commit after each file to release database lock
  1479. # This prevents long-running transactions from blocking other requests
  1480. await db.commit()
  1481. except Exception as e:
  1482. logger.error("Failed to extract %s: %s", zip_path, e)
  1483. errors.append(ZipExtractError(filename=os.path.basename(zip_path), error=str(e)))
  1484. # Rollback the failed file but continue with others
  1485. await db.rollback()
  1486. return ZipExtractResponse(
  1487. extracted=len(extracted_files),
  1488. folders_created=folders_created,
  1489. files=extracted_files,
  1490. errors=errors,
  1491. )
  1492. except zipfile.BadZipFile:
  1493. raise HTTPException(status_code=400, detail="Invalid or corrupted ZIP file")
  1494. except Exception as e:
  1495. logger.error("ZIP extraction failed: %s", e, exc_info=True)
  1496. raise HTTPException(status_code=500, detail=f"ZIP extraction failed: {str(e)}")
  1497. finally:
  1498. # Clean up temp file
  1499. try:
  1500. os.unlink(tmp_path)
  1501. except OSError:
  1502. pass # Best-effort temp file cleanup; ignore if already removed
  1503. # ============ STL Thumbnail Batch Generation ============
  1504. @router.post("/generate-stl-thumbnails", response_model=BatchThumbnailResponse)
  1505. async def batch_generate_stl_thumbnails(
  1506. request: BatchThumbnailRequest,
  1507. db: AsyncSession = Depends(get_db),
  1508. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPDATE_ALL)),
  1509. ):
  1510. """Generate thumbnails for STL files in batch.
  1511. Note: Requires library:update_all permission since this is a batch operation
  1512. that may affect files owned by different users.
  1513. Can generate thumbnails for:
  1514. - Specific file IDs (file_ids)
  1515. - All STL files in a folder (folder_id)
  1516. - All STL files missing thumbnails (all_missing=True)
  1517. """
  1518. thumbnails_dir = get_library_thumbnails_dir()
  1519. results: list[BatchThumbnailResult] = []
  1520. # Build query based on request
  1521. query = LibraryFile.active().where(LibraryFile.file_type == "stl")
  1522. if request.file_ids:
  1523. # Specific files
  1524. query = query.where(LibraryFile.id.in_(request.file_ids))
  1525. elif request.folder_id is not None:
  1526. # All STL files in a specific folder
  1527. query = query.where(LibraryFile.folder_id == request.folder_id)
  1528. if not request.all_missing:
  1529. # If not specifically asking for missing thumbnails, get all
  1530. pass
  1531. else:
  1532. query = query.where(LibraryFile.thumbnail_path.is_(None))
  1533. elif request.all_missing:
  1534. # All STL files without thumbnails
  1535. query = query.where(LibraryFile.thumbnail_path.is_(None))
  1536. else:
  1537. # No criteria specified - return empty
  1538. return BatchThumbnailResponse(
  1539. processed=0,
  1540. succeeded=0,
  1541. failed=0,
  1542. results=[],
  1543. )
  1544. result = await db.execute(query)
  1545. stl_files = result.scalars().all()
  1546. succeeded = 0
  1547. failed = 0
  1548. for stl_file in stl_files:
  1549. file_path = to_absolute_path(stl_file.file_path)
  1550. if not file_path or not file_path.exists():
  1551. results.append(
  1552. BatchThumbnailResult(
  1553. file_id=stl_file.id,
  1554. filename=stl_file.filename,
  1555. success=False,
  1556. error="File not found on disk",
  1557. )
  1558. )
  1559. failed += 1
  1560. continue
  1561. try:
  1562. thumbnail_path = generate_stl_thumbnail(file_path, thumbnails_dir)
  1563. if thumbnail_path:
  1564. # Update database with relative path
  1565. stl_file.thumbnail_path = to_relative_path(thumbnail_path)
  1566. await db.flush()
  1567. results.append(
  1568. BatchThumbnailResult(
  1569. file_id=stl_file.id,
  1570. filename=stl_file.filename,
  1571. success=True,
  1572. )
  1573. )
  1574. succeeded += 1
  1575. else:
  1576. results.append(
  1577. BatchThumbnailResult(
  1578. file_id=stl_file.id,
  1579. filename=stl_file.filename,
  1580. success=False,
  1581. error="Thumbnail generation failed",
  1582. )
  1583. )
  1584. failed += 1
  1585. except Exception as e:
  1586. logger.error("Failed to generate thumbnail for %s: %s", stl_file.filename, e)
  1587. results.append(
  1588. BatchThumbnailResult(
  1589. file_id=stl_file.id,
  1590. filename=stl_file.filename,
  1591. success=False,
  1592. error=str(e),
  1593. )
  1594. )
  1595. failed += 1
  1596. await db.commit()
  1597. return BatchThumbnailResponse(
  1598. processed=len(stl_files),
  1599. succeeded=succeeded,
  1600. failed=failed,
  1601. results=results,
  1602. )
  1603. # ============ Queue Operations ============
  1604. # NOTE: These routes must be defined BEFORE /files/{file_id} to avoid path parameter conflicts
  1605. def is_sliced_file(filename: str) -> bool:
  1606. """Check if a file is a sliced (printable) file.
  1607. Sliced files are:
  1608. - .gcode files
  1609. - .3mf files that contain '.gcode.' in the name (e.g., filename.gcode.3mf)
  1610. """
  1611. lower = filename.lower()
  1612. return lower.endswith(".gcode") or ".gcode." in lower
  1613. @router.post("/files/add-to-queue", response_model=AddToQueueResponse)
  1614. async def add_files_to_queue(
  1615. request: AddToQueueRequest,
  1616. db: AsyncSession = Depends(get_db),
  1617. _: User | None = Depends(require_permission_if_auth_enabled(Permission.QUEUE_CREATE)),
  1618. ):
  1619. """Add library files to the print queue.
  1620. Only sliced files (.gcode or .gcode.3mf) can be added to the queue.
  1621. The archive will be created automatically when the print starts.
  1622. """
  1623. added: list[AddToQueueResult] = []
  1624. errors: list[AddToQueueError] = []
  1625. # Get all requested files
  1626. result = await db.execute(LibraryFile.active().where(LibraryFile.id.in_(request.file_ids)))
  1627. files = {f.id: f for f in result.scalars().all()}
  1628. # Get max position for queue ordering
  1629. pos_result = await db.execute(select(func.coalesce(func.max(PrintQueueItem.position), 0)))
  1630. max_position = pos_result.scalar() or 0
  1631. for file_id in request.file_ids:
  1632. lib_file = files.get(file_id)
  1633. if not lib_file:
  1634. errors.append(AddToQueueError(file_id=file_id, filename="(not found)", error="File not found"))
  1635. continue
  1636. # Validate file is sliced
  1637. if not is_sliced_file(lib_file.filename):
  1638. errors.append(
  1639. AddToQueueError(
  1640. file_id=file_id,
  1641. filename=lib_file.filename,
  1642. error="Not a sliced file. Only .gcode or .gcode.3mf files can be printed.",
  1643. )
  1644. )
  1645. continue
  1646. try:
  1647. # Verify file exists on disk
  1648. file_path = Path(app_settings.base_dir) / lib_file.file_path
  1649. if not file_path.exists():
  1650. errors.append(
  1651. AddToQueueError(file_id=file_id, filename=lib_file.filename, error="File not found on disk")
  1652. )
  1653. continue
  1654. # Create queue item referencing library file (archive created at print start)
  1655. max_position += 1
  1656. queue_item = PrintQueueItem(
  1657. printer_id=None, # Unassigned
  1658. library_file_id=file_id,
  1659. position=max_position,
  1660. status="pending",
  1661. )
  1662. db.add(queue_item)
  1663. await db.flush() # Get queue_item.id
  1664. added.append(
  1665. AddToQueueResult(
  1666. file_id=file_id,
  1667. filename=lib_file.filename,
  1668. queue_item_id=queue_item.id,
  1669. )
  1670. )
  1671. except Exception as e:
  1672. logger.exception("Error adding file %s to queue", file_id)
  1673. errors.append(AddToQueueError(file_id=file_id, filename=lib_file.filename, error=str(e)))
  1674. await db.commit()
  1675. return AddToQueueResponse(added=added, errors=errors)
  1676. @router.get("/files/{file_id}/plates")
  1677. async def get_library_file_plates(
  1678. file_id: int,
  1679. db: AsyncSession = Depends(get_db),
  1680. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  1681. ):
  1682. """Get available plates from a multi-plate 3MF library file.
  1683. Returns a list of plates with their index, name, thumbnail availability,
  1684. and filament requirements. For single-plate exports, returns a single plate.
  1685. """
  1686. import json
  1687. import defusedxml.ElementTree as ET
  1688. # Get the library file
  1689. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  1690. lib_file = result.scalar_one_or_none()
  1691. if not lib_file:
  1692. raise HTTPException(status_code=404, detail="File not found")
  1693. file_path = Path(app_settings.base_dir) / lib_file.file_path
  1694. if not file_path.exists():
  1695. raise HTTPException(status_code=404, detail="File not found on disk")
  1696. # Only 3MF files have plates
  1697. if not lib_file.filename.lower().endswith(".3mf"):
  1698. return {"file_id": file_id, "filename": lib_file.filename, "plates": [], "is_multi_plate": False}
  1699. plates = []
  1700. try:
  1701. with zipfile.ZipFile(file_path, "r") as zf:
  1702. namelist = zf.namelist()
  1703. # Find all plate gcode files to determine available plates
  1704. gcode_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".gcode")]
  1705. # If no gcode is present (source-only or unsliced), fall back to plate JSON/PNG
  1706. plate_indices: list[int] = []
  1707. if gcode_files:
  1708. # Extract plate indices from gcode filenames
  1709. for gf in gcode_files:
  1710. try:
  1711. plate_str = gf[15:-6] # Remove "Metadata/plate_" and ".gcode"
  1712. plate_indices.append(int(plate_str))
  1713. except ValueError:
  1714. pass # Skip gcode file with non-numeric plate index
  1715. else:
  1716. plate_json_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".json")]
  1717. plate_png_files = [
  1718. n
  1719. for n in namelist
  1720. if n.startswith("Metadata/plate_")
  1721. and n.endswith(".png")
  1722. and "_small" not in n
  1723. and "no_light" not in n
  1724. ]
  1725. plate_name_candidates = plate_json_files + plate_png_files
  1726. plate_re = re.compile(r"^Metadata/plate_(\d+)\.(json|png)$")
  1727. seen_indices: set[int] = set()
  1728. for name in plate_name_candidates:
  1729. match = plate_re.match(name)
  1730. if match:
  1731. try:
  1732. index = int(match.group(1))
  1733. except ValueError:
  1734. continue
  1735. if index in seen_indices:
  1736. continue
  1737. seen_indices.add(index)
  1738. plate_indices.append(index)
  1739. if not plate_indices:
  1740. # No plate metadata found
  1741. return {"file_id": file_id, "filename": lib_file.filename, "plates": [], "is_multi_plate": False}
  1742. plate_indices.sort()
  1743. # Parse model_settings.config for plate names + object assignments
  1744. plate_names = {}
  1745. plate_object_ids: dict[int, list[str]] = {}
  1746. object_names_by_id: dict[str, str] = {}
  1747. if "Metadata/model_settings.config" in namelist:
  1748. try:
  1749. model_content = zf.read("Metadata/model_settings.config").decode()
  1750. model_root = ET.fromstring(model_content)
  1751. for obj_elem in model_root.findall(".//object"):
  1752. obj_id = obj_elem.get("id")
  1753. if not obj_id:
  1754. continue
  1755. name_meta = obj_elem.find("metadata[@key='name']")
  1756. obj_name = name_meta.get("value") if name_meta is not None else None
  1757. if obj_name:
  1758. object_names_by_id[obj_id] = obj_name
  1759. for plate_elem in model_root.findall(".//plate"):
  1760. plater_id = None
  1761. plater_name = None
  1762. for meta in plate_elem.findall("metadata"):
  1763. key = meta.get("key")
  1764. value = meta.get("value")
  1765. if key == "plater_id" and value:
  1766. try:
  1767. plater_id = int(value)
  1768. except ValueError:
  1769. pass # Ignore plate with non-numeric plater_id
  1770. elif key == "plater_name" and value:
  1771. plater_name = value.strip()
  1772. if plater_id is not None and plater_name:
  1773. plate_names[plater_id] = plater_name
  1774. if plater_id is not None:
  1775. for instance_elem in plate_elem.findall("model_instance"):
  1776. for inst_meta in instance_elem.findall("metadata"):
  1777. if inst_meta.get("key") == "object_id":
  1778. obj_id = inst_meta.get("value")
  1779. if not obj_id:
  1780. continue
  1781. plate_object_ids.setdefault(plater_id, [])
  1782. if obj_id not in plate_object_ids[plater_id]:
  1783. plate_object_ids[plater_id].append(obj_id)
  1784. except Exception:
  1785. pass # model_settings.config is optional; skip if missing or malformed
  1786. # Parse slice_info.config for plate metadata
  1787. plate_metadata = {}
  1788. if "Metadata/slice_info.config" in namelist:
  1789. content = zf.read("Metadata/slice_info.config").decode()
  1790. root = ET.fromstring(content)
  1791. for plate_elem in root.findall(".//plate"):
  1792. plate_info = {"filaments": [], "prediction": None, "weight": None, "name": None, "objects": []}
  1793. plate_index = None
  1794. for meta in plate_elem.findall("metadata"):
  1795. key = meta.get("key")
  1796. value = meta.get("value")
  1797. if key == "index" and value:
  1798. try:
  1799. plate_index = int(value)
  1800. except ValueError:
  1801. pass # Ignore plate with non-numeric index
  1802. elif key == "prediction" and value:
  1803. try:
  1804. plate_info["prediction"] = int(value)
  1805. except ValueError:
  1806. pass # Leave prediction as None if not a valid integer
  1807. elif key == "weight" and value:
  1808. try:
  1809. plate_info["weight"] = float(value)
  1810. except ValueError:
  1811. pass # Leave weight as None if not a valid number
  1812. # Get filaments used in this plate
  1813. for filament_elem in plate_elem.findall("filament"):
  1814. filament_id = filament_elem.get("id")
  1815. filament_type = filament_elem.get("type", "")
  1816. filament_color = filament_elem.get("color", "")
  1817. used_g = filament_elem.get("used_g", "0")
  1818. used_m = filament_elem.get("used_m", "0")
  1819. try:
  1820. used_grams = float(used_g)
  1821. except (ValueError, TypeError):
  1822. used_grams = 0
  1823. if used_grams > 0 and filament_id:
  1824. plate_info["filaments"].append(
  1825. {
  1826. "slot_id": int(filament_id),
  1827. "type": filament_type,
  1828. "color": filament_color,
  1829. "used_grams": round(used_grams, 1),
  1830. "used_meters": float(used_m) if used_m else 0,
  1831. }
  1832. )
  1833. plate_info["filaments"].sort(key=lambda x: x["slot_id"])
  1834. # Collect object names
  1835. for obj_elem in plate_elem.findall("object"):
  1836. obj_name = obj_elem.get("name")
  1837. if obj_name and obj_name not in plate_info["objects"]:
  1838. plate_info["objects"].append(obj_name)
  1839. # Set plate name
  1840. if plate_index is not None:
  1841. custom_name = plate_names.get(plate_index)
  1842. if custom_name:
  1843. plate_info["name"] = custom_name
  1844. elif plate_info["objects"]:
  1845. plate_info["name"] = plate_info["objects"][0]
  1846. plate_metadata[plate_index] = plate_info
  1847. # Parse plate_*.json for object lists when slice_info is missing
  1848. plate_json_objects: dict[int, list[str]] = {}
  1849. for name in namelist:
  1850. match = re.match(r"^Metadata/plate_(\d+)\.json$", name)
  1851. if not match:
  1852. continue
  1853. try:
  1854. plate_index = int(match.group(1))
  1855. except ValueError:
  1856. continue
  1857. try:
  1858. payload = json.loads(zf.read(name).decode())
  1859. bbox_objects = payload.get("bbox_objects", [])
  1860. names: list[str] = []
  1861. for obj in bbox_objects:
  1862. obj_name = obj.get("name") if isinstance(obj, dict) else None
  1863. if obj_name and obj_name not in names:
  1864. names.append(obj_name)
  1865. if names:
  1866. plate_json_objects[plate_index] = names
  1867. except Exception:
  1868. continue
  1869. # Build plate list
  1870. for idx in plate_indices:
  1871. meta = plate_metadata.get(idx, {})
  1872. has_thumbnail = f"Metadata/plate_{idx}.png" in namelist
  1873. objects = meta.get("objects", [])
  1874. if not objects:
  1875. objects = plate_json_objects.get(idx, [])
  1876. if not objects and plate_object_ids.get(idx):
  1877. objects = [
  1878. object_names_by_id.get(obj_id, f"Object {obj_id}") for obj_id in plate_object_ids.get(idx, [])
  1879. ]
  1880. plate_name = meta.get("name")
  1881. if not plate_name:
  1882. plate_name = plate_names.get(idx)
  1883. if not plate_name and objects:
  1884. plate_name = objects[0]
  1885. plates.append(
  1886. {
  1887. "index": idx,
  1888. "name": plate_name,
  1889. "objects": objects,
  1890. "object_count": len(objects),
  1891. "has_thumbnail": has_thumbnail,
  1892. "thumbnail_url": f"/api/v1/library/files/{file_id}/plate-thumbnail/{idx}"
  1893. if has_thumbnail
  1894. else None,
  1895. "print_time_seconds": meta.get("prediction"),
  1896. "filament_used_grams": meta.get("weight"),
  1897. "filaments": meta.get("filaments", []),
  1898. }
  1899. )
  1900. except Exception as e:
  1901. logger.warning("Failed to parse plates from library file %s: %s", file_id, e)
  1902. return {
  1903. "file_id": file_id,
  1904. "filename": lib_file.filename,
  1905. "plates": plates,
  1906. "is_multi_plate": len(plates) > 1,
  1907. }
  1908. @router.get("/files/{file_id}/plate-thumbnail/{plate_index}")
  1909. async def get_library_file_plate_thumbnail(
  1910. file_id: int,
  1911. plate_index: int,
  1912. db: AsyncSession = Depends(get_db),
  1913. _: None = RequireCameraStreamTokenIfAuthEnabled,
  1914. ):
  1915. """Get the thumbnail image for a specific plate from a library file."""
  1916. from starlette.responses import Response
  1917. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  1918. lib_file = result.scalar_one_or_none()
  1919. if not lib_file:
  1920. raise HTTPException(status_code=404, detail="File not found")
  1921. file_path = Path(app_settings.base_dir) / lib_file.file_path
  1922. if not file_path.exists():
  1923. raise HTTPException(status_code=404, detail="File not found on disk")
  1924. try:
  1925. with zipfile.ZipFile(file_path, "r") as zf:
  1926. thumb_path = f"Metadata/plate_{plate_index}.png"
  1927. if thumb_path in zf.namelist():
  1928. data = zf.read(thumb_path)
  1929. return Response(content=data, media_type="image/png")
  1930. except Exception:
  1931. pass # Archive unreadable or thumbnail missing; fall through to 404
  1932. raise HTTPException(status_code=404, detail=f"Thumbnail for plate {plate_index} not found")
  1933. @router.get("/files/{file_id}/filament-requirements")
  1934. async def get_library_file_filament_requirements(
  1935. file_id: int,
  1936. plate_id: int | None = None,
  1937. db: AsyncSession = Depends(get_db),
  1938. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  1939. ):
  1940. """Get filament requirements from a library file.
  1941. Parses the 3MF file to extract filament slot IDs, types, colors, and usage.
  1942. This enables AMS slot assignment when printing from the file manager.
  1943. Args:
  1944. file_id: The library file ID
  1945. plate_id: Optional plate index to get filaments for a specific plate
  1946. """
  1947. import defusedxml.ElementTree as ET
  1948. # Get the library file
  1949. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  1950. lib_file = result.scalar_one_or_none()
  1951. if not lib_file:
  1952. raise HTTPException(status_code=404, detail="File not found")
  1953. # Get the full file path
  1954. file_path = Path(app_settings.base_dir) / lib_file.file_path
  1955. if not file_path.exists():
  1956. raise HTTPException(status_code=404, detail="File not found on disk")
  1957. # Only 3MF files have parseable filament info
  1958. if not lib_file.filename.lower().endswith(".3mf"):
  1959. return {"file_id": file_id, "filename": lib_file.filename, "plate_id": plate_id, "filaments": []}
  1960. filaments = []
  1961. try:
  1962. with zipfile.ZipFile(file_path, "r") as zf:
  1963. # Parse slice_info.config for filament requirements
  1964. if "Metadata/slice_info.config" in zf.namelist():
  1965. content = zf.read("Metadata/slice_info.config").decode()
  1966. root = ET.fromstring(content)
  1967. if plate_id is not None:
  1968. # Find filaments for specific plate
  1969. for plate_elem in root.findall(".//plate"):
  1970. # Check if this is the requested plate
  1971. plate_index = None
  1972. for meta in plate_elem.findall("metadata"):
  1973. if meta.get("key") == "index":
  1974. try:
  1975. plate_index = int(meta.get("value", ""))
  1976. except ValueError:
  1977. pass # Skip plate with non-numeric index value
  1978. break
  1979. if plate_index == plate_id:
  1980. # Extract filaments from this plate
  1981. for filament_elem in plate_elem.findall("filament"):
  1982. filament_id = filament_elem.get("id")
  1983. filament_type = filament_elem.get("type", "")
  1984. filament_color = filament_elem.get("color", "")
  1985. used_g = filament_elem.get("used_g", "0")
  1986. used_m = filament_elem.get("used_m", "0")
  1987. tray_info_idx = filament_elem.get("tray_info_idx", "")
  1988. try:
  1989. used_grams = float(used_g)
  1990. except (ValueError, TypeError):
  1991. used_grams = 0
  1992. if used_grams > 0 and filament_id:
  1993. filaments.append(
  1994. {
  1995. "slot_id": int(filament_id),
  1996. "type": filament_type,
  1997. "color": filament_color,
  1998. "used_grams": round(used_grams, 1),
  1999. "used_meters": float(used_m) if used_m else 0,
  2000. "tray_info_idx": tray_info_idx,
  2001. }
  2002. )
  2003. break
  2004. else:
  2005. # Extract all filaments with used_g > 0 (for single-plate or overview)
  2006. for filament_elem in root.findall(".//filament"):
  2007. filament_id = filament_elem.get("id")
  2008. filament_type = filament_elem.get("type", "")
  2009. filament_color = filament_elem.get("color", "")
  2010. used_g = filament_elem.get("used_g", "0")
  2011. used_m = filament_elem.get("used_m", "0")
  2012. tray_info_idx = filament_elem.get("tray_info_idx", "")
  2013. try:
  2014. used_grams = float(used_g)
  2015. except (ValueError, TypeError):
  2016. used_grams = 0
  2017. if used_grams > 0 and filament_id:
  2018. filaments.append(
  2019. {
  2020. "slot_id": int(filament_id),
  2021. "type": filament_type,
  2022. "color": filament_color,
  2023. "used_grams": round(used_grams, 1),
  2024. "used_meters": float(used_m) if used_m else 0,
  2025. "tray_info_idx": tray_info_idx,
  2026. }
  2027. )
  2028. # Sort by slot ID
  2029. filaments.sort(key=lambda x: x["slot_id"])
  2030. # Enrich with nozzle mapping for dual-nozzle printers
  2031. nozzle_mapping = extract_nozzle_mapping_from_3mf(zf)
  2032. if nozzle_mapping:
  2033. for filament in filaments:
  2034. filament["nozzle_id"] = nozzle_mapping.get(filament["slot_id"])
  2035. except Exception as e:
  2036. logger.warning("Failed to parse filament requirements from library file %s: %s", file_id, e)
  2037. return {
  2038. "file_id": file_id,
  2039. "filename": lib_file.filename,
  2040. "plate_id": plate_id,
  2041. "filaments": filaments,
  2042. }
  2043. def _strip_3mf_embedded_settings(zip_bytes: bytes) -> bytes:
  2044. """Remove ``Metadata/project_settings.config`` from a 3MF.
  2045. Bambuddy supplies the slicer profile triplet via the sidecar's
  2046. ``--load-settings`` path; the 3MF's embedded settings would otherwise be
  2047. validated by the CLI first and can fail with sentinel-value range
  2048. checks (`prime_tower_brim_width: -1 not in range`, etc.) regardless of
  2049. what we pass via ``--load-settings``. Stripping the embedded config
  2050. forces the CLI to use the supplied profiles only. Geometry, color, and
  2051. multi-part data inside the 3MF are preserved.
  2052. """
  2053. from io import BytesIO
  2054. src = BytesIO(zip_bytes)
  2055. dst = BytesIO()
  2056. with zipfile.ZipFile(src, "r") as zin, zipfile.ZipFile(dst, "w", zipfile.ZIP_DEFLATED) as zout:
  2057. for item in zin.infolist():
  2058. if item.filename == "Metadata/project_settings.config":
  2059. continue
  2060. zout.writestr(item, zin.read(item.filename))
  2061. return dst.getvalue()
  2062. async def _run_slicer_with_fallback(
  2063. db: AsyncSession,
  2064. *,
  2065. model_bytes: bytes,
  2066. model_filename: str,
  2067. request: SliceRequest,
  2068. ):
  2069. """Validate presets, dispatch to the right sidecar, run the slicer with
  2070. the auto-fallback for 3MF inputs whose `--load-settings` path crashes the
  2071. CLI. Returns ``(SliceResult, used_embedded_settings: bool)``. Raises
  2072. ``HTTPException`` for any caller-facing error.
  2073. """
  2074. from backend.app.api.routes.settings import get_setting
  2075. from backend.app.models.local_preset import LocalPreset
  2076. from backend.app.services.slicer_api import (
  2077. SlicerApiServerError,
  2078. SlicerApiService,
  2079. SlicerApiUnavailableError,
  2080. SlicerInputError,
  2081. )
  2082. # Profile triplet — every slot must match the expected preset_type
  2083. presets: dict[str, str] = {}
  2084. for pid, expected_type, key in (
  2085. (request.printer_preset_id, "printer", "printer"),
  2086. (request.process_preset_id, "process", "process"),
  2087. (request.filament_preset_id, "filament", "filament"),
  2088. ):
  2089. preset = await db.get(LocalPreset, pid)
  2090. if preset is None or preset.preset_type != expected_type:
  2091. raise HTTPException(
  2092. status_code=400,
  2093. detail=f"Invalid {key} preset id (expected preset_type='{expected_type}')",
  2094. )
  2095. presets[key] = preset.setting
  2096. # Slicer routing — pick the sidecar URL by preferred_slicer.
  2097. # The per-install URL setting (Settings UI → Slicer card) wins; an
  2098. # empty value falls back to the SLICER_API_URL / BAMBU_STUDIO_API_URL
  2099. # env defaults defined in core/config.py.
  2100. preferred = (await get_setting(db, "preferred_slicer")) or "bambu_studio"
  2101. if preferred == "orcaslicer":
  2102. configured = await get_setting(db, "orcaslicer_api_url")
  2103. api_url = (configured or app_settings.slicer_api_url).strip()
  2104. elif preferred == "bambu_studio":
  2105. configured = await get_setting(db, "bambu_studio_api_url")
  2106. api_url = (configured or app_settings.bambu_studio_api_url).strip()
  2107. else:
  2108. raise HTTPException(
  2109. status_code=400,
  2110. detail=f"Unknown preferred_slicer setting: '{preferred}'. Expected 'orcaslicer' or 'bambu_studio'.",
  2111. )
  2112. is_3mf = model_filename.lower().endswith(".3mf")
  2113. primary_bytes = model_bytes
  2114. if is_3mf:
  2115. try:
  2116. primary_bytes = _strip_3mf_embedded_settings(model_bytes)
  2117. except (zipfile.BadZipFile, KeyError) as exc:
  2118. raise HTTPException(status_code=400, detail=f"Source 3MF is corrupt: {exc}") from exc
  2119. used_embedded_settings = False
  2120. service = SlicerApiService(api_url)
  2121. try:
  2122. try:
  2123. result = await service.slice_with_profiles(
  2124. model_bytes=primary_bytes,
  2125. model_filename=model_filename,
  2126. printer_profile_json=presets["printer"],
  2127. process_profile_json=presets["process"],
  2128. filament_profile_json=presets["filament"],
  2129. plate=request.plate,
  2130. export_3mf=request.export_3mf,
  2131. )
  2132. except SlicerApiServerError as exc:
  2133. if not is_3mf:
  2134. raise
  2135. logger.warning(
  2136. "Slicer CLI rejected --load-settings for %s (%s); retrying with embedded settings",
  2137. model_filename,
  2138. exc,
  2139. )
  2140. result = await service.slice_without_profiles(
  2141. model_bytes=model_bytes,
  2142. model_filename=model_filename,
  2143. plate=request.plate,
  2144. export_3mf=request.export_3mf,
  2145. )
  2146. used_embedded_settings = True
  2147. except SlicerInputError as exc:
  2148. raise HTTPException(status_code=400, detail=str(exc)) from exc
  2149. except SlicerApiServerError as exc:
  2150. raise HTTPException(status_code=502, detail=str(exc)) from exc
  2151. except SlicerApiUnavailableError as exc:
  2152. raise HTTPException(status_code=502, detail=str(exc)) from exc
  2153. finally:
  2154. await service.close()
  2155. return result, used_embedded_settings
  2156. async def slice_and_persist(
  2157. db: AsyncSession,
  2158. *,
  2159. model_bytes: bytes,
  2160. model_filename: str,
  2161. folder_id: int | None,
  2162. extra_metadata: dict | None,
  2163. request: SliceRequest,
  2164. current_user_id: int | None,
  2165. ) -> SliceResponse:
  2166. """Slice a model and save the result as a new ``LibraryFile`` in
  2167. ``folder_id`` (same folder as the source by convention).
  2168. Always exports as ``.gcode.3mf`` so the existing library thumbnail
  2169. pipeline works on the new file. Plain ``.gcode`` would have no
  2170. embedded thumbnail to extract.
  2171. """
  2172. from backend.app.services.archive import ThreeMFParser
  2173. library_request = request.model_copy(update={"export_3mf": True})
  2174. result, used_embedded_settings = await _run_slicer_with_fallback(
  2175. db,
  2176. model_bytes=model_bytes,
  2177. model_filename=model_filename,
  2178. request=library_request,
  2179. )
  2180. base_name = model_filename.rsplit(".", 1)[0]
  2181. out_filename = f"{base_name}.gcode.3mf"
  2182. unique_name = f"{uuid.uuid4().hex}.gcode.3mf"
  2183. out_path = get_library_files_dir() / unique_name
  2184. out_path.write_bytes(result.content)
  2185. # Extract thumbnail from the produced 3MF so the library card shows a
  2186. # preview. Failures here aren't fatal — the file is still useful
  2187. # without a thumbnail.
  2188. thumbnail_relative: str | None = None
  2189. parsed_metadata: dict = {}
  2190. try:
  2191. parser = ThreeMFParser(str(out_path))
  2192. parsed = parser.parse()
  2193. thumb_data = parsed.get("_thumbnail_data")
  2194. thumb_ext = parsed.get("_thumbnail_ext", ".png")
  2195. if thumb_data:
  2196. thumb_filename = f"{uuid.uuid4().hex}{thumb_ext}"
  2197. thumb_path = get_library_thumbnails_dir() / thumb_filename
  2198. thumb_path.write_bytes(thumb_data)
  2199. thumbnail_relative = to_relative_path(thumb_path)
  2200. cleaned = _clean_3mf_metadata(parsed)
  2201. if isinstance(cleaned, dict):
  2202. parsed_metadata = cleaned
  2203. except Exception as exc:
  2204. logger.warning("Failed to parse sliced 3MF metadata for %s: %s", out_filename, exc)
  2205. # The parsed 3MF metadata carries a `print_name` lifted from the source
  2206. # file's embedded settings (BambuStudio always sets this; OrcaSlicer
  2207. # often leaves it blank). The FileManager listing prefers print_name
  2208. # over filename for display, which makes a sliced row indistinguishable
  2209. # from its source. Drop print_name so the listing falls back to the
  2210. # actual filename — which already ends in ".gcode.3mf" and self-describes
  2211. # as the sliced output.
  2212. metadata: dict = {k: v for k, v in parsed_metadata.items() if k != "print_name"}
  2213. metadata.update(
  2214. {
  2215. "print_time_seconds": result.print_time_seconds,
  2216. "filament_used_g": result.filament_used_g,
  2217. "filament_used_mm": result.filament_used_mm,
  2218. }
  2219. )
  2220. if used_embedded_settings:
  2221. metadata["used_embedded_settings"] = True
  2222. if extra_metadata:
  2223. metadata.update(extra_metadata)
  2224. new_file = LibraryFile(
  2225. folder_id=folder_id,
  2226. filename=out_filename,
  2227. file_path=to_relative_path(out_path),
  2228. # Sliced output is a `.gcode.3mf` zip with embedded G-code, but the
  2229. # user-facing meaning is "ready-to-print G-code" — using "gcode"
  2230. # gives it the same badge as plain .gcode files and distinguishes
  2231. # it from un-sliced `.3mf` source models.
  2232. file_type="gcode",
  2233. file_size=len(result.content),
  2234. file_hash=hashlib.sha256(result.content).hexdigest(),
  2235. thumbnail_path=thumbnail_relative,
  2236. file_metadata=metadata,
  2237. source_type="sliced",
  2238. created_by_id=current_user_id,
  2239. )
  2240. db.add(new_file)
  2241. await db.commit()
  2242. await db.refresh(new_file)
  2243. return SliceResponse(
  2244. library_file_id=new_file.id,
  2245. name=new_file.filename,
  2246. print_time_seconds=result.print_time_seconds,
  2247. filament_used_g=result.filament_used_g,
  2248. filament_used_mm=result.filament_used_mm,
  2249. used_embedded_settings=used_embedded_settings,
  2250. )
  2251. async def slice_and_persist_as_archive(
  2252. db: AsyncSession,
  2253. *,
  2254. model_bytes: bytes,
  2255. model_filename: str,
  2256. request: SliceRequest,
  2257. source_archive, # PrintArchive — hint kept loose to avoid cyclic import
  2258. current_user_id: int | None,
  2259. ):
  2260. """Slice a model and save the result as a new ``PrintArchive`` row,
  2261. inheriting printer / project / makerworld metadata from the source
  2262. archive. Always exports as a `.gcode.3mf` so the existing thumbnail
  2263. and plates infrastructure (which expects a zip-shaped 3MF) works on
  2264. the new archive. Returns ``SliceArchiveResponse``.
  2265. """
  2266. from backend.app.models.archive import PrintArchive
  2267. from backend.app.schemas.slicer import SliceArchiveResponse
  2268. from backend.app.services.archive import ThreeMFParser
  2269. # Archive sinks always want a 3MF. The library route still respects the
  2270. # caller's `export_3mf` flag; here we override.
  2271. archive_request = request.model_copy(update={"export_3mf": True})
  2272. result, used_embedded_settings = await _run_slicer_with_fallback(
  2273. db,
  2274. model_bytes=model_bytes,
  2275. model_filename=model_filename,
  2276. request=archive_request,
  2277. )
  2278. base_name = model_filename.rsplit(".", 1)[0]
  2279. out_filename = f"{base_name}.gcode.3mf"
  2280. timestamp = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S")
  2281. printer_folder = str(source_archive.printer_id) if source_archive.printer_id is not None else "unassigned"
  2282. archive_subdir = f"{timestamp}_{base_name}_sliced"
  2283. archive_dir = app_settings.archive_dir / printer_folder / archive_subdir
  2284. archive_dir.mkdir(parents=True, exist_ok=True)
  2285. out_path = archive_dir / out_filename
  2286. out_path.write_bytes(result.content)
  2287. # Extract a thumbnail from the produced 3MF so the new archive card has
  2288. # a preview. The 3MF parser pulls Metadata/plate_*.png; failures here
  2289. # shouldn't fail the whole slice — the archive row is still useful
  2290. # without a thumbnail.
  2291. thumbnail_path: str | None = None
  2292. parsed_metadata: dict = {}
  2293. try:
  2294. parser = ThreeMFParser(str(out_path))
  2295. parsed = parser.parse()
  2296. thumb_data = parsed.get("_thumbnail_data")
  2297. thumb_ext = parsed.get("_thumbnail_ext", ".png")
  2298. if thumb_data:
  2299. thumb_dest = archive_dir / f"thumbnail{thumb_ext}"
  2300. thumb_dest.write_bytes(thumb_data)
  2301. thumbnail_path = str(thumb_dest.relative_to(app_settings.base_dir))
  2302. parsed_metadata = {k: v for k, v in parsed.items() if not k.startswith("_")}
  2303. except Exception as exc:
  2304. logger.warning("Failed to parse sliced 3MF metadata for %s: %s", out_filename, exc)
  2305. metadata = dict(source_archive.extra_data) if source_archive.extra_data else {}
  2306. metadata.update(parsed_metadata)
  2307. metadata.update(
  2308. {
  2309. "sliced_from_archive_id": source_archive.id,
  2310. "print_time_seconds": result.print_time_seconds,
  2311. "filament_used_g": result.filament_used_g,
  2312. "filament_used_mm": result.filament_used_mm,
  2313. }
  2314. )
  2315. if used_embedded_settings:
  2316. metadata["used_embedded_settings"] = True
  2317. new_archive = PrintArchive(
  2318. printer_id=source_archive.printer_id,
  2319. project_id=source_archive.project_id,
  2320. filename=out_filename,
  2321. file_path=str(out_path.relative_to(app_settings.base_dir)),
  2322. file_size=len(result.content),
  2323. content_hash=hashlib.sha256(result.content).hexdigest(),
  2324. thumbnail_path=thumbnail_path,
  2325. # Inherit identity from the source archive so the new entry shows
  2326. # up alongside its sibling in the archives list.
  2327. print_name=(source_archive.print_name or base_name) + " (re-sliced)",
  2328. print_time_seconds=result.print_time_seconds,
  2329. filament_used_grams=result.filament_used_g or None,
  2330. filament_type=source_archive.filament_type,
  2331. filament_color=source_archive.filament_color,
  2332. layer_height=source_archive.layer_height,
  2333. nozzle_diameter=source_archive.nozzle_diameter,
  2334. sliced_for_model=source_archive.sliced_for_model,
  2335. makerworld_url=source_archive.makerworld_url,
  2336. designer=source_archive.designer,
  2337. # Sliced-but-not-printed: keep status default ("completed") so it
  2338. # surfaces in the normal archives list, but do not stamp
  2339. # started/completed_at — the user hasn't actually printed it yet.
  2340. extra_data=metadata,
  2341. created_by_id=current_user_id,
  2342. )
  2343. db.add(new_archive)
  2344. await db.commit()
  2345. await db.refresh(new_archive)
  2346. return SliceArchiveResponse(
  2347. archive_id=new_archive.id,
  2348. name=new_archive.print_name or out_filename,
  2349. print_time_seconds=result.print_time_seconds,
  2350. filament_used_g=result.filament_used_g,
  2351. filament_used_mm=result.filament_used_mm,
  2352. used_embedded_settings=used_embedded_settings,
  2353. )
  2354. @router.post("/files/{file_id}/slice", status_code=202)
  2355. async def slice_library_file(
  2356. file_id: int,
  2357. request: SliceRequest,
  2358. db: AsyncSession = Depends(get_db),
  2359. current_user: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  2360. ):
  2361. """Enqueue a slice job for a library file. Returns 202 + job_id; the
  2362. slice runs in the background, the caller polls `GET /slice-jobs/{id}`.
  2363. """
  2364. from backend.app.core.database import async_session
  2365. from backend.app.services.slice_dispatch import (
  2366. http_exception_to_job_error,
  2367. slice_dispatch,
  2368. )
  2369. src_result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2370. lib_file = src_result.scalar_one_or_none()
  2371. if not lib_file:
  2372. raise HTTPException(status_code=404, detail="File not found")
  2373. src_lower = (lib_file.filename or "").lower()
  2374. if not (
  2375. src_lower.endswith(".stl")
  2376. or src_lower.endswith(".3mf")
  2377. or src_lower.endswith(".step")
  2378. or src_lower.endswith(".stp")
  2379. ):
  2380. raise HTTPException(status_code=400, detail="Source file must be STL, 3MF, or STEP")
  2381. src_path = Path(app_settings.base_dir) / lib_file.file_path
  2382. if not src_path.exists():
  2383. raise HTTPException(status_code=404, detail="Source file missing on disk")
  2384. # Capture inputs the bg task needs — the request DB session is closed
  2385. # before the background task runs.
  2386. model_bytes = src_path.read_bytes()
  2387. folder_id = lib_file.folder_id
  2388. source_lib_file_id = lib_file.id
  2389. user_id = current_user.id if current_user else None
  2390. # If the source has a `print_name` in its metadata (BambuStudio always
  2391. # sets this; OrcaSlicer often leaves it blank), derive the sliced
  2392. # output's filename from it instead of the raw filename. The source
  2393. # row's display already prefers print_name, so the sliced row's
  2394. # filename ("Piggo the piggy bank.gcode.3mf") will match the source's
  2395. # display name ("Piggo the piggy bank") with the gcode extension added.
  2396. src_print_name = None
  2397. if lib_file.file_metadata:
  2398. candidate = lib_file.file_metadata.get("print_name")
  2399. if isinstance(candidate, str) and candidate.strip():
  2400. src_print_name = candidate.strip()
  2401. src_ext = Path(lib_file.filename).suffix.lower() or ".3mf"
  2402. model_filename = f"{src_print_name}{src_ext}" if src_print_name else lib_file.filename
  2403. async def _run():
  2404. async with async_session() as task_db:
  2405. try:
  2406. response = await slice_and_persist(
  2407. task_db,
  2408. model_bytes=model_bytes,
  2409. model_filename=model_filename,
  2410. folder_id=folder_id,
  2411. extra_metadata={"sliced_from_library_file_id": source_lib_file_id},
  2412. request=request,
  2413. current_user_id=user_id,
  2414. )
  2415. except HTTPException as exc:
  2416. raise http_exception_to_job_error(exc) from exc
  2417. return response.model_dump()
  2418. job = await slice_dispatch.enqueue(
  2419. kind="library_file",
  2420. source_id=lib_file.id,
  2421. source_name=lib_file.filename,
  2422. run=_run,
  2423. )
  2424. return {
  2425. "job_id": job.id,
  2426. "status": job.status,
  2427. "status_url": f"/api/v1/slice-jobs/{job.id}",
  2428. }
  2429. @router.post("/files/{file_id}/print")
  2430. async def print_library_file(
  2431. file_id: int,
  2432. printer_id: int,
  2433. body: FilePrintRequest | None = None,
  2434. db: AsyncSession = Depends(get_db),
  2435. current_user: User | None = Depends(require_permission_if_auth_enabled(Permission.PRINTERS_CONTROL)),
  2436. ):
  2437. """Dispatch a library file for send/start on a printer.
  2438. The actual send/start work is handled asynchronously by background
  2439. dispatch so the UI can continue immediately.
  2440. Only sliced files (.gcode or .gcode.3mf) can be printed.
  2441. """
  2442. from backend.app.models.printer import Printer
  2443. from backend.app.services.background_dispatch import DispatchEnqueueRejected, background_dispatch
  2444. from backend.app.services.printer_manager import printer_manager
  2445. # Use defaults if no body provided
  2446. if body is None:
  2447. body = FilePrintRequest()
  2448. # Get the library file
  2449. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2450. lib_file = result.scalar_one_or_none()
  2451. if not lib_file:
  2452. raise HTTPException(status_code=404, detail="File not found")
  2453. # Validate file is sliced
  2454. if not is_sliced_file(lib_file.filename):
  2455. raise HTTPException(
  2456. status_code=400,
  2457. detail="Not a sliced file. Only .gcode or .gcode.3mf files can be printed.",
  2458. )
  2459. # Get the full file path
  2460. file_path = Path(app_settings.base_dir) / lib_file.file_path
  2461. if not file_path.exists():
  2462. raise HTTPException(status_code=404, detail="File not found on disk")
  2463. # Get printer
  2464. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2465. printer = result.scalar_one_or_none()
  2466. if not printer:
  2467. raise HTTPException(status_code=404, detail="Printer not found")
  2468. # Check printer is connected
  2469. if not printer_manager.is_connected(printer_id):
  2470. raise HTTPException(status_code=400, detail="Printer is not connected")
  2471. # Validate project exists before dispatching so a bogus ID yields 404, not a FK-constraint 500
  2472. if body.project_id is not None:
  2473. project_result = await db.execute(select(Project).where(Project.id == body.project_id))
  2474. if not project_result.scalar_one_or_none():
  2475. raise HTTPException(status_code=404, detail="Project not found")
  2476. plate_name = body.plate_name
  2477. if not plate_name and body.plate_id is not None:
  2478. plate_name = f"Plate {body.plate_id}"
  2479. dispatch_source_name = lib_file.filename
  2480. if plate_name:
  2481. dispatch_source_name = f"{lib_file.filename} • {plate_name}"
  2482. try:
  2483. dispatch_result = await background_dispatch.dispatch_print_library_file(
  2484. file_id=file_id,
  2485. filename=dispatch_source_name,
  2486. printer_id=printer_id,
  2487. printer_name=printer.name,
  2488. options=body.model_dump(exclude_none=True, exclude={"cleanup_library_after_dispatch"}),
  2489. project_id=body.project_id,
  2490. requested_by_user_id=current_user.id if current_user else None,
  2491. requested_by_username=current_user.username if current_user else None,
  2492. cleanup_library_after_dispatch=body.cleanup_library_after_dispatch,
  2493. )
  2494. except DispatchEnqueueRejected as e:
  2495. raise HTTPException(status_code=409, detail=str(e)) from e
  2496. return {
  2497. "status": "dispatched",
  2498. "printer_id": printer_id,
  2499. "archive_id": None,
  2500. "filename": lib_file.filename,
  2501. "dispatch_job_id": dispatch_result["dispatch_job_id"],
  2502. "dispatch_position": dispatch_result["dispatch_position"],
  2503. }
  2504. # ============ File Detail Endpoints ============
  2505. @router.get("/files/{file_id}", response_model=FileResponseSchema)
  2506. async def get_file(
  2507. file_id: int,
  2508. db: AsyncSession = Depends(get_db),
  2509. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  2510. ):
  2511. """Get a file by ID with full details."""
  2512. result = await db.execute(
  2513. LibraryFile.active().options(selectinload(LibraryFile.created_by)).where(LibraryFile.id == file_id)
  2514. )
  2515. file = result.scalar_one_or_none()
  2516. if not file:
  2517. raise HTTPException(status_code=404, detail="File not found")
  2518. # Get folder name
  2519. folder_name = None
  2520. if file.folder_id:
  2521. folder_result = await db.execute(select(LibraryFolder.name).where(LibraryFolder.id == file.folder_id))
  2522. folder_name = folder_result.scalar()
  2523. # Get project name
  2524. project_name = None
  2525. if file.project_id:
  2526. project_result = await db.execute(select(Project.name).where(Project.id == file.project_id))
  2527. project_name = project_result.scalar()
  2528. # Get duplicates
  2529. duplicates = []
  2530. duplicate_count = 0
  2531. if file.file_hash:
  2532. dup_result = await db.execute(
  2533. select(LibraryFile, LibraryFolder.name)
  2534. .outerjoin(LibraryFolder, LibraryFile.folder_id == LibraryFolder.id)
  2535. .where(
  2536. LibraryFile.file_hash == file.file_hash,
  2537. LibraryFile.id != file.id,
  2538. LibraryFile.deleted_at.is_(None),
  2539. )
  2540. )
  2541. for dup_file, dup_folder_name in dup_result.all():
  2542. duplicates.append(
  2543. FileDuplicate(
  2544. id=dup_file.id,
  2545. filename=dup_file.filename,
  2546. folder_id=dup_file.folder_id,
  2547. folder_name=dup_folder_name,
  2548. created_at=dup_file.created_at,
  2549. )
  2550. )
  2551. duplicate_count = len(duplicates)
  2552. # Extract key metadata fields
  2553. print_name = None
  2554. print_time = None
  2555. filament_grams = None
  2556. sliced_for_model = None
  2557. if file.file_metadata:
  2558. print_name = file.file_metadata.get("print_name")
  2559. print_time = file.file_metadata.get("print_time_seconds")
  2560. filament_grams = file.file_metadata.get("filament_used_grams")
  2561. sliced_for_model = file.file_metadata.get("sliced_for_model")
  2562. return FileResponseSchema(
  2563. id=file.id,
  2564. folder_id=file.folder_id,
  2565. folder_name=folder_name,
  2566. project_id=file.project_id,
  2567. project_name=project_name,
  2568. filename=file.filename,
  2569. file_path=file.file_path,
  2570. file_type=file.file_type,
  2571. file_size=file.file_size,
  2572. file_hash=file.file_hash,
  2573. thumbnail_path=file.thumbnail_path,
  2574. metadata=file.file_metadata,
  2575. print_count=file.print_count,
  2576. last_printed_at=file.last_printed_at,
  2577. notes=file.notes,
  2578. duplicates=duplicates if duplicates else None,
  2579. duplicate_count=duplicate_count,
  2580. created_by_id=file.created_by_id,
  2581. created_by_username=file.created_by.username if file.created_by else None,
  2582. created_at=file.created_at,
  2583. updated_at=file.updated_at,
  2584. print_name=print_name,
  2585. print_time_seconds=print_time,
  2586. filament_used_grams=filament_grams,
  2587. sliced_for_model=sliced_for_model,
  2588. )
  2589. @router.put("/files/{file_id}", response_model=FileResponseSchema)
  2590. async def update_file(
  2591. file_id: int,
  2592. data: FileUpdate,
  2593. db: AsyncSession = Depends(get_db),
  2594. auth_result: tuple[User | None, bool] = Depends(
  2595. require_ownership_permission(
  2596. Permission.LIBRARY_UPDATE_ALL,
  2597. Permission.LIBRARY_UPDATE_OWN,
  2598. )
  2599. ),
  2600. ):
  2601. """Update a file's metadata."""
  2602. user, can_modify_all = auth_result
  2603. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2604. file = result.scalar_one_or_none()
  2605. if not file:
  2606. raise HTTPException(status_code=404, detail="File not found")
  2607. # Ownership check
  2608. if not can_modify_all:
  2609. if file.created_by_id != user.id:
  2610. raise HTTPException(status_code=403, detail="You can only update your own files")
  2611. if data.filename is not None:
  2612. # Validate filename doesn't contain path separators
  2613. if "/" in data.filename or "\\" in data.filename:
  2614. raise HTTPException(status_code=400, detail="Filename cannot contain path separators")
  2615. file.filename = data.filename
  2616. # Also update print_name in file_metadata so the display name matches
  2617. if file.file_metadata and "print_name" in file.file_metadata:
  2618. file.file_metadata = {**file.file_metadata, "print_name": data.filename}
  2619. if data.folder_id is not None:
  2620. if data.folder_id == 0:
  2621. file.folder_id = None
  2622. else:
  2623. # Verify folder exists
  2624. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.folder_id))
  2625. if not folder_result.scalar_one_or_none():
  2626. raise HTTPException(status_code=404, detail="Folder not found")
  2627. file.folder_id = data.folder_id
  2628. if data.project_id is not None:
  2629. if data.project_id == 0:
  2630. file.project_id = None
  2631. else:
  2632. # Verify project exists
  2633. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  2634. if not project_result.scalar_one_or_none():
  2635. raise HTTPException(status_code=404, detail="Project not found")
  2636. file.project_id = data.project_id
  2637. if data.notes is not None:
  2638. file.notes = data.notes if data.notes else None
  2639. await db.commit()
  2640. await db.refresh(file)
  2641. # Return full response (reuse get_file logic)
  2642. return await get_file(file_id, db)
  2643. @router.delete("/files/{file_id}")
  2644. async def delete_file(
  2645. file_id: int,
  2646. db: AsyncSession = Depends(get_db),
  2647. auth_result: tuple[User | None, bool] = Depends(
  2648. require_ownership_permission(
  2649. Permission.LIBRARY_DELETE_ALL,
  2650. Permission.LIBRARY_DELETE_OWN,
  2651. )
  2652. ),
  2653. ):
  2654. """Move a file to the trash (soft-delete).
  2655. The file's bytes and thumbnail stay on disk until the trash sweeper
  2656. hard-deletes the row after the retention window (see #1008). External
  2657. files skip the trash entirely — they can't be restored from disk and the
  2658. underlying file is outside Bambuddy's control, so we just drop the DB
  2659. record and thumbnail.
  2660. """
  2661. user, can_modify_all = auth_result
  2662. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2663. file = result.scalar_one_or_none()
  2664. if not file:
  2665. raise HTTPException(status_code=404, detail="File not found")
  2666. # Ownership check
  2667. if not can_modify_all:
  2668. if file.created_by_id != user.id:
  2669. raise HTTPException(status_code=403, detail="You can only delete your own files")
  2670. if file.is_external:
  2671. # External files bypass the trash — just drop the DB row + our thumbnail.
  2672. abs_thumb_path = to_absolute_path(file.thumbnail_path)
  2673. if abs_thumb_path and abs_thumb_path.exists():
  2674. try:
  2675. abs_thumb_path.unlink()
  2676. except OSError as e:
  2677. logger.warning("Failed to delete thumbnail from disk: %s", e)
  2678. await db.delete(file)
  2679. await db.commit()
  2680. return {"status": "success", "message": "File deleted", "trashed": False}
  2681. # Managed file: soft-delete. Sweeper removes bytes + thumbnail after retention.
  2682. file.deleted_at = datetime.now(timezone.utc)
  2683. await db.commit()
  2684. return {"status": "success", "message": "File moved to trash", "trashed": True}
  2685. # ============ File Content Endpoints ============
  2686. @router.get("/files/{file_id}/download")
  2687. async def download_file(
  2688. file_id: int,
  2689. db: AsyncSession = Depends(get_db),
  2690. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  2691. ):
  2692. """Download a file."""
  2693. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2694. file = result.scalar_one_or_none()
  2695. if not file:
  2696. raise HTTPException(status_code=404, detail="File not found")
  2697. abs_path = to_absolute_path(file.file_path)
  2698. if not abs_path or not abs_path.exists():
  2699. raise HTTPException(status_code=404, detail="File not found on disk")
  2700. return FastAPIFileResponse(
  2701. str(abs_path),
  2702. filename=file.filename,
  2703. media_type="application/octet-stream",
  2704. )
  2705. @router.post("/files/{file_id}/slicer-token")
  2706. async def create_library_slicer_token(
  2707. file_id: int,
  2708. db: AsyncSession = Depends(get_db),
  2709. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  2710. ):
  2711. """Create a short-lived download token for opening files in slicer applications.
  2712. Slicer protocol handlers (bambustudioopen://, orcaslicer://) cannot send
  2713. auth headers, so they use this token in the URL path instead.
  2714. """
  2715. from backend.app.core.auth import create_slicer_download_token
  2716. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2717. file = result.scalar_one_or_none()
  2718. if not file:
  2719. raise HTTPException(status_code=404, detail="File not found")
  2720. token = await create_slicer_download_token("library", file_id)
  2721. return {"token": token}
  2722. @router.get("/files/{file_id}/dl/{token}/{filename}")
  2723. async def download_library_file_for_slicer(
  2724. file_id: int,
  2725. token: str,
  2726. filename: str,
  2727. db: AsyncSession = Depends(get_db),
  2728. ):
  2729. """Download a library file using a slicer download token.
  2730. Token-authenticated (no auth headers needed). The token is short-lived
  2731. and single-use, created by POST /files/{file_id}/slicer-token.
  2732. Filename is at the end of the URL so slicers can detect the file format.
  2733. """
  2734. from backend.app.core.auth import verify_slicer_download_token
  2735. if not await verify_slicer_download_token(token, "library", file_id):
  2736. raise HTTPException(status_code=403, detail="Invalid or expired download token")
  2737. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2738. file = result.scalar_one_or_none()
  2739. if not file:
  2740. raise HTTPException(status_code=404, detail="File not found")
  2741. abs_path = to_absolute_path(file.file_path)
  2742. if not abs_path or not abs_path.exists():
  2743. raise HTTPException(status_code=404, detail="File not found on disk")
  2744. return FastAPIFileResponse(
  2745. str(abs_path),
  2746. filename=file.filename,
  2747. media_type="application/octet-stream",
  2748. )
  2749. @router.get("/files/{file_id}/thumbnail")
  2750. async def get_thumbnail(
  2751. file_id: int,
  2752. db: AsyncSession = Depends(get_db),
  2753. _: None = RequireCameraStreamTokenIfAuthEnabled,
  2754. ):
  2755. """Get a file's thumbnail."""
  2756. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2757. file = result.scalar_one_or_none()
  2758. if not file:
  2759. raise HTTPException(status_code=404, detail="File not found")
  2760. abs_thumb_path = to_absolute_path(file.thumbnail_path)
  2761. if not abs_thumb_path or not abs_thumb_path.exists():
  2762. raise HTTPException(status_code=404, detail="Thumbnail not found")
  2763. # Detect media type from extension
  2764. thumb_ext = abs_thumb_path.suffix.lower()
  2765. media_types = {
  2766. ".png": "image/png",
  2767. ".jpg": "image/jpeg",
  2768. ".jpeg": "image/jpeg",
  2769. ".gif": "image/gif",
  2770. ".webp": "image/webp",
  2771. }
  2772. media_type = media_types.get(thumb_ext, "image/png")
  2773. return FastAPIFileResponse(str(abs_thumb_path), media_type=media_type)
  2774. @router.get("/files/{file_id}/gcode")
  2775. async def get_gcode(
  2776. file_id: int,
  2777. db: AsyncSession = Depends(get_db),
  2778. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  2779. ):
  2780. """Get gcode for a file (for preview)."""
  2781. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2782. file = result.scalar_one_or_none()
  2783. if not file:
  2784. raise HTTPException(status_code=404, detail="File not found")
  2785. abs_path = to_absolute_path(file.file_path)
  2786. if not abs_path or not abs_path.exists():
  2787. raise HTTPException(status_code=404, detail="File not found on disk")
  2788. if file.file_type == "gcode":
  2789. return FastAPIFileResponse(str(abs_path), media_type="text/plain")
  2790. elif file.file_type == "3mf":
  2791. # Extract gcode from 3mf
  2792. try:
  2793. with zipfile.ZipFile(str(abs_path), "r") as zf:
  2794. # Find gcode file
  2795. gcode_files = [n for n in zf.namelist() if n.endswith(".gcode")]
  2796. if not gcode_files:
  2797. raise HTTPException(status_code=404, detail="No gcode found in 3MF file")
  2798. gcode_content = zf.read(gcode_files[0])
  2799. from fastapi.responses import Response
  2800. return Response(content=gcode_content, media_type="text/plain")
  2801. except zipfile.BadZipFile:
  2802. raise HTTPException(status_code=400, detail="Invalid 3MF file")
  2803. else:
  2804. raise HTTPException(status_code=400, detail="Unsupported file type")
  2805. # ============ Bulk Operations ============
  2806. @router.post("/files/move")
  2807. async def move_files(
  2808. data: FileMoveRequest,
  2809. db: AsyncSession = Depends(get_db),
  2810. auth_result: tuple[User | None, bool] = Depends(
  2811. require_ownership_permission(
  2812. Permission.LIBRARY_UPDATE_ALL,
  2813. Permission.LIBRARY_UPDATE_OWN,
  2814. )
  2815. ),
  2816. ):
  2817. """Move multiple files to a folder.
  2818. Files not owned by the user are skipped (unless user has *_all permission).
  2819. """
  2820. user, can_modify_all = auth_result
  2821. # Verify folder exists if specified
  2822. if data.folder_id is not None:
  2823. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.folder_id))
  2824. target_folder = folder_result.scalar_one_or_none()
  2825. if not target_folder:
  2826. raise HTTPException(status_code=404, detail="Folder not found")
  2827. if target_folder.is_external and target_folder.external_readonly:
  2828. raise HTTPException(status_code=403, detail="Cannot move files to a read-only external folder")
  2829. # Update files
  2830. moved = 0
  2831. skipped = 0
  2832. for file_id in data.file_ids:
  2833. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2834. file = result.scalar_one_or_none()
  2835. if file:
  2836. # Ownership check
  2837. if not can_modify_all and file.created_by_id != user.id:
  2838. skipped += 1
  2839. continue
  2840. # Cannot move external files out of their folder
  2841. if file.is_external:
  2842. skipped += 1
  2843. continue
  2844. file.folder_id = data.folder_id
  2845. moved += 1
  2846. return {"status": "success", "moved": moved, "skipped": skipped}
  2847. @router.post("/bulk-delete", response_model=BulkDeleteResponse)
  2848. async def bulk_delete(
  2849. data: BulkDeleteRequest,
  2850. db: AsyncSession = Depends(get_db),
  2851. auth_result: tuple[User | None, bool] = Depends(
  2852. require_ownership_permission(
  2853. Permission.LIBRARY_DELETE_ALL,
  2854. Permission.LIBRARY_DELETE_OWN,
  2855. )
  2856. ),
  2857. ):
  2858. """Delete multiple files and/or folders.
  2859. Files not owned by the user are skipped (unless user has *_all permission).
  2860. """
  2861. user, can_modify_all = auth_result
  2862. deleted_files = 0
  2863. deleted_folders = 0
  2864. skipped_files = 0
  2865. # Delete files first. Managed files go to trash (sweeper hard-deletes bytes
  2866. # later); external files bypass trash since their disk state is outside our
  2867. # control and can't be restored from trash anyway.
  2868. now = datetime.now(timezone.utc)
  2869. for file_id in data.file_ids:
  2870. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2871. file = result.scalar_one_or_none()
  2872. if not file:
  2873. continue
  2874. if not can_modify_all and file.created_by_id != user.id:
  2875. skipped_files += 1
  2876. continue
  2877. if file.is_external:
  2878. abs_thumb_path = to_absolute_path(file.thumbnail_path)
  2879. if abs_thumb_path and abs_thumb_path.exists():
  2880. try:
  2881. abs_thumb_path.unlink()
  2882. except OSError as e:
  2883. logger.warning("Failed to delete thumbnail from disk: %s", e)
  2884. await db.delete(file)
  2885. else:
  2886. file.deleted_at = now
  2887. deleted_files += 1
  2888. # Delete folders (cascade will handle contents)
  2889. # Note: Folders don't have ownership tracking currently, require *_all permission
  2890. for folder_id in data.folder_ids:
  2891. if not can_modify_all:
  2892. # Users without *_all permission cannot delete folders
  2893. continue
  2894. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  2895. folder = result.scalar_one_or_none()
  2896. if folder:
  2897. # Count files that will be deleted
  2898. file_count_result = await db.execute(
  2899. select(func.count(LibraryFile.id)).where(
  2900. LibraryFile.folder_id == folder_id,
  2901. LibraryFile.deleted_at.is_(None),
  2902. )
  2903. )
  2904. deleted_files += file_count_result.scalar() or 0
  2905. await db.delete(folder)
  2906. deleted_folders += 1
  2907. await db.commit()
  2908. return BulkDeleteResponse(deleted_files=deleted_files, deleted_folders=deleted_folders)
  2909. # ============ Stats Endpoint ============
  2910. @router.get("/stats")
  2911. async def get_library_stats(
  2912. db: AsyncSession = Depends(get_db),
  2913. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  2914. ):
  2915. """Get library statistics."""
  2916. # Stats exclude trashed files — users see counts/sizes for what's actually in the library.
  2917. active_only = LibraryFile.deleted_at.is_(None)
  2918. # Total files
  2919. total_files_result = await db.execute(select(func.count(LibraryFile.id)).where(active_only))
  2920. total_files = total_files_result.scalar() or 0
  2921. # Total folders
  2922. total_folders_result = await db.execute(select(func.count(LibraryFolder.id)))
  2923. total_folders = total_folders_result.scalar() or 0
  2924. # Total size
  2925. total_size_result = await db.execute(select(func.sum(LibraryFile.file_size)).where(active_only))
  2926. total_size = total_size_result.scalar() or 0
  2927. # Files by type
  2928. type_result = await db.execute(
  2929. select(LibraryFile.file_type, func.count(LibraryFile.id)).where(active_only).group_by(LibraryFile.file_type)
  2930. )
  2931. files_by_type = dict(type_result.all())
  2932. # Total prints
  2933. total_prints_result = await db.execute(select(func.sum(LibraryFile.print_count)).where(active_only))
  2934. total_prints = total_prints_result.scalar() or 0
  2935. # Disk space info
  2936. library_dir = get_library_dir()
  2937. try:
  2938. disk_stat = shutil.disk_usage(library_dir)
  2939. disk_free_bytes = disk_stat.free
  2940. disk_total_bytes = disk_stat.total
  2941. disk_used_bytes = disk_stat.used
  2942. except OSError:
  2943. disk_free_bytes = 0
  2944. disk_total_bytes = 0
  2945. disk_used_bytes = 0
  2946. return {
  2947. "total_files": total_files,
  2948. "total_folders": total_folders,
  2949. "total_size_bytes": total_size,
  2950. "files_by_type": files_by_type,
  2951. "total_prints": total_prints,
  2952. "disk_free_bytes": disk_free_bytes,
  2953. "disk_total_bytes": disk_total_bytes,
  2954. "disk_used_bytes": disk_used_bytes,
  2955. }