library.py 196 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668
  1. """API routes for File Manager (Library) functionality."""
  2. import asyncio
  3. import base64
  4. import binascii
  5. import contextlib
  6. import hashlib
  7. import json
  8. import logging
  9. import os
  10. import re
  11. import shutil
  12. import uuid
  13. import zipfile
  14. from datetime import datetime, timezone
  15. from pathlib import Path
  16. from fastapi import APIRouter, Depends, File, HTTPException, Query, Response, UploadFile
  17. from fastapi.responses import FileResponse as FastAPIFileResponse
  18. from sqlalchemy import func, select
  19. from sqlalchemy.ext.asyncio import AsyncSession
  20. from sqlalchemy.orm import selectinload
  21. from backend.app.api.routes.cloud import resolve_api_key_cloud_owner
  22. from backend.app.core.auth import (
  23. RequireCameraStreamTokenIfAuthEnabled,
  24. require_ownership_permission,
  25. require_permission_if_auth_enabled,
  26. )
  27. from backend.app.core.config import settings as app_settings
  28. from backend.app.core.database import async_session, get_db
  29. from backend.app.core.permissions import Permission
  30. from backend.app.models.archive import PrintArchive
  31. from backend.app.models.library import LibraryFile, LibraryFolder
  32. from backend.app.models.print_queue import PrintQueueItem
  33. from backend.app.models.project import Project
  34. from backend.app.models.user import User
  35. from backend.app.schemas.library import (
  36. AddToQueueError,
  37. AddToQueueRequest,
  38. AddToQueueResponse,
  39. AddToQueueResult,
  40. BatchThumbnailRequest,
  41. BatchThumbnailResponse,
  42. BatchThumbnailResult,
  43. BulkDeleteRequest,
  44. BulkDeleteResponse,
  45. ExternalFolderCreate,
  46. FileDuplicate,
  47. FileListResponse,
  48. FileMoveRequest,
  49. FilePrintRequest,
  50. FileResponse as FileResponseSchema,
  51. FileUpdate,
  52. FileUploadResponse,
  53. FolderCreate,
  54. FolderResponse,
  55. FolderTreeItem,
  56. FolderUpdate,
  57. ZipExtractError,
  58. ZipExtractResponse,
  59. ZipExtractResult,
  60. )
  61. from backend.app.schemas.slicer import SliceRequest, SliceResponse
  62. from backend.app.services.archive import ThreeMFParser
  63. from backend.app.services.stl_thumbnail import MIN_USABLE_STL_BYTES, generate_stl_thumbnail
  64. from backend.app.utils.filename import InvalidFilenameError, validate_print_filename
  65. from backend.app.utils.threemf_tools import (
  66. extract_embedded_presets_from_3mf,
  67. extract_nozzle_mapping_from_3mf,
  68. extract_project_filaments_from_3mf,
  69. )
  70. logger = logging.getLogger(__name__)
  71. router = APIRouter(prefix="/library", tags=["library"])
  72. def get_library_dir() -> Path:
  73. """Get the library storage directory."""
  74. base_dir = Path(app_settings.archive_dir)
  75. library_dir = base_dir / "library"
  76. library_dir.mkdir(parents=True, exist_ok=True)
  77. return library_dir
  78. def get_library_files_dir() -> Path:
  79. """Get the directory for library files."""
  80. files_dir = get_library_dir() / "files"
  81. files_dir.mkdir(parents=True, exist_ok=True)
  82. return files_dir
  83. def classify_file_type(filename: str) -> str:
  84. """Return the canonical ``LibraryFile.file_type`` for *filename*.
  85. Compound extensions are preserved — a `.gcode.3mf` file (a sliced
  86. output, still a 3MF zip on disk) is classified ``gcode.3mf`` rather
  87. than ``3mf``. Pre-#1600 this was only done in the external-scan
  88. path; the upload / ZIP-extract / in-process paths all stripped to
  89. the trailing extension and stored ``3mf``, so the FE had to accept
  90. both. Unified here so every ingest path stores the same value and
  91. downstream gates (gcode download, file-type filter, thumbnail
  92. extraction) only need to handle one canonical name per file family.
  93. Files with no extension classify as ``unknown``.
  94. """
  95. lower = filename.lower()
  96. if lower.endswith(".gcode.3mf"):
  97. return "gcode.3mf"
  98. ext = os.path.splitext(lower)[1]
  99. return ext[1:] if ext else "unknown"
  100. def get_library_thumbnails_dir() -> Path:
  101. """Get the directory for library thumbnails."""
  102. thumbnails_dir = get_library_dir() / "thumbnails"
  103. thumbnails_dir.mkdir(parents=True, exist_ok=True)
  104. return thumbnails_dir
  105. def to_relative_path(absolute_path: Path | str) -> str:
  106. """Convert an absolute path to a path relative to base_dir for storage."""
  107. if not absolute_path:
  108. return ""
  109. abs_path = Path(absolute_path)
  110. base_dir = Path(app_settings.base_dir)
  111. try:
  112. return str(abs_path.relative_to(base_dir))
  113. except ValueError:
  114. # Path is not under base_dir, return as-is (shouldn't happen normally)
  115. return str(abs_path)
  116. def to_absolute_path(relative_path: str | None) -> Path | None:
  117. """Convert a relative path (from database) to an absolute path for file operations."""
  118. if not relative_path:
  119. return None
  120. path = Path(relative_path)
  121. # Handle already-absolute paths verbatim (backwards compatibility during migration).
  122. # Legacy DB rows may store absolute paths that predate the base_dir layout; the
  123. # traversal guard below only applies to relative paths coming from user input.
  124. if path.is_absolute():
  125. return path.resolve()
  126. base = Path(app_settings.base_dir).resolve()
  127. resolved = (base / relative_path).resolve()
  128. # Guard against path traversal — resolved path must stay inside base_dir.
  129. # Use is_relative_to() to avoid the /data/app vs /data/app_evil prefix confusion
  130. # that a plain startswith(str(base)) check would miss.
  131. if not resolved.is_relative_to(base):
  132. raise ValueError(f"Path escapes base directory: {relative_path!r}")
  133. return resolved
  134. def calculate_file_hash(file_path: Path) -> str:
  135. """Calculate SHA256 hash of a file."""
  136. sha256_hash = hashlib.sha256()
  137. with open(file_path, "rb") as f:
  138. for byte_block in iter(lambda: f.read(4096), b""):
  139. sha256_hash.update(byte_block)
  140. return sha256_hash.hexdigest()
  141. def validate_print_file_upload(filename: str, content: bytes) -> None:
  142. """Reject obviously-unprintable uploads early so the printer doesn't see them (#1401).
  143. Bambu printers in network mode only parse ``.gcode.3mf`` zip containers
  144. — raw ``.gcode`` and corrupt/non-zip ``.3mf`` uploads cascade into a
  145. confusing "Printing stopped because the printer was unable to parse the
  146. 3mf file" rejection 30 seconds after the user clicks Print. The
  147. background dispatcher (``background_dispatch.py``) appends ``.3mf`` to
  148. a raw-gcode filename when constructing the FTP destination, which is
  149. how the printer ends up with a file named ``.gcode.3mf`` whose body is
  150. raw gcode — exactly the shape that triggers the firmware parse
  151. failure. Catching both classes here gives an actionable error at the
  152. upload itself.
  153. Compares the filename suffix rather than ``os.path.splitext`` because
  154. compound extensions like ``.gcode.3mf`` show up as just ``.3mf`` after
  155. ``splitext`` — same content validation needs to fire for both
  156. single-``.3mf`` and ``.gcode.3mf`` uploads.
  157. Raises ``HTTPException(400, ...)`` with a human-readable message on
  158. rejection; returns ``None`` for valid (or irrelevant — e.g. STL,
  159. image) uploads.
  160. """
  161. lower_filename = filename.lower()
  162. is_3mf_upload = lower_filename.endswith(".3mf")
  163. is_raw_gcode_upload = lower_filename.endswith(".gcode") and not lower_filename.endswith(".gcode.3mf")
  164. if is_raw_gcode_upload:
  165. raise HTTPException(
  166. status_code=400,
  167. detail=(
  168. "Raw .gcode files can't be printed on Bambu printers in network mode — "
  169. "they need a .gcode.3mf zip container (gcode plus metadata). Re-export from "
  170. "your slicer and make sure the file ends in '.gcode.3mf', not just '.gcode'. "
  171. "If your OS hides extensions, double-check the file with the extension visible."
  172. ),
  173. )
  174. if is_3mf_upload and not content.startswith(b"PK\x03\x04"):
  175. raise HTTPException(
  176. status_code=400,
  177. detail=(
  178. "This .3mf file isn't a valid ZIP container. 3MF files are ZIP archives — "
  179. "either the file is corrupted or it's raw gcode renamed to .3mf. Re-export "
  180. "from your slicer using its 'Export Plate Sliced File' action."
  181. ),
  182. )
  183. def _resolve_upload_destination(target_folder: LibraryFolder | None, filename: str) -> tuple[Path, bool]:
  184. """Resolve the on-disk destination for an uploaded file.
  185. Non-external target: returns ``(<library_files_dir>/<uuid><ext>, False)``.
  186. Writable external target: writes to ``<external_path>/<filename>``
  187. (preserves the real filename so the file is recognisable on the mount);
  188. returns ``(dest, True)``. Raises ``HTTPException`` for read-only external
  189. folders (403), missing/inaccessible/non-writable external paths (400), and
  190. filename collisions on the external mount (409). See #1112 — previously
  191. uploads to writable external folders were silently misrouted to the
  192. internal library dir.
  193. """
  194. if target_folder is not None and target_folder.is_external:
  195. if target_folder.external_readonly:
  196. raise HTTPException(status_code=403, detail="Cannot upload to a read-only external folder")
  197. if not target_folder.external_path:
  198. raise HTTPException(status_code=400, detail="External folder has no configured path")
  199. ext_dir = Path(target_folder.external_path)
  200. if not ext_dir.exists() or not ext_dir.is_dir():
  201. raise HTTPException(
  202. status_code=400,
  203. detail=f"External path is not accessible: {target_folder.external_path}",
  204. )
  205. if not os.access(ext_dir, os.W_OK):
  206. raise HTTPException(
  207. status_code=400,
  208. detail=f"External path is not writable: {target_folder.external_path}",
  209. )
  210. # Guard against path-traversal via a pathological filename — join then
  211. # verify the resolved destination is still inside the external dir.
  212. dest = (ext_dir / filename).resolve() # SEC-PATH-OK: resolve + relative_to containment check on next line
  213. try:
  214. dest.relative_to(ext_dir.resolve())
  215. except ValueError:
  216. raise HTTPException(status_code=400, detail="Invalid filename")
  217. if dest.exists():
  218. raise HTTPException(
  219. status_code=409,
  220. detail=f"A file named {filename!r} already exists in the external folder",
  221. )
  222. return dest, True
  223. ext = os.path.splitext(filename)[1].lower()
  224. return get_library_files_dir() / f"{uuid.uuid4().hex}{ext}", False
  225. def _stored_file_path(abs_path: Path, is_external: bool) -> str:
  226. """Produce the value to persist in ``LibraryFile.file_path``.
  227. External files store the absolute mount path directly (same as scan does),
  228. so ``to_absolute_path`` round-trips through its ``is_absolute()`` fast
  229. path. Managed files store a path relative to ``base_dir`` for portability.
  230. """
  231. return str(abs_path) if is_external else to_relative_path(abs_path)
  232. class _MoveSkip(Exception):
  233. """Signalled by ``_move_file_bytes`` to skip a file with a user-visible reason.
  234. Carries an optional `code` for machine-friendly grouping (the
  235. front-end can localise it) and a fallback English `reason` for logs.
  236. """
  237. def __init__(self, code: str, reason: str):
  238. super().__init__(reason)
  239. self.code = code
  240. self.reason = reason
  241. def _resolve_source_disk_path(file: LibraryFile) -> Path | None:
  242. """Return the absolute on-disk path for an existing LibraryFile, or None
  243. if it can't be located (legacy DB row, deleted file, etc.)."""
  244. if file.is_external:
  245. return Path(file.file_path) if file.file_path else None
  246. return to_absolute_path(file.file_path)
  247. def _move_file_bytes(file: LibraryFile, target_folder: LibraryFolder | None) -> str:
  248. """Physically relocate `file`'s bytes to match `target_folder`.
  249. Used by the move endpoint when source/target straddle the
  250. managed↔external boundary (#1112 follow-up — the prior implementation
  251. updated the DB row's ``folder_id`` but never moved the bytes, so a
  252. file moved to an external SMB folder showed up in Bambuddy's UI but
  253. not on the NAS).
  254. Returns the new ``file_path`` value to persist (relative for managed
  255. targets, absolute for external targets — matches the upload + scan
  256. paths). Raises ``_MoveSkip`` for any condition that would make the
  257. move unsafe (target unwritable, filename collision, source missing).
  258. The copy-then-unlink ordering means a partial copy followed by a
  259. failed unlink leaves both the source and the dest on disk — better
  260. than the symmetric "rename or move" which would lose the source if
  261. the target write didn't complete on a flaky mount. The DB row stays
  262. pointed at the source until the caller commits the new ``file_path``.
  263. """
  264. src = _resolve_source_disk_path(file)
  265. if not src or not src.exists():
  266. raise _MoveSkip("source_missing", "source file missing on disk")
  267. target_is_external = target_folder is not None and target_folder.is_external
  268. if target_is_external:
  269. if target_folder.external_readonly:
  270. # Already blocked at top level, but defence-in-depth.
  271. raise _MoveSkip("target_readonly", "target external folder is read-only")
  272. if not target_folder.external_path:
  273. raise _MoveSkip("target_misconfigured", "target external folder has no path")
  274. ext_dir = Path(target_folder.external_path)
  275. if not ext_dir.exists() or not ext_dir.is_dir():
  276. raise _MoveSkip("target_inaccessible", f"target path not accessible: {ext_dir}")
  277. if not os.access(ext_dir, os.W_OK):
  278. raise _MoveSkip("target_unwritable", f"target path not writable: {ext_dir}")
  279. dest = (ext_dir / file.filename).resolve() # SEC-PATH-OK: resolve + relative_to containment check on next line
  280. try:
  281. dest.relative_to(ext_dir.resolve())
  282. except ValueError:
  283. raise _MoveSkip("invalid_filename", f"unsafe filename: {file.filename!r}") from None
  284. if dest.exists():
  285. raise _MoveSkip("name_collision", f"a file named {file.filename!r} already exists in target")
  286. try:
  287. shutil.copy2(src, dest)
  288. except OSError as e:
  289. # Clean up partial dest so a retry can succeed.
  290. with contextlib.suppress(OSError):
  291. dest.unlink(missing_ok=True)
  292. raise _MoveSkip("copy_failed", f"copy failed: {e}") from e
  293. else:
  294. # → managed (root or non-external folder): generate a fresh UUID
  295. # filename in the internal store so we don't collide with another
  296. # file that happens to share `filename`.
  297. ext = src.suffix.lower()
  298. dest = get_library_files_dir() / f"{uuid.uuid4().hex}{ext}"
  299. try:
  300. shutil.copy2(src, dest)
  301. except OSError as e:
  302. with contextlib.suppress(OSError):
  303. dest.unlink(missing_ok=True)
  304. raise _MoveSkip("copy_failed", f"copy failed: {e}") from e
  305. # Copy succeeded — unlink the original. A failure here leaves an
  306. # orphan on disk but the DB row is consistent against the new dest.
  307. try:
  308. src.unlink(missing_ok=True)
  309. except OSError as e:
  310. logger.warning(
  311. "Move: copied %s → %s but couldn't remove source: %s",
  312. src,
  313. dest,
  314. e,
  315. )
  316. return _stored_file_path(dest, is_external=target_is_external)
  317. def _clean_3mf_metadata(obj):
  318. """Strip bytes and thumbnail-carrier keys so the payload is JSON-storable.
  319. Shared by ``upload_file`` and :func:`save_3mf_bytes_to_library` — the
  320. ``ThreeMFParser`` output embeds the thumbnail bytes under
  321. ``_thumbnail_data``/``_thumbnail_ext`` and may also include raw bytes in
  322. other fields, none of which can be JSON-encoded.
  323. """
  324. if isinstance(obj, dict):
  325. return {
  326. k: _clean_3mf_metadata(v)
  327. for k, v in obj.items()
  328. if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext")
  329. }
  330. if isinstance(obj, list):
  331. return [_clean_3mf_metadata(i) for i in obj if not isinstance(i, bytes)]
  332. if isinstance(obj, bytes):
  333. return None
  334. return obj
  335. def _read_3mf_entry(zip_path: Path, entry: str) -> bytes | None:
  336. """Return the raw bytes of an entry inside a 3MF (ZIP), or ``None`` when
  337. the file isn't a parseable zip / doesn't contain that entry / any IO
  338. error. Used to lift the source archive's per-plate render onto a
  339. re-sliced archive (#1493 follow-up) — the slicer CLI often doesn't
  340. emit a fresh ``Metadata/plate_N.png`` and the project-wide cover-art
  341. fallback in :class:`ThreeMFParser` looks unrelated to the actual slice.
  342. """
  343. try:
  344. with zipfile.ZipFile(zip_path, "r") as zf:
  345. if entry not in zf.namelist():
  346. return None
  347. return zf.read(entry)
  348. except (zipfile.BadZipFile, OSError, KeyError):
  349. return None
  350. def _without_print_name(metadata: dict | None) -> dict | None:
  351. """Drop the embedded 3MF Title (``print_name``) from library-file metadata.
  352. The 3MF ``<metadata name="Title">`` holds the in-app project title — the
  353. generic ``"Exported 3D Model"`` for a Bambu Studio "Save As", a marketing
  354. title for a MakerWorld download — never the filename the user saved as.
  355. The FileManager keys its display name, search and sort off ``print_name``,
  356. so storing it makes every card show the wrong name (#1489). A library
  357. file's display name is its filename; only ``PrintArchive`` carries a real
  358. ``print_name``. Returns the input unchanged when there's nothing to strip;
  359. otherwise a new dict (never mutates the argument).
  360. """
  361. if not metadata or "print_name" not in metadata:
  362. return metadata
  363. return {k: v for k, v in metadata.items() if k != "print_name"}
  364. async def save_3mf_bytes_to_library(
  365. db: AsyncSession,
  366. *,
  367. file_bytes: bytes,
  368. filename: str,
  369. folder_id: int | None = None,
  370. source_type: str | None = None,
  371. source_url: str | None = None,
  372. owner_id: int | None = None,
  373. ) -> tuple[LibraryFile, bool]:
  374. """Save a 3MF blob into the library and return ``(library_file, was_existing)``.
  375. Used by routes that receive a 3MF in-process rather than as a multipart
  376. upload (currently: MakerWorld import; reusable for any future source that
  377. fetches bytes server-side). Deduplicates by ``source_url`` when provided —
  378. if a LibraryFile with the same source_url already exists, the existing
  379. row is returned and the bytes are NOT re-saved (MakerWorld signed URLs
  380. change each download, so hash-based dedupe alone would miss re-imports).
  381. Parses 3MF metadata + thumbnail the same way the multipart upload route
  382. does, via :class:`ThreeMFParser`. Paths are stored as relative so the
  383. library is portable across installs.
  384. """
  385. # Source-URL-based dedupe: return the existing row untouched.
  386. if source_url:
  387. existing = await db.execute(LibraryFile.active().where(LibraryFile.source_url == source_url).limit(1))
  388. existing_row = existing.scalar_one_or_none()
  389. if existing_row is not None:
  390. return existing_row, True
  391. # Persist bytes to disk under a UUID-scoped filename; keep the original
  392. # extension so downstream logic (ThreeMFParser, thumbnail viewer) works.
  393. ext = os.path.splitext(filename)[1].lower() or ".3mf"
  394. unique_filename = f"{uuid.uuid4().hex}{ext}"
  395. file_path = (
  396. get_library_files_dir() / unique_filename
  397. ) # SEC-PATH-OK: unique_filename = uuid.uuid4().hex + ext, generated on the previous line
  398. with open(file_path, "wb") as fh:
  399. fh.write(file_bytes)
  400. file_hash = calculate_file_hash(file_path)
  401. # Extract metadata + thumbnail from the 3MF.
  402. metadata: dict | None = None
  403. thumbnail_path: str | None = None
  404. if ext == ".3mf":
  405. try:
  406. parser = ThreeMFParser(str(file_path))
  407. raw_metadata = parser.parse()
  408. thumb_data = raw_metadata.get("_thumbnail_data")
  409. thumb_ext = raw_metadata.get("_thumbnail_ext", ".png")
  410. if thumb_data:
  411. thumbs_dir = get_library_thumbnails_dir()
  412. thumb_filename = f"{uuid.uuid4().hex}{thumb_ext}"
  413. thumb_path = thumbs_dir / thumb_filename # SEC-PATH-OK: thumb_filename = uuid.uuid4().hex + thumb_ext
  414. with open(thumb_path, "wb") as fh:
  415. fh.write(thumb_data)
  416. thumbnail_path = str(thumb_path)
  417. metadata = _clean_3mf_metadata(raw_metadata) or None
  418. except Exception as exc:
  419. # Matches the multipart upload route's behaviour — a bad 3MF should
  420. # still land in the library so the user can see / delete it rather
  421. # than failing the whole request.
  422. logger.warning("Failed to parse 3MF %s: %s", filename, exc)
  423. library_file = LibraryFile(
  424. folder_id=folder_id,
  425. filename=filename,
  426. file_path=to_relative_path(file_path),
  427. file_type=classify_file_type(filename),
  428. file_size=len(file_bytes),
  429. file_hash=file_hash,
  430. thumbnail_path=to_relative_path(thumbnail_path) if thumbnail_path else None,
  431. file_metadata=_without_print_name(metadata),
  432. source_type=source_type,
  433. source_url=source_url,
  434. created_by_id=owner_id,
  435. )
  436. db.add(library_file)
  437. await db.commit()
  438. await db.refresh(library_file)
  439. return library_file, False
  440. def extract_gcode_thumbnail(file_path: Path) -> bytes | None:
  441. """Extract embedded thumbnail from gcode file.
  442. Supports PrusaSlicer/BambuStudio format:
  443. ; thumbnail begin WxH SIZE
  444. ; base64data...
  445. ; thumbnail end
  446. """
  447. try:
  448. thumbnail_data = None
  449. in_thumbnail = False
  450. thumbnail_lines = []
  451. best_size = 0
  452. with open(file_path, errors="ignore") as f:
  453. # Only read first 50KB for performance (thumbnails are at the start)
  454. content = f.read(50000)
  455. for line in content.split("\n"):
  456. line = line.strip()
  457. # Check for thumbnail start
  458. if line.startswith("; thumbnail begin"):
  459. in_thumbnail = True
  460. thumbnail_lines = []
  461. # Parse dimensions: "; thumbnail begin 300x300 12345"
  462. match = re.search(r"(\d+)x(\d+)", line)
  463. if match:
  464. width = int(match.group(1))
  465. # Prefer larger thumbnails (up to 300px)
  466. if width > best_size and width <= 300:
  467. best_size = width
  468. continue
  469. # Check for thumbnail end
  470. if line.startswith("; thumbnail end"):
  471. if in_thumbnail and thumbnail_lines:
  472. try:
  473. # Decode the base64 data
  474. b64_data = "".join(thumbnail_lines)
  475. decoded = base64.b64decode(b64_data)
  476. # Only keep if this is the best size or first valid thumbnail
  477. if thumbnail_data is None or best_size > 0:
  478. thumbnail_data = decoded
  479. except (binascii.Error, ValueError):
  480. pass # Skip thumbnail with invalid base64 data
  481. in_thumbnail = False
  482. thumbnail_lines = []
  483. continue
  484. # Collect thumbnail data
  485. if in_thumbnail and line.startswith(";"):
  486. # Remove the leading "; " or ";"
  487. data_line = line[1:].strip()
  488. if data_line:
  489. thumbnail_lines.append(data_line)
  490. return thumbnail_data
  491. except Exception as e:
  492. logger.warning("Failed to extract gcode thumbnail: %s", e)
  493. return None
  494. def create_image_thumbnail(file_path: Path, thumbnails_dir: Path, max_size: int = 256) -> str | None:
  495. """Create a thumbnail from an image file.
  496. For small images, copies directly. For larger images, resizes.
  497. Returns the thumbnail path or None on failure.
  498. """
  499. try:
  500. from PIL import Image
  501. thumb_filename = f"{uuid.uuid4().hex}.png"
  502. thumb_path = thumbnails_dir / thumb_filename # SEC-PATH-OK: thumb_filename = uuid.uuid4().hex + ".png"
  503. with Image.open(file_path) as img:
  504. # Convert to RGB if necessary (for PNG with transparency, etc.)
  505. if img.mode in ("RGBA", "LA", "P"):
  506. # Create white background for transparency
  507. background = Image.new("RGB", img.size, (255, 255, 255))
  508. if img.mode == "P":
  509. img = img.convert("RGBA")
  510. background.paste(img, mask=img.split()[-1] if img.mode == "RGBA" else None)
  511. img = background
  512. elif img.mode != "RGB":
  513. img = img.convert("RGB")
  514. # Resize if larger than max_size
  515. if img.width > max_size or img.height > max_size:
  516. img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
  517. img.save(thumb_path, "PNG", optimize=True)
  518. return str(thumb_path)
  519. except ImportError:
  520. # PIL not installed, just copy the file if it's small enough
  521. logger.warning("PIL not installed, copying image as thumbnail")
  522. try:
  523. file_size = file_path.stat().st_size
  524. if file_size < 500000: # Less than 500KB
  525. thumb_filename = f"{uuid.uuid4().hex}{file_path.suffix}"
  526. thumb_path = (
  527. thumbnails_dir / thumb_filename
  528. ) # SEC-PATH-OK: thumb_filename = uuid.uuid4().hex + file_path.suffix
  529. shutil.copy2(file_path, thumb_path)
  530. return str(thumb_path)
  531. except OSError:
  532. pass # File inaccessible; fall through to return None
  533. return None
  534. except Exception as e:
  535. logger.warning("Failed to create image thumbnail: %s", e)
  536. return None
  537. # Supported image extensions for thumbnails
  538. IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".tiff", ".tif"}
  539. async def _backfill_external_stl_thumbnails(folder_ids: list[int]) -> None:
  540. """Generate STL thumbnails for an external folder tree in the background.
  541. Spawned via ``asyncio.create_task`` from ``scan_external_folder`` so the
  542. HTTP request can return as soon as the filesystem walk + folder/file rows
  543. are committed. Thumbnails for thousands of STL files would otherwise hold
  544. the request open for many minutes (each file triggers a ``trimesh.load``
  545. + matplotlib render, ~1-5s each) and the FE modal times out before the
  546. final ``db.commit()`` runs — causing the original symptom in #1299 where
  547. subdirectories never showed up because nothing got committed.
  548. Opens its own session because the request session is closed by the time
  549. this task starts running. Commits per-file so a worker restart mid-run
  550. only loses the in-flight file. Caps STL load to a single file at a time
  551. to avoid memory pressure on systems with many huge STLs.
  552. """
  553. if not folder_ids:
  554. return
  555. thumbnails_dir = get_library_thumbnails_dir()
  556. async with async_session() as db:
  557. result = await db.execute(
  558. LibraryFile.active().where(
  559. LibraryFile.folder_id.in_(folder_ids),
  560. LibraryFile.file_type == "stl",
  561. LibraryFile.thumbnail_path.is_(None),
  562. )
  563. )
  564. stl_files = result.scalars().all()
  565. if not stl_files:
  566. return
  567. logger.info(
  568. "Backfilling STL thumbnails: %d file(s) across %d folder(s)",
  569. len(stl_files),
  570. len(folder_ids),
  571. )
  572. for stl_file in stl_files:
  573. abs_path = to_absolute_path(stl_file.file_path)
  574. if not abs_path or not abs_path.exists():
  575. continue
  576. # Pre-skip files too small to contain even a single triangle.
  577. # Bulk-uploaded ZIPs of stub STLs would otherwise trigger one
  578. # trimesh.load() call + one debug log line per stub.
  579. try:
  580. if abs_path.stat().st_size < MIN_USABLE_STL_BYTES:
  581. continue
  582. except OSError:
  583. continue
  584. try:
  585. thumb_path = generate_stl_thumbnail(abs_path, thumbnails_dir)
  586. except Exception as exc: # noqa: BLE001 — never let one bad STL kill the rest
  587. logger.debug("STL thumbnail backfill skipped %s: %s", abs_path, exc)
  588. continue
  589. if thumb_path:
  590. stl_file.thumbnail_path = to_relative_path(Path(thumb_path))
  591. await db.commit()
  592. # ============ Folder Endpoints ============
  593. @router.get("/folders", response_model=list[FolderTreeItem])
  594. @router.get("/folders/", response_model=list[FolderTreeItem])
  595. async def list_folders(
  596. response: Response,
  597. db: AsyncSession = Depends(get_db),
  598. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  599. ):
  600. """Get all folders as a tree structure."""
  601. # Prevent browser caching of folder list
  602. response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
  603. # Get all folders with project and archive joins
  604. result = await db.execute(
  605. select(LibraryFolder, Project.name, PrintArchive.print_name)
  606. .outerjoin(Project, LibraryFolder.project_id == Project.id)
  607. .outerjoin(PrintArchive, LibraryFolder.archive_id == PrintArchive.id)
  608. .order_by(LibraryFolder.name)
  609. )
  610. rows = result.all()
  611. # Get file counts per folder
  612. file_counts_result = await db.execute(
  613. select(LibraryFile.folder_id, func.count(LibraryFile.id))
  614. .where(LibraryFile.folder_id.isnot(None), LibraryFile.deleted_at.is_(None))
  615. .group_by(LibraryFile.folder_id)
  616. )
  617. file_counts = dict(file_counts_result.all())
  618. # Build tree structure
  619. folder_map = {}
  620. root_folders = []
  621. for folder, project_name, archive_name in rows:
  622. folder_item = FolderTreeItem(
  623. id=folder.id,
  624. name=folder.name,
  625. parent_id=folder.parent_id,
  626. project_id=folder.project_id,
  627. archive_id=folder.archive_id,
  628. project_name=project_name,
  629. archive_name=archive_name,
  630. is_external=folder.is_external,
  631. external_path=folder.external_path,
  632. external_readonly=folder.external_readonly,
  633. file_count=file_counts.get(folder.id, 0),
  634. children=[],
  635. )
  636. folder_map[folder.id] = folder_item
  637. # Link children to parents
  638. for folder, _, _ in rows:
  639. folder_item = folder_map[folder.id]
  640. if folder.parent_id is None:
  641. root_folders.append(folder_item)
  642. elif folder.parent_id in folder_map:
  643. folder_map[folder.parent_id].children.append(folder_item)
  644. return root_folders
  645. @router.get("/folders/by-project/{project_id}", response_model=list[FolderResponse])
  646. async def get_folders_by_project(
  647. project_id: int,
  648. db: AsyncSession = Depends(get_db),
  649. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  650. ):
  651. """Get all folders linked to a specific project."""
  652. result = await db.execute(
  653. select(LibraryFolder, Project.name)
  654. .outerjoin(Project, LibraryFolder.project_id == Project.id)
  655. .where(LibraryFolder.project_id == project_id)
  656. .order_by(LibraryFolder.name)
  657. )
  658. rows = result.all()
  659. folders = []
  660. for folder, project_name in rows:
  661. # Get file count
  662. file_count_result = await db.execute(
  663. select(func.count(LibraryFile.id)).where(
  664. LibraryFile.folder_id == folder.id,
  665. LibraryFile.deleted_at.is_(None),
  666. )
  667. )
  668. file_count = file_count_result.scalar() or 0
  669. folders.append(
  670. FolderResponse(
  671. id=folder.id,
  672. name=folder.name,
  673. parent_id=folder.parent_id,
  674. project_id=folder.project_id,
  675. archive_id=folder.archive_id,
  676. project_name=project_name,
  677. archive_name=None,
  678. is_external=folder.is_external,
  679. external_path=folder.external_path,
  680. external_readonly=folder.external_readonly,
  681. external_show_hidden=folder.external_show_hidden,
  682. file_count=file_count,
  683. created_at=folder.created_at,
  684. updated_at=folder.updated_at,
  685. )
  686. )
  687. return folders
  688. @router.get("/folders/by-archive/{archive_id}", response_model=list[FolderResponse])
  689. async def get_folders_by_archive(
  690. archive_id: int,
  691. db: AsyncSession = Depends(get_db),
  692. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  693. ):
  694. """Get all folders linked to a specific archive."""
  695. result = await db.execute(
  696. select(LibraryFolder, PrintArchive.print_name)
  697. .outerjoin(PrintArchive, LibraryFolder.archive_id == PrintArchive.id)
  698. .where(LibraryFolder.archive_id == archive_id)
  699. .order_by(LibraryFolder.name)
  700. )
  701. rows = result.all()
  702. folders = []
  703. for folder, archive_name in rows:
  704. # Get file count
  705. file_count_result = await db.execute(
  706. select(func.count(LibraryFile.id)).where(
  707. LibraryFile.folder_id == folder.id,
  708. LibraryFile.deleted_at.is_(None),
  709. )
  710. )
  711. file_count = file_count_result.scalar() or 0
  712. folders.append(
  713. FolderResponse(
  714. id=folder.id,
  715. name=folder.name,
  716. parent_id=folder.parent_id,
  717. project_id=folder.project_id,
  718. archive_id=folder.archive_id,
  719. project_name=None,
  720. archive_name=archive_name,
  721. is_external=folder.is_external,
  722. external_path=folder.external_path,
  723. external_readonly=folder.external_readonly,
  724. external_show_hidden=folder.external_show_hidden,
  725. file_count=file_count,
  726. created_at=folder.created_at,
  727. updated_at=folder.updated_at,
  728. )
  729. )
  730. return folders
  731. @router.post("/folders", response_model=FolderResponse)
  732. @router.post("/folders/", response_model=FolderResponse)
  733. async def create_folder(
  734. data: FolderCreate,
  735. db: AsyncSession = Depends(get_db),
  736. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  737. ):
  738. """Create a new folder."""
  739. # Verify parent exists if specified
  740. if data.parent_id is not None:
  741. parent_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.parent_id))
  742. if not parent_result.scalar_one_or_none():
  743. raise HTTPException(status_code=404, detail="Parent folder not found")
  744. # Verify project exists if specified
  745. project_name = None
  746. if data.project_id is not None:
  747. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  748. project = project_result.scalar_one_or_none()
  749. if not project:
  750. raise HTTPException(status_code=404, detail="Project not found")
  751. project_name = project.name
  752. # Verify archive exists if specified
  753. archive_name = None
  754. if data.archive_id is not None:
  755. archive_result = await db.execute(select(PrintArchive).where(PrintArchive.id == data.archive_id))
  756. archive = archive_result.scalar_one_or_none()
  757. if not archive:
  758. raise HTTPException(status_code=404, detail="Archive not found")
  759. archive_name = archive.print_name
  760. folder = LibraryFolder(
  761. name=data.name,
  762. parent_id=data.parent_id,
  763. project_id=data.project_id,
  764. archive_id=data.archive_id,
  765. )
  766. db.add(folder)
  767. await db.commit()
  768. await db.refresh(folder)
  769. return FolderResponse(
  770. id=folder.id,
  771. name=folder.name,
  772. parent_id=folder.parent_id,
  773. project_id=folder.project_id,
  774. archive_id=folder.archive_id,
  775. project_name=project_name,
  776. archive_name=archive_name,
  777. is_external=folder.is_external,
  778. external_path=folder.external_path,
  779. external_readonly=folder.external_readonly,
  780. external_show_hidden=folder.external_show_hidden,
  781. file_count=0,
  782. created_at=folder.created_at,
  783. updated_at=folder.updated_at,
  784. )
  785. @router.get("/folders/{folder_id}", response_model=FolderResponse)
  786. async def get_folder(
  787. folder_id: int,
  788. db: AsyncSession = Depends(get_db),
  789. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  790. ):
  791. """Get a folder by ID."""
  792. result = await db.execute(
  793. select(LibraryFolder, Project.name, PrintArchive.print_name)
  794. .outerjoin(Project, LibraryFolder.project_id == Project.id)
  795. .outerjoin(PrintArchive, LibraryFolder.archive_id == PrintArchive.id)
  796. .where(LibraryFolder.id == folder_id)
  797. )
  798. row = result.one_or_none()
  799. if not row:
  800. raise HTTPException(status_code=404, detail="Folder not found")
  801. folder, project_name, archive_name = row
  802. # Get file count
  803. file_count_result = await db.execute(
  804. select(func.count(LibraryFile.id)).where(
  805. LibraryFile.folder_id == folder_id,
  806. LibraryFile.deleted_at.is_(None),
  807. )
  808. )
  809. file_count = file_count_result.scalar() or 0
  810. return FolderResponse(
  811. id=folder.id,
  812. name=folder.name,
  813. parent_id=folder.parent_id,
  814. project_id=folder.project_id,
  815. archive_id=folder.archive_id,
  816. project_name=project_name,
  817. archive_name=archive_name,
  818. is_external=folder.is_external,
  819. external_path=folder.external_path,
  820. external_readonly=folder.external_readonly,
  821. external_show_hidden=folder.external_show_hidden,
  822. file_count=file_count,
  823. created_at=folder.created_at,
  824. updated_at=folder.updated_at,
  825. )
  826. @router.put("/folders/{folder_id}", response_model=FolderResponse)
  827. async def update_folder(
  828. folder_id: int,
  829. data: FolderUpdate,
  830. db: AsyncSession = Depends(get_db),
  831. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPDATE_ALL)),
  832. ):
  833. """Update a folder.
  834. Note: Folders require library:update_all permission since they don't have
  835. ownership tracking.
  836. """
  837. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  838. folder = result.scalar_one_or_none()
  839. if not folder:
  840. raise HTTPException(status_code=404, detail="Folder not found")
  841. if data.name is not None:
  842. folder.name = data.name
  843. if data.parent_id is not None:
  844. # Prevent circular reference
  845. if data.parent_id == folder_id:
  846. raise HTTPException(status_code=400, detail="Folder cannot be its own parent")
  847. # Check for circular reference in ancestors
  848. if data.parent_id != 0: # 0 means move to root
  849. current_id = data.parent_id
  850. while current_id is not None:
  851. if current_id == folder_id:
  852. raise HTTPException(status_code=400, detail="Cannot move folder into its own subtree")
  853. parent_result = await db.execute(select(LibraryFolder.parent_id).where(LibraryFolder.id == current_id))
  854. current_id = parent_result.scalar()
  855. folder.parent_id = data.parent_id
  856. else:
  857. folder.parent_id = None
  858. # Update project_id (0 to unlink)
  859. if data.project_id is not None:
  860. if data.project_id == 0:
  861. folder.project_id = None
  862. else:
  863. # Verify project exists
  864. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  865. if not project_result.scalar_one_or_none():
  866. raise HTTPException(status_code=404, detail="Project not found")
  867. folder.project_id = data.project_id
  868. # Update archive_id (0 to unlink)
  869. if data.archive_id is not None:
  870. if data.archive_id == 0:
  871. folder.archive_id = None
  872. else:
  873. # Verify archive exists
  874. archive_result = await db.execute(select(PrintArchive).where(PrintArchive.id == data.archive_id))
  875. if not archive_result.scalar_one_or_none():
  876. raise HTTPException(status_code=404, detail="Archive not found")
  877. folder.archive_id = data.archive_id
  878. await db.commit()
  879. await db.refresh(folder)
  880. # Get file count and names
  881. file_count_result = await db.execute(
  882. select(func.count(LibraryFile.id)).where(
  883. LibraryFile.folder_id == folder_id,
  884. LibraryFile.deleted_at.is_(None),
  885. )
  886. )
  887. file_count = file_count_result.scalar() or 0
  888. # Get project and archive names
  889. project_name = None
  890. archive_name = None
  891. if folder.project_id:
  892. project_result = await db.execute(select(Project.name).where(Project.id == folder.project_id))
  893. project_name = project_result.scalar()
  894. if folder.archive_id:
  895. archive_result = await db.execute(select(PrintArchive.print_name).where(PrintArchive.id == folder.archive_id))
  896. archive_name = archive_result.scalar()
  897. return FolderResponse(
  898. id=folder.id,
  899. name=folder.name,
  900. parent_id=folder.parent_id,
  901. project_id=folder.project_id,
  902. archive_id=folder.archive_id,
  903. project_name=project_name,
  904. archive_name=archive_name,
  905. is_external=folder.is_external,
  906. external_path=folder.external_path,
  907. external_readonly=folder.external_readonly,
  908. external_show_hidden=folder.external_show_hidden,
  909. file_count=file_count,
  910. created_at=folder.created_at,
  911. updated_at=folder.updated_at,
  912. )
  913. @router.delete("/folders/{folder_id}")
  914. async def delete_folder(
  915. folder_id: int,
  916. db: AsyncSession = Depends(get_db),
  917. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_DELETE_ALL)),
  918. ):
  919. """Delete a folder and all its contents (cascade).
  920. Note: Folders require library:delete_all permission since they don't have
  921. ownership tracking.
  922. """
  923. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  924. folder = result.scalar_one_or_none()
  925. if not folder:
  926. raise HTTPException(status_code=404, detail="Folder not found")
  927. # External folders: only remove DB records, never delete files from external path
  928. is_ext = folder.is_external
  929. # Get all files in this folder and subfolders to delete from disk
  930. async def get_all_file_ids(fid: int) -> list[int]:
  931. """Recursively get all file IDs in a folder tree."""
  932. file_ids = []
  933. # Get files in this folder
  934. files_result = await db.execute(
  935. select(LibraryFile.id, LibraryFile.file_path, LibraryFile.thumbnail_path, LibraryFile.is_external).where(
  936. LibraryFile.folder_id == fid
  937. )
  938. )
  939. for fid_val, file_path, thumb_path, file_is_ext in files_result.all():
  940. file_ids.append(fid_val)
  941. # Only delete non-external files from disk
  942. if not is_ext and not file_is_ext:
  943. try:
  944. if file_path and os.path.exists(file_path):
  945. os.remove(file_path)
  946. if thumb_path and os.path.exists(thumb_path):
  947. os.remove(thumb_path)
  948. except OSError as e:
  949. logger.warning("Failed to delete file: %s", e)
  950. # Get child folders and recurse
  951. children_result = await db.execute(select(LibraryFolder.id).where(LibraryFolder.parent_id == fid))
  952. for (child_id,) in children_result.all():
  953. file_ids.extend(await get_all_file_ids(child_id))
  954. return file_ids
  955. await get_all_file_ids(folder_id)
  956. # Delete folder (cascade will handle files and subfolders)
  957. await db.delete(folder)
  958. await db.commit()
  959. return {"status": "success", "message": "Folder deleted"}
  960. # ============ External Folder Endpoints ============
  961. # GHSA-r2qv follow-up (audit finding I1): external-folder mount path uses an
  962. # allowlist of operator-opted-in roots rather than the original denylist of
  963. # system directories. The denylist shape was fail-open-on-growth — anything
  964. # not enumerated (``/data`` containing other users' archives, ``/root``,
  965. # arbitrary NFS/SMB mounts, the Bambuddy ``LOG_DIR``) could be mounted by any
  966. # user with ``LIBRARY_UPLOAD``. The allowlist defaults to empty and is
  967. # extended via the ``BAMBUDDY_EXTERNAL_ROOTS`` env var (colon-separated
  968. # absolute paths). The route is additionally gated on ``SETTINGS_UPDATE``
  969. # (admin scope) rather than ``LIBRARY_UPLOAD`` because mounting host paths
  970. # is an operator-level capability that crosses user boundaries.
  971. # Bambuddy-owned data directories. Hardcode-rejected even if the operator
  972. # tries to add them to ``BAMBUDDY_EXTERNAL_ROOTS`` — mounting these would
  973. # allow reading other users' archives, log files, or the static assets path.
  974. def _bambuddy_reserved_roots() -> tuple[Path, ...]:
  975. """Resolved Bambuddy-owned directories that may NEVER be mounted as an
  976. external folder regardless of the operator's allowlist.
  977. Resolved at call time because tests patch ``settings.base_dir`` /
  978. ``settings.log_dir`` to a temp dir; resolving lazily picks up the
  979. patched values rather than module-import-time values.
  980. """
  981. from backend.app.core.config import settings as app_settings
  982. reserved = [app_settings.base_dir, app_settings.log_dir, app_settings.static_dir, app_settings.archive_dir]
  983. return tuple(Path(p).resolve() for p in reserved if p is not None)
  984. def _allowed_external_roots() -> tuple[Path, ...]:
  985. """Parse ``BAMBUDDY_EXTERNAL_ROOTS`` into resolved allowed roots.
  986. Empty env var (the default) means external folders are disabled.
  987. Operators opt in explicitly: ``BAMBUDDY_EXTERNAL_ROOTS=/mnt/library:/srv/3d``
  988. Returns a tuple of resolved ``Path`` objects; entries that don't
  989. resolve to absolute paths are silently dropped (operator error, not
  990. a security boundary). Resolved lazily so tests can monkeypatch.
  991. """
  992. raw = os.environ.get("BAMBUDDY_EXTERNAL_ROOTS", "")
  993. roots: list[Path] = []
  994. for entry in raw.split(":"):
  995. entry = entry.strip()
  996. if not entry:
  997. continue
  998. try:
  999. resolved = Path(entry).resolve()
  1000. except (OSError, RuntimeError): # noqa: BLE001 — operator config error, not a security boundary
  1001. continue
  1002. if resolved.is_absolute():
  1003. roots.append(resolved)
  1004. return tuple(roots)
  1005. def _path_within(child: Path, parent: Path) -> bool:
  1006. """Return True if ``child`` is ``parent`` or any descendant.
  1007. Uses ``Path.relative_to`` semantics (raises ``ValueError`` on miss)
  1008. instead of string ``startswith``, which would falsely match
  1009. ``/data-other`` against ``/data``. ``Path.is_relative_to`` is the
  1010. sanctioned form on Python 3.9+; both are available here.
  1011. """
  1012. try:
  1013. child.relative_to(parent)
  1014. except ValueError:
  1015. return False
  1016. return True
  1017. # Supported file extensions for external folder scanning
  1018. _SCANNABLE_EXTENSIONS = {
  1019. ".3mf",
  1020. ".gcode",
  1021. ".gcode.3mf",
  1022. ".stl",
  1023. ".obj",
  1024. ".step",
  1025. ".stp",
  1026. ".png",
  1027. ".jpg",
  1028. ".jpeg",
  1029. ".gif",
  1030. ".webp",
  1031. ".svg",
  1032. }
  1033. def _validate_external_path(path_str: str) -> Path:
  1034. """Validate an external path is safe to mount.
  1035. Allowlist semantics:
  1036. 1. Path must be absolute and resolve cleanly (symlink-escape rejected
  1037. implicitly by the resolved-startswith check below).
  1038. 2. Path must fall under one of the roots enumerated in
  1039. ``BAMBUDDY_EXTERNAL_ROOTS``; empty allowlist (the default)
  1040. means external folders are not available on this deployment.
  1041. 3. Path must NOT fall under any Bambuddy-owned directory (``base_dir``,
  1042. ``log_dir``, ``static_dir``, ``archive_dir``) — the reserved set
  1043. takes precedence over the allowlist, so an operator who accidentally
  1044. sets ``BAMBUDDY_EXTERNAL_ROOTS=/`` does not expose ``/data``.
  1045. 4. Existence + directory-type + readability gates remain.
  1046. """
  1047. path = Path(path_str).resolve()
  1048. if not path.is_absolute():
  1049. raise HTTPException(status_code=400, detail="Path must be absolute")
  1050. allowed_roots = _allowed_external_roots()
  1051. if not allowed_roots:
  1052. raise HTTPException(
  1053. status_code=400,
  1054. detail=(
  1055. "External folders are not enabled on this deployment. Ask the "
  1056. "operator to set BAMBUDDY_EXTERNAL_ROOTS=<colon-separated paths>."
  1057. ),
  1058. )
  1059. # Reserved (Bambuddy-owned) paths are rejected before the allowlist check
  1060. # so an over-broad allowlist (e.g. operator set "/" for testing) cannot
  1061. # expose Bambuddy's own data dir or log dir.
  1062. for reserved in _bambuddy_reserved_roots():
  1063. if _path_within(path, reserved):
  1064. raise HTTPException(
  1065. status_code=400,
  1066. detail=f"Cannot mount Bambuddy-managed directory: {reserved}",
  1067. )
  1068. if not any(_path_within(path, root) for root in allowed_roots):
  1069. raise HTTPException(
  1070. status_code=400,
  1071. detail=(
  1072. f"Path '{path}' is not within an allowed external root. "
  1073. f"Allowed roots: {', '.join(str(r) for r in allowed_roots)}"
  1074. ),
  1075. )
  1076. if not path.exists():
  1077. raise HTTPException(status_code=400, detail=f"Path does not exist: {path}")
  1078. if not path.is_dir():
  1079. raise HTTPException(status_code=400, detail=f"Path is not a directory: {path}")
  1080. # Check readability
  1081. if not os.access(path, os.R_OK):
  1082. raise HTTPException(status_code=400, detail=f"Path is not readable: {path}")
  1083. return path
  1084. @router.post("/folders/external", response_model=FolderResponse)
  1085. async def create_external_folder(
  1086. data: ExternalFolderCreate,
  1087. db: AsyncSession = Depends(get_db),
  1088. # GHSA-r2qv follow-up (I1): elevated from LIBRARY_UPLOAD to SETTINGS_UPDATE.
  1089. # Registering a host filesystem path as a Bambuddy library folder is an
  1090. # operator-level capability that crosses user boundaries (one user's
  1091. # registered external folder is visible to every other user via
  1092. # /api/v1/library/folders). LIBRARY_UPLOAD was always the wrong scope —
  1093. # SETTINGS_UPDATE is the admin-class gate that already protects every
  1094. # other host-affecting setting (SMTP, LDAP, cloud, smart plugs).
  1095. _: User | None = Depends(require_permission_if_auth_enabled(Permission.SETTINGS_UPDATE)),
  1096. ):
  1097. """Create an external folder that points to a host directory."""
  1098. resolved = _validate_external_path(data.external_path)
  1099. # Check no other external folder already points to this path
  1100. existing = await db.execute(
  1101. select(LibraryFolder).where(
  1102. LibraryFolder.is_external.is_(True),
  1103. LibraryFolder.external_path == str(resolved),
  1104. )
  1105. )
  1106. if existing.scalar_one_or_none():
  1107. raise HTTPException(status_code=409, detail="An external folder already exists for this path")
  1108. # Verify parent exists if specified
  1109. if data.parent_id is not None:
  1110. parent_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.parent_id))
  1111. if not parent_result.scalar_one_or_none():
  1112. raise HTTPException(status_code=404, detail="Parent folder not found")
  1113. folder = LibraryFolder(
  1114. name=data.name,
  1115. parent_id=data.parent_id,
  1116. is_external=True,
  1117. external_path=str(resolved),
  1118. external_readonly=data.readonly,
  1119. external_show_hidden=data.show_hidden,
  1120. )
  1121. db.add(folder)
  1122. await db.commit()
  1123. await db.refresh(folder)
  1124. return FolderResponse(
  1125. id=folder.id,
  1126. name=folder.name,
  1127. parent_id=folder.parent_id,
  1128. project_id=None,
  1129. archive_id=None,
  1130. is_external=True,
  1131. external_path=folder.external_path,
  1132. external_readonly=folder.external_readonly,
  1133. external_show_hidden=folder.external_show_hidden,
  1134. file_count=0,
  1135. created_at=folder.created_at,
  1136. updated_at=folder.updated_at,
  1137. )
  1138. @router.post("/folders/{folder_id}/scan")
  1139. async def scan_external_folder(
  1140. folder_id: int,
  1141. db: AsyncSession = Depends(get_db),
  1142. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  1143. ):
  1144. """Scan an external folder and sync files to the database.
  1145. Discovers new files, removes DB entries for deleted files.
  1146. Does not copy files — stores the external path directly.
  1147. """
  1148. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  1149. folder = result.scalar_one_or_none()
  1150. if not folder:
  1151. raise HTTPException(status_code=404, detail="Folder not found")
  1152. if not folder.is_external or not folder.external_path:
  1153. raise HTTPException(status_code=400, detail="Not an external folder")
  1154. ext_path = Path(folder.external_path)
  1155. if not ext_path.exists() or not ext_path.is_dir():
  1156. raise HTTPException(status_code=400, detail=f"External path is not accessible: {folder.external_path}")
  1157. # Collect all existing child external subfolder IDs (single query)
  1158. all_folder_ids = [folder_id]
  1159. child_result = await db.execute(
  1160. select(LibraryFolder).where(
  1161. LibraryFolder.is_external.is_(True),
  1162. LibraryFolder.parent_id.isnot(None),
  1163. )
  1164. )
  1165. all_child_folders = child_result.scalars().all()
  1166. # Walk the parent chain to find all descendants of folder_id
  1167. parent_to_children: dict[int, list] = {}
  1168. for cf in all_child_folders:
  1169. parent_to_children.setdefault(cf.parent_id, []).append(cf)
  1170. queue = [folder_id]
  1171. while queue:
  1172. pid = queue.pop()
  1173. for child in parent_to_children.get(pid, []):
  1174. all_folder_ids.append(child.id)
  1175. queue.append(child.id)
  1176. # Get existing DB files across root and all subfolders
  1177. existing_result = await db.execute(
  1178. LibraryFile.active().where(
  1179. LibraryFile.folder_id.in_(all_folder_ids),
  1180. LibraryFile.is_external.is_(True),
  1181. )
  1182. )
  1183. existing_files = {f.file_path: f for f in existing_result.scalars().all()}
  1184. # Build folder cache: relative path -> folder_id (for resolving subfolders)
  1185. # Pre-populate with existing child folders keyed by their external_path
  1186. folder_cache: dict[str, int] = {"": folder_id}
  1187. for fid in all_folder_ids:
  1188. if fid == folder_id:
  1189. continue
  1190. # Find the child folder object
  1191. for cf in all_child_folders:
  1192. if cf.id == fid and cf.external_path:
  1193. try:
  1194. rel = str(Path(cf.external_path).relative_to(ext_path))
  1195. if rel != ".":
  1196. folder_cache[rel] = cf.id
  1197. except ValueError:
  1198. pass
  1199. # Scan the directory
  1200. added = 0
  1201. removed = 0
  1202. found_paths: set[str] = set()
  1203. seen_rel_dirs: set[str] = set()
  1204. for dirpath, dirnames, filenames in os.walk(ext_path):
  1205. # Filter hidden directories unless configured
  1206. if not folder.external_show_hidden:
  1207. dirnames[:] = [d for d in dirnames if not d.startswith(".")]
  1208. rel_dir = str(Path(dirpath).relative_to(ext_path))
  1209. if rel_dir == ".":
  1210. rel_dir = ""
  1211. seen_rel_dirs.add(rel_dir)
  1212. # Resolve or create subfolder chain for this directory
  1213. if rel_dir and rel_dir not in folder_cache:
  1214. parts = Path(rel_dir).parts
  1215. current_path = ""
  1216. current_parent = folder_id
  1217. for part in parts:
  1218. current_path = f"{current_path}/{part}".lstrip("/")
  1219. if current_path in folder_cache:
  1220. current_parent = folder_cache[current_path]
  1221. else:
  1222. existing_sub = await db.execute(
  1223. select(LibraryFolder).where(
  1224. LibraryFolder.name == part,
  1225. LibraryFolder.parent_id == current_parent,
  1226. LibraryFolder.is_external.is_(True),
  1227. )
  1228. )
  1229. existing_folder = existing_sub.scalar_one_or_none()
  1230. if existing_folder:
  1231. current_parent = existing_folder.id
  1232. else:
  1233. new_folder = LibraryFolder(
  1234. name=part,
  1235. parent_id=current_parent,
  1236. is_external=True,
  1237. external_path=str(
  1238. ext_path / current_path
  1239. ), # SEC-PATH-OK: current_path built from Path(rel_dir).parts of an os.walk descent under ext_path
  1240. external_readonly=folder.external_readonly,
  1241. external_show_hidden=folder.external_show_hidden,
  1242. )
  1243. db.add(new_folder)
  1244. await db.flush()
  1245. current_parent = new_folder.id
  1246. folder_cache[current_path] = current_parent
  1247. target_folder_id = folder_cache.get(rel_dir, folder_id)
  1248. for filename in filenames:
  1249. # Skip hidden files unless configured
  1250. if not folder.external_show_hidden and filename.startswith("."):
  1251. continue
  1252. filepath = (
  1253. Path(dirpath) / filename
  1254. ) # SEC-PATH-OK: dirpath + filename from os.walk(ext_path); filesystem-discovered, not user input
  1255. ext = filepath.suffix.lower()
  1256. # Check for compound extensions like .gcode.3mf
  1257. if ext not in _SCANNABLE_EXTENSIONS:
  1258. # Check compound
  1259. compound = "".join(filepath.suffixes[-2:]).lower() if len(filepath.suffixes) >= 2 else ""
  1260. if compound not in _SCANNABLE_EXTENSIONS:
  1261. continue
  1262. # Resolve symlinks and ensure still under external_path
  1263. try:
  1264. real_path = filepath.resolve()
  1265. real_path.relative_to(ext_path.resolve())
  1266. except (ValueError, OSError):
  1267. continue # Symlink escapes the external dir
  1268. file_path_str = str(filepath)
  1269. found_paths.add(file_path_str)
  1270. if file_path_str in existing_files:
  1271. continue # Already tracked
  1272. # Get file info
  1273. try:
  1274. stat = filepath.stat()
  1275. except OSError:
  1276. continue
  1277. file_type = classify_file_type(filename)
  1278. # Extract thumbnail for 3mf files (including .gcode.3mf sliced
  1279. # outputs — those are 3MF zips on disk and carry the same
  1280. # thumbnail Metadata/plate_1.png the parser reads). Pre-#1600
  1281. # the gate was `file_type == "3mf"` alone, so .gcode.3mf files
  1282. # in external folders silently got no thumbnail.
  1283. thumbnail_path = None
  1284. file_metadata = None
  1285. if file_type in ("3mf", "gcode.3mf"):
  1286. try:
  1287. parser = ThreeMFParser(str(filepath))
  1288. raw_metadata = parser.parse()
  1289. if raw_metadata:
  1290. # Extract thumbnail before cleaning metadata
  1291. thumb_data = raw_metadata.get("_thumbnail_data")
  1292. thumbnail_ext = raw_metadata.get("_thumbnail_ext", ".png")
  1293. if thumb_data:
  1294. thumb_dir = get_library_thumbnails_dir()
  1295. thumb_filename = f"{uuid.uuid4().hex}{thumbnail_ext}"
  1296. thumb_full = (
  1297. thumb_dir / thumb_filename
  1298. ) # SEC-PATH-OK: thumb_filename = uuid.uuid4().hex + thumbnail_ext
  1299. thumb_full.write_bytes(thumb_data)
  1300. thumbnail_path = to_relative_path(thumb_full)
  1301. # Clean metadata - remove non-JSON-serializable data (bytes, etc.)
  1302. def clean_metadata(obj):
  1303. if isinstance(obj, dict):
  1304. return {
  1305. k: clean_metadata(v)
  1306. for k, v in obj.items()
  1307. if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext")
  1308. }
  1309. elif isinstance(obj, list):
  1310. return [clean_metadata(i) for i in obj if not isinstance(i, bytes)]
  1311. elif isinstance(obj, bytes):
  1312. return None
  1313. return obj
  1314. file_metadata = clean_metadata(raw_metadata)
  1315. except Exception as e:
  1316. logger.debug("Failed to extract metadata from external 3mf %s: %s", filepath, e)
  1317. # STL thumbnails are deferred to a background task spawned after
  1318. # the scan's db.commit() — see _backfill_external_stl_thumbnails.
  1319. # Doing them inline would block the HTTP request for minutes on a
  1320. # large NAS mount (#1299).
  1321. # Extract gcode thumbnail
  1322. if file_type == "gcode" and thumbnail_path is None:
  1323. thumb_data = extract_gcode_thumbnail(filepath)
  1324. if thumb_data:
  1325. thumb_dir = get_library_thumbnails_dir()
  1326. thumb_filename = f"{uuid.uuid4().hex}.png"
  1327. thumb_full = thumb_dir / thumb_filename # SEC-PATH-OK: thumb_filename = uuid.uuid4().hex + ".png"
  1328. thumb_full.write_bytes(thumb_data)
  1329. thumbnail_path = to_relative_path(thumb_full)
  1330. # Create thumbnail for image files
  1331. if ext.lower() in IMAGE_EXTENSIONS and thumbnail_path is None:
  1332. thumbnail_path_str = create_image_thumbnail(filepath, get_library_thumbnails_dir())
  1333. if thumbnail_path_str:
  1334. thumbnail_path = to_relative_path(Path(thumbnail_path_str))
  1335. db_file = LibraryFile(
  1336. folder_id=target_folder_id,
  1337. is_external=True,
  1338. filename=filename,
  1339. file_path=file_path_str,
  1340. file_type=file_type,
  1341. file_size=stat.st_size,
  1342. file_hash=None, # Skip hashing external files for performance
  1343. thumbnail_path=thumbnail_path,
  1344. file_metadata=_without_print_name(file_metadata),
  1345. )
  1346. db.add(db_file)
  1347. added += 1
  1348. # Remove DB entries for files that no longer exist on disk
  1349. for path_str, db_file in existing_files.items():
  1350. if path_str not in found_paths:
  1351. # Clean up thumbnail if we generated one
  1352. if db_file.thumbnail_path:
  1353. try:
  1354. abs_thumb = to_absolute_path(db_file.thumbnail_path)
  1355. if abs_thumb and abs_thumb.exists():
  1356. abs_thumb.unlink()
  1357. except OSError:
  1358. pass
  1359. await db.delete(db_file)
  1360. removed += 1
  1361. # Remove empty subfolders whose directories no longer exist on disk
  1362. # Process deepest-first by sorting on path depth (descending)
  1363. subfolder_entries = [(rel, fid) for rel, fid in folder_cache.items() if rel and fid != folder_id]
  1364. subfolder_entries.sort(key=lambda x: x[0].count("/"), reverse=True)
  1365. for rel_path, sub_fid in subfolder_entries:
  1366. if rel_path in seen_rel_dirs:
  1367. continue # Directory still exists on disk
  1368. # Check if subfolder has any remaining files
  1369. file_count_result = await db.execute(
  1370. select(func.count(LibraryFile.id)).where(
  1371. LibraryFile.folder_id == sub_fid,
  1372. LibraryFile.deleted_at.is_(None),
  1373. )
  1374. )
  1375. if (file_count_result.scalar() or 0) == 0:
  1376. # Check if it has any remaining child folders
  1377. child_count_result = await db.execute(
  1378. select(func.count(LibraryFolder.id)).where(LibraryFolder.parent_id == sub_fid)
  1379. )
  1380. if (child_count_result.scalar() or 0) == 0:
  1381. sub_folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == sub_fid))
  1382. sub_folder_obj = sub_folder_result.scalar_one_or_none()
  1383. if sub_folder_obj:
  1384. await db.delete(sub_folder_obj)
  1385. await db.commit()
  1386. # Spawn STL thumbnail backfill in the background — the scan endpoint
  1387. # returns immediately so the FE modal closes and subdirectories are
  1388. # visible right away; thumbnails fill in over the following seconds /
  1389. # minutes as the task processes each STL file. Survives FE refresh —
  1390. # the task lives in the FastAPI event loop, not the request scope.
  1391. # folder_cache.values() covers the root + every pre-existing subfolder
  1392. # + every subfolder created during this scan. all_folder_ids on its own
  1393. # would miss the newly-created ones (it's snapshotted before the walk).
  1394. asyncio.create_task(
  1395. _backfill_external_stl_thumbnails(list(set(folder_cache.values()))),
  1396. name=f"stl-backfill-folder-{folder_id}",
  1397. )
  1398. return {"status": "success", "added": added, "removed": removed}
  1399. # ============ File Endpoints ============
  1400. @router.get("/files", response_model=list[FileListResponse])
  1401. @router.get("/files/", response_model=list[FileListResponse])
  1402. async def list_files(
  1403. response: Response,
  1404. folder_id: int | None = None,
  1405. project_id: int | None = None,
  1406. include_root: bool = True,
  1407. db: AsyncSession = Depends(get_db),
  1408. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  1409. ):
  1410. """List files, optionally filtered by folder or project.
  1411. Args:
  1412. folder_id: Filter by folder ID. If None and include_root=True, returns root files.
  1413. project_id: Return all files across folders linked to this project (bulk fetch, avoids N+1).
  1414. include_root: If True and folder_id is None, returns files at root level.
  1415. If False and folder_id is None, returns all files.
  1416. """
  1417. query = LibraryFile.active().options(selectinload(LibraryFile.created_by))
  1418. if folder_id is not None:
  1419. query = query.where(LibraryFile.folder_id == folder_id)
  1420. elif project_id is not None:
  1421. # Single join instead of one query per folder (avoids N+1 pattern)
  1422. query = query.join(LibraryFolder, LibraryFile.folder_id == LibraryFolder.id)
  1423. query = query.where(LibraryFolder.project_id == project_id)
  1424. elif include_root:
  1425. query = query.where(LibraryFile.folder_id.is_(None))
  1426. query = query.order_by(LibraryFile.filename)
  1427. result = await db.execute(query)
  1428. files = result.scalars().all()
  1429. # Get duplicate counts
  1430. hash_counts = {}
  1431. if files:
  1432. hashes = [f.file_hash for f in files if f.file_hash]
  1433. if hashes:
  1434. dup_result = await db.execute(
  1435. select(LibraryFile.file_hash, func.count(LibraryFile.id))
  1436. .where(LibraryFile.file_hash.in_(hashes), LibraryFile.deleted_at.is_(None))
  1437. .group_by(LibraryFile.file_hash)
  1438. )
  1439. hash_counts = {h: c - 1 for h, c in dup_result.all()} # -1 to exclude self
  1440. # Prevent browser caching of file list
  1441. response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
  1442. file_list = []
  1443. for f in files:
  1444. # Extract key metadata for display
  1445. print_name = None
  1446. print_time = None
  1447. filament_grams = None
  1448. sliced_for_model = None
  1449. if f.file_metadata:
  1450. print_name = f.file_metadata.get("print_name")
  1451. print_time = f.file_metadata.get("print_time_seconds")
  1452. filament_grams = f.file_metadata.get("filament_used_grams")
  1453. sliced_for_model = f.file_metadata.get("sliced_for_model")
  1454. file_list.append(
  1455. FileListResponse(
  1456. id=f.id,
  1457. folder_id=f.folder_id,
  1458. is_external=f.is_external,
  1459. filename=f.filename,
  1460. file_type=f.file_type,
  1461. file_size=f.file_size,
  1462. thumbnail_path=f.thumbnail_path,
  1463. print_count=f.print_count,
  1464. duplicate_count=hash_counts.get(f.file_hash, 0) if f.file_hash else 0,
  1465. created_by_id=f.created_by_id,
  1466. created_by_username=f.created_by.username if f.created_by else None,
  1467. created_at=f.created_at,
  1468. print_name=print_name,
  1469. print_time_seconds=print_time,
  1470. filament_used_grams=filament_grams,
  1471. sliced_for_model=sliced_for_model,
  1472. )
  1473. )
  1474. return file_list
  1475. @router.post("/files", response_model=FileUploadResponse)
  1476. @router.post("/files/", response_model=FileUploadResponse)
  1477. async def upload_file(
  1478. file: UploadFile = File(...),
  1479. folder_id: int | None = None,
  1480. generate_stl_thumbnails: bool = Query(default=True),
  1481. db: AsyncSession = Depends(get_db),
  1482. current_user: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  1483. ):
  1484. """Upload a file to the library."""
  1485. try:
  1486. if not file.filename:
  1487. raise HTTPException(status_code=400, detail="Filename is required")
  1488. filename = file.filename
  1489. # Reject FAT32/exFAT-incompatible filenames up front (#1540).
  1490. try:
  1491. validate_print_filename(filename)
  1492. except InvalidFilenameError as e:
  1493. raise HTTPException(status_code=400, detail=str(e)) from e
  1494. ext = os.path.splitext(filename)[1].lower()
  1495. # `file_type` is compound-aware (`gcode.3mf` for sliced outputs).
  1496. # `ext` stays the trailing extension because the on-disk filename
  1497. # uses it directly and the 3MF-parse branch below still gates on
  1498. # `ext == ".3mf"`, which is correct for both `.3mf` and `.gcode.3mf`.
  1499. file_type = classify_file_type(filename)
  1500. # Verify folder exists if specified
  1501. target_folder = None
  1502. if folder_id is not None:
  1503. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  1504. target_folder = folder_result.scalar_one_or_none()
  1505. if not target_folder:
  1506. raise HTTPException(status_code=404, detail="Folder not found")
  1507. # Writable external folders write through to the mount so the file is
  1508. # visible outside Bambuddy (#1112); everything else lands under the
  1509. # internal library dir with a UUID-scoped filename. Resolved BEFORE
  1510. # the content validation below so folder-permission rejections
  1511. # (403 read-only, 400 missing path, 409 collision) still surface
  1512. # before any "bad file format" 400 — preserves existing error
  1513. # ordering / tests.
  1514. file_path, is_external_upload = _resolve_upload_destination(target_folder, filename)
  1515. # Read upload now so the validation can sniff magic bytes; the file
  1516. # is written to disk only after the checks. #1401.
  1517. content = await file.read()
  1518. validate_print_file_upload(filename, content)
  1519. # Save file
  1520. with open(file_path, "wb") as f:
  1521. f.write(content)
  1522. # Calculate hash
  1523. file_hash = calculate_file_hash(file_path)
  1524. # Check for duplicates
  1525. dup_result = await db.execute(
  1526. select(LibraryFile.id).where(LibraryFile.file_hash == file_hash, LibraryFile.deleted_at.is_(None)).limit(1)
  1527. )
  1528. duplicate_of = dup_result.scalar()
  1529. # Extract metadata and thumbnail
  1530. metadata = {}
  1531. thumbnail_path = None
  1532. thumbnails_dir = get_library_thumbnails_dir()
  1533. if ext == ".3mf":
  1534. try:
  1535. parser = ThreeMFParser(str(file_path))
  1536. raw_metadata = parser.parse()
  1537. # Extract thumbnail before cleaning metadata
  1538. thumbnail_data = raw_metadata.get("_thumbnail_data")
  1539. thumbnail_ext = raw_metadata.get("_thumbnail_ext", ".png")
  1540. # Save thumbnail if extracted
  1541. if thumbnail_data:
  1542. thumb_filename = f"{uuid.uuid4().hex}{thumbnail_ext}"
  1543. thumb_path = (
  1544. thumbnails_dir / thumb_filename
  1545. ) # SEC-PATH-OK: thumb_filename = uuid.uuid4().hex + thumbnail_ext
  1546. with open(thumb_path, "wb") as f:
  1547. f.write(thumbnail_data)
  1548. thumbnail_path = str(thumb_path)
  1549. # Clean metadata - remove non-JSON-serializable data (bytes, etc.)
  1550. def clean_metadata(obj):
  1551. if isinstance(obj, dict):
  1552. return {
  1553. k: clean_metadata(v)
  1554. for k, v in obj.items()
  1555. if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext")
  1556. }
  1557. elif isinstance(obj, list):
  1558. return [clean_metadata(i) for i in obj if not isinstance(i, bytes)]
  1559. elif isinstance(obj, bytes):
  1560. return None
  1561. return obj
  1562. metadata = clean_metadata(raw_metadata)
  1563. except Exception as e:
  1564. logger.warning("Failed to parse 3MF: %s", e)
  1565. elif ext == ".gcode":
  1566. # Extract embedded thumbnail from gcode
  1567. try:
  1568. thumbnail_data = extract_gcode_thumbnail(file_path)
  1569. if thumbnail_data:
  1570. thumb_filename = f"{uuid.uuid4().hex}.png"
  1571. thumb_path = (
  1572. thumbnails_dir / thumb_filename
  1573. ) # SEC-PATH-OK: thumb_filename = uuid.uuid4().hex + ".png"
  1574. with open(thumb_path, "wb") as f:
  1575. f.write(thumbnail_data)
  1576. thumbnail_path = str(thumb_path)
  1577. except Exception as e:
  1578. logger.warning("Failed to extract gcode thumbnail: %s", e)
  1579. elif ext.lower() in IMAGE_EXTENSIONS:
  1580. # For image files, create a thumbnail from the image itself
  1581. thumbnail_path = create_image_thumbnail(file_path, thumbnails_dir)
  1582. elif ext == ".stl":
  1583. # Generate STL thumbnail if enabled. Same MIN_USABLE_STL_BYTES
  1584. # pre-skip as extract_zip_file — stubs / placeholders below this
  1585. # size can't contain a triangle so trimesh would return an empty
  1586. # mesh anyway.
  1587. if generate_stl_thumbnails:
  1588. try:
  1589. if file_path.stat().st_size >= MIN_USABLE_STL_BYTES:
  1590. thumbnail_path = generate_stl_thumbnail(file_path, thumbnails_dir)
  1591. except OSError:
  1592. pass
  1593. # Create database entry (managed files store relative paths for portability;
  1594. # external files store the absolute mount path — same shape as scan produces)
  1595. library_file = LibraryFile(
  1596. folder_id=folder_id,
  1597. is_external=is_external_upload,
  1598. filename=filename,
  1599. file_path=_stored_file_path(file_path, is_external_upload),
  1600. file_type=file_type,
  1601. file_size=len(content),
  1602. file_hash=file_hash,
  1603. thumbnail_path=to_relative_path(thumbnail_path) if thumbnail_path else None,
  1604. file_metadata=_without_print_name(metadata) if metadata else None,
  1605. created_by_id=current_user.id if current_user else None,
  1606. )
  1607. db.add(library_file)
  1608. await db.commit()
  1609. await db.refresh(library_file)
  1610. return FileUploadResponse(
  1611. id=library_file.id,
  1612. filename=library_file.filename,
  1613. file_type=library_file.file_type,
  1614. file_size=library_file.file_size,
  1615. thumbnail_path=library_file.thumbnail_path,
  1616. duplicate_of=duplicate_of,
  1617. metadata=library_file.file_metadata,
  1618. )
  1619. except HTTPException:
  1620. raise
  1621. except Exception as e:
  1622. logger.error("Upload failed for %s: %s", file.filename, e, exc_info=True)
  1623. raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)}")
  1624. @router.post("/files/extract-zip", response_model=ZipExtractResponse)
  1625. async def extract_zip_file(
  1626. file: UploadFile = File(...),
  1627. folder_id: int | None = Query(default=None),
  1628. preserve_structure: bool = Query(default=True),
  1629. create_folder_from_zip: bool = Query(default=False),
  1630. generate_stl_thumbnails: bool = Query(default=True),
  1631. db: AsyncSession = Depends(get_db),
  1632. current_user: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  1633. ):
  1634. """Upload and extract a ZIP file to the library.
  1635. Args:
  1636. file: The ZIP file to extract
  1637. folder_id: Target folder ID (None = root)
  1638. preserve_structure: If True, recreate folder structure from ZIP; if False, extract all files flat
  1639. create_folder_from_zip: If True, create a folder named after the ZIP file and extract into it
  1640. generate_stl_thumbnails: If True, generate thumbnails for STL files
  1641. """
  1642. import tempfile
  1643. if not file.filename or not file.filename.lower().endswith(".zip"):
  1644. raise HTTPException(status_code=400, detail="Only ZIP files are supported")
  1645. # Verify target folder exists if specified
  1646. if folder_id is not None:
  1647. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  1648. target_folder = folder_result.scalar_one_or_none()
  1649. if not target_folder:
  1650. raise HTTPException(status_code=404, detail="Target folder not found")
  1651. if target_folder.is_external and target_folder.external_readonly:
  1652. raise HTTPException(status_code=403, detail="Cannot extract ZIP to a read-only external folder")
  1653. if target_folder.is_external:
  1654. # Writable external folders aren't supported by extract-zip because the
  1655. # nested-subfolder creation path would need to mkdir on the mount and
  1656. # create matching is_external=True LibraryFolder rows — a separate
  1657. # design. Direct the user at Scan, which already handles that shape
  1658. # (#1112).
  1659. raise HTTPException(
  1660. status_code=400,
  1661. detail=(
  1662. "Cannot extract ZIP directly into an external folder. "
  1663. "Extract the ZIP on the external mount and run 'Scan External Folder' instead."
  1664. ),
  1665. )
  1666. # Save ZIP to temp file
  1667. try:
  1668. with tempfile.NamedTemporaryFile(delete=False, suffix=".zip") as tmp:
  1669. content = await file.read()
  1670. tmp.write(content)
  1671. tmp_path = tmp.name
  1672. except Exception as e:
  1673. raise HTTPException(status_code=500, detail=f"Failed to save ZIP file: {str(e)}")
  1674. extracted_files: list[ZipExtractResult] = []
  1675. errors: list[ZipExtractError] = []
  1676. folders_created = 0
  1677. folder_cache: dict[str, int] = {} # path -> folder_id
  1678. # If create_folder_from_zip is True, create a folder named after the ZIP file
  1679. zip_folder_id = folder_id
  1680. logger.info(
  1681. f"ZIP extraction: create_folder_from_zip={create_folder_from_zip}, folder_id={folder_id}, filename={file.filename}"
  1682. )
  1683. if create_folder_from_zip and file.filename:
  1684. # Remove .zip extension to get folder name
  1685. zip_folder_name = file.filename[:-4] if file.filename.lower().endswith(".zip") else file.filename
  1686. # Check if folder already exists
  1687. existing = await db.execute(
  1688. select(LibraryFolder).where(
  1689. LibraryFolder.name == zip_folder_name,
  1690. LibraryFolder.parent_id == folder_id if folder_id else LibraryFolder.parent_id.is_(None),
  1691. )
  1692. )
  1693. existing_folder = existing.scalar_one_or_none()
  1694. if existing_folder:
  1695. zip_folder_id = existing_folder.id
  1696. logger.info("Reusing existing folder '%s' with id=%s", zip_folder_name, zip_folder_id)
  1697. else:
  1698. # Create folder
  1699. new_folder = LibraryFolder(name=zip_folder_name, parent_id=folder_id)
  1700. db.add(new_folder)
  1701. await db.flush()
  1702. await db.commit() # Commit folder creation immediately
  1703. zip_folder_id = new_folder.id
  1704. folders_created += 1
  1705. logger.info("Created new folder '%s' with id=%s", zip_folder_name, zip_folder_id)
  1706. try:
  1707. with zipfile.ZipFile(tmp_path, "r") as zf:
  1708. # Filter out directories and hidden/system files
  1709. file_list = [
  1710. name
  1711. for name in zf.namelist()
  1712. if not name.endswith("/")
  1713. and not name.startswith("__MACOSX")
  1714. and not os.path.basename(name).startswith(".")
  1715. ]
  1716. for zip_path in file_list:
  1717. try:
  1718. # Determine target folder (use zip_folder_id as base if create_folder_from_zip was used)
  1719. target_folder_id = zip_folder_id
  1720. if preserve_structure:
  1721. # Get directory path from ZIP
  1722. dir_path = os.path.dirname(zip_path)
  1723. if dir_path:
  1724. # Create folder structure
  1725. parts = dir_path.split("/")
  1726. current_parent = zip_folder_id
  1727. current_path = ""
  1728. for part in parts:
  1729. if not part:
  1730. continue
  1731. current_path = f"{current_path}/{part}" if current_path else part
  1732. if current_path in folder_cache:
  1733. current_parent = folder_cache[current_path]
  1734. else:
  1735. # Check if folder exists
  1736. existing = await db.execute(
  1737. select(LibraryFolder).where(
  1738. LibraryFolder.name == part,
  1739. LibraryFolder.parent_id == current_parent
  1740. if current_parent
  1741. else LibraryFolder.parent_id.is_(None),
  1742. )
  1743. )
  1744. existing_folder = existing.scalar_one_or_none()
  1745. if existing_folder:
  1746. current_parent = existing_folder.id
  1747. else:
  1748. # Create folder
  1749. new_folder = LibraryFolder(name=part, parent_id=current_parent)
  1750. db.add(new_folder)
  1751. await db.flush()
  1752. current_parent = new_folder.id
  1753. folders_created += 1
  1754. folder_cache[current_path] = current_parent
  1755. target_folder_id = current_parent
  1756. # Extract file
  1757. filename = os.path.basename(zip_path)
  1758. ext = os.path.splitext(filename)[1].lower()
  1759. file_type = classify_file_type(filename)
  1760. # Generate unique filename for storage
  1761. unique_filename = f"{uuid.uuid4().hex}{ext}"
  1762. file_path = (
  1763. get_library_files_dir() / unique_filename
  1764. ) # SEC-PATH-OK: unique_filename = uuid.uuid4().hex + ext
  1765. # Extract and save file
  1766. file_content = zf.read(zip_path)
  1767. with open(file_path, "wb") as f:
  1768. f.write(file_content)
  1769. # Calculate hash
  1770. file_hash = calculate_file_hash(file_path)
  1771. # Extract metadata and thumbnail for 3MF files
  1772. metadata = {}
  1773. thumbnail_path = None
  1774. thumbnails_dir = get_library_thumbnails_dir()
  1775. if ext == ".3mf":
  1776. try:
  1777. parser = ThreeMFParser(str(file_path))
  1778. raw_metadata = parser.parse()
  1779. thumbnail_data = raw_metadata.get("_thumbnail_data")
  1780. thumbnail_ext = raw_metadata.get("_thumbnail_ext", ".png")
  1781. if thumbnail_data:
  1782. thumb_filename = f"{uuid.uuid4().hex}{thumbnail_ext}"
  1783. thumb_path = (
  1784. thumbnails_dir / thumb_filename
  1785. ) # SEC-PATH-OK: thumb_filename = uuid.uuid4().hex + thumbnail_ext
  1786. with open(thumb_path, "wb") as f:
  1787. f.write(thumbnail_data)
  1788. thumbnail_path = str(thumb_path)
  1789. def clean_metadata(obj):
  1790. if isinstance(obj, dict):
  1791. return {
  1792. k: clean_metadata(v)
  1793. for k, v in obj.items()
  1794. if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext")
  1795. }
  1796. elif isinstance(obj, list):
  1797. return [clean_metadata(i) for i in obj if not isinstance(i, bytes)]
  1798. elif isinstance(obj, bytes):
  1799. return None
  1800. return obj
  1801. metadata = clean_metadata(raw_metadata)
  1802. except Exception as e:
  1803. logger.warning("Failed to parse 3MF from ZIP: %s", e)
  1804. elif ext == ".gcode":
  1805. try:
  1806. thumbnail_data = extract_gcode_thumbnail(file_path)
  1807. if thumbnail_data:
  1808. thumb_filename = f"{uuid.uuid4().hex}.png"
  1809. thumb_path = (
  1810. thumbnails_dir / thumb_filename
  1811. ) # SEC-PATH-OK: thumb_filename = uuid.uuid4().hex + ".png"
  1812. with open(thumb_path, "wb") as f:
  1813. f.write(thumbnail_data)
  1814. thumbnail_path = str(thumb_path)
  1815. except Exception as e:
  1816. logger.warning("Failed to extract gcode thumbnail from ZIP: %s", e)
  1817. elif ext.lower() in IMAGE_EXTENSIONS:
  1818. thumbnail_path = create_image_thumbnail(file_path, thumbnails_dir)
  1819. elif ext == ".stl":
  1820. # Generate STL thumbnail if enabled. Pre-skip files
  1821. # below MIN_USABLE_STL_BYTES — they can't contain
  1822. # even a single triangle, and bulk-uploaded ZIPs of
  1823. # stub STLs would otherwise log one debug line per
  1824. # file via the empty-mesh branch in trimesh.load.
  1825. if generate_stl_thumbnails and len(file_content) >= MIN_USABLE_STL_BYTES:
  1826. thumbnail_path = generate_stl_thumbnail(file_path, thumbnails_dir)
  1827. # Create database entry (store relative paths for portability)
  1828. library_file = LibraryFile(
  1829. folder_id=target_folder_id,
  1830. filename=filename,
  1831. file_path=to_relative_path(file_path),
  1832. file_type=file_type,
  1833. file_size=len(file_content),
  1834. file_hash=file_hash,
  1835. thumbnail_path=to_relative_path(thumbnail_path) if thumbnail_path else None,
  1836. file_metadata=_without_print_name(metadata) if metadata else None,
  1837. created_by_id=current_user.id if current_user else None,
  1838. )
  1839. db.add(library_file)
  1840. await db.flush()
  1841. await db.refresh(library_file)
  1842. extracted_files.append(
  1843. ZipExtractResult(
  1844. filename=filename,
  1845. file_id=library_file.id,
  1846. folder_id=target_folder_id,
  1847. )
  1848. )
  1849. # Commit after each file to release database lock
  1850. # This prevents long-running transactions from blocking other requests
  1851. await db.commit()
  1852. except Exception as e:
  1853. logger.error("Failed to extract %s: %s", zip_path, e)
  1854. errors.append(ZipExtractError(filename=os.path.basename(zip_path), error=str(e)))
  1855. # Rollback the failed file but continue with others
  1856. await db.rollback()
  1857. return ZipExtractResponse(
  1858. extracted=len(extracted_files),
  1859. folders_created=folders_created,
  1860. files=extracted_files,
  1861. errors=errors,
  1862. )
  1863. except zipfile.BadZipFile:
  1864. raise HTTPException(status_code=400, detail="Invalid or corrupted ZIP file")
  1865. except Exception as e:
  1866. logger.error("ZIP extraction failed: %s", e, exc_info=True)
  1867. raise HTTPException(status_code=500, detail=f"ZIP extraction failed: {str(e)}")
  1868. finally:
  1869. # Clean up temp file
  1870. try:
  1871. os.unlink(tmp_path)
  1872. except OSError:
  1873. pass # Best-effort temp file cleanup; ignore if already removed
  1874. # ============ STL Thumbnail Batch Generation ============
  1875. @router.post("/generate-stl-thumbnails", response_model=BatchThumbnailResponse)
  1876. async def batch_generate_stl_thumbnails(
  1877. request: BatchThumbnailRequest,
  1878. db: AsyncSession = Depends(get_db),
  1879. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPDATE_ALL)),
  1880. ):
  1881. """Generate thumbnails for STL files in batch.
  1882. Note: Requires library:update_all permission since this is a batch operation
  1883. that may affect files owned by different users.
  1884. Can generate thumbnails for:
  1885. - Specific file IDs (file_ids)
  1886. - All STL files in a folder (folder_id)
  1887. - All STL files missing thumbnails (all_missing=True)
  1888. """
  1889. thumbnails_dir = get_library_thumbnails_dir()
  1890. results: list[BatchThumbnailResult] = []
  1891. # Build query based on request
  1892. query = LibraryFile.active().where(LibraryFile.file_type == "stl")
  1893. if request.file_ids:
  1894. # Specific files
  1895. query = query.where(LibraryFile.id.in_(request.file_ids))
  1896. elif request.folder_id is not None:
  1897. # All STL files in a specific folder
  1898. query = query.where(LibraryFile.folder_id == request.folder_id)
  1899. if not request.all_missing:
  1900. # If not specifically asking for missing thumbnails, get all
  1901. pass
  1902. else:
  1903. query = query.where(LibraryFile.thumbnail_path.is_(None))
  1904. elif request.all_missing:
  1905. # All STL files without thumbnails
  1906. query = query.where(LibraryFile.thumbnail_path.is_(None))
  1907. else:
  1908. # No criteria specified - return empty
  1909. return BatchThumbnailResponse(
  1910. processed=0,
  1911. succeeded=0,
  1912. failed=0,
  1913. results=[],
  1914. )
  1915. result = await db.execute(query)
  1916. stl_files = result.scalars().all()
  1917. succeeded = 0
  1918. failed = 0
  1919. for stl_file in stl_files:
  1920. file_path = to_absolute_path(stl_file.file_path)
  1921. if not file_path or not file_path.exists():
  1922. results.append(
  1923. BatchThumbnailResult(
  1924. file_id=stl_file.id,
  1925. filename=stl_file.filename,
  1926. success=False,
  1927. error="File not found on disk",
  1928. )
  1929. )
  1930. failed += 1
  1931. continue
  1932. try:
  1933. thumbnail_path = generate_stl_thumbnail(file_path, thumbnails_dir)
  1934. if thumbnail_path:
  1935. # Update database with relative path
  1936. stl_file.thumbnail_path = to_relative_path(thumbnail_path)
  1937. await db.flush()
  1938. results.append(
  1939. BatchThumbnailResult(
  1940. file_id=stl_file.id,
  1941. filename=stl_file.filename,
  1942. success=True,
  1943. )
  1944. )
  1945. succeeded += 1
  1946. else:
  1947. results.append(
  1948. BatchThumbnailResult(
  1949. file_id=stl_file.id,
  1950. filename=stl_file.filename,
  1951. success=False,
  1952. error="Thumbnail generation failed",
  1953. )
  1954. )
  1955. failed += 1
  1956. except Exception as e:
  1957. logger.error("Failed to generate thumbnail for %s: %s", stl_file.filename, e)
  1958. results.append(
  1959. BatchThumbnailResult(
  1960. file_id=stl_file.id,
  1961. filename=stl_file.filename,
  1962. success=False,
  1963. error=str(e),
  1964. )
  1965. )
  1966. failed += 1
  1967. await db.commit()
  1968. return BatchThumbnailResponse(
  1969. processed=len(stl_files),
  1970. succeeded=succeeded,
  1971. failed=failed,
  1972. results=results,
  1973. )
  1974. # ============ Queue Operations ============
  1975. # NOTE: These routes must be defined BEFORE /files/{file_id} to avoid path parameter conflicts
  1976. def is_sliced_file(filename: str) -> bool:
  1977. """Check if a file is a sliced (printable) file.
  1978. Sliced files are:
  1979. - .gcode files
  1980. - .3mf files that contain '.gcode.' in the name (e.g., filename.gcode.3mf)
  1981. """
  1982. lower = filename.lower()
  1983. return lower.endswith(".gcode") or ".gcode." in lower
  1984. @router.post("/files/add-to-queue", response_model=AddToQueueResponse)
  1985. async def add_files_to_queue(
  1986. request: AddToQueueRequest,
  1987. db: AsyncSession = Depends(get_db),
  1988. _: User | None = Depends(require_permission_if_auth_enabled(Permission.QUEUE_CREATE)),
  1989. ):
  1990. """Add library files to the print queue.
  1991. Only sliced files (.gcode or .gcode.3mf) can be added to the queue.
  1992. The archive will be created automatically when the print starts.
  1993. """
  1994. added: list[AddToQueueResult] = []
  1995. errors: list[AddToQueueError] = []
  1996. # Get all requested files
  1997. result = await db.execute(LibraryFile.active().where(LibraryFile.id.in_(request.file_ids)))
  1998. files = {f.id: f for f in result.scalars().all()}
  1999. # Get max position for queue ordering
  2000. pos_result = await db.execute(select(func.coalesce(func.max(PrintQueueItem.position), 0)))
  2001. max_position = pos_result.scalar() or 0
  2002. for file_id in request.file_ids:
  2003. lib_file = files.get(file_id)
  2004. if not lib_file:
  2005. errors.append(AddToQueueError(file_id=file_id, filename="(not found)", error="File not found"))
  2006. continue
  2007. # Validate file is sliced
  2008. if not is_sliced_file(lib_file.filename):
  2009. errors.append(
  2010. AddToQueueError(
  2011. file_id=file_id,
  2012. filename=lib_file.filename,
  2013. error="Not a sliced file. Only .gcode or .gcode.3mf files can be printed.",
  2014. )
  2015. )
  2016. continue
  2017. try:
  2018. # Verify file exists on disk
  2019. file_path = Path(app_settings.base_dir) / lib_file.file_path
  2020. if not file_path.exists():
  2021. errors.append(
  2022. AddToQueueError(file_id=file_id, filename=lib_file.filename, error="File not found on disk")
  2023. )
  2024. continue
  2025. # Create queue item referencing library file (archive created at print start)
  2026. max_position += 1
  2027. queue_item = PrintQueueItem(
  2028. printer_id=None, # Unassigned
  2029. library_file_id=file_id,
  2030. position=max_position,
  2031. status="pending",
  2032. )
  2033. db.add(queue_item)
  2034. await db.flush() # Get queue_item.id
  2035. added.append(
  2036. AddToQueueResult(
  2037. file_id=file_id,
  2038. filename=lib_file.filename,
  2039. queue_item_id=queue_item.id,
  2040. )
  2041. )
  2042. except Exception as e:
  2043. logger.exception("Error adding file %s to queue", file_id)
  2044. errors.append(AddToQueueError(file_id=file_id, filename=lib_file.filename, error=str(e)))
  2045. await db.commit()
  2046. return AddToQueueResponse(added=added, errors=errors)
  2047. @router.get("/files/{file_id}/plates")
  2048. async def get_library_file_plates(
  2049. file_id: int,
  2050. db: AsyncSession = Depends(get_db),
  2051. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  2052. ):
  2053. """Get available plates from a multi-plate 3MF library file.
  2054. Returns a list of plates with their index, name, thumbnail availability,
  2055. and filament requirements. For single-plate exports, returns a single plate.
  2056. """
  2057. import json
  2058. import defusedxml.ElementTree as ET
  2059. # Get the library file
  2060. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2061. lib_file = result.scalar_one_or_none()
  2062. if not lib_file:
  2063. raise HTTPException(status_code=404, detail="File not found")
  2064. file_path = Path(app_settings.base_dir) / lib_file.file_path
  2065. if not file_path.exists():
  2066. raise HTTPException(status_code=404, detail="File not found on disk")
  2067. # Only 3MF files have plates
  2068. if not lib_file.filename.lower().endswith(".3mf"):
  2069. return {"file_id": file_id, "filename": lib_file.filename, "plates": [], "is_multi_plate": False}
  2070. plates = []
  2071. # Printer / process preset names the 3MF was prepared with — used by the
  2072. # SliceModal to default its dropdowns (#1325). Initialised here so the
  2073. # final return never raises NameError when the file isn't a valid zip.
  2074. embedded_presets: dict[str, str | None] = {"printer": None, "process": None}
  2075. try:
  2076. with zipfile.ZipFile(file_path, "r") as zf:
  2077. namelist = zf.namelist()
  2078. embedded_presets = extract_embedded_presets_from_3mf(zf)
  2079. # Find all plate gcode files to determine available plates
  2080. gcode_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".gcode")]
  2081. # If no gcode is present (source-only or unsliced), fall back to plate JSON/PNG
  2082. plate_indices: list[int] = []
  2083. if gcode_files:
  2084. # Extract plate indices from gcode filenames
  2085. for gf in gcode_files:
  2086. try:
  2087. plate_str = gf[15:-6] # Remove "Metadata/plate_" and ".gcode"
  2088. plate_indices.append(int(plate_str))
  2089. except ValueError:
  2090. pass # Skip gcode file with non-numeric plate index
  2091. else:
  2092. plate_json_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".json")]
  2093. plate_png_files = [
  2094. n
  2095. for n in namelist
  2096. if n.startswith("Metadata/plate_")
  2097. and n.endswith(".png")
  2098. and "_small" not in n
  2099. and "no_light" not in n
  2100. ]
  2101. plate_name_candidates = plate_json_files + plate_png_files
  2102. plate_re = re.compile(r"^Metadata/plate_(\d+)\.(json|png)$")
  2103. seen_indices: set[int] = set()
  2104. for name in plate_name_candidates:
  2105. match = plate_re.match(name)
  2106. if match:
  2107. try:
  2108. index = int(match.group(1))
  2109. except ValueError:
  2110. continue
  2111. if index in seen_indices:
  2112. continue
  2113. seen_indices.add(index)
  2114. plate_indices.append(index)
  2115. if not plate_indices:
  2116. # No plate metadata found
  2117. return {"file_id": file_id, "filename": lib_file.filename, "plates": [], "is_multi_plate": False}
  2118. plate_indices.sort()
  2119. # Parse model_settings.config for plate names + object assignments
  2120. plate_names = {}
  2121. plate_object_ids: dict[int, list[str]] = {}
  2122. object_names_by_id: dict[str, str] = {}
  2123. if "Metadata/model_settings.config" in namelist:
  2124. try:
  2125. model_content = zf.read("Metadata/model_settings.config").decode()
  2126. model_root = ET.fromstring(model_content)
  2127. for obj_elem in model_root.findall(".//object"):
  2128. obj_id = obj_elem.get("id")
  2129. if not obj_id:
  2130. continue
  2131. name_meta = obj_elem.find("metadata[@key='name']")
  2132. obj_name = name_meta.get("value") if name_meta is not None else None
  2133. if obj_name:
  2134. object_names_by_id[obj_id] = obj_name
  2135. for plate_elem in model_root.findall(".//plate"):
  2136. plater_id = None
  2137. plater_name = None
  2138. for meta in plate_elem.findall("metadata"):
  2139. key = meta.get("key")
  2140. value = meta.get("value")
  2141. if key == "plater_id" and value:
  2142. try:
  2143. plater_id = int(value)
  2144. except ValueError:
  2145. pass # Ignore plate with non-numeric plater_id
  2146. elif key == "plater_name" and value:
  2147. plater_name = value.strip()
  2148. if plater_id is not None and plater_name:
  2149. plate_names[plater_id] = plater_name
  2150. if plater_id is not None:
  2151. for instance_elem in plate_elem.findall("model_instance"):
  2152. for inst_meta in instance_elem.findall("metadata"):
  2153. if inst_meta.get("key") == "object_id":
  2154. obj_id = inst_meta.get("value")
  2155. if not obj_id:
  2156. continue
  2157. plate_object_ids.setdefault(plater_id, [])
  2158. if obj_id not in plate_object_ids[plater_id]:
  2159. plate_object_ids[plater_id].append(obj_id)
  2160. except Exception:
  2161. pass # model_settings.config is optional; skip if missing or malformed
  2162. # Parse slice_info.config for plate metadata
  2163. plate_metadata = {}
  2164. if "Metadata/slice_info.config" in namelist:
  2165. content = zf.read("Metadata/slice_info.config").decode()
  2166. root = ET.fromstring(content)
  2167. for plate_elem in root.findall(".//plate"):
  2168. plate_info = {"filaments": [], "prediction": None, "weight": None, "name": None, "objects": []}
  2169. plate_index = None
  2170. for meta in plate_elem.findall("metadata"):
  2171. key = meta.get("key")
  2172. value = meta.get("value")
  2173. if key == "index" and value:
  2174. try:
  2175. plate_index = int(value)
  2176. except ValueError:
  2177. pass # Ignore plate with non-numeric index
  2178. elif key == "prediction" and value:
  2179. try:
  2180. plate_info["prediction"] = int(value)
  2181. except ValueError:
  2182. pass # Leave prediction as None if not a valid integer
  2183. elif key == "weight" and value:
  2184. try:
  2185. plate_info["weight"] = float(value)
  2186. except ValueError:
  2187. pass # Leave weight as None if not a valid number
  2188. # Get filaments used in this plate
  2189. for filament_elem in plate_elem.findall("filament"):
  2190. filament_id = filament_elem.get("id")
  2191. filament_type = filament_elem.get("type", "")
  2192. filament_color = filament_elem.get("color", "")
  2193. used_g = filament_elem.get("used_g", "0")
  2194. used_m = filament_elem.get("used_m", "0")
  2195. try:
  2196. used_grams = float(used_g)
  2197. except (ValueError, TypeError):
  2198. used_grams = 0
  2199. if used_grams > 0 and filament_id:
  2200. plate_info["filaments"].append(
  2201. {
  2202. "slot_id": int(filament_id),
  2203. "type": filament_type,
  2204. "color": filament_color,
  2205. "used_grams": round(used_grams, 1),
  2206. "used_meters": float(used_m) if used_m else 0,
  2207. }
  2208. )
  2209. plate_info["filaments"].sort(key=lambda x: x["slot_id"])
  2210. # Collect object names
  2211. for obj_elem in plate_elem.findall("object"):
  2212. obj_name = obj_elem.get("name")
  2213. if obj_name and obj_name not in plate_info["objects"]:
  2214. plate_info["objects"].append(obj_name)
  2215. # Set plate name
  2216. if plate_index is not None:
  2217. custom_name = plate_names.get(plate_index)
  2218. if custom_name:
  2219. plate_info["name"] = custom_name
  2220. elif plate_info["objects"]:
  2221. plate_info["name"] = plate_info["objects"][0]
  2222. plate_metadata[plate_index] = plate_info
  2223. # Parse plate_*.json for object lists when slice_info is missing
  2224. plate_json_objects: dict[int, list[str]] = {}
  2225. for name in namelist:
  2226. match = re.match(r"^Metadata/plate_(\d+)\.json$", name)
  2227. if not match:
  2228. continue
  2229. try:
  2230. plate_index = int(match.group(1))
  2231. except ValueError:
  2232. continue
  2233. try:
  2234. payload = json.loads(zf.read(name).decode())
  2235. bbox_objects = payload.get("bbox_objects", [])
  2236. names: list[str] = []
  2237. for obj in bbox_objects:
  2238. obj_name = obj.get("name") if isinstance(obj, dict) else None
  2239. if obj_name and obj_name not in names:
  2240. names.append(obj_name)
  2241. if names:
  2242. plate_json_objects[plate_index] = names
  2243. except Exception:
  2244. continue
  2245. # Build plate list
  2246. for idx in plate_indices:
  2247. meta = plate_metadata.get(idx, {})
  2248. has_thumbnail = f"Metadata/plate_{idx}.png" in namelist
  2249. objects = meta.get("objects", [])
  2250. if not objects:
  2251. objects = plate_json_objects.get(idx, [])
  2252. if not objects and plate_object_ids.get(idx):
  2253. objects = [
  2254. object_names_by_id.get(obj_id, f"Object {obj_id}") for obj_id in plate_object_ids.get(idx, [])
  2255. ]
  2256. plate_name = meta.get("name")
  2257. if not plate_name:
  2258. plate_name = plate_names.get(idx)
  2259. if not plate_name and objects:
  2260. plate_name = objects[0]
  2261. plates.append(
  2262. {
  2263. "index": idx,
  2264. "name": plate_name,
  2265. "objects": objects,
  2266. "object_count": len(objects),
  2267. "has_thumbnail": has_thumbnail,
  2268. "thumbnail_url": f"/api/v1/library/files/{file_id}/plate-thumbnail/{idx}"
  2269. if has_thumbnail
  2270. else None,
  2271. "print_time_seconds": meta.get("prediction"),
  2272. "filament_used_grams": meta.get("weight"),
  2273. "filaments": meta.get("filaments", []),
  2274. }
  2275. )
  2276. except Exception as e:
  2277. logger.warning("Failed to parse plates from library file %s: %s", file_id, e)
  2278. return {
  2279. "file_id": file_id,
  2280. "filename": lib_file.filename,
  2281. "plates": plates,
  2282. "is_multi_plate": len(plates) > 1,
  2283. "embedded_printer": embedded_presets["printer"],
  2284. "embedded_process": embedded_presets["process"],
  2285. }
  2286. @router.get("/files/{file_id}/plate-thumbnail/{plate_index}")
  2287. async def get_library_file_plate_thumbnail(
  2288. file_id: int,
  2289. plate_index: int,
  2290. db: AsyncSession = Depends(get_db),
  2291. _: None = RequireCameraStreamTokenIfAuthEnabled,
  2292. ):
  2293. """Get the thumbnail image for a specific plate from a library file."""
  2294. from starlette.responses import Response
  2295. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2296. lib_file = result.scalar_one_or_none()
  2297. if not lib_file:
  2298. raise HTTPException(status_code=404, detail="File not found")
  2299. file_path = Path(app_settings.base_dir) / lib_file.file_path
  2300. if not file_path.exists():
  2301. raise HTTPException(status_code=404, detail="File not found on disk")
  2302. try:
  2303. with zipfile.ZipFile(file_path, "r") as zf:
  2304. thumb_path = f"Metadata/plate_{plate_index}.png"
  2305. if thumb_path in zf.namelist():
  2306. data = zf.read(thumb_path)
  2307. return Response(content=data, media_type="image/png")
  2308. except Exception:
  2309. pass # Archive unreadable or thumbnail missing; fall through to 404
  2310. raise HTTPException(status_code=404, detail=f"Thumbnail for plate {plate_index} not found")
  2311. async def _try_preview_slice_filaments(
  2312. db: AsyncSession,
  2313. *,
  2314. kind: str,
  2315. source_id: int,
  2316. plate_id: int,
  2317. file_path: Path,
  2318. request_id: str | None = None,
  2319. bundle_id: str | None = None,
  2320. printer_name: str | None = None,
  2321. process_name: str | None = None,
  2322. filament_names: list[str] | None = None,
  2323. ) -> list[dict] | None:
  2324. """Run a preview slice via the user's configured sidecar. Same shape as
  2325. the matching helper in archives.py — see that module for rationale.
  2326. ``request_id``: when supplied, forwarded to the sidecar so the
  2327. SliceModal's inline spinner + toast can poll the matching progress
  2328. endpoint and show "Generating G-code (45%)" for the preview as well.
  2329. ``bundle_id`` / ``printer_name`` / ``process_name`` / ``filament_names``:
  2330. when all are supplied, the preview uses ``slice_with_bundle`` against
  2331. the named bundle's preset triplet so the preview's gram numbers reflect
  2332. the same profiles the real print will use. Partial context falls back
  2333. to the embedded-settings path so a half-completed Bundle-tier selection
  2334. in the modal doesn't error out.
  2335. """
  2336. from backend.app.api.routes.settings import get_setting
  2337. from backend.app.services.slice_preview import get_preview_filaments
  2338. preferred = (await get_setting(db, "preferred_slicer")) or "bambu_studio"
  2339. if preferred == "orcaslicer":
  2340. configured = await get_setting(db, "orcaslicer_api_url")
  2341. api_url = (configured or app_settings.slicer_api_url).strip()
  2342. elif preferred == "bambu_studio":
  2343. configured = await get_setting(db, "bambu_studio_api_url")
  2344. api_url = (configured or app_settings.bambu_studio_api_url).strip()
  2345. else:
  2346. return None
  2347. if not api_url:
  2348. return None
  2349. try:
  2350. file_bytes = file_path.read_bytes()
  2351. except OSError:
  2352. return None
  2353. return await get_preview_filaments(
  2354. kind=kind,
  2355. source_id=source_id,
  2356. plate_id=plate_id,
  2357. file_bytes=file_bytes,
  2358. file_name=file_path.name,
  2359. api_url=api_url,
  2360. request_id=request_id,
  2361. bundle_id=bundle_id,
  2362. printer_name=printer_name,
  2363. process_name=process_name,
  2364. filament_names=filament_names,
  2365. )
  2366. @router.get("/files/{file_id}/filament-requirements")
  2367. async def get_library_file_filament_requirements(
  2368. file_id: int,
  2369. plate_id: int | None = None,
  2370. request_id: str | None = None,
  2371. bundle_id: str | None = None,
  2372. printer_name: str | None = None,
  2373. process_name: str | None = None,
  2374. filament_names: str | None = None,
  2375. db: AsyncSession = Depends(get_db),
  2376. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  2377. ):
  2378. """Get filament requirements from a library file.
  2379. Parses the 3MF file to extract filament slot IDs, types, colors, and usage.
  2380. This enables AMS slot assignment when printing from the file manager.
  2381. Args:
  2382. file_id: The library file ID
  2383. plate_id: Optional plate index to get filaments for a specific plate
  2384. bundle_id / printer_name / process_name / filament_names: Optional
  2385. bundle context. When all four are supplied, the preview slice
  2386. (run for unsliced project files) uses ``slice_with_bundle``
  2387. against the named preset triplet instead of the embedded-
  2388. settings fallback. ``filament_names`` is comma- or semicolon-
  2389. separated to mirror the slice route's multi-color form.
  2390. """
  2391. import defusedxml.ElementTree as ET
  2392. # Get the library file
  2393. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2394. lib_file = result.scalar_one_or_none()
  2395. if not lib_file:
  2396. raise HTTPException(status_code=404, detail="File not found")
  2397. # Get the full file path
  2398. file_path = Path(app_settings.base_dir) / lib_file.file_path
  2399. if not file_path.exists():
  2400. raise HTTPException(status_code=404, detail="File not found on disk")
  2401. # Only 3MF files have parseable filament info
  2402. if not lib_file.filename.lower().endswith(".3mf"):
  2403. return {"file_id": file_id, "filename": lib_file.filename, "plate_id": plate_id, "filaments": []}
  2404. filaments = []
  2405. try:
  2406. with zipfile.ZipFile(file_path, "r") as zf:
  2407. # Parse slice_info.config for filament requirements
  2408. if "Metadata/slice_info.config" in zf.namelist():
  2409. content = zf.read("Metadata/slice_info.config").decode()
  2410. root = ET.fromstring(content)
  2411. if plate_id is not None:
  2412. # Find filaments for specific plate
  2413. for plate_elem in root.findall(".//plate"):
  2414. # Check if this is the requested plate
  2415. plate_index = None
  2416. for meta in plate_elem.findall("metadata"):
  2417. if meta.get("key") == "index":
  2418. try:
  2419. plate_index = int(meta.get("value", ""))
  2420. except ValueError:
  2421. pass # Skip plate with non-numeric index value
  2422. break
  2423. if plate_index == plate_id:
  2424. # Extract filaments from this plate
  2425. for filament_elem in plate_elem.findall("filament"):
  2426. filament_id = filament_elem.get("id")
  2427. filament_type = filament_elem.get("type", "")
  2428. filament_color = filament_elem.get("color", "")
  2429. used_g = filament_elem.get("used_g", "0")
  2430. used_m = filament_elem.get("used_m", "0")
  2431. tray_info_idx = filament_elem.get("tray_info_idx", "")
  2432. try:
  2433. used_grams = float(used_g)
  2434. except (ValueError, TypeError):
  2435. used_grams = 0
  2436. if used_grams > 0 and filament_id:
  2437. filaments.append(
  2438. {
  2439. "slot_id": int(filament_id),
  2440. "type": filament_type,
  2441. "color": filament_color,
  2442. "used_grams": round(used_grams, 1),
  2443. "used_meters": float(used_m) if used_m else 0,
  2444. "tray_info_idx": tray_info_idx,
  2445. # Sliced output already pre-filtered by used_g>0,
  2446. # so every entry that survives is in fact used by
  2447. # this plate. Print-dispatch consumers ignore the
  2448. # flag; SliceModal uses it to enable/disable rows.
  2449. "used_in_plate": True,
  2450. }
  2451. )
  2452. break
  2453. else:
  2454. # Extract all filaments with used_g > 0 (for single-plate or overview)
  2455. for filament_elem in root.findall(".//filament"):
  2456. filament_id = filament_elem.get("id")
  2457. filament_type = filament_elem.get("type", "")
  2458. filament_color = filament_elem.get("color", "")
  2459. used_g = filament_elem.get("used_g", "0")
  2460. used_m = filament_elem.get("used_m", "0")
  2461. tray_info_idx = filament_elem.get("tray_info_idx", "")
  2462. try:
  2463. used_grams = float(used_g)
  2464. except (ValueError, TypeError):
  2465. used_grams = 0
  2466. if used_grams > 0 and filament_id:
  2467. filaments.append(
  2468. {
  2469. "slot_id": int(filament_id),
  2470. "type": filament_type,
  2471. "color": filament_color,
  2472. "used_grams": round(used_grams, 1),
  2473. "used_meters": float(used_m) if used_m else 0,
  2474. "tray_info_idx": tray_info_idx,
  2475. "used_in_plate": True,
  2476. }
  2477. )
  2478. # Unsliced project files: slice_info had no per-plate data.
  2479. # Return the FULL project_settings.config AMS slot list so
  2480. # the slicer CLI receives a profile for every project slot
  2481. # (otherwise it silently fills the gap from embedded
  2482. # defaults — surfaces as "I picked white but the print has
  2483. # grey" because the source's grey support filament leaks
  2484. # into the output). Use the preview slice to mark which
  2485. # slots the picked plate actually consumes; the SliceModal
  2486. # disables the unused rows so the user only interacts with
  2487. # the dropdowns that matter, while the backend still has
  2488. # the complete list to pass to the CLI.
  2489. if not filaments:
  2490. project_filaments = extract_project_filaments_from_3mf(zf)
  2491. used_slot_ids: set[int] = set()
  2492. if project_filaments and plate_id is not None:
  2493. # Bundle context flows through optional query params so
  2494. # callers without a Bundle-tier selection (the common
  2495. # case) hit the same path as before.
  2496. parsed_filament_names: list[str] | None = None
  2497. if filament_names:
  2498. parsed_filament_names = [
  2499. n.strip() for n in filament_names.replace(";", ",").split(",") if n.strip()
  2500. ] or None
  2501. preview = await _try_preview_slice_filaments(
  2502. db,
  2503. kind="library_file",
  2504. source_id=file_id,
  2505. plate_id=plate_id,
  2506. file_path=file_path,
  2507. request_id=request_id,
  2508. bundle_id=bundle_id,
  2509. printer_name=printer_name,
  2510. process_name=process_name,
  2511. filament_names=parsed_filament_names,
  2512. )
  2513. if preview is not None:
  2514. used_slot_ids = {f["slot_id"] for f in preview}
  2515. # Default to "every slot is used" when preview-slice
  2516. # didn't produce data: better to over-enable dropdowns
  2517. # than under-enable and have the user unable to pick a
  2518. # filament the plate actually uses.
  2519. fallback_all_used = not used_slot_ids
  2520. for f in project_filaments:
  2521. f["used_in_plate"] = fallback_all_used or f["slot_id"] in used_slot_ids
  2522. filaments = project_filaments
  2523. # Sort by slot ID
  2524. filaments.sort(key=lambda x: x["slot_id"])
  2525. # Enrich with nozzle mapping for dual-nozzle printers
  2526. nozzle_mapping = extract_nozzle_mapping_from_3mf(zf)
  2527. if nozzle_mapping:
  2528. for filament in filaments:
  2529. filament["nozzle_id"] = nozzle_mapping.get(filament["slot_id"])
  2530. except Exception as e:
  2531. logger.warning("Failed to parse filament requirements from library file %s: %s", file_id, e)
  2532. return {
  2533. "file_id": file_id,
  2534. "filename": lib_file.filename,
  2535. "plate_id": plate_id,
  2536. "filaments": filaments,
  2537. }
  2538. _STRIPPABLE_3MF_CONFIGS = frozenset(
  2539. {
  2540. # Settings dump used by --load-settings validation; the CLI tries to
  2541. # match its sentinel values (`prime_tower_brim_width: -1`, empty
  2542. # arrays) against the supplied profile and rejects out-of-range.
  2543. "Metadata/project_settings.config",
  2544. # Per-object settings overrides referencing the source plate's
  2545. # filament IDs / printer IDs. When the user picks a different
  2546. # printer / filament triplet, the IDs no longer resolve and the
  2547. # CLI exits non-zero on input validation.
  2548. "Metadata/model_settings.config",
  2549. # Slicer-version + plate-config + filament-mapping snapshot from
  2550. # the original slice. Includes the original printer model and
  2551. # filament references; mismatches against `--load-settings`
  2552. # consistently surfaced as `Slicer CLI failed (500)` for every
  2553. # 3MF in production. Removing it lets the CLI build a fresh slice
  2554. # plan from the supplied profile triplet.
  2555. "Metadata/slice_info.config",
  2556. # Multi-part / split-mesh metadata referencing object IDs from the
  2557. # original slice. Strip for the same reason — preserves the geometry
  2558. # in `3D/3dmodel.model` while dropping the orphan references.
  2559. "Metadata/cut_information.xml",
  2560. }
  2561. )
  2562. def _strip_3mf_embedded_settings(zip_bytes: bytes) -> bytes:
  2563. """Remove embedded slicer-config metadata from a 3MF.
  2564. Bambuddy supplies the slicer profile triplet via the sidecar's
  2565. ``--load-settings`` path; the 3MF's embedded settings would otherwise be
  2566. validated by the CLI first and can fail with sentinel-value range
  2567. checks (`prime_tower_brim_width: -1 not in range`, etc.) regardless of
  2568. what we pass via ``--load-settings``. Stripping the embedded configs
  2569. forces the CLI to use the supplied profiles only. Geometry
  2570. (``3D/3dmodel.model``), thumbnails, color, and multi-part data inside
  2571. the 3MF are preserved.
  2572. The set of strippable filenames is centralised in
  2573. ``_STRIPPABLE_3MF_CONFIGS`` — see that constant for the per-file
  2574. rationale. Project-settings alone wasn't enough: real-world Bambu
  2575. Studio 3MFs cross-reference printer / filament IDs from the other
  2576. metadata configs, and any single leftover triggered the validation
  2577. failure that made every profile-driven slice fall back to embedded
  2578. settings.
  2579. """
  2580. from io import BytesIO
  2581. src = BytesIO(zip_bytes)
  2582. dst = BytesIO()
  2583. with zipfile.ZipFile(src, "r") as zin, zipfile.ZipFile(dst, "w", zipfile.ZIP_DEFLATED) as zout:
  2584. for item in zin.infolist():
  2585. if item.filename in _STRIPPABLE_3MF_CONFIGS:
  2586. continue
  2587. zout.writestr(item, zin.read(item.filename))
  2588. return dst.getvalue()
  2589. # Keys in ``Metadata/project_settings.config`` that BambuStudio writes ``"-1"``
  2590. # to when the user wants the value inherited from the parent process preset.
  2591. # The CLI's ``StaticPrintConfig`` validator runs against the embedded settings
  2592. # *before* ``--load-settings`` overrides apply, so a sentinel ``"-1"`` trips
  2593. # the field's lower-bound range check and the CLI exits non-zero before our
  2594. # profile triplet is ever consulted (#1201 — MakerWorld P2S models).
  2595. #
  2596. # Allowlisted (rather than "strip every '-1' value") because some fields
  2597. # legitimately accept negative numbers (z_offset, translation values, etc.)
  2598. # and a blanket strip would silently corrupt those.
  2599. #
  2600. # Add new entries here as more reports surface — the slicer's error message
  2601. # names the offending field directly (`<field>: -1 not in range [...]`).
  2602. _PROJECT_SETTINGS_SENTINEL_KEYS = frozenset(
  2603. {
  2604. # Reported in #1201 (MakerWorld P2S 3MFs).
  2605. "raft_first_layer_expansion",
  2606. "tree_support_wall_count",
  2607. # Cited in the strip-experiment comment block above as a known sentinel
  2608. # case from earlier reports.
  2609. "prime_tower_brim_width",
  2610. }
  2611. )
  2612. def _sanitize_project_settings_sentinels(zip_bytes: bytes) -> bytes:
  2613. """Strip ``"-1"`` inherit-from-parent sentinels from the 3MF's
  2614. ``Metadata/project_settings.config`` so the slicer CLI's range validator
  2615. accepts the file (#1201).
  2616. Removes only allowlisted keys (see ``_PROJECT_SETTINGS_SENTINEL_KEYS``)
  2617. when their value is exactly ``"-1"``. The rest of the config — and every
  2618. other entry in the zip — is preserved byte-for-byte. Unlike the earlier
  2619. full-strip experiment (see ``_strip_3mf_embedded_settings`` and the
  2620. cautionary comment in ``_run_slicer_with_fallback``) this leaves
  2621. ``StaticPrintConfig`` initialisation intact: the file is still present,
  2622. still parses, and the slicer falls back to the supplied
  2623. ``--load-settings`` value for the removed key.
  2624. Returns the original bytes unchanged when no sanitisation is needed
  2625. (input isn't a valid zip, no ``project_settings.config``, no allowlisted
  2626. sentinels present, or any other parse failure) so the caller can pass
  2627. the result on without further checks.
  2628. """
  2629. from io import BytesIO
  2630. try:
  2631. with zipfile.ZipFile(BytesIO(zip_bytes), "r") as zin:
  2632. if "Metadata/project_settings.config" not in zin.namelist():
  2633. return zip_bytes
  2634. try:
  2635. config = json.loads(zin.read("Metadata/project_settings.config").decode("utf-8"))
  2636. except (json.JSONDecodeError, UnicodeDecodeError):
  2637. return zip_bytes
  2638. if not isinstance(config, dict):
  2639. return zip_bytes
  2640. removed = [key for key in _PROJECT_SETTINGS_SENTINEL_KEYS if config.get(key) == "-1"]
  2641. if not removed:
  2642. return zip_bytes
  2643. for key in removed:
  2644. config.pop(key, None)
  2645. patched = json.dumps(config)
  2646. logger.info(
  2647. "3MF sanitiser: removed sentinel '-1' for keys %s — slicer will use --load-settings defaults",
  2648. sorted(removed),
  2649. )
  2650. dst = BytesIO()
  2651. with zipfile.ZipFile(dst, "w", zipfile.ZIP_DEFLATED) as zout:
  2652. for item in zin.infolist():
  2653. if item.filename == "Metadata/project_settings.config":
  2654. zout.writestr(item, patched)
  2655. else:
  2656. zout.writestr(item, zin.read(item.filename))
  2657. return dst.getvalue()
  2658. except (zipfile.BadZipFile, OSError):
  2659. return zip_bytes
  2660. def _patch_process_bed_type(process_json: str, bed_type: str) -> str:
  2661. """Overwrite ``curr_bed_type`` in a process-profile JSON before forwarding
  2662. to the slicer sidecar.
  2663. The slicer CLI reads the build-plate type from the process profile's
  2664. ``curr_bed_type`` field. When the user picks a non-default plate in the
  2665. SliceModal (#1337), we patch the resolved JSON in place rather than
  2666. asking them to clone the preset just to switch a plate. Returns the
  2667. original string unchanged when the JSON can't be parsed or isn't a
  2668. dict — the slicer will then run with whatever the preset originally
  2669. specified, which is the safe fall-back path.
  2670. """
  2671. try:
  2672. profile = json.loads(process_json)
  2673. except json.JSONDecodeError:
  2674. logger.warning("Bed-type override skipped: process profile is not valid JSON")
  2675. return process_json
  2676. if not isinstance(profile, dict):
  2677. return process_json
  2678. profile["curr_bed_type"] = bed_type
  2679. return json.dumps(profile)
  2680. # The sidecar prefixes the slicer CLI's own error_string with this when the
  2681. # slicer ran and rejected the job (model off the bed, incompatible filament
  2682. # temps, range validation) — as opposed to the CLI crashing before it could
  2683. # evaluate the job at all.
  2684. _SLICER_REJECTION_MARKER = "Slicing failed with error from slicer:"
  2685. def _slicer_rejection_message(error_text: str) -> str | None:
  2686. """Extract the slicer's own rejection reason from a sidecar error string,
  2687. or ``None`` when the failure is not a slicer content rejection.
  2688. A content rejection means ``--load-settings`` *was* applied — the slicer
  2689. got far enough to evaluate the model against the chosen printer and say
  2690. no. Retrying with the 3MF's embedded settings would then only "succeed"
  2691. by silently reverting to the source file's original printer, masking the
  2692. real problem; such failures must reach the user instead.
  2693. """
  2694. if _SLICER_REJECTION_MARKER not in error_text:
  2695. return None
  2696. reason = error_text.split(_SLICER_REJECTION_MARKER, 1)[1]
  2697. # Trim the sidecar's trailing exit-code note and any stderr/stdout dump.
  2698. for cut in (": Slicer process failed", "\nstderr:", "\nstdout:"):
  2699. idx = reason.find(cut)
  2700. if idx != -1:
  2701. reason = reason[:idx]
  2702. return reason.strip() or None
  2703. async def _run_slicer_with_fallback(
  2704. db: AsyncSession,
  2705. *,
  2706. model_bytes: bytes,
  2707. model_filename: str,
  2708. request: SliceRequest,
  2709. current_user_id: int | None = None,
  2710. job_id: int | None = None,
  2711. ):
  2712. """Validate presets, dispatch to the right sidecar, run the slicer with
  2713. the auto-fallback for 3MF inputs whose `--load-settings` path crashes the
  2714. CLI. Returns ``(SliceResult, used_embedded_settings: bool)``. Raises
  2715. ``HTTPException`` for any caller-facing error.
  2716. `current_user_id` is needed to resolve **cloud** presets — the cloud token
  2717. is per-user when auth is enabled. For the legacy / local-only path it can
  2718. be left ``None``.
  2719. `job_id`: when set, a request_id is generated and a parallel poller
  2720. pushes the sidecar's --pipe-fed progress events onto
  2721. ``slice_dispatch.set_progress(job_id, ...)`` so the UI's persistent
  2722. toast can show "Generating G-code (75%)" instead of just elapsed
  2723. time. Pass None for synchronous routes that aren't tracked by the
  2724. dispatcher.
  2725. """
  2726. from backend.app.api.routes.settings import get_setting
  2727. from backend.app.services.preset_resolver import resolve_preset_ref
  2728. from backend.app.services.slicer_api import (
  2729. SlicerApiServerError,
  2730. SlicerApiService,
  2731. SlicerApiUnavailableError,
  2732. SlicerInputError,
  2733. )
  2734. # Bundle dispatch path: when SliceRequest.bundle is set, the schema
  2735. # validator short-circuited the presets-required check, so the
  2736. # PresetRef fields may all be None. Skip resolve_preset_ref entirely
  2737. # — the sidecar will materialise the per-category JSONs from the
  2738. # bundle's extracted directory at slice time.
  2739. use_bundle = request.bundle is not None
  2740. user: User | None = None
  2741. presets: dict[str, str] = {}
  2742. filament_jsons: list[str] = []
  2743. if not use_bundle:
  2744. # Resolve each slot via the source-aware resolver. The schema
  2745. # validator has already normalised legacy `*_preset_id: int`
  2746. # fields into `PresetRef(source='local', id=str(int))`, so all
  2747. # three are guaranteed non-None here.
  2748. if current_user_id is not None:
  2749. user = await db.get(User, current_user_id)
  2750. refs = {
  2751. "printer": request.printer_preset,
  2752. "process": request.process_preset,
  2753. }
  2754. for slot, ref in refs.items():
  2755. assert ref is not None, "schema validator guarantees PresetRef is set"
  2756. presets[slot] = await resolve_preset_ref(db, user, ref, slot)
  2757. # Multi-color: resolve each filament slot in plate order. The schema
  2758. # validator backfilled `filament_presets` from the legacy `filament_preset`
  2759. # field for single-color callers, so this list is always non-empty.
  2760. for ref in request.filament_presets:
  2761. assert ref is not None, "schema validator guarantees filament list is non-None"
  2762. filament_jsons.append(await resolve_preset_ref(db, user, ref, "filament"))
  2763. # Bed-type override (#1337): patch curr_bed_type onto the resolved
  2764. # process JSON so the slicer's StaticPrintConfig pass picks up the
  2765. # user's pick instead of whatever the process preset defaults to.
  2766. # Without this, slicing an STL of ABS onto a process preset whose
  2767. # default is "Cool Plate" fails with "Plate 1: Cool Plate does not
  2768. # support filament 1" — the reporter's exact scenario. Only applies
  2769. # to the resolved-preset path; bundle mode would need a sidecar-side
  2770. # mechanism to patch presets it materialises from disk.
  2771. if request.bed_type:
  2772. presets["process"] = _patch_process_bed_type(presets["process"], request.bed_type)
  2773. # Slicer routing — pick the sidecar URL by preferred_slicer.
  2774. # The per-install URL setting (Settings UI → Slicer card) wins; an
  2775. # empty value falls back to the SLICER_API_URL / BAMBU_STUDIO_API_URL
  2776. # env defaults defined in core/config.py.
  2777. preferred = (await get_setting(db, "preferred_slicer")) or "bambu_studio"
  2778. if preferred == "orcaslicer":
  2779. configured = await get_setting(db, "orcaslicer_api_url")
  2780. api_url = (configured or app_settings.slicer_api_url).strip()
  2781. elif preferred == "bambu_studio":
  2782. configured = await get_setting(db, "bambu_studio_api_url")
  2783. api_url = (configured or app_settings.bambu_studio_api_url).strip()
  2784. else:
  2785. raise HTTPException(
  2786. status_code=400,
  2787. detail=f"Unknown preferred_slicer setting: '{preferred}'. Expected 'orcaslicer' or 'bambu_studio'.",
  2788. )
  2789. # Note: an earlier version of this code stripped Metadata/project_settings.
  2790. # config + model_settings.config + slice_info.config + cut_information.xml
  2791. # before forwarding the 3MF, the theory being that --load-settings would
  2792. # then take precedence cleanly. That theory was wrong: model_settings.
  2793. # config carries the plate definitions the CLI needs to map `--slice N`
  2794. # to a real plate, and slice_info / project_settings supply baseline
  2795. # config the CLI's StaticPrintConfig pass needs at all. Stripping ANY
  2796. # of them caused the CLI to silently exit immediately after
  2797. # "Initializing StaticPrintConfigs" — exit code 0, no result.json, no
  2798. # stderr — which Node's child_process treated as failure and Bambuddy
  2799. # then masked by falling back to slice_without_profiles using the
  2800. # un-stripped bytes (and the source's embedded printer). Net effect:
  2801. # every 3MF slice with profiles silently produced wrong-printer output.
  2802. # Forwarding the original bytes lets --load-settings override the
  2803. # specific fields the user changed (printer/process/filament) while
  2804. # the embedded plate / model definitions remain intact.
  2805. is_3mf = model_filename.lower().endswith(".3mf")
  2806. primary_bytes = model_bytes
  2807. if is_3mf:
  2808. # Strip "-1" inherit-from-parent sentinels from
  2809. # Metadata/project_settings.config so the CLI's StaticPrintConfig
  2810. # range validator accepts the file (#1201). Surgical — keeps the
  2811. # config present, just removes the offending keys; the supplied
  2812. # --load-settings (and the fallback's embedded values for keys we
  2813. # didn't touch) still drive the slice.
  2814. primary_bytes = _sanitize_project_settings_sentinels(primary_bytes)
  2815. used_embedded_settings = False
  2816. service = SlicerApiService(api_url)
  2817. # #1493: cross-nozzle-class re-slice (single <-> dual). Without
  2818. # intervention the slicer rejects with either "G-code in unprintable
  2819. # area of multi-extruder printers" (the source's X1C-coordinate layout
  2820. # lands in the H2D's per-nozzle dead zone) or — worse — segfaults
  2821. # inside ZFiller's polygon clipping when the geometry pipeline trips
  2822. # on the cross-class transition. Forwarding the sidecar's --arrange
  2823. # flag for these cases lets BambuStudio reposition objects for the
  2824. # target bed and reconcile the embedded project_settings.config
  2825. # against the new printer, the same way the GUI's "Switch Printer"
  2826. # operation does. --arrange WILL reposition objects, so we only
  2827. # enable it on a true class crossing — same-printer slices keep the
  2828. # user's deliberate layout. The bed-type and arrange flags are
  2829. # orthogonal so this decision doesn't interact with the #1337 build-
  2830. # plate override.
  2831. cross_class_arrange = False
  2832. if is_3mf:
  2833. from backend.app.services.slicer_3mf_convert import (
  2834. extract_source_printer_model,
  2835. )
  2836. from backend.app.utils.printer_models import is_dual_nozzle_model
  2837. source_model = extract_source_printer_model(primary_bytes)
  2838. target_model = await _resolve_target_printer_model(db, user, request)
  2839. if source_model and target_model and is_dual_nozzle_model(source_model) != is_dual_nozzle_model(target_model):
  2840. logger.info(
  2841. "Cross-nozzle-class re-slice (%s -> %s, %s): enabling --arrange so BS reconciles "
  2842. "the embedded project layout against the target printer",
  2843. source_model,
  2844. target_model,
  2845. "bundle" if use_bundle else "presets",
  2846. )
  2847. cross_class_arrange = True
  2848. # When this slice is dispatcher-tracked, generate a request_id so
  2849. # the sidecar publishes progress under it, and wire a callback that
  2850. # forwards each frame onto SliceDispatchService.set_progress for the
  2851. # status-poll endpoint to surface to the UI.
  2852. progress_request_id: str | None = None
  2853. progress_callback = None
  2854. if job_id is not None:
  2855. from uuid import uuid4
  2856. from backend.app.services.slice_dispatch import slice_dispatch as _dispatch
  2857. progress_request_id = str(uuid4())
  2858. def _on_progress(snapshot: dict) -> None:
  2859. _dispatch.set_progress(job_id, snapshot)
  2860. progress_callback = _on_progress
  2861. # SliceModal lets the user pick a filament profile per slot, but each
  2862. # plate uses only a subset of the slots. The unused-slot dropdowns get
  2863. # whatever default the modal serves up — and a heterogeneous default
  2864. # (e.g. ABS in slot 2 next to a PLA in the used slot 1) makes
  2865. # BambuStudio reject the slice with "the temperature difference of
  2866. # the filaments used is too large" (exit 194) even though the G-code
  2867. # never touches the unused slot. Replace unused-slot entries with the
  2868. # slot-1 selection before the real slice so the loaded-filament set
  2869. # is materially homogeneous.
  2870. bundle_filament_names: list[str] | None = None
  2871. if is_3mf and request.plate is not None:
  2872. from backend.app.services.slicer_3mf_convert import substitute_unused_plate_filaments
  2873. if use_bundle:
  2874. assert request.bundle is not None
  2875. bundle_filament_names = substitute_unused_plate_filaments(
  2876. primary_bytes, request.plate, list(request.bundle.filament_names)
  2877. )
  2878. else:
  2879. filament_jsons = substitute_unused_plate_filaments(primary_bytes, request.plate, filament_jsons)
  2880. # Cross-class slice-all loop (#1493): when the user asks for
  2881. # ``plate=0`` (all plates) AND the source's nozzle class differs from
  2882. # the target's, ``--slice 0 --arrange 1`` consolidates every plate's
  2883. # objects onto a single target bed (BS's ``--arrange`` is project-
  2884. # wide) — either packing them all together or rejecting with "Some
  2885. # objects are located over the boundary of the heated bed" when
  2886. # nothing fits. Slice each plate independently with ``--arrange 1``
  2887. # and merge the per-plate outputs into one multi-plate 3MF instead.
  2888. # Same-class slice-all goes through the regular path below — the
  2889. # sidecar's native ``--slice 0`` produces the right shape directly.
  2890. use_cross_class_slice_all = cross_class_arrange and request.plate == 0 and request.export_3mf
  2891. try:
  2892. try:
  2893. if use_cross_class_slice_all:
  2894. from backend.app.services.slicer_3mf_convert import (
  2895. count_plates_in_3mf,
  2896. merge_plate_3mfs,
  2897. )
  2898. plate_count = count_plates_in_3mf(primary_bytes)
  2899. if plate_count == 0:
  2900. raise HTTPException(
  2901. status_code=400,
  2902. detail=(
  2903. "Couldn't read plate count from the source 3MF for cross-class "
  2904. "slice-all. The source may be malformed or missing "
  2905. "Metadata/model_settings.config."
  2906. ),
  2907. )
  2908. logger.info(
  2909. "Cross-class slice-all: looping over %d plates with --arrange per plate, then merging",
  2910. plate_count,
  2911. )
  2912. from backend.app.services.slicer_api import SliceResult
  2913. per_plate_results: list[tuple[int, SliceResult]] = []
  2914. # Forward the same progress request_id + callback to each
  2915. # per-plate sub-call so the toast keeps showing the
  2916. # sidecar's stage messages ("Generating G-code 45%…").
  2917. # The sub-calls run sequentially, so the poller for plate
  2918. # N is cancelled before plate N+1's poller starts — no
  2919. # cross-talk between plate streams. Wrap the callback to
  2920. # surface "(plate N/M)" alongside the slicer's stage
  2921. # message so the user sees progress through the whole
  2922. # multi-plate loop, not just one plate at a time.
  2923. def _wrap_progress_for_plate(plate_num: int, total: int):
  2924. if progress_callback is None:
  2925. return None
  2926. def _cb(snapshot: dict) -> None:
  2927. snapshot = dict(snapshot)
  2928. snapshot["multi_plate_index"] = plate_num
  2929. snapshot["multi_plate_count"] = total
  2930. progress_callback(snapshot)
  2931. return _cb
  2932. for plate_num in range(1, plate_count + 1):
  2933. plate_cb = _wrap_progress_for_plate(plate_num, plate_count)
  2934. if use_bundle:
  2935. assert request.bundle is not None
  2936. per_plate = await service.slice_with_bundle(
  2937. model_bytes=primary_bytes,
  2938. model_filename=model_filename,
  2939. bundle_id=request.bundle.bundle_id,
  2940. printer_name=request.bundle.printer_name,
  2941. process_name=request.bundle.process_name,
  2942. filament_names=(
  2943. bundle_filament_names
  2944. if bundle_filament_names is not None
  2945. else request.bundle.filament_names
  2946. ),
  2947. plate=plate_num,
  2948. export_3mf=True,
  2949. arrange=True,
  2950. bed_type=request.bed_type,
  2951. request_id=progress_request_id,
  2952. on_progress=plate_cb,
  2953. )
  2954. else:
  2955. per_plate = await service.slice_with_profiles(
  2956. model_bytes=primary_bytes,
  2957. model_filename=model_filename,
  2958. printer_profile_json=presets["printer"],
  2959. process_profile_json=presets["process"],
  2960. filament_profile_jsons=filament_jsons,
  2961. plate=plate_num,
  2962. export_3mf=True,
  2963. arrange=True,
  2964. request_id=progress_request_id,
  2965. on_progress=plate_cb,
  2966. )
  2967. per_plate_results.append((plate_num, per_plate))
  2968. # Merge the N single-plate 3MFs into one multi-plate 3MF.
  2969. # ``primary_bytes`` is the source 3MF: it carries the
  2970. # original per-plate previews the slicer's --arrange
  2971. # pass doesn't regenerate, so the merger can fall back
  2972. # to those for each plate's cover image.
  2973. merged_bytes = merge_plate_3mfs(
  2974. [(n, r.content) for n, r in per_plate_results],
  2975. source_3mf_bytes=primary_bytes,
  2976. )
  2977. # Synthetic SliceResult: totals are the sum of each
  2978. # plate's so the archive card shows the project's print
  2979. # time and filament use, not just plate 1's.
  2980. result = SliceResult(
  2981. content=merged_bytes,
  2982. print_time_seconds=sum(r.print_time_seconds for _, r in per_plate_results),
  2983. filament_used_g=sum(r.filament_used_g for _, r in per_plate_results),
  2984. filament_used_mm=sum(r.filament_used_mm for _, r in per_plate_results),
  2985. )
  2986. elif use_bundle:
  2987. # Bundle dispatch: sidecar materialises the JSON triplet
  2988. # from the stored .bbscfg by name. ``request.bundle`` is
  2989. # guaranteed non-None here by the use_bundle branch above.
  2990. assert request.bundle is not None
  2991. result = await service.slice_with_bundle(
  2992. model_bytes=primary_bytes,
  2993. model_filename=model_filename,
  2994. bundle_id=request.bundle.bundle_id,
  2995. printer_name=request.bundle.printer_name,
  2996. process_name=request.bundle.process_name,
  2997. filament_names=bundle_filament_names
  2998. if bundle_filament_names is not None
  2999. else request.bundle.filament_names,
  3000. plate=request.plate,
  3001. export_3mf=request.export_3mf,
  3002. arrange=cross_class_arrange,
  3003. bed_type=request.bed_type,
  3004. request_id=progress_request_id,
  3005. on_progress=progress_callback,
  3006. )
  3007. else:
  3008. result = await service.slice_with_profiles(
  3009. model_bytes=primary_bytes,
  3010. model_filename=model_filename,
  3011. printer_profile_json=presets["printer"],
  3012. process_profile_json=presets["process"],
  3013. filament_profile_jsons=filament_jsons,
  3014. plate=request.plate,
  3015. export_3mf=request.export_3mf,
  3016. arrange=cross_class_arrange,
  3017. request_id=progress_request_id,
  3018. on_progress=progress_callback,
  3019. )
  3020. except SlicerApiServerError as exc:
  3021. rejection = _slicer_rejection_message(str(exc))
  3022. if rejection:
  3023. # The slicer ran and rejected the job for a content reason —
  3024. # the chosen printer/process/filament *were* applied. Falling
  3025. # back to embedded settings would silently re-slice for the
  3026. # source 3MF's original printer and hide the real problem
  3027. # (e.g. re-slicing an H2D model for an X1C: the object is off
  3028. # the smaller bed). Surface the slicer's reason instead.
  3029. raise HTTPException(status_code=400, detail=rejection) from exc
  3030. if not is_3mf:
  3031. raise
  3032. logger.warning(
  3033. "Slicer CLI failed on the --load-settings path for %s (%s); retrying with embedded settings",
  3034. model_filename,
  3035. exc,
  3036. )
  3037. # Forward the same request_id + callback so the toast's live
  3038. # progress keeps updating across the fallback retry instead
  3039. # of going blank for the rest of the slice. Use the sanitised
  3040. # bytes — the embedded-settings path also reads the same
  3041. # project_settings.config and the same range validator runs
  3042. # there too, so without sanitisation the fallback would die
  3043. # on the same sentinel error (#1201). Same fallback applies
  3044. # to the bundle path: if the resolved triplet crashes the CLI,
  3045. # embedded settings give the user *something* rather than a
  3046. # hard failure (the SliceModal flags the difference via
  3047. # used_embedded_settings).
  3048. result = await service.slice_without_profiles(
  3049. model_bytes=primary_bytes,
  3050. model_filename=model_filename,
  3051. plate=request.plate,
  3052. export_3mf=request.export_3mf,
  3053. request_id=progress_request_id,
  3054. on_progress=progress_callback,
  3055. )
  3056. used_embedded_settings = True
  3057. except SlicerInputError as exc:
  3058. raise HTTPException(status_code=400, detail=str(exc)) from exc
  3059. except SlicerApiServerError as exc:
  3060. raise HTTPException(status_code=502, detail=str(exc)) from exc
  3061. except SlicerApiUnavailableError as exc:
  3062. raise HTTPException(status_code=502, detail=str(exc)) from exc
  3063. finally:
  3064. await service.close()
  3065. return result, used_embedded_settings
  3066. def _canonical_printer_model(raw: str | None) -> str | None:
  3067. """Normalise a printer-preset name / ``printer_model`` field to a canonical
  3068. model code. Strips the BambuStudio ``"# "`` user-clone prefix and the
  3069. ``" 0.4 nozzle"`` variant suffix that preset names carry but bare model
  3070. names don't — without this, ``"Bambu Lab H2D 0.4 nozzle"`` wouldn't
  3071. normalise to ``H2D``."""
  3072. import re
  3073. from backend.app.utils.printer_models import normalize_printer_model
  3074. if not raw:
  3075. return None
  3076. cleaned = str(raw).strip()
  3077. if cleaned.startswith("# "):
  3078. cleaned = cleaned[2:].strip()
  3079. cleaned = re.sub(r"\s+0\.\d+\s+nozzle$", "", cleaned, flags=re.IGNORECASE)
  3080. return normalize_printer_model(cleaned) if cleaned else None
  3081. async def _resolve_target_printer_model(db: AsyncSession, user: User | None, request: SliceRequest) -> str | None:
  3082. """Best-effort: the printer model a slice request targets.
  3083. Returns ``None`` when it can't be determined (the nozzle-class guard
  3084. then simply doesn't fire — fail-open, never blocks a slice spuriously).
  3085. """
  3086. from backend.app.services.preset_resolver import resolve_preset_ref
  3087. if request.bundle is not None:
  3088. return _canonical_printer_model(request.bundle.printer_name)
  3089. if request.printer_preset is None:
  3090. return None
  3091. try:
  3092. printer_json = await resolve_preset_ref(db, user, request.printer_preset, "printer")
  3093. data = json.loads(printer_json)
  3094. if not isinstance(data, dict):
  3095. return None
  3096. return _canonical_printer_model(
  3097. data.get("printer_model") or data.get("printer_settings_id") or data.get("name")
  3098. )
  3099. except Exception:
  3100. return None
  3101. async def guard_nozzle_class_reslice(
  3102. db: AsyncSession, user: User | None, request: SliceRequest, source_model: str | None
  3103. ) -> None:
  3104. """No-op guard, retained for call-site compatibility.
  3105. Cross-nozzle-class re-slicing is handled by ``_run_slicer_with_fallback``'s
  3106. two-pass conversion (#1493): a 1mm cube is sliced with the target triplet
  3107. (via either ``slice_with_profiles`` or ``slice_with_bundle``, whichever
  3108. dispatch mode the caller is using) to produce a fresh target-shaped
  3109. ``Metadata/project_settings.config``, which is then spliced into the
  3110. source 3MF before the real slice. So this guard never needs to block
  3111. anymore — both preset and bundle paths are covered.
  3112. The function and its call sites in ``archives.py`` / the library re-slice
  3113. route are kept so external pinned-version forks and downstream patches
  3114. don't break, but it does nothing on a successful slice path. If the
  3115. two-pass conversion fails inside the slicer, the existing
  3116. ``SlicerApiServerError`` / ``_slicer_rejection_message`` plumbing
  3117. surfaces the CLI's actual error to the user — which is more informative
  3118. than the old "isn't supported yet" 400 the guard used to raise.
  3119. """
  3120. return None
  3121. async def slice_and_persist(
  3122. db: AsyncSession,
  3123. *,
  3124. model_bytes: bytes,
  3125. model_filename: str,
  3126. folder_id: int | None,
  3127. extra_metadata: dict | None,
  3128. request: SliceRequest,
  3129. current_user_id: int | None,
  3130. job_id: int | None = None,
  3131. ) -> SliceResponse:
  3132. """Slice a model and save the result as a new ``LibraryFile`` in
  3133. ``folder_id`` (same folder as the source by convention).
  3134. Always exports as ``.gcode.3mf`` so the existing library thumbnail
  3135. pipeline works on the new file. Plain ``.gcode`` would have no
  3136. embedded thumbnail to extract.
  3137. """
  3138. from backend.app.services.archive import ThreeMFParser
  3139. library_request = request.model_copy(update={"export_3mf": True})
  3140. result, used_embedded_settings = await _run_slicer_with_fallback(
  3141. db,
  3142. model_bytes=model_bytes,
  3143. model_filename=model_filename,
  3144. request=library_request,
  3145. current_user_id=current_user_id,
  3146. job_id=job_id,
  3147. )
  3148. base_name = model_filename.rsplit(".", 1)[0]
  3149. out_filename = f"{base_name}.gcode.3mf"
  3150. unique_name = f"{uuid.uuid4().hex}.gcode.3mf"
  3151. out_path = get_library_files_dir() / unique_name # SEC-PATH-OK: unique_name = uuid.uuid4().hex + ".gcode.3mf"
  3152. out_path.write_bytes(result.content)
  3153. # Extract thumbnail from the produced 3MF so the library card shows a
  3154. # preview. Failures here aren't fatal — the file is still useful
  3155. # without a thumbnail.
  3156. thumbnail_relative: str | None = None
  3157. parsed_metadata: dict = {}
  3158. try:
  3159. parser = ThreeMFParser(str(out_path))
  3160. parsed = parser.parse()
  3161. thumb_data = parsed.get("_thumbnail_data")
  3162. thumb_ext = parsed.get("_thumbnail_ext", ".png")
  3163. if thumb_data:
  3164. thumb_filename = f"{uuid.uuid4().hex}{thumb_ext}"
  3165. thumb_path = get_library_thumbnails_dir() / thumb_filename
  3166. thumb_path.write_bytes(thumb_data)
  3167. thumbnail_relative = to_relative_path(thumb_path)
  3168. cleaned = _clean_3mf_metadata(parsed)
  3169. if isinstance(cleaned, dict):
  3170. parsed_metadata = cleaned
  3171. except Exception as exc:
  3172. logger.warning("Failed to parse sliced 3MF metadata for %s: %s", out_filename, exc)
  3173. # Drop the embedded `print_name` (see _without_print_name) so the sliced
  3174. # row's display falls back to its ".gcode.3mf" filename instead of the
  3175. # source file's project title, which would make the two indistinguishable.
  3176. metadata: dict = dict(_without_print_name(parsed_metadata) or {})
  3177. # Some slicer-sidecar builds leave the X-Filament-Used-* response headers
  3178. # unset, so result.filament_used_g/_mm arrive as 0 even for a real
  3179. # multi-hour print. Fall back to the totals ThreeMFParser read from the
  3180. # produced 3MF's own G-code header.
  3181. filament_g = result.filament_used_g or parsed_metadata.get("filament_used_grams") or 0.0
  3182. filament_mm = result.filament_used_mm or parsed_metadata.get("filament_used_mm") or 0.0
  3183. metadata.update(
  3184. {
  3185. "print_time_seconds": result.print_time_seconds,
  3186. "filament_used_g": filament_g,
  3187. "filament_used_mm": filament_mm,
  3188. }
  3189. )
  3190. if used_embedded_settings:
  3191. metadata["used_embedded_settings"] = True
  3192. if extra_metadata:
  3193. metadata.update(extra_metadata)
  3194. new_file = LibraryFile(
  3195. folder_id=folder_id,
  3196. filename=out_filename,
  3197. file_path=to_relative_path(out_path),
  3198. # Sliced output is a `.gcode.3mf` zip with embedded G-code, but the
  3199. # user-facing meaning is "ready-to-print G-code" — using "gcode"
  3200. # gives it the same badge as plain .gcode files and distinguishes
  3201. # it from un-sliced `.3mf` source models.
  3202. file_type="gcode",
  3203. file_size=len(result.content),
  3204. file_hash=hashlib.sha256(result.content).hexdigest(),
  3205. thumbnail_path=thumbnail_relative,
  3206. file_metadata=metadata,
  3207. source_type="sliced",
  3208. created_by_id=current_user_id,
  3209. )
  3210. db.add(new_file)
  3211. await db.commit()
  3212. # No refresh: expire_on_commit=False keeps id/filename accessible, and
  3213. # refreshing here flakes under pytest-xdist when teardown of a sibling
  3214. # test races the SELECT.
  3215. return SliceResponse(
  3216. library_file_id=new_file.id,
  3217. name=new_file.filename,
  3218. print_time_seconds=result.print_time_seconds,
  3219. filament_used_g=filament_g,
  3220. filament_used_mm=filament_mm,
  3221. used_embedded_settings=used_embedded_settings,
  3222. )
  3223. async def slice_and_persist_as_archive(
  3224. db: AsyncSession,
  3225. *,
  3226. model_bytes: bytes,
  3227. model_filename: str,
  3228. request: SliceRequest,
  3229. source_archive, # PrintArchive — hint kept loose to avoid cyclic import
  3230. current_user_id: int | None,
  3231. job_id: int | None = None,
  3232. ):
  3233. """Slice a model and save the result as a new ``PrintArchive`` row,
  3234. inheriting printer / project / makerworld metadata from the source
  3235. archive. Always exports as a `.gcode.3mf` so the existing thumbnail
  3236. and plates infrastructure (which expects a zip-shaped 3MF) works on
  3237. the new archive. Returns ``SliceArchiveResponse``.
  3238. """
  3239. from backend.app.models.archive import PrintArchive
  3240. from backend.app.schemas.slicer import SliceArchiveResponse
  3241. from backend.app.services.archive import ThreeMFParser
  3242. # Archive sinks always want a 3MF. The library route still respects the
  3243. # caller's `export_3mf` flag; here we override.
  3244. archive_request = request.model_copy(update={"export_3mf": True})
  3245. result, used_embedded_settings = await _run_slicer_with_fallback(
  3246. db,
  3247. model_bytes=model_bytes,
  3248. model_filename=model_filename,
  3249. request=archive_request,
  3250. job_id=job_id,
  3251. current_user_id=current_user_id,
  3252. )
  3253. base_name = model_filename.rsplit(".", 1)[0]
  3254. out_filename = f"{base_name}.gcode.3mf"
  3255. timestamp = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S")
  3256. printer_folder = str(source_archive.printer_id) if source_archive.printer_id is not None else "unassigned"
  3257. archive_subdir = f"{timestamp}_{base_name}_sliced"
  3258. archive_dir = (
  3259. app_settings.archive_dir / printer_folder / archive_subdir
  3260. ) # SEC-PATH-OK: printer_folder = str(int|None), archive_subdir = f"{timestamp}_{base_name}_sliced" where base_name went through _safe_filename
  3261. archive_dir.mkdir(parents=True, exist_ok=True)
  3262. out_path = (
  3263. archive_dir / out_filename
  3264. ) # SEC-PATH-OK: out_filename = f"{base_name}.gcode.3mf" where base_name went through _safe_filename
  3265. out_path.write_bytes(result.content)
  3266. # Extract a thumbnail for the new archive card. Priority order:
  3267. # 1. Source archive's ``Metadata/plate_{N}.png`` — the GUI-rendered
  3268. # preview of the same plate the user is re-slicing. Closer to
  3269. # "what's actually printing" than any other available image
  3270. # (with --arrange the layout may differ slightly, but objects
  3271. # and colours match).
  3272. # 2. ``ThreeMFParser`` fallback chain on the sliced output: the
  3273. # slicer's own per-plate render if it wrote one, then the
  3274. # project-wide thumbnail under ``Auxiliaries/.thumbnails/``.
  3275. # BambuStudio CLI frequently doesn't emit a fresh per-plate render
  3276. # (slice writes the new gcode but leaves the preview slot empty),
  3277. # so without (1) the card falls all the way through to the
  3278. # MakerWorld-style cover art — visually unrelated to what the user
  3279. # picked, see #1493 follow-up. Failures don't fail the slice — the
  3280. # archive row is still useful without a thumbnail.
  3281. plate_num = request.plate or 1
  3282. thumbnail_path: str | None = None
  3283. parsed_metadata: dict = {}
  3284. src_3mf_path = app_settings.base_dir / source_archive.file_path
  3285. source_plate_bytes = _read_3mf_entry(src_3mf_path, f"Metadata/plate_{plate_num}.png")
  3286. if source_plate_bytes:
  3287. thumb_dest = archive_dir / "thumbnail.png"
  3288. thumb_dest.write_bytes(source_plate_bytes)
  3289. thumbnail_path = str(thumb_dest.relative_to(app_settings.base_dir))
  3290. try:
  3291. parser = ThreeMFParser(str(out_path), plate_number=plate_num)
  3292. parsed = parser.parse()
  3293. if thumbnail_path is None:
  3294. thumb_data = parsed.get("_thumbnail_data")
  3295. thumb_ext = parsed.get("_thumbnail_ext", ".png")
  3296. if thumb_data:
  3297. thumb_dest = archive_dir / f"thumbnail{thumb_ext}"
  3298. thumb_dest.write_bytes(thumb_data)
  3299. thumbnail_path = str(thumb_dest.relative_to(app_settings.base_dir))
  3300. parsed_metadata = {k: v for k, v in parsed.items() if not k.startswith("_")}
  3301. except Exception as exc:
  3302. logger.warning("Failed to parse sliced 3MF metadata for %s: %s", out_filename, exc)
  3303. metadata = dict(source_archive.extra_data) if source_archive.extra_data else {}
  3304. metadata.update(parsed_metadata)
  3305. # Fall back to the produced 3MF's G-code-header totals when the sidecar
  3306. # leaves the X-Filament-Used-* headers unset (result.filament_used_g == 0
  3307. # even for a real multi-hour print).
  3308. filament_g = result.filament_used_g or parsed_metadata.get("filament_used_grams") or 0.0
  3309. filament_mm = result.filament_used_mm or parsed_metadata.get("filament_used_mm") or 0.0
  3310. metadata.update(
  3311. {
  3312. "sliced_from_archive_id": source_archive.id,
  3313. "print_time_seconds": result.print_time_seconds,
  3314. "filament_used_g": filament_g,
  3315. "filament_used_mm": filament_mm,
  3316. }
  3317. )
  3318. if used_embedded_settings:
  3319. metadata["used_embedded_settings"] = True
  3320. # Prefer the actually-used filament list from the sliced output's
  3321. # slice_info.config (parsed_metadata.filament_* — only entries with
  3322. # used_g > 0). Falling back to the source_archive's list would
  3323. # surface every project-wide AMS slot, including ones the picked
  3324. # plate doesn't use (16+ swatches on the card for a 2-color print).
  3325. new_filament_type = parsed_metadata.get("filament_type") or source_archive.filament_type
  3326. new_filament_color = parsed_metadata.get("filament_color") or source_archive.filament_color
  3327. # When the user re-slices for a different printer model than the source,
  3328. # the source's printer_id (e.g. an H2D's "Workshop H2C") no longer
  3329. # represents where the new archive can be reprinted. The archive card
  3330. # and reprint modal both read printer_id first and only fall back to
  3331. # sliced_for_model when it's None, so leaving the inherited id makes
  3332. # the X1C-sliced card display the source H2D's printer name.
  3333. # Same pitfall as the sliced_for_model copy a few lines below.
  3334. new_target_model = parsed_metadata.get("sliced_for_model") or source_archive.sliced_for_model
  3335. is_cross_model_reslice = (
  3336. new_target_model is not None
  3337. and source_archive.sliced_for_model is not None
  3338. and new_target_model != source_archive.sliced_for_model
  3339. )
  3340. new_printer_id = None if is_cross_model_reslice else source_archive.printer_id
  3341. new_archive = PrintArchive(
  3342. printer_id=new_printer_id,
  3343. project_id=source_archive.project_id,
  3344. filename=out_filename,
  3345. file_path=str(out_path.relative_to(app_settings.base_dir)),
  3346. file_size=len(result.content),
  3347. content_hash=hashlib.sha256(result.content).hexdigest(),
  3348. thumbnail_path=thumbnail_path,
  3349. # Inherit identity from the source archive so the new entry shows
  3350. # up alongside its sibling in the archives list.
  3351. print_name=(source_archive.print_name or base_name) + " (re-sliced)",
  3352. print_time_seconds=result.print_time_seconds,
  3353. filament_used_grams=filament_g or None,
  3354. filament_type=new_filament_type,
  3355. filament_color=new_filament_color,
  3356. layer_height=source_archive.layer_height,
  3357. nozzle_diameter=source_archive.nozzle_diameter,
  3358. # The re-sliced output is for whatever printer the user just picked,
  3359. # not the source archive's printer — read the model the slicer baked
  3360. # into the new 3MF, falling back to the source only if it's absent.
  3361. # (Copying source_archive.sliced_for_model kept a cross-printer
  3362. # re-slice, e.g. X1C→H2D, showing the old "X1C sliced" model.)
  3363. sliced_for_model=parsed_metadata.get("sliced_for_model") or source_archive.sliced_for_model,
  3364. # Build plate type that the sliced output was produced for (#1493
  3365. # follow-up): the frontend's ArchiveCard reads ``archive.bed_type``
  3366. # off the top-level column, not extra_data, so without this lift the
  3367. # re-sliced card had no plate badge. ThreeMFParser pulls it from the
  3368. # sliced 3MF's ``slice_info.config`` ``curr_bed_type``; if that's
  3369. # absent (older sidecar / older slice profile) the source archive's
  3370. # bed_type is the right default.
  3371. bed_type=parsed_metadata.get("bed_type") or source_archive.bed_type,
  3372. makerworld_url=source_archive.makerworld_url,
  3373. designer=source_archive.designer,
  3374. # Sliced-but-not-printed: keep status default ("completed") so it
  3375. # surfaces in the normal archives list, but do not stamp
  3376. # started/completed_at — the user hasn't actually printed it yet.
  3377. extra_data=metadata,
  3378. created_by_id=current_user_id,
  3379. )
  3380. db.add(new_archive)
  3381. await db.commit()
  3382. await db.refresh(new_archive)
  3383. return SliceArchiveResponse(
  3384. archive_id=new_archive.id,
  3385. name=new_archive.print_name or out_filename,
  3386. print_time_seconds=result.print_time_seconds,
  3387. filament_used_g=filament_g,
  3388. filament_used_mm=filament_mm,
  3389. used_embedded_settings=used_embedded_settings,
  3390. )
  3391. @router.post("/files/{file_id}/slice", status_code=202)
  3392. async def slice_library_file(
  3393. file_id: int,
  3394. request: SliceRequest,
  3395. db: AsyncSession = Depends(get_db),
  3396. current_user: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  3397. api_key_cloud_owner: User | None = Depends(resolve_api_key_cloud_owner),
  3398. ):
  3399. """Enqueue a slice job for a library file. Returns 202 + job_id; the
  3400. slice runs in the background, the caller polls `GET /slice-jobs/{id}`.
  3401. """
  3402. from backend.app.core.database import async_session
  3403. from backend.app.services.slice_dispatch import (
  3404. http_exception_to_job_error,
  3405. slice_dispatch,
  3406. )
  3407. src_result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3408. lib_file = src_result.scalar_one_or_none()
  3409. if not lib_file:
  3410. raise HTTPException(status_code=404, detail="File not found")
  3411. src_lower = (lib_file.filename or "").lower()
  3412. if not (
  3413. src_lower.endswith(".stl")
  3414. or src_lower.endswith(".3mf")
  3415. or src_lower.endswith(".step")
  3416. or src_lower.endswith(".stp")
  3417. ):
  3418. raise HTTPException(status_code=400, detail="Source file must be STL, 3MF, or STEP")
  3419. src_path = Path(app_settings.base_dir) / lib_file.file_path
  3420. if not src_path.exists():
  3421. raise HTTPException(status_code=404, detail="Source file missing on disk")
  3422. # Capture inputs the bg task needs — the request DB session is closed
  3423. # before the background task runs.
  3424. model_bytes = src_path.read_bytes()
  3425. folder_id = lib_file.folder_id
  3426. source_lib_file_id = lib_file.id
  3427. # API-keyed callers get None from the auth gate (auth.py keeps that
  3428. # behaviour to avoid a wider scope expansion). Fall back to the API
  3429. # key's owner so cloud-preset resolution can read the stored
  3430. # cloud_token (#1182 follow-up).
  3431. cloud_token_user = current_user or api_key_cloud_owner
  3432. user_id = cloud_token_user.id if cloud_token_user else None
  3433. # If the source has a `print_name` in its metadata (BambuStudio always
  3434. # sets this; OrcaSlicer often leaves it blank), derive the sliced
  3435. # output's filename from it instead of the raw filename. The source
  3436. # row's display already prefers print_name, so the sliced row's
  3437. # filename ("Piggo the piggy bank.gcode.3mf") will match the source's
  3438. # display name ("Piggo the piggy bank") with the gcode extension added.
  3439. src_print_name = None
  3440. if lib_file.file_metadata:
  3441. candidate = lib_file.file_metadata.get("print_name")
  3442. if isinstance(candidate, str) and candidate.strip():
  3443. src_print_name = candidate.strip()
  3444. src_ext = Path(lib_file.filename).suffix.lower() or ".3mf"
  3445. model_filename = f"{src_print_name}{src_ext}" if src_print_name else lib_file.filename
  3446. # Block a cross-nozzle-class re-slice (single-nozzle <-> H2D) up front.
  3447. # Fires only when the source is itself a sliced file (carries
  3448. # sliced_for_model); a plain un-sliced model has no source nozzle class.
  3449. await guard_nozzle_class_reslice(
  3450. db,
  3451. cloud_token_user,
  3452. request,
  3453. (lib_file.file_metadata or {}).get("sliced_for_model"),
  3454. )
  3455. async def _run(job_id: int):
  3456. async with async_session() as task_db:
  3457. try:
  3458. response = await slice_and_persist(
  3459. task_db,
  3460. model_bytes=model_bytes,
  3461. model_filename=model_filename,
  3462. folder_id=folder_id,
  3463. extra_metadata={"sliced_from_library_file_id": source_lib_file_id},
  3464. request=request,
  3465. current_user_id=user_id,
  3466. job_id=job_id,
  3467. )
  3468. except HTTPException as exc:
  3469. raise http_exception_to_job_error(exc) from exc
  3470. return response.model_dump()
  3471. job = await slice_dispatch.enqueue(
  3472. kind="library_file",
  3473. source_id=lib_file.id,
  3474. source_name=lib_file.filename,
  3475. run=_run,
  3476. )
  3477. return {
  3478. "job_id": job.id,
  3479. "status": job.status,
  3480. "status_url": f"/api/v1/slice-jobs/{job.id}",
  3481. }
  3482. @router.post("/files/{file_id}/print")
  3483. async def print_library_file(
  3484. file_id: int,
  3485. printer_id: int,
  3486. body: FilePrintRequest | None = None,
  3487. db: AsyncSession = Depends(get_db),
  3488. current_user: User | None = Depends(require_permission_if_auth_enabled(Permission.PRINTERS_CONTROL)),
  3489. ):
  3490. """Dispatch a library file for send/start on a printer.
  3491. The actual send/start work is handled asynchronously by background
  3492. dispatch so the UI can continue immediately.
  3493. Only sliced files (.gcode or .gcode.3mf) can be printed.
  3494. """
  3495. from backend.app.models.printer import Printer
  3496. from backend.app.services.background_dispatch import DispatchEnqueueRejected, background_dispatch
  3497. from backend.app.services.printer_manager import printer_manager
  3498. # Use defaults if no body provided
  3499. if body is None:
  3500. body = FilePrintRequest()
  3501. # Get the library file
  3502. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3503. lib_file = result.scalar_one_or_none()
  3504. if not lib_file:
  3505. raise HTTPException(status_code=404, detail="File not found")
  3506. # Validate file is sliced
  3507. if not is_sliced_file(lib_file.filename):
  3508. raise HTTPException(
  3509. status_code=400,
  3510. detail="Not a sliced file. Only .gcode or .gcode.3mf files can be printed.",
  3511. )
  3512. # Filenames containing FAT32/exFAT-illegal characters would 553 at
  3513. # FTP upload time (#1540). Older rows may pre-date the rename-time
  3514. # validation, so reject the print attempt with an actionable message
  3515. # rather than silently renaming user data.
  3516. try:
  3517. validate_print_filename(lib_file.filename)
  3518. except InvalidFilenameError as e:
  3519. raise HTTPException(status_code=400, detail=str(e)) from e
  3520. # Get the full file path
  3521. file_path = Path(app_settings.base_dir) / lib_file.file_path
  3522. if not file_path.exists():
  3523. raise HTTPException(status_code=404, detail="File not found on disk")
  3524. # Get printer
  3525. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  3526. printer = result.scalar_one_or_none()
  3527. if not printer:
  3528. raise HTTPException(status_code=404, detail="Printer not found")
  3529. # Check printer is connected
  3530. if not printer_manager.is_connected(printer_id):
  3531. raise HTTPException(status_code=400, detail="Printer is not connected")
  3532. # Validate project exists before dispatching so a bogus ID yields 404, not a FK-constraint 500
  3533. if body.project_id is not None:
  3534. project_result = await db.execute(select(Project).where(Project.id == body.project_id))
  3535. if not project_result.scalar_one_or_none():
  3536. raise HTTPException(status_code=404, detail="Project not found")
  3537. plate_name = body.plate_name
  3538. if not plate_name and body.plate_id is not None:
  3539. plate_name = f"Plate {body.plate_id}"
  3540. dispatch_source_name = lib_file.filename
  3541. if plate_name:
  3542. dispatch_source_name = f"{lib_file.filename} • {plate_name}"
  3543. try:
  3544. dispatch_result = await background_dispatch.dispatch_print_library_file(
  3545. file_id=file_id,
  3546. filename=dispatch_source_name,
  3547. printer_id=printer_id,
  3548. printer_name=printer.name,
  3549. options=body.model_dump(exclude_none=True, exclude={"cleanup_library_after_dispatch"}),
  3550. project_id=body.project_id,
  3551. requested_by_user_id=current_user.id if current_user else None,
  3552. requested_by_username=current_user.username if current_user else None,
  3553. cleanup_library_after_dispatch=body.cleanup_library_after_dispatch,
  3554. )
  3555. except DispatchEnqueueRejected as e:
  3556. raise HTTPException(status_code=409, detail=str(e)) from e
  3557. return {
  3558. "status": "dispatched",
  3559. "printer_id": printer_id,
  3560. "archive_id": None,
  3561. "filename": lib_file.filename,
  3562. "dispatch_job_id": dispatch_result["dispatch_job_id"],
  3563. "dispatch_position": dispatch_result["dispatch_position"],
  3564. }
  3565. # ============ File Detail Endpoints ============
  3566. @router.get("/files/{file_id}", response_model=FileResponseSchema)
  3567. async def get_file(
  3568. file_id: int,
  3569. db: AsyncSession = Depends(get_db),
  3570. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  3571. ):
  3572. """Get a file by ID with full details."""
  3573. result = await db.execute(
  3574. LibraryFile.active().options(selectinload(LibraryFile.created_by)).where(LibraryFile.id == file_id)
  3575. )
  3576. file = result.scalar_one_or_none()
  3577. if not file:
  3578. raise HTTPException(status_code=404, detail="File not found")
  3579. # Get folder name
  3580. folder_name = None
  3581. if file.folder_id:
  3582. folder_result = await db.execute(select(LibraryFolder.name).where(LibraryFolder.id == file.folder_id))
  3583. folder_name = folder_result.scalar()
  3584. # Get project name
  3585. project_name = None
  3586. if file.project_id:
  3587. project_result = await db.execute(select(Project.name).where(Project.id == file.project_id))
  3588. project_name = project_result.scalar()
  3589. # Get duplicates
  3590. duplicates = []
  3591. duplicate_count = 0
  3592. if file.file_hash:
  3593. dup_result = await db.execute(
  3594. select(LibraryFile, LibraryFolder.name)
  3595. .outerjoin(LibraryFolder, LibraryFile.folder_id == LibraryFolder.id)
  3596. .where(
  3597. LibraryFile.file_hash == file.file_hash,
  3598. LibraryFile.id != file.id,
  3599. LibraryFile.deleted_at.is_(None),
  3600. )
  3601. )
  3602. for dup_file, dup_folder_name in dup_result.all():
  3603. duplicates.append(
  3604. FileDuplicate(
  3605. id=dup_file.id,
  3606. filename=dup_file.filename,
  3607. folder_id=dup_file.folder_id,
  3608. folder_name=dup_folder_name,
  3609. created_at=dup_file.created_at,
  3610. )
  3611. )
  3612. duplicate_count = len(duplicates)
  3613. # Extract key metadata fields
  3614. print_name = None
  3615. print_time = None
  3616. filament_grams = None
  3617. sliced_for_model = None
  3618. if file.file_metadata:
  3619. print_name = file.file_metadata.get("print_name")
  3620. print_time = file.file_metadata.get("print_time_seconds")
  3621. filament_grams = file.file_metadata.get("filament_used_grams")
  3622. sliced_for_model = file.file_metadata.get("sliced_for_model")
  3623. return FileResponseSchema(
  3624. id=file.id,
  3625. folder_id=file.folder_id,
  3626. folder_name=folder_name,
  3627. project_id=file.project_id,
  3628. project_name=project_name,
  3629. filename=file.filename,
  3630. file_path=file.file_path,
  3631. file_type=file.file_type,
  3632. file_size=file.file_size,
  3633. file_hash=file.file_hash,
  3634. thumbnail_path=file.thumbnail_path,
  3635. metadata=file.file_metadata,
  3636. print_count=file.print_count,
  3637. last_printed_at=file.last_printed_at,
  3638. notes=file.notes,
  3639. duplicates=duplicates if duplicates else None,
  3640. duplicate_count=duplicate_count,
  3641. created_by_id=file.created_by_id,
  3642. created_by_username=file.created_by.username if file.created_by else None,
  3643. created_at=file.created_at,
  3644. updated_at=file.updated_at,
  3645. print_name=print_name,
  3646. print_time_seconds=print_time,
  3647. filament_used_grams=filament_grams,
  3648. sliced_for_model=sliced_for_model,
  3649. )
  3650. @router.put("/files/{file_id}", response_model=FileResponseSchema)
  3651. async def update_file(
  3652. file_id: int,
  3653. data: FileUpdate,
  3654. db: AsyncSession = Depends(get_db),
  3655. auth_result: tuple[User | None, bool] = Depends(
  3656. require_ownership_permission(
  3657. Permission.LIBRARY_UPDATE_ALL,
  3658. Permission.LIBRARY_UPDATE_OWN,
  3659. )
  3660. ),
  3661. ):
  3662. """Update a file's metadata."""
  3663. user, can_modify_all = auth_result
  3664. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3665. file = result.scalar_one_or_none()
  3666. if not file:
  3667. raise HTTPException(status_code=404, detail="File not found")
  3668. # Ownership check
  3669. if not can_modify_all:
  3670. if file.created_by_id != user.id:
  3671. raise HTTPException(status_code=403, detail="You can only update your own files")
  3672. if data.filename is not None:
  3673. # Bambu printer SD cards are FAT32/exFAT; reject the same set Bambu
  3674. # Studio refuses on save so we fail here with a clear message
  3675. # instead of an obscure FTP 553 at print time (#1540).
  3676. try:
  3677. validate_print_filename(data.filename)
  3678. except InvalidFilenameError as e:
  3679. raise HTTPException(status_code=400, detail=str(e)) from e
  3680. file.filename = data.filename
  3681. # No print_name to keep in sync — library files display by filename,
  3682. # and _without_print_name strips the embedded 3MF Title on import (#1489).
  3683. if data.folder_id is not None:
  3684. if data.folder_id == 0:
  3685. file.folder_id = None
  3686. else:
  3687. # Verify folder exists
  3688. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.folder_id))
  3689. if not folder_result.scalar_one_or_none():
  3690. raise HTTPException(status_code=404, detail="Folder not found")
  3691. file.folder_id = data.folder_id
  3692. if data.project_id is not None:
  3693. if data.project_id == 0:
  3694. file.project_id = None
  3695. else:
  3696. # Verify project exists
  3697. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  3698. if not project_result.scalar_one_or_none():
  3699. raise HTTPException(status_code=404, detail="Project not found")
  3700. file.project_id = data.project_id
  3701. if data.notes is not None:
  3702. file.notes = data.notes if data.notes else None
  3703. await db.commit()
  3704. await db.refresh(file)
  3705. # Return full response (reuse get_file logic)
  3706. return await get_file(file_id, db)
  3707. @router.delete("/files/{file_id}")
  3708. async def delete_file(
  3709. file_id: int,
  3710. db: AsyncSession = Depends(get_db),
  3711. auth_result: tuple[User | None, bool] = Depends(
  3712. require_ownership_permission(
  3713. Permission.LIBRARY_DELETE_ALL,
  3714. Permission.LIBRARY_DELETE_OWN,
  3715. )
  3716. ),
  3717. ):
  3718. """Move a file to the trash (soft-delete).
  3719. The file's bytes and thumbnail stay on disk until the trash sweeper
  3720. hard-deletes the row after the retention window (see #1008). External
  3721. files skip the trash entirely — they can't be restored from disk and the
  3722. underlying file is outside Bambuddy's control, so we just drop the DB
  3723. record and thumbnail.
  3724. """
  3725. user, can_modify_all = auth_result
  3726. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3727. file = result.scalar_one_or_none()
  3728. if not file:
  3729. raise HTTPException(status_code=404, detail="File not found")
  3730. # Ownership check
  3731. if not can_modify_all:
  3732. if file.created_by_id != user.id:
  3733. raise HTTPException(status_code=403, detail="You can only delete your own files")
  3734. if file.is_external:
  3735. # External files bypass the trash — just drop the DB row + our thumbnail.
  3736. abs_thumb_path = to_absolute_path(file.thumbnail_path)
  3737. if abs_thumb_path and abs_thumb_path.exists():
  3738. try:
  3739. abs_thumb_path.unlink()
  3740. except OSError as e:
  3741. logger.warning("Failed to delete thumbnail from disk: %s", e)
  3742. await db.delete(file)
  3743. await db.commit()
  3744. return {"status": "success", "message": "File deleted", "trashed": False}
  3745. # Managed file: soft-delete. Sweeper removes bytes + thumbnail after retention.
  3746. file.deleted_at = datetime.now(timezone.utc)
  3747. await db.commit()
  3748. return {"status": "success", "message": "File moved to trash", "trashed": True}
  3749. # ============ File Content Endpoints ============
  3750. @router.get("/files/{file_id}/download")
  3751. async def download_file(
  3752. file_id: int,
  3753. db: AsyncSession = Depends(get_db),
  3754. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  3755. ):
  3756. """Download a file."""
  3757. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3758. file = result.scalar_one_or_none()
  3759. if not file:
  3760. raise HTTPException(status_code=404, detail="File not found")
  3761. abs_path = to_absolute_path(file.file_path)
  3762. if not abs_path or not abs_path.exists():
  3763. raise HTTPException(status_code=404, detail="File not found on disk")
  3764. return FastAPIFileResponse(
  3765. str(abs_path),
  3766. filename=file.filename,
  3767. media_type="application/octet-stream",
  3768. )
  3769. @router.post("/files/{file_id}/slicer-token")
  3770. async def create_library_slicer_token(
  3771. file_id: int,
  3772. db: AsyncSession = Depends(get_db),
  3773. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  3774. ):
  3775. """Create a short-lived download token for opening files in slicer applications.
  3776. Slicer protocol handlers (bambustudioopen://, orcaslicer://) cannot send
  3777. auth headers, so they use this token in the URL path instead.
  3778. """
  3779. from backend.app.core.auth import create_slicer_download_token
  3780. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3781. file = result.scalar_one_or_none()
  3782. if not file:
  3783. raise HTTPException(status_code=404, detail="File not found")
  3784. token = await create_slicer_download_token("library", file_id)
  3785. return {"token": token}
  3786. @router.get("/files/{file_id}/dl/{token}/{filename}")
  3787. async def download_library_file_for_slicer(
  3788. file_id: int,
  3789. token: str,
  3790. filename: str,
  3791. db: AsyncSession = Depends(get_db),
  3792. ):
  3793. """Download a library file using a slicer download token.
  3794. Token-authenticated (no auth headers needed). The token is short-lived
  3795. and single-use, created by POST /files/{file_id}/slicer-token.
  3796. Filename is at the end of the URL so slicers can detect the file format.
  3797. """
  3798. from backend.app.core.auth import verify_slicer_download_token
  3799. if not await verify_slicer_download_token(token, "library", file_id):
  3800. raise HTTPException(status_code=403, detail="Invalid or expired download token")
  3801. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3802. file = result.scalar_one_or_none()
  3803. if not file:
  3804. raise HTTPException(status_code=404, detail="File not found")
  3805. abs_path = to_absolute_path(file.file_path)
  3806. if not abs_path or not abs_path.exists():
  3807. raise HTTPException(status_code=404, detail="File not found on disk")
  3808. return FastAPIFileResponse(
  3809. str(abs_path),
  3810. filename=file.filename,
  3811. media_type="application/octet-stream",
  3812. )
  3813. @router.get("/files/{file_id}/thumbnail")
  3814. async def get_thumbnail(
  3815. file_id: int,
  3816. db: AsyncSession = Depends(get_db),
  3817. _: None = RequireCameraStreamTokenIfAuthEnabled,
  3818. ):
  3819. """Get a file's thumbnail."""
  3820. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3821. file = result.scalar_one_or_none()
  3822. if not file:
  3823. raise HTTPException(status_code=404, detail="File not found")
  3824. abs_thumb_path = to_absolute_path(file.thumbnail_path)
  3825. if not abs_thumb_path or not abs_thumb_path.exists():
  3826. raise HTTPException(status_code=404, detail="Thumbnail not found")
  3827. # Detect media type from extension
  3828. thumb_ext = abs_thumb_path.suffix.lower()
  3829. media_types = {
  3830. ".png": "image/png",
  3831. ".jpg": "image/jpeg",
  3832. ".jpeg": "image/jpeg",
  3833. ".gif": "image/gif",
  3834. ".webp": "image/webp",
  3835. }
  3836. media_type = media_types.get(thumb_ext, "image/png")
  3837. return FastAPIFileResponse(str(abs_thumb_path), media_type=media_type)
  3838. @router.get("/files/{file_id}/gcode")
  3839. async def get_gcode(
  3840. file_id: int,
  3841. db: AsyncSession = Depends(get_db),
  3842. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  3843. ):
  3844. """Get gcode for a file (for preview)."""
  3845. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3846. file = result.scalar_one_or_none()
  3847. if not file:
  3848. raise HTTPException(status_code=404, detail="File not found")
  3849. abs_path = to_absolute_path(file.file_path)
  3850. if not abs_path or not abs_path.exists():
  3851. raise HTTPException(status_code=404, detail="File not found on disk")
  3852. if file.file_type == "gcode":
  3853. return FastAPIFileResponse(str(abs_path), media_type="text/plain")
  3854. elif file.file_type in ("3mf", "gcode.3mf"):
  3855. # Extract gcode from 3mf zip container. `.gcode.3mf` sliced outputs
  3856. # carry the same `Metadata/plate_*.gcode` entries as a `.3mf`, so
  3857. # the unzip path is identical — just had to expand the gate.
  3858. try:
  3859. with zipfile.ZipFile(str(abs_path), "r") as zf:
  3860. # Find gcode file
  3861. gcode_files = [n for n in zf.namelist() if n.endswith(".gcode")]
  3862. if not gcode_files:
  3863. raise HTTPException(status_code=404, detail="No gcode found in 3MF file")
  3864. gcode_content = zf.read(gcode_files[0])
  3865. from fastapi.responses import Response
  3866. return Response(content=gcode_content, media_type="text/plain")
  3867. except zipfile.BadZipFile:
  3868. raise HTTPException(status_code=400, detail="Invalid 3MF file")
  3869. else:
  3870. raise HTTPException(status_code=400, detail="Unsupported file type")
  3871. # ============ Bulk Operations ============
  3872. @router.post("/files/move")
  3873. async def move_files(
  3874. data: FileMoveRequest,
  3875. db: AsyncSession = Depends(get_db),
  3876. auth_result: tuple[User | None, bool] = Depends(
  3877. require_ownership_permission(
  3878. Permission.LIBRARY_UPDATE_ALL,
  3879. Permission.LIBRARY_UPDATE_OWN,
  3880. )
  3881. ),
  3882. ):
  3883. """Move multiple files to a folder.
  3884. Cross-boundary moves (managed ↔ external, or external ↔ external)
  3885. physically relocate the bytes — see ``_move_file_bytes``. Same-boundary
  3886. moves stay DB-only because the file's on-disk location doesn't depend
  3887. on which managed folder owns it.
  3888. Files not owned by the user are skipped (unless user has ``*_all``
  3889. permission). Each skip carries a structured reason so the UI can
  3890. surface "5 of 10 files were skipped: 3 had filename collisions on
  3891. the NAS, 2 are no longer on disk" rather than a blank "skipped: 5".
  3892. """
  3893. user, can_modify_all = auth_result
  3894. # Verify folder exists if specified
  3895. target_folder: LibraryFolder | None = None
  3896. if data.folder_id is not None:
  3897. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.folder_id))
  3898. target_folder = folder_result.scalar_one_or_none()
  3899. if not target_folder:
  3900. raise HTTPException(status_code=404, detail="Folder not found")
  3901. if target_folder.is_external and target_folder.external_readonly:
  3902. raise HTTPException(status_code=403, detail="Cannot move files to a read-only external folder")
  3903. target_is_external = target_folder is not None and target_folder.is_external
  3904. moved = 0
  3905. skipped = 0
  3906. skipped_reasons: list[dict] = []
  3907. for file_id in data.file_ids:
  3908. result = await db.execute(
  3909. LibraryFile.active().options(selectinload(LibraryFile.folder)).where(LibraryFile.id == file_id)
  3910. )
  3911. file = result.scalar_one_or_none()
  3912. if not file:
  3913. continue
  3914. # Ownership check
  3915. if not can_modify_all and file.created_by_id != user.id:
  3916. skipped += 1
  3917. skipped_reasons.append({"file_id": file_id, "code": "not_owner", "reason": "not the file owner"})
  3918. continue
  3919. # No bytes need to move when both ends are managed (same-boundary).
  3920. if not file.is_external and not target_is_external:
  3921. file.folder_id = data.folder_id
  3922. moved += 1
  3923. continue
  3924. # Block moves out of a read-only external mount. The user only has
  3925. # read access to the source, and a move is semantically a delete on
  3926. # the source — which a read-only mount can't fulfil. Without this
  3927. # guard we'd succeed at copying to the target, fail to unlink the
  3928. # source, and the same file would now exist in two places (with
  3929. # the DB pointing at only one).
  3930. if file.is_external and file.folder is not None and file.folder.external_readonly:
  3931. skipped += 1
  3932. skipped_reasons.append(
  3933. {"file_id": file_id, "code": "source_readonly", "reason": "source is on a read-only external folder"}
  3934. )
  3935. continue
  3936. # Otherwise relocate the bytes, then update the DB row to match.
  3937. try:
  3938. new_file_path = _move_file_bytes(file, target_folder)
  3939. except _MoveSkip as e:
  3940. skipped += 1
  3941. skipped_reasons.append({"file_id": file_id, "code": e.code, "reason": e.reason})
  3942. continue
  3943. file.is_external = target_is_external
  3944. file.folder_id = data.folder_id
  3945. file.file_path = new_file_path
  3946. # External rows historically carry `file_hash=None` (scan skips
  3947. # hashing). When pulling an external file into managed storage,
  3948. # compute the hash so dedup detection works for future uploads
  3949. # of the same content.
  3950. if not target_is_external and file.file_hash is None:
  3951. try:
  3952. abs_path = to_absolute_path(new_file_path)
  3953. if abs_path:
  3954. file.file_hash = calculate_file_hash(abs_path)
  3955. except OSError:
  3956. pass # leave hash null; dedup just won't match this row
  3957. moved += 1
  3958. await db.commit()
  3959. return {
  3960. "status": "success",
  3961. "moved": moved,
  3962. "skipped": skipped,
  3963. "skipped_reasons": skipped_reasons,
  3964. }
  3965. @router.post("/bulk-delete", response_model=BulkDeleteResponse)
  3966. async def bulk_delete(
  3967. data: BulkDeleteRequest,
  3968. db: AsyncSession = Depends(get_db),
  3969. auth_result: tuple[User | None, bool] = Depends(
  3970. require_ownership_permission(
  3971. Permission.LIBRARY_DELETE_ALL,
  3972. Permission.LIBRARY_DELETE_OWN,
  3973. )
  3974. ),
  3975. ):
  3976. """Delete multiple files and/or folders.
  3977. Files not owned by the user are skipped (unless user has *_all permission).
  3978. """
  3979. user, can_modify_all = auth_result
  3980. deleted_files = 0
  3981. deleted_folders = 0
  3982. skipped_files = 0
  3983. # Delete files first. Managed files go to trash (sweeper hard-deletes bytes
  3984. # later); external files bypass trash since their disk state is outside our
  3985. # control and can't be restored from trash anyway.
  3986. now = datetime.now(timezone.utc)
  3987. for file_id in data.file_ids:
  3988. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3989. file = result.scalar_one_or_none()
  3990. if not file:
  3991. continue
  3992. if not can_modify_all and file.created_by_id != user.id:
  3993. skipped_files += 1
  3994. continue
  3995. if file.is_external:
  3996. abs_thumb_path = to_absolute_path(file.thumbnail_path)
  3997. if abs_thumb_path and abs_thumb_path.exists():
  3998. try:
  3999. abs_thumb_path.unlink()
  4000. except OSError as e:
  4001. logger.warning("Failed to delete thumbnail from disk: %s", e)
  4002. await db.delete(file)
  4003. else:
  4004. file.deleted_at = now
  4005. deleted_files += 1
  4006. # Delete folders (cascade will handle contents)
  4007. # Note: Folders don't have ownership tracking currently, require *_all permission
  4008. for folder_id in data.folder_ids:
  4009. if not can_modify_all:
  4010. # Users without *_all permission cannot delete folders
  4011. continue
  4012. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  4013. folder = result.scalar_one_or_none()
  4014. if folder:
  4015. # Count files that will be deleted
  4016. file_count_result = await db.execute(
  4017. select(func.count(LibraryFile.id)).where(
  4018. LibraryFile.folder_id == folder_id,
  4019. LibraryFile.deleted_at.is_(None),
  4020. )
  4021. )
  4022. deleted_files += file_count_result.scalar() or 0
  4023. await db.delete(folder)
  4024. deleted_folders += 1
  4025. await db.commit()
  4026. return BulkDeleteResponse(deleted_files=deleted_files, deleted_folders=deleted_folders)
  4027. # ============ Stats Endpoint ============
  4028. @router.get("/stats")
  4029. async def get_library_stats(
  4030. db: AsyncSession = Depends(get_db),
  4031. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  4032. ):
  4033. """Get library statistics."""
  4034. # Stats exclude trashed files — users see counts/sizes for what's actually in the library.
  4035. active_only = LibraryFile.deleted_at.is_(None)
  4036. # Total files
  4037. total_files_result = await db.execute(select(func.count(LibraryFile.id)).where(active_only))
  4038. total_files = total_files_result.scalar() or 0
  4039. # Total folders
  4040. total_folders_result = await db.execute(select(func.count(LibraryFolder.id)))
  4041. total_folders = total_folders_result.scalar() or 0
  4042. # Total size
  4043. total_size_result = await db.execute(select(func.sum(LibraryFile.file_size)).where(active_only))
  4044. total_size = total_size_result.scalar() or 0
  4045. # Files by type
  4046. type_result = await db.execute(
  4047. select(LibraryFile.file_type, func.count(LibraryFile.id)).where(active_only).group_by(LibraryFile.file_type)
  4048. )
  4049. files_by_type = dict(type_result.all())
  4050. # Total prints
  4051. total_prints_result = await db.execute(select(func.sum(LibraryFile.print_count)).where(active_only))
  4052. total_prints = total_prints_result.scalar() or 0
  4053. # Disk space info
  4054. library_dir = get_library_dir()
  4055. try:
  4056. disk_stat = shutil.disk_usage(library_dir)
  4057. disk_free_bytes = disk_stat.free
  4058. disk_total_bytes = disk_stat.total
  4059. disk_used_bytes = disk_stat.used
  4060. except OSError:
  4061. disk_free_bytes = 0
  4062. disk_total_bytes = 0
  4063. disk_used_bytes = 0
  4064. return {
  4065. "total_files": total_files,
  4066. "total_folders": total_folders,
  4067. "total_size_bytes": total_size,
  4068. "files_by_type": files_by_type,
  4069. "total_prints": total_prints,
  4070. "disk_free_bytes": disk_free_bytes,
  4071. "disk_total_bytes": disk_total_bytes,
  4072. "disk_used_bytes": disk_used_bytes,
  4073. }