library.py 286 KB

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