library.py 186 KB

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