library.py 230 KB

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