archives.py 258 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153
  1. import asyncio
  2. import io
  3. import json
  4. import logging
  5. import re as _re
  6. import zipfile
  7. from collections import defaultdict
  8. from dataclasses import replace
  9. from datetime import date, datetime, timedelta, timezone
  10. from html import escape as html_escape
  11. from pathlib import Path
  12. from fastapi import APIRouter, Depends, File, Form, HTTPException, Query, Request, UploadFile
  13. from fastapi.responses import FileResponse, Response
  14. from sqlalchemy import and_, case, func, or_, select
  15. from sqlalchemy.ext.asyncio import AsyncSession
  16. from sqlalchemy.sql.elements import ColumnElement
  17. from backend.app.core import database
  18. from backend.app.core.auth import (
  19. ApiKeyActor,
  20. MediaOrRequestPrinterScope,
  21. RequestActor,
  22. RequestPrinterScope,
  23. RequirePermissionIfAuthEnabled,
  24. probe_permissions_if_auth_enabled,
  25. require_media_token_ownership,
  26. require_ownership_permission,
  27. )
  28. from backend.app.core.config import settings
  29. from backend.app.core.database import get_db
  30. from backend.app.core.permissions import Permission
  31. from backend.app.core.printer_scope import PrinterScope
  32. from backend.app.core.row_visibility import archive_scope_clause, whole_archive_scope_clause
  33. from backend.app.models.archive import PrintArchive
  34. from backend.app.models.filament import Filament
  35. from backend.app.models.print_log import INCLUDE_OPEN_RUNS, PrintLogEntry
  36. from backend.app.models.printer import Printer
  37. from backend.app.models.project import Project
  38. from backend.app.models.spool_usage_history import SpoolUsageHistory
  39. from backend.app.models.user import User
  40. from backend.app.schemas.archive import ArchiveResponse, ArchiveSlim, ArchiveStats, ArchiveUpdate
  41. from backend.app.schemas.print_log import PrintLogResponse
  42. from backend.app.schemas.slicer import SliceRequest
  43. from backend.app.services.archive import ArchiveService
  44. from backend.app.services.archive_scope import (
  45. ScopedView,
  46. ensure_whole_archive_in_scope,
  47. printed_in_scope,
  48. scoped_view,
  49. scoped_views,
  50. )
  51. from backend.app.services.bambu_ftp import ftps_handshake_blocked, list_files_result_async
  52. from backend.app.services.design_settings import overrides_from_config
  53. from backend.app.services.filament_requirements import annotate_rack_groups
  54. from backend.app.services.print_confirmation import (
  55. is_one_tap_request,
  56. is_unattended_fetch,
  57. )
  58. from backend.app.services.print_cost_estimate import default_cost_per_kg as _default_cost_per_kg, price_one_print
  59. from backend.app.services.print_log import owns_media, runs_with_timelapse
  60. from backend.app.services.print_storage import (
  61. REASON_FTP_TRANSFER_FAILED,
  62. REASON_FTPS_COOLOFF,
  63. REASON_INTERNAL_HISTORY,
  64. REASON_INTERNAL_STORAGE,
  65. REASON_NO_EXTERNAL_STORAGE,
  66. )
  67. from backend.app.services.printer_media import VIDEO_SUFFIXES, match_ipcam_chunks
  68. from backend.app.utils.archive_paths import archive_photos_dir, find_archive_photo
  69. from backend.app.utils.http import build_content_disposition, download_error_response, safe_download_filename
  70. from backend.app.utils.local_time import day_zone, local_date_end, local_date_start, to_naive_utc
  71. from backend.app.utils.threemf_tools import (
  72. default_plate_gcode_name,
  73. expand_to_project_slots,
  74. extract_embedded_presets_from_3mf,
  75. extract_nozzle_mapping_from_3mf,
  76. extract_project_filaments_from_3mf,
  77. names_carry_gcode,
  78. select_plate_gcode_name,
  79. )
  80. logger = logging.getLogger(__name__)
  81. router = APIRouter(prefix="/archives", tags=["archives"])
  82. _PRINTER_MEDIA_LIST_TIMEOUT_SECONDS = 8.0
  83. # Path of the embedded slicer config inside a BambuStudio/OrcaSlicer 3MF.
  84. _PROJECT_SETTINGS_PATH = "Metadata/project_settings.config"
  85. def _safe_filename(filename: str) -> str:
  86. """Extract basename from a client-supplied filename, preventing path traversal.
  87. Normalizes backslashes (Windows paths) before extracting so that
  88. '..\\\\..\\\\evil.3mf' is correctly stripped to 'evil.3mf' on Linux.
  89. """
  90. return Path(filename.replace("\\", "/")).name
  91. _TIMELAPSE_FILENAME_TS_RE = _re.compile(r"(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})")
  92. _DEFAULT_TIMELAPSE_OFFSETS_HOURS: tuple[int, ...] = (0, 8, -8, 7, -7, 1, -1)
  93. _DEFAULT_TIMELAPSE_TOLERANCE = timedelta(hours=4)
  94. _DEFAULT_TIMELAPSE_AMBIGUITY_MARGIN = timedelta(minutes=15)
  95. def _match_timelapse_by_timestamp(
  96. video_files: list[dict],
  97. archive_start: datetime | None,
  98. *,
  99. tolerance: timedelta = _DEFAULT_TIMELAPSE_TOLERANCE,
  100. ambiguity_margin: timedelta = _DEFAULT_TIMELAPSE_AMBIGUITY_MARGIN,
  101. offsets_hours: tuple[int, ...] = _DEFAULT_TIMELAPSE_OFFSETS_HOURS,
  102. ) -> tuple[dict | None, timedelta | None]:
  103. """Pick the timelapse whose filename timestamp best matches the print start time.
  104. Bambu timelapse filenames embed the printer-local START time (e.g.
  105. "video_2026-05-08_09-41-29.mp4"). The printer's clock may be offset from the
  106. server's — especially in LAN-Only mode where NTP is unreachable — so we try a
  107. small set of common UTC offsets and keep the (video, offset) pair with the
  108. smallest absolute distance from archive_start. We deliberately do NOT consider
  109. archive_end here: the filename is start time, not end time, so comparing it to
  110. completion is not a real signal (Strategy 3 handles end via file mtime).
  111. Because the offset list densely covers a wide span, an unrelated video's
  112. filename can coincidentally land near a later print's start at some offset.
  113. To avoid that false positive, we require the best (video, offset) pair to
  114. beat the next-best pair *from a different video* by at least `ambiguity_margin`.
  115. When the top two candidates from different videos are too close to call,
  116. we return None and let the caller fall back to manual selection.
  117. """
  118. if archive_start is None:
  119. return None, None
  120. # (diff, video) for every (video, offset) pair within tolerance.
  121. candidates: list[tuple[timedelta, dict]] = []
  122. for f in video_files:
  123. fname = f.get("name", "")
  124. m = _TIMELAPSE_FILENAME_TS_RE.search(fname)
  125. if not m:
  126. continue
  127. try:
  128. file_time = datetime.strptime(m.group(1), "%Y-%m-%d_%H-%M-%S")
  129. except ValueError:
  130. continue
  131. for hour_offset in offsets_hours:
  132. adjusted = file_time - timedelta(hours=hour_offset)
  133. diff = abs(adjusted - archive_start)
  134. if diff <= tolerance:
  135. candidates.append((diff, f))
  136. if not candidates:
  137. return None, None
  138. candidates.sort(key=lambda c: c[0])
  139. best_diff, best_video = candidates[0]
  140. best_name = best_video.get("name")
  141. for diff, video in candidates[1:]:
  142. if video.get("name") != best_name and (diff - best_diff) < ambiguity_margin:
  143. # Another video matches almost as well — refuse to auto-pick.
  144. return None, None
  145. return best_video, best_diff
  146. async def _claimed_timelapse_stems(db, printer_id: int | None, exclude_archive_id: int) -> set[str]:
  147. """Video filenames already attached to another print of this printer
  148. (#2704): ``print_log.claimed_timelapse_stems``, for the print the archive
  149. shows, its newest run."""
  150. from backend.app.services.print_log import claimed_timelapse_stems, newest_run
  151. run = await newest_run(db, exclude_archive_id)
  152. return await claimed_timelapse_stems(
  153. db, printer_id, archive_id=exclude_archive_id, run_id=run.id if run is not None else None
  154. )
  155. def _visible_archive_clauses(
  156. user, can_read_all: bool, printer_scope: PrinterScope, *, whole: bool = False, by_runs: bool = True
  157. ) -> list:
  158. """WHERE clauses for the archives a caller may see, as _ensure_archive_visible decides one by one.
  159. ``whole``: only those every print of which they see, for a change to the
  160. whole archive (``ensure_whole_archive_in_scope``). Not ``by_runs``: only
  161. those on their printers, for listings that show the archive's latest
  162. print as it is, without the caller's view of it (``_scoped``)."""
  163. clauses = []
  164. if whole:
  165. scope_clause = whole_archive_scope_clause(printer_scope)
  166. elif by_runs:
  167. scope_clause = archive_scope_clause(printer_scope)
  168. else:
  169. scope_clause = printer_scope.where(PrintArchive.printer_id)
  170. if scope_clause is not None:
  171. clauses.append(scope_clause)
  172. if not can_read_all:
  173. # Own archives only; an ownerless one needs read-all, and no user sees none.
  174. clauses.append(PrintArchive.created_by_id == user.id if user is not None else PrintArchive.id.is_(None))
  175. return clauses
  176. async def _ensure_archive_visible(
  177. archive: PrintArchive | None,
  178. user: User | ApiKeyActor | None,
  179. can_read_all: bool,
  180. printer_scope: PrinterScope,
  181. ) -> PrintArchive:
  182. """Per-archive visibility gate for ownership-scoped reads (#1726-adjacent).
  183. Returns ``archive`` if the caller is allowed to see it; raises 404 otherwise.
  184. Single enforcement point used by every detail / download / sub-resource
  185. route so we can't accidentally leak a row through a less-guarded sibling.
  186. Rules:
  187. - Missing archive or soft-deleted (``deleted_at != None``) → 404.
  188. - Caller with ARCHIVES_READ_ALL or auth disabled (``can_read_all=True``,
  189. ``user`` may be None) → archive returned.
  190. - Caller without ARCHIVES_READ_ALL and ``archive.created_by_id != user.id``
  191. → 404, deliberately NOT 403. 403 leaks "this id exists but you can't
  192. see it" — enumeration-friendly. 404 is indistinguishable from a
  193. nonexistent id. Pre-GHSA fix the caller saw 200 here (the PoC vector).
  194. - Ownerless rows (``created_by_id is None``) require ALL — fail-closed
  195. per ``feedback_no_fail_open_in_auth``.
  196. - An archive neither on a printer in ``printer_scope`` nor printed on
  197. one (any of its runs) → 404 (#1727, archive redesign).
  198. """
  199. if not archive or archive.deleted_at is not None:
  200. raise HTTPException(404, "Archive not found")
  201. # An archive from a printer the caller can't see is as missing as the
  202. # printer itself (#1727), unless one of its prints ran on one of theirs:
  203. # plates of one archive print on several printers. Archives with no
  204. # printer stay governed by ownership alone.
  205. if not printer_scope.allows(archive.printer_id):
  206. async with database.async_session() as db:
  207. if not await printed_in_scope(db, archive, printer_scope):
  208. raise HTTPException(404, "Archive not found")
  209. if can_read_all:
  210. return archive
  211. # Auth enabled, caller has _OWN only.
  212. if user is None:
  213. # Defensive: should be unreachable (RequirePermissionIfAuthEnabled
  214. # would have 401'd already), but never trust user identity to be
  215. # non-None when can_read_all is False.
  216. raise HTTPException(404, "Archive not found")
  217. if archive.created_by_id is None or archive.created_by_id != user.id:
  218. raise HTTPException(404, "Archive not found")
  219. return archive
  220. async def _ensure_whole_archive(archive: PrintArchive, printer_scope: PrinterScope) -> None:
  221. """For a change to the whole archive (its file, its video, the archive
  222. itself): 404 when the caller can't see it, 403 when it also printed on
  223. printers outside their scope (``ensure_whole_archive_in_scope``)."""
  224. if printer_scope.is_unrestricted:
  225. return
  226. async with database.async_session() as db:
  227. if not await printed_in_scope(db, archive, printer_scope):
  228. raise HTTPException(404, "Archive not found")
  229. await ensure_whole_archive_in_scope(db, archive, printer_scope)
  230. async def _media_hidden(archive: PrintArchive, printer_scope: PrinterScope, photo: str | None = None) -> bool:
  231. """Whether the archive's video (or ``photo``) is of a print on a printer
  232. outside the caller's scope: a camera's images go no further than the
  233. printer does (#1727, archive redesign)."""
  234. if printer_scope.is_unrestricted:
  235. return False
  236. async with database.async_session() as db:
  237. view = await scoped_view(db, archive, printer_scope)
  238. if view is None:
  239. return False
  240. if photo is not None:
  241. return photo in view.hidden_photos
  242. return view.hidden_timelapse is not None and view.hidden_timelapse == archive.timelapse_path
  243. async def _visible_photos(archive: PrintArchive, printer_scope: PrinterScope) -> list[str] | None:
  244. """The archive's photos the caller may see (``_media_hidden``)."""
  245. if printer_scope.is_unrestricted or not archive.photos:
  246. return archive.photos
  247. async with database.async_session() as db:
  248. view = await scoped_view(db, archive, printer_scope)
  249. if view is None:
  250. return archive.photos
  251. return [p for p in archive.photos if p not in view.hidden_photos] or None
  252. async def _ensure_media_visible(archive: PrintArchive, printer_scope: PrinterScope, photo: str | None = None) -> None:
  253. """404 for media ``_media_hidden`` hides, as for media that isn't there."""
  254. if await _media_hidden(archive, printer_scope, photo):
  255. raise HTTPException(404, "Photo not found" if photo is not None else "Timelapse not found")
  256. def _validate_user_filter_permission(current_user: User | ApiKeyActor | None, created_by_id: int | None):
  257. """Raise 403 if created_by_id filter is used without stats:filter_by_user permission."""
  258. if created_by_id is None or current_user is None:
  259. return
  260. if current_user.is_admin:
  261. return
  262. if not current_user.has_permission(Permission.STATS_FILTER_BY_USER.value):
  263. raise HTTPException(status_code=403, detail="Permission stats:filter_by_user required")
  264. def _apply_run_user_filter(conditions: list, created_by_id: int | None):
  265. """Append created_by_id filter scoped to PrintLogEntry rows."""
  266. from backend.app.models.print_log import PrintLogEntry
  267. if created_by_id is not None:
  268. if created_by_id == -1:
  269. conditions.append(PrintLogEntry.created_by_id.is_(None))
  270. else:
  271. conditions.append(PrintLogEntry.created_by_id == created_by_id)
  272. def compute_time_accuracy(archive: PrintArchive, run_aggregate: dict | None = None) -> dict:
  273. """Compute actual print time and accuracy for an archive.
  274. Returns dict with actual_time_seconds and time_accuracy.
  275. time_accuracy = (estimated / actual) * 100
  276. - 100% = perfect estimate
  277. - >100% = print was faster than estimated
  278. - <100% = print took longer than estimated
  279. When ``run_aggregate`` indicates the archive has more than one logged
  280. run (multi-plate file printed plate-by-plate, or reprints), both
  281. fields are suppressed: ``archive.started_at / completed_at`` reflect
  282. the LATEST run only, while ``archive.print_time_seconds`` is the
  283. whole-file estimate (post-#1593 the parser sums across plates), so
  284. comparing the two describes different scopes. The card-rendering
  285. frontend falls through to ``archive.print_time_seconds`` for the
  286. time display and hides the badge when ``time_accuracy`` is null —
  287. that's the desired "show estimate, no badge" presentation for
  288. multi-run archives (#1608). Single-run archives keep the original
  289. badge behaviour verbatim.
  290. """
  291. result: dict[str, int | float | None] = {"actual_time_seconds": None, "time_accuracy": None}
  292. # Multi-run archives: the per-run actual (started_at..completed_at on
  293. # the archive row) is incommensurable with the whole-file estimate.
  294. # Both fields are cleared so the card shows estimate + no badge.
  295. if run_aggregate and (run_aggregate.get("run_count") or 0) > 1:
  296. return result
  297. # A run whose times were not measured (#3310): its end is the slicer's
  298. # estimate or the reconnect. The card shows the estimate and no badge,
  299. # as for multi-run archives.
  300. if run_aggregate and run_aggregate.get("unmeasured_run_count"):
  301. return result
  302. if archive.started_at and archive.completed_at and archive.status == "completed":
  303. actual_seconds = int((archive.completed_at - archive.started_at).total_seconds())
  304. if actual_seconds > 0:
  305. result["actual_time_seconds"] = actual_seconds
  306. if archive.print_time_seconds and archive.print_time_seconds > 0:
  307. # Calculate accuracy as percentage
  308. accuracy = (archive.print_time_seconds / actual_seconds) * 100
  309. # Sanity check: skip unreasonable values (e.g., manually changed status)
  310. # Valid range: 5% to 500% (print took 20x longer to 5x faster than estimated)
  311. if 5 <= accuracy <= 500:
  312. result["time_accuracy"] = round(accuracy, 1)
  313. return result
  314. async def _load_run_aggregates(
  315. db: AsyncSession, archive_ids: list[int], printer_scope: PrinterScope | None = None
  316. ) -> dict[int, dict]:
  317. """Batch-load per-archive run aggregates from PrintLogEntry.
  318. Returns ``{archive_id: {run_count, last_run_at, total_filament_actual_grams,
  319. successful_run_count, failed_run_count, unmeasured_run_count}}``. Archives with no logged runs are
  320. absent from the map; callers should treat that as zero/none.
  321. With ``printer_scope``, only the runs on its printers count: a caller
  322. limited to some printers sees only their prints (archive redesign).
  323. """
  324. from backend.app.models.print_log import PrintLogEntry
  325. if not archive_ids:
  326. return {}
  327. rows = await db.execute(
  328. select(
  329. PrintLogEntry.archive_id,
  330. func.count(PrintLogEntry.id).label("run_count"),
  331. func.max(PrintLogEntry.started_at).label("last_run_at"),
  332. func.coalesce(func.sum(PrintLogEntry.filament_used_grams), 0).label("total_filament"),
  333. func.sum(case((PrintLogEntry.status == "completed", 1), else_=0)).label("successful"),
  334. func.sum(case((PrintLogEntry.status == "failed", 1), else_=0)).label("failed"),
  335. func.sum(case((PrintLogEntry.duration_unmeasured.is_(True), 1), else_=0)).label("estimated"),
  336. )
  337. .where(
  338. PrintLogEntry.archive_id.in_(archive_ids),
  339. *(
  340. [clause]
  341. if printer_scope is not None and (clause := printer_scope.where(PrintLogEntry.printer_id)) is not None
  342. else []
  343. ),
  344. )
  345. .group_by(PrintLogEntry.archive_id)
  346. )
  347. aggregates: dict[int, dict] = {}
  348. for archive_id, run_count, last_run_at, total_filament, successful, failed, estimated in rows.all():
  349. aggregates[archive_id] = {
  350. "run_count": int(run_count or 0),
  351. "last_run_at": last_run_at,
  352. "total_filament_actual_grams": float(total_filament) if total_filament else None,
  353. "successful_run_count": int(successful or 0),
  354. "failed_run_count": int(failed or 0),
  355. "unmeasured_run_count": int(estimated or 0),
  356. }
  357. return aggregates
  358. # What PATCH may change of an archive printed on printers outside the
  359. # caller's scope: the file's own notes, tags, favourite and project.
  360. _FILE_FIELDS = frozenset({"tags", "notes", "is_favorite", "project_id"})
  361. # What PATCH with a run_id may change: one run's outcome.
  362. _RUN_OUTCOME_FIELDS = frozenset({"user_verdict", "failure_reason", "status"})
  363. def _with_run_outcome(data: dict, run: PrintLogEntry) -> dict:
  364. """An archive response showing one of its runs: that print's printer,
  365. outcome, times, plate and verdict instead of the latest run's."""
  366. data.update(
  367. {
  368. "run_id": run.id,
  369. "printer_id": run.printer_id if run.printer_id is not None else data["printer_id"],
  370. "status": run.status,
  371. "started_at": run.started_at,
  372. "completed_at": run.completed_at,
  373. "plate_id": run.plate_id if run.plate_id is not None else data["plate_id"],
  374. "failure_reason": run.failure_reason,
  375. "user_verdict": run.user_verdict,
  376. "user_verdict_source": run.user_verdict_source,
  377. "user_verdict_at": run.user_verdict_at,
  378. # A run logged before runs carried the opt-in asks as its archive.
  379. "confirm_requested": bool(run.confirm_requested)
  380. if run.confirm_requested is not None
  381. else data["confirm_requested"],
  382. }
  383. )
  384. # Its own video and finish photos, once it keeps them; one logged before
  385. # shares the archive's.
  386. if owns_media(run):
  387. data["timelapse_path"] = run.timelapse_path
  388. if run.photos is not None:
  389. data["photos"] = run.photos or None
  390. return data
  391. def _scoped(data: dict, view: ScopedView | None) -> dict:
  392. """An archive response as a caller limited to some printers sees it
  393. (#1727, archive redesign): their latest print of it when the archive's
  394. latest is on a printer outside their scope, and without the video and
  395. finish photos of prints outside it."""
  396. if view is None:
  397. return data
  398. if view.run is not None:
  399. _with_run_outcome(data, view.run)
  400. # The archive's times and its energy and wear are of its latest and
  401. # first prints, maybe on a printer outside the scope: the badge reads as
  402. # for any archive printed more than once, and the figures are the shown
  403. # print's own.
  404. data["actual_time_seconds"] = data["time_accuracy"] = None
  405. for name in ("energy_kwh", "energy_cost", "wear_cost"):
  406. data[name] = getattr(view.shown, name) if view.shown is not None else None
  407. if view.hidden_timelapse is not None and data["timelapse_path"] == view.hidden_timelapse:
  408. data["timelapse_path"] = None
  409. if data["photos"]:
  410. data["photos"] = [p for p in data["photos"] if p not in view.hidden_photos] or None
  411. return data
  412. def archive_to_response(
  413. archive: PrintArchive,
  414. duplicates: list[dict] | None = None,
  415. duplicate_count: int = 0,
  416. duplicate_sequence: int = 0,
  417. original_archive_id: int | None = None,
  418. run_aggregate: dict | None = None,
  419. ) -> dict:
  420. """Convert archive model to response dict with computed fields."""
  421. data = {
  422. "id": archive.id,
  423. "printer_id": archive.printer_id,
  424. "project_id": archive.project_id,
  425. "project_name": archive.project.name if archive.project else None,
  426. "filename": archive.filename,
  427. "file_path": archive.file_path,
  428. "file_size": archive.file_size,
  429. "content_hash": archive.content_hash,
  430. "thumbnail_path": archive.thumbnail_path,
  431. "timelapse_path": archive.timelapse_path,
  432. "source_3mf_path": archive.source_3mf_path,
  433. "f3d_path": archive.f3d_path,
  434. "duplicates": duplicates,
  435. "duplicate_count": duplicate_count if duplicates is None else len(duplicates),
  436. "duplicate_sequence": duplicate_sequence,
  437. "original_archive_id": original_archive_id,
  438. "print_name": archive.print_name,
  439. "plate_id": archive.plate_id,
  440. "print_time_seconds": archive.print_time_seconds,
  441. "filament_used_grams": archive.filament_used_grams,
  442. "filament_type": archive.filament_type,
  443. "filament_color": archive.filament_color,
  444. "layer_height": archive.layer_height,
  445. "total_layers": archive.total_layers,
  446. "nozzle_diameter": archive.nozzle_diameter,
  447. "bed_temperature": archive.bed_temperature,
  448. "bed_type": archive.bed_type,
  449. "nozzle_temperature": archive.nozzle_temperature,
  450. "sliced_for_model": archive.sliced_for_model,
  451. "status": archive.status,
  452. "started_at": archive.started_at,
  453. "completed_at": archive.completed_at,
  454. "extra_data": archive.extra_data,
  455. "makerworld_url": archive.makerworld_url,
  456. "designer": archive.designer,
  457. "external_url": archive.external_url,
  458. "is_favorite": archive.is_favorite,
  459. "tags": archive.tags,
  460. "notes": archive.notes,
  461. "cost": archive.cost,
  462. "photos": archive.photos,
  463. "failure_reason": archive.failure_reason,
  464. # Post-print outcome confirmation (#1898). confirm_token stays
  465. # server-side — it is a capability and never belongs in a response.
  466. "user_verdict": archive.user_verdict,
  467. "user_verdict_source": archive.user_verdict_source,
  468. "user_verdict_at": archive.user_verdict_at,
  469. # bool() because the column is nullable to match the migration; the
  470. # response contract stays a strict bool either way.
  471. "confirm_requested": bool(archive.confirm_requested),
  472. "quantity": archive.quantity,
  473. "energy_kwh": archive.energy_kwh,
  474. "energy_cost": archive.energy_cost,
  475. "wear_cost": archive.wear_cost,
  476. "created_at": archive.created_at,
  477. # User tracking (Issue #206)
  478. "created_by_id": archive.created_by_id,
  479. "created_by_username": archive.created_by.username if archive.created_by else None,
  480. }
  481. # Add computed time accuracy fields. ``run_aggregate`` lets
  482. # ``compute_time_accuracy`` suppress the badge for multi-run archives
  483. # where the per-run actual / whole-file estimate scopes don't match
  484. # (#1608).
  485. accuracy_data = compute_time_accuracy(archive, run_aggregate)
  486. data.update(accuracy_data)
  487. if run_aggregate:
  488. data["run_count"] = run_aggregate.get("run_count", 0)
  489. data["last_run_at"] = run_aggregate.get("last_run_at")
  490. data["total_filament_actual_grams"] = run_aggregate.get("total_filament_actual_grams")
  491. data["successful_run_count"] = run_aggregate.get("successful_run_count", 0)
  492. data["failed_run_count"] = run_aggregate.get("failed_run_count", 0)
  493. return data
  494. @router.get("/", response_model=list[ArchiveResponse])
  495. async def list_archives(
  496. printer_id: int | None = None,
  497. project_id: int | None = None,
  498. date_from: date | None = Query(None),
  499. date_to: date | None = Query(None),
  500. tz: str | None = Query(
  501. None, max_length=64, description="IANA zone the dates are calendar days in; the server's when omitted"
  502. ),
  503. limit: int = 50,
  504. offset: int = 0,
  505. db: AsyncSession = Depends(get_db, scope="function"),
  506. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  507. require_ownership_permission(
  508. Permission.ARCHIVES_READ_ALL,
  509. Permission.ARCHIVES_READ_OWN,
  510. )
  511. ),
  512. printer_scope: PrinterScope = RequestPrinterScope,
  513. ):
  514. """List archived prints."""
  515. user, can_read_all = auth_result
  516. visible_to_user_id = user.id if (user is not None and not can_read_all) else None
  517. service = ArchiveService(db)
  518. archives = await service.list_archives(
  519. printer_id=printer_id,
  520. project_id=project_id,
  521. date_from=date_from,
  522. date_to=date_to,
  523. limit=limit,
  524. offset=offset,
  525. visible_to_user_id=visible_to_user_id,
  526. printer_scope=printer_scope,
  527. day_zone=day_zone(tz),
  528. )
  529. # Get sets of duplicate hashes and duplicate (name, hash) pairs (efficient single queries),
  530. # counted among the archives this caller may see, as the duplicates view lists them.
  531. visible = _visible_archive_clauses(user, can_read_all, printer_scope)
  532. duplicate_hashes, duplicate_name_hash_pairs = await service.get_duplicate_hashes_and_names(visible)
  533. # Batch-load duplicate groups once for the current page keys.
  534. duplicate_hashes_in_page = {
  535. a.content_hash for a in archives if a.content_hash and a.content_hash in duplicate_hashes
  536. }
  537. duplicate_name_hash_keys_in_page = {
  538. (a.print_name.lower(), a.content_hash)
  539. for a in archives
  540. if a.print_name and a.content_hash and (a.print_name.lower(), a.content_hash) in duplicate_name_hash_pairs
  541. }
  542. duplicate_meta_by_archive_id: dict[int, tuple[int, int, int]] = {}
  543. if duplicate_hashes_in_page or duplicate_name_hash_keys_in_page:
  544. duplicate_group_conditions = []
  545. if duplicate_hashes_in_page:
  546. duplicate_group_conditions.append(PrintArchive.content_hash.in_(duplicate_hashes_in_page))
  547. if duplicate_name_hash_keys_in_page:
  548. name_hash_conditions = [
  549. and_(func.lower(PrintArchive.print_name) == name, PrintArchive.content_hash == hash_)
  550. for name, hash_ in duplicate_name_hash_keys_in_page
  551. ]
  552. duplicate_group_conditions.extend(name_hash_conditions)
  553. duplicate_group_rows = await db.execute(
  554. select(
  555. PrintArchive.id,
  556. PrintArchive.created_at,
  557. PrintArchive.content_hash,
  558. func.lower(PrintArchive.print_name).label("print_name_lower"),
  559. ).where(or_(*duplicate_group_conditions), PrintArchive.deleted_at.is_(None), *visible)
  560. )
  561. duplicate_groups_by_hash: dict[str, list[tuple[int, datetime]]] = defaultdict(list)
  562. duplicate_groups_by_name_hash: dict[tuple[str, str], list[tuple[int, datetime]]] = defaultdict(list)
  563. for archive_id, created_at, content_hash, print_name_lower in duplicate_group_rows.all():
  564. if content_hash and content_hash in duplicate_hashes_in_page:
  565. duplicate_groups_by_hash[content_hash].append((archive_id, created_at))
  566. if (
  567. print_name_lower
  568. and content_hash
  569. and (print_name_lower, content_hash) in duplicate_name_hash_keys_in_page
  570. ):
  571. duplicate_groups_by_name_hash[(print_name_lower, content_hash)].append((archive_id, created_at))
  572. for group in duplicate_groups_by_hash.values():
  573. if len(group) < 2:
  574. continue
  575. group.sort(key=lambda x: x[1])
  576. original_id = group[0][0]
  577. duplicate_count = len(group) - 1
  578. for sequence, (archive_id, _) in enumerate(group):
  579. duplicate_meta_by_archive_id[archive_id] = (sequence, original_id, duplicate_count)
  580. # Keep hash-based grouping precedence; name/hash groups only fill missing items.
  581. for group in duplicate_groups_by_name_hash.values():
  582. if len(group) < 2:
  583. continue
  584. group.sort(key=lambda x: x[1])
  585. original_id = group[0][0]
  586. duplicate_count = len(group) - 1
  587. for sequence, (archive_id, _) in enumerate(group):
  588. duplicate_meta_by_archive_id.setdefault(archive_id, (sequence, original_id, duplicate_count))
  589. run_aggregates = await _load_run_aggregates(db, [a.id for a in archives], printer_scope)
  590. views = await scoped_views(db, archives, printer_scope)
  591. # Build response with duplicate sequence and original archive ID pre-computed
  592. result = []
  593. for a in archives:
  594. has_hash_dup = a.content_hash in duplicate_hashes if a.content_hash else False
  595. has_name_dup = (
  596. bool(a.print_name and a.content_hash)
  597. and (a.print_name.lower(), a.content_hash) in duplicate_name_hash_pairs
  598. )
  599. has_duplicate = has_hash_dup or has_name_dup
  600. # Pre-compute duplicate sequence and original archive ID
  601. duplicate_sequence = 0
  602. original_archive_id: int | None = None
  603. duplicate_count = 1 if has_duplicate else 0
  604. if has_duplicate and a.id in duplicate_meta_by_archive_id:
  605. duplicate_sequence, original_archive_id, duplicate_count = duplicate_meta_by_archive_id[a.id]
  606. result.append(
  607. _scoped(
  608. archive_to_response(
  609. a,
  610. duplicate_count=duplicate_count,
  611. duplicate_sequence=duplicate_sequence,
  612. original_archive_id=original_archive_id,
  613. run_aggregate=run_aggregates.get(a.id),
  614. ),
  615. views.get(a.id),
  616. )
  617. )
  618. return result
  619. @router.get("/last-per-printer", response_model=list[ArchiveResponse])
  620. async def list_last_archive_per_printer(
  621. db: AsyncSession = Depends(get_db, scope="function"),
  622. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  623. require_ownership_permission(
  624. Permission.ARCHIVES_READ_ALL,
  625. Permission.ARCHIVES_READ_OWN,
  626. )
  627. ),
  628. printer_scope: PrinterScope = RequestPrinterScope,
  629. ):
  630. """The latest print of every printer, for the printer cards: its archive
  631. as that run printed (``run_id``, its outcome and verdict).
  632. By the printer's latest run: plates of one archive on two printers are
  633. each their printer's last print, while the archive names only the one
  634. started last. A printer with no run logged gets its latest archive, as
  635. before runs. One query for the whole Printers page instead of one list
  636. request per card. Leaves out the duplicate detection the full listing
  637. does: the card shows a name and an outcome prompt, and on a farm that scan
  638. ran once per printer on every page load.
  639. """
  640. from sqlalchemy.orm import selectinload
  641. user, can_read_all = auth_result
  642. filters: list[ColumnElement[bool]] = [PrintArchive.deleted_at.is_(None)]
  643. if user is not None and not can_read_all:
  644. filters.append(PrintArchive.created_by_id == user.id)
  645. run_filters: list[ColumnElement[bool]] = [PrintLogEntry.printer_id.isnot(None)]
  646. # Only printers the caller may see (#1727): the run's. Its archive is
  647. # theirs to see by that run (archive redesign).
  648. if (clause := printer_scope.where(PrintLogEntry.printer_id)) is not None:
  649. run_filters.append(clause)
  650. ranked_runs = (
  651. select(
  652. PrintLogEntry.id,
  653. func.row_number()
  654. .over(
  655. partition_by=PrintLogEntry.printer_id,
  656. # As the archives below: a run without a start (logged
  657. # without one) by when it was logged.
  658. order_by=(
  659. func.coalesce(PrintLogEntry.started_at, PrintLogEntry.created_at).desc(),
  660. PrintLogEntry.id.desc(),
  661. ),
  662. )
  663. .label("rn"),
  664. )
  665. .join(PrintArchive, PrintArchive.id == PrintLogEntry.archive_id)
  666. .where(*run_filters, *filters)
  667. .subquery()
  668. )
  669. # The print running now counts: its card shows it, not the one before.
  670. runs = (
  671. await db.scalars(
  672. select(PrintLogEntry)
  673. .where(PrintLogEntry.id.in_(select(ranked_runs.c.id).where(ranked_runs.c.rn == 1)))
  674. .execution_options(**{INCLUDE_OPEN_RUNS: True})
  675. )
  676. ).all()
  677. printers_with_runs = {r.printer_id for r in runs}
  678. archive_filters: list[ColumnElement[bool]] = [*filters, PrintArchive.printer_id.isnot(None)]
  679. if (clause := printer_scope.where(PrintArchive.printer_id)) is not None:
  680. archive_filters.append(clause)
  681. if printers_with_runs:
  682. archive_filters.append(PrintArchive.printer_id.not_in(printers_with_runs))
  683. ranked = (
  684. select(
  685. PrintArchive.id,
  686. func.row_number()
  687. .over(
  688. partition_by=PrintArchive.printer_id,
  689. # A reprint reuses its archive row, moving it to the printer it
  690. # runs on with a fresh started_at, so the latest run start, not
  691. # the row's age, tells which print a printer ran last.
  692. order_by=(
  693. func.coalesce(PrintArchive.started_at, PrintArchive.created_at).desc(),
  694. PrintArchive.id.desc(),
  695. ),
  696. )
  697. .label("rn"),
  698. )
  699. .where(*archive_filters)
  700. .subquery()
  701. )
  702. without_runs = set((await db.scalars(select(ranked.c.id).where(ranked.c.rn == 1))).all())
  703. archive_ids = {r.archive_id for r in runs} | without_runs
  704. result = await db.execute(
  705. select(PrintArchive)
  706. .options(selectinload(PrintArchive.project), selectinload(PrintArchive.created_by))
  707. .where(PrintArchive.id.in_(archive_ids))
  708. )
  709. archives = {a.id: a for a in result.scalars().all()}
  710. run_aggregates = await _load_run_aggregates(db, list(archives), printer_scope)
  711. views = await scoped_views(db, archives.values(), printer_scope)
  712. def response(archive: PrintArchive) -> dict:
  713. return _scoped(
  714. archive_to_response(archive, run_aggregate=run_aggregates.get(archive.id)), views.get(archive.id)
  715. )
  716. cards = [_with_run_outcome(response(archives[r.archive_id]), r) for r in runs if r.archive_id in archives]
  717. cards += [response(archives[i]) for i in without_runs if i in archives]
  718. return sorted(cards, key=lambda card: card["printer_id"])
  719. @router.get("/no-3mf-warning")
  720. async def no_3mf_warning(
  721. db: AsyncSession = Depends(get_db, scope="function"),
  722. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  723. require_ownership_permission(
  724. Permission.ARCHIVES_READ_ALL,
  725. Permission.ARCHIVES_READ_OWN,
  726. )
  727. ),
  728. printer_scope: PrinterScope = RequestPrinterScope,
  729. ):
  730. """Whether to nudge the user about a print that archived without its 3MF,
  731. and why. True iff any archive in the last 30 days was created via the
  732. no-3MF fallback path.
  733. Also returns ``reason``, because the advice differs and the original
  734. single-cause wording sent people the wrong way. Historically the only
  735. known cause was install step 4 ("Store sent files on external storage")
  736. being off in the slicer, so the banner said so unconditionally. On
  737. H2-series, P2S and X2D that advice is actively wrong: the setting is already
  738. on and turning it on again changes nothing, because the printer keeps the
  739. sliced file on internal storage that FTPS does not serve at all (#2780).
  740. ``reason`` is the slug from :mod:`print_storage` when we recorded one,
  741. else None for the original slicer-setting case. When archives disagree the
  742. most specific known reason wins — one printer storing internally is a real
  743. finding worth explaining, and it should not be masked by another printer's
  744. plain missing-file fallback.
  745. Complements the connection-diagnostic ``external_storage`` check, which
  746. only catches the printer-side variant of the setting. On older slicers
  747. where the toggle lives only in BambuStudio, the printer never reports it
  748. and the diagnostic passes — this endpoint surfaces the symptom instead.
  749. Dismissal is handled client-side via localStorage (one-shot): once the
  750. user has been told, no further nudge until they clear browser storage.
  751. The backend stays stateless.
  752. """
  753. user, can_read_all = auth_result
  754. cutoff = datetime.now(timezone.utc) - timedelta(days=30)
  755. conditions = [
  756. PrintArchive.created_at >= cutoff,
  757. PrintArchive.deleted_at.is_(None),
  758. PrintArchive.extra_data.isnot(None),
  759. ]
  760. if user is not None and not can_read_all:
  761. conditions.append(PrintArchive.created_by_id == user.id)
  762. if (scope_clause := archive_scope_clause(printer_scope)) is not None:
  763. conditions.append(scope_clause)
  764. result = await db.execute(select(PrintArchive.extra_data).where(*conditions))
  765. reasons: set[str] = set()
  766. has_fallback = False
  767. for (extra_data,) in result.all():
  768. if not extra_data or not extra_data.get("no_3mf_available"):
  769. continue
  770. has_fallback = True
  771. reason = extra_data.get("no_3mf_reason")
  772. if reason:
  773. reasons.add(reason)
  774. if not has_fallback:
  775. return {"has_fallback": False, "reason": None}
  776. # Most specific first. Archives predating this field carry no reason at
  777. # all, so an install with one H2C and three older printers still gets the
  778. # H2C explanation rather than the generic one.
  779. #
  780. # REASON_FTPS_COOLOFF leads, and it is the only one of these that reports a
  781. # fault rather than a choice: the printer's file service refused a TLS
  782. # handshake, so the sweep never ran and nothing about where the file went
  783. # was ever tested. The other three describe an install working as
  784. # configured, and each ends in something the operator can change. This one
  785. # ends in "your printer is doing something we cannot yet explain", which is
  786. # both the more urgent thing to say and the thing that produces a useful
  787. # report. It also has to outrank them because the banner dismisses one-shot
  788. # into localStorage: a reason ranked below another is not merely deferred,
  789. # it is never shown to that user again (#2780).
  790. #
  791. # Ranking it first cannot mask a permanent cause, because a cool-off row is
  792. # not permanent. The retry #2957 schedules clears the row's markers when it
  793. # lands, so a row still carrying this slug is one where the retry failed too
  794. # -- a printer whose file service is still refusing, days later.
  795. #
  796. # REASON_FTP_TRANSFER_FAILED sits second for the same reasons and one more:
  797. # it is the only slug here whose remedy is a Bambuddy setting rather than a
  798. # slicer one or a card. It ranks below the cool-off because a printer that
  799. # will not complete a TLS handshake is the worse fault of the two, and its
  800. # own retry (#3063) clears the row the same way, so a row still carrying
  801. # this slug is one where three later attempts also ran out of time.
  802. #
  803. # REASON_INTERNAL_HISTORY comes last on purpose, even though it is the
  804. # narrowest: it is the one cause with no remedy at all -- the file was
  805. # already on the printer, in an area port 990 does not serve. The two ahead
  806. # of it each end in something the operator can do, so when an install has
  807. # both, the actionable explanation is the one worth the banner (#1820).
  808. for candidate in (
  809. REASON_FTPS_COOLOFF,
  810. REASON_FTP_TRANSFER_FAILED,
  811. REASON_INTERNAL_STORAGE,
  812. REASON_NO_EXTERNAL_STORAGE,
  813. REASON_INTERNAL_HISTORY,
  814. ):
  815. if candidate in reasons:
  816. return {"has_fallback": True, "reason": candidate}
  817. return {"has_fallback": True, "reason": None}
  818. @router.get("/slim", response_model=list[ArchiveSlim])
  819. async def list_archives_slim(
  820. date_from: date | None = Query(None),
  821. date_to: date | None = Query(None),
  822. tz: str | None = Query(
  823. None, max_length=64, description="IANA zone the dates are calendar days in; the server's when omitted"
  824. ),
  825. created_by_id: int | None = Query(None, description="Filter by user who created the print (-1 for no user)"),
  826. limit: int = Query(default=10000, le=50000),
  827. offset: int = 0,
  828. db: AsyncSession = Depends(get_db, scope="function"),
  829. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  830. require_ownership_permission(
  831. Permission.ARCHIVES_READ_ALL,
  832. Permission.ARCHIVES_READ_OWN,
  833. )
  834. ),
  835. printer_scope: PrinterScope = RequestPrinterScope,
  836. ):
  837. """Per-event listing for stats/dashboard widgets.
  838. Reads from print_log_entries so reprints contribute each run and
  839. orphaned events (archive deleted, log row survived via ON DELETE
  840. SET NULL) still aggregate consistently with Quick Stats. The sliced
  841. print_time_seconds is joined from the archive when available; for
  842. orphan events it is null and downstream widgets fall back to the
  843. measured duration_seconds.
  844. """
  845. from backend.app.models.print_log import PrintLogEntry
  846. current_user, can_read_all = auth_result
  847. _validate_user_filter_permission(current_user, created_by_id)
  848. # Callers without ARCHIVES_READ_ALL can only see their own runs — pin
  849. # the filter unconditionally so a caller-supplied ?created_by_id=
  850. # can't widen the listing past their own scope. The existing
  851. # _validate_user_filter_permission rejects ?created_by_id= without
  852. # STATS_FILTER_BY_USER, so the only way to reach this is owner-self
  853. # filtering anyway, but pinning here is the fail-closed default.
  854. if current_user is not None and not can_read_all:
  855. created_by_id = current_user.id
  856. filters = []
  857. if date_from:
  858. dt_from = local_date_start(date_from, day_zone(tz))
  859. filters.append(PrintLogEntry.created_at >= dt_from)
  860. if date_to:
  861. dt_to = local_date_end(date_to, day_zone(tz))
  862. filters.append(PrintLogEntry.created_at <= dt_to)
  863. _apply_run_user_filter(filters, created_by_id)
  864. if (clause := printer_scope.where(PrintLogEntry.printer_id)) is not None:
  865. filters.append(clause)
  866. query = (
  867. select(
  868. PrintLogEntry.printer_id,
  869. PrintLogEntry.print_name,
  870. PrintArchive.print_time_seconds,
  871. PrintLogEntry.started_at,
  872. PrintLogEntry.completed_at,
  873. PrintLogEntry.duration_seconds,
  874. PrintLogEntry.filament_used_grams,
  875. PrintLogEntry.filament_type,
  876. PrintLogEntry.filament_color,
  877. PrintLogEntry.status,
  878. PrintLogEntry.cost,
  879. PrintLogEntry.energy_kwh,
  880. PrintLogEntry.energy_cost,
  881. PrintLogEntry.wear_cost,
  882. PrintLogEntry.created_at,
  883. )
  884. .outerjoin(PrintArchive, PrintArchive.id == PrintLogEntry.archive_id)
  885. .where(*filters)
  886. .order_by(PrintLogEntry.created_at.desc())
  887. .limit(limit)
  888. .offset(offset)
  889. )
  890. result = await db.execute(query)
  891. rows = result.all()
  892. return [
  893. {
  894. "printer_id": r.printer_id,
  895. "print_name": r.print_name,
  896. "print_time_seconds": r.print_time_seconds,
  897. "actual_time_seconds": (
  898. # Measured elapsed time for every status (#1390): failed /
  899. # cancelled prints still ran for some duration, and Quick
  900. # Stats already counts that. Widgets that fall back to
  901. # print_time_seconds (slicer estimate) for non-completed
  902. # events would diverge from Quick Stats — so expose the
  903. # measured value here unconditionally.
  904. #
  905. # Trust an explicit 0 (reconciled aborts store it deliberately;
  906. # their real end time is unknown) instead of recomputing the
  907. # multi-day disconnect gap from the timestamps (#2592).
  908. r.duration_seconds
  909. if r.duration_seconds is not None
  910. else (
  911. int((r.completed_at - r.started_at).total_seconds())
  912. if r.started_at and r.completed_at and (r.completed_at - r.started_at).total_seconds() > 0
  913. else None
  914. )
  915. ),
  916. "filament_used_grams": r.filament_used_grams,
  917. "filament_type": r.filament_type,
  918. "filament_color": r.filament_color,
  919. "status": r.status,
  920. "started_at": r.started_at,
  921. "completed_at": r.completed_at,
  922. "cost": r.cost,
  923. "energy_kwh": r.energy_kwh,
  924. "energy_cost": r.energy_cost,
  925. "wear_cost": r.wear_cost,
  926. "quantity": 1,
  927. "created_at": r.created_at,
  928. }
  929. for r in rows
  930. ]
  931. @router.get("/search", response_model=list[ArchiveResponse])
  932. async def search_archives(
  933. q: str = Query(..., min_length=2, description="Search query"),
  934. printer_id: int | None = None,
  935. project_id: int | None = None,
  936. status: str | None = None,
  937. limit: int = 50,
  938. offset: int = 0,
  939. db: AsyncSession = Depends(get_db, scope="function"),
  940. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  941. require_ownership_permission(
  942. Permission.ARCHIVES_READ_ALL,
  943. Permission.ARCHIVES_READ_OWN,
  944. )
  945. ),
  946. printer_scope: PrinterScope = RequestPrinterScope,
  947. ):
  948. """Full-text search across archives.
  949. Searches print_name, filename, tags, notes, designer, and filament_type fields.
  950. Supports partial matches with wildcards (e.g., 'vor*' matches 'voron').
  951. """
  952. from sqlalchemy import text
  953. from sqlalchemy.orm import selectinload
  954. from backend.app.core.db_dialect import is_sqlite
  955. user, can_read_all = auth_result
  956. own_only = user is not None and not can_read_all
  957. search_term = q.strip()
  958. # Build dialect-specific full-text search query
  959. if is_sqlite():
  960. # SQLite FTS5: wildcard suffix for partial matches
  961. if not search_term.endswith("*"):
  962. search_term = f"{search_term}*"
  963. fts_query = text("""
  964. SELECT rowid FROM archive_fts
  965. WHERE archive_fts MATCH :search_term
  966. ORDER BY rank
  967. LIMIT :limit OFFSET :offset
  968. """)
  969. else:
  970. # PostgreSQL: tsvector + plainto_tsquery with prefix matching
  971. fts_query = text("""
  972. SELECT id FROM print_archives
  973. WHERE to_tsvector('simple',
  974. COALESCE(print_name, '') || ' ' ||
  975. COALESCE(filename, '') || ' ' ||
  976. COALESCE(tags, '') || ' ' ||
  977. COALESCE(notes, '') || ' ' ||
  978. COALESCE(designer, '') || ' ' ||
  979. COALESCE(filament_type, '')
  980. ) @@ to_tsquery('simple', :search_term)
  981. LIMIT :limit OFFSET :offset
  982. """)
  983. # Convert "benchy" to "benchy:*" for prefix matching in tsquery
  984. search_term = " & ".join(f"{word}:*" for word in search_term.split() if word)
  985. try:
  986. result = await db.execute(fts_query, {"search_term": search_term, "limit": limit + 100, "offset": 0})
  987. matched_ids = [row[0] for row in result.fetchall()]
  988. except Exception as e:
  989. logger.warning("FTS search failed, falling back to LIKE search: %s", e)
  990. # Fallback to LIKE search if FTS fails
  991. like_pattern = f"%{q}%"
  992. query = (
  993. select(PrintArchive)
  994. .options(selectinload(PrintArchive.project))
  995. .where(
  996. (
  997. (PrintArchive.print_name.ilike(like_pattern))
  998. | (PrintArchive.filename.ilike(like_pattern))
  999. | (PrintArchive.tags.ilike(like_pattern))
  1000. | (PrintArchive.notes.ilike(like_pattern))
  1001. | (PrintArchive.designer.ilike(like_pattern))
  1002. | (PrintArchive.filament_type.ilike(like_pattern))
  1003. ),
  1004. PrintArchive.deleted_at.is_(None),
  1005. )
  1006. .order_by(PrintArchive.created_at.desc())
  1007. )
  1008. if printer_id:
  1009. query = query.where(PrintArchive.printer_id == printer_id)
  1010. if project_id:
  1011. query = query.where(PrintArchive.project_id == project_id)
  1012. if status:
  1013. query = query.where(PrintArchive.status == status)
  1014. if own_only:
  1015. query = query.where(PrintArchive.created_by_id == user.id)
  1016. if (clause := archive_scope_clause(printer_scope)) is not None:
  1017. query = query.where(clause)
  1018. query = query.limit(limit).offset(offset)
  1019. result = await db.execute(query)
  1020. archives = result.scalars().all()
  1021. # Load run aggregates so multi-run archives' time/accuracy badge is
  1022. # suppressed consistently with the main list endpoint (#1608).
  1023. run_aggregates = await _load_run_aggregates(db, [a.id for a in archives], printer_scope)
  1024. views = await scoped_views(db, archives, printer_scope)
  1025. return [
  1026. _scoped(archive_to_response(a, run_aggregate=run_aggregates.get(a.id)), views.get(a.id)) for a in archives
  1027. ]
  1028. if not matched_ids:
  1029. return []
  1030. # Fetch full archive records for matched IDs (excluding soft-deleted, #1343)
  1031. query = (
  1032. select(PrintArchive)
  1033. .options(selectinload(PrintArchive.project))
  1034. .where(PrintArchive.id.in_(matched_ids), PrintArchive.deleted_at.is_(None))
  1035. )
  1036. if own_only:
  1037. query = query.where(PrintArchive.created_by_id == user.id)
  1038. if (clause := archive_scope_clause(printer_scope)) is not None:
  1039. query = query.where(clause)
  1040. # Apply additional filters
  1041. if printer_id:
  1042. query = query.where(PrintArchive.printer_id == printer_id)
  1043. if project_id:
  1044. query = query.where(PrintArchive.project_id == project_id)
  1045. if status:
  1046. query = query.where(PrintArchive.status == status)
  1047. result = await db.execute(query)
  1048. archives_dict = {a.id: a for a in result.scalars().all()}
  1049. # Preserve FTS ranking order and apply pagination
  1050. ordered_archives = [archives_dict[id] for id in matched_ids if id in archives_dict]
  1051. paginated = ordered_archives[offset : offset + limit]
  1052. # Load run aggregates so multi-run archives' time/accuracy badge is
  1053. # suppressed consistently with the main list endpoint (#1608).
  1054. run_aggregates = await _load_run_aggregates(db, [a.id for a in paginated], printer_scope)
  1055. views = await scoped_views(db, paginated, printer_scope)
  1056. return [_scoped(archive_to_response(a, run_aggregate=run_aggregates.get(a.id)), views.get(a.id)) for a in paginated]
  1057. @router.post("/search/rebuild-index")
  1058. async def rebuild_search_index(
  1059. db: AsyncSession = Depends(get_db, scope="function"),
  1060. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  1061. ):
  1062. """Rebuild the full-text search index from existing archives.
  1063. Use this if search results seem incomplete or incorrect.
  1064. """
  1065. from sqlalchemy import text
  1066. from backend.app.core.db_dialect import is_sqlite
  1067. try:
  1068. if is_sqlite():
  1069. # SQLite: rebuild FTS5 virtual table
  1070. await db.execute(text("DELETE FROM archive_fts"))
  1071. await db.execute(
  1072. text("""
  1073. INSERT INTO archive_fts(rowid, print_name, filename, tags, notes, designer, filament_type)
  1074. SELECT id, print_name, filename, tags, notes, designer, filament_type
  1075. FROM print_archives
  1076. """)
  1077. )
  1078. await db.commit()
  1079. result = await db.execute(text("SELECT COUNT(*) FROM archive_fts"))
  1080. count = result.scalar() or 0
  1081. else:
  1082. # PostgreSQL: GIN index is auto-maintained, just reindex
  1083. await db.execute(text("REINDEX INDEX idx_archives_fulltext"))
  1084. await db.commit()
  1085. result = await db.execute(text("SELECT COUNT(*) FROM print_archives"))
  1086. count = result.scalar() or 0
  1087. return {"message": f"Search index rebuilt with {count} entries"}
  1088. except Exception as e:
  1089. logger.error("Failed to rebuild search index: %s", e)
  1090. raise HTTPException(status_code=500, detail=f"Failed to rebuild index: {str(e)}")
  1091. @router.get("/analysis/failures")
  1092. async def analyze_failures(
  1093. days: int | None = None,
  1094. date_from: date | None = Query(None),
  1095. date_to: date | None = Query(None),
  1096. tz: str | None = Query(
  1097. None, max_length=64, description="IANA zone the dates are calendar days in; the server's when omitted"
  1098. ),
  1099. printer_id: int | None = None,
  1100. project_id: int | None = None,
  1101. created_by_id: int | None = Query(None, description="Filter by user who created the print (-1 for no user)"),
  1102. db: AsyncSession = Depends(get_db, scope="function"),
  1103. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1104. require_ownership_permission(
  1105. Permission.ARCHIVES_READ_ALL,
  1106. Permission.ARCHIVES_READ_OWN,
  1107. )
  1108. ),
  1109. printer_scope: PrinterScope = RequestPrinterScope,
  1110. ):
  1111. """Analyze failure patterns across prints.
  1112. Returns failure statistics including:
  1113. - Overall failure rate
  1114. - Failures by reason, filament type, printer
  1115. - Time of day distribution
  1116. - Recent failures
  1117. - Weekly trend
  1118. """
  1119. current_user, can_read_all = auth_result
  1120. _validate_user_filter_permission(current_user, created_by_id)
  1121. # Callers without ARCHIVES_READ_ALL are scoped to their own runs (#2).
  1122. if current_user is not None and not can_read_all:
  1123. created_by_id = current_user.id
  1124. from backend.app.services.failure_analysis import FailureAnalysisService
  1125. service = FailureAnalysisService(db)
  1126. return await service.analyze_failures(
  1127. days=days,
  1128. date_from=date_from,
  1129. date_to=date_to,
  1130. printer_id=printer_id,
  1131. project_id=project_id,
  1132. created_by_id=created_by_id,
  1133. printer_scope=printer_scope,
  1134. day_zone=day_zone(tz),
  1135. )
  1136. @router.get("/compare")
  1137. async def compare_archives(
  1138. archive_ids: str = Query(..., description="Comma-separated archive IDs (2-5)"),
  1139. db: AsyncSession = Depends(get_db, scope="function"),
  1140. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1141. require_ownership_permission(
  1142. Permission.ARCHIVES_READ_ALL,
  1143. Permission.ARCHIVES_READ_OWN,
  1144. )
  1145. ),
  1146. printer_scope: PrinterScope = RequestPrinterScope,
  1147. ):
  1148. """Compare multiple archives side by side.
  1149. Compares print settings, filament usage, and print times.
  1150. Also analyzes correlation between settings and success/failure.
  1151. Args:
  1152. archive_ids: Comma-separated list of 2-5 archive IDs to compare
  1153. """
  1154. from backend.app.services.archive_comparison import ArchiveComparisonService
  1155. user, can_read_all = auth_result
  1156. # Parse and validate archive IDs
  1157. try:
  1158. ids = [int(id.strip()) for id in archive_ids.split(",")]
  1159. except ValueError:
  1160. raise HTTPException(400, "Invalid archive IDs format")
  1161. if len(ids) < 2:
  1162. raise HTTPException(400, "At least 2 archives required for comparison")
  1163. if len(ids) > 5:
  1164. raise HTTPException(400, "Maximum 5 archives can be compared at once")
  1165. # Verify the caller is allowed to see every archive in the comparison —
  1166. # one not-owned id in the list would otherwise leak its full detail block.
  1167. # _ensure_archive_visible raises 404 on the first miss (same 404 the
  1168. # single-archive endpoint would return).
  1169. if user is not None and not can_read_all:
  1170. existing = await db.execute(
  1171. select(PrintArchive.id, PrintArchive.created_by_id, PrintArchive.deleted_at).where(PrintArchive.id.in_(ids))
  1172. )
  1173. owners_by_id = {row.id: row for row in existing.all()}
  1174. for archive_id in ids:
  1175. row = owners_by_id.get(archive_id)
  1176. if row is None or row.deleted_at is not None or row.created_by_id != user.id:
  1177. raise HTTPException(404, "Archive not found")
  1178. # Every compared archive must be on a printer the caller can see (#1727).
  1179. # Not one seen by a run only: the comparison shows each archive's latest
  1180. # print as it is, which may be on a printer outside their scope.
  1181. if (scope_clause := printer_scope.where(PrintArchive.printer_id)) is not None:
  1182. seen = await db.scalar(select(func.count(PrintArchive.id)).where(PrintArchive.id.in_(ids), scope_clause))
  1183. if seen != len(set(ids)):
  1184. raise HTTPException(404, "Archive not found")
  1185. service = ArchiveComparisonService(db)
  1186. try:
  1187. return await service.compare_archives(ids)
  1188. except ValueError as e:
  1189. raise HTTPException(400, str(e))
  1190. @router.get("/export")
  1191. async def export_archives(
  1192. format: str = Query("csv", description="Export format: csv or xlsx"),
  1193. fields: str | None = Query(None, description="Comma-separated field names"),
  1194. printer_id: int | None = None,
  1195. project_id: int | None = None,
  1196. status: str | None = None,
  1197. date_from: str | None = Query(None, description="Start date (ISO format)"),
  1198. date_to: str | None = Query(None, description="End date (ISO format)"),
  1199. tz: str | None = Query(
  1200. None, max_length=64, description="IANA zone the dates are calendar days in; the server's when omitted"
  1201. ),
  1202. search: str | None = None,
  1203. db: AsyncSession = Depends(get_db, scope="function"),
  1204. printer_scope: PrinterScope = RequestPrinterScope,
  1205. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1206. require_ownership_permission(
  1207. Permission.ARCHIVES_READ_ALL,
  1208. Permission.ARCHIVES_READ_OWN,
  1209. )
  1210. ),
  1211. ):
  1212. """Export archives to CSV or Excel format.
  1213. Returns a downloadable file with archive data.
  1214. """
  1215. from datetime import datetime
  1216. from fastapi.responses import StreamingResponse
  1217. from backend.app.services.export import ExportService
  1218. user, can_read_all = auth_result
  1219. visible_to_user_id = user.id if (user is not None and not can_read_all) else None
  1220. if format not in ("csv", "xlsx"):
  1221. raise HTTPException(400, "Format must be 'csv' or 'xlsx'")
  1222. # Parse fields
  1223. field_list = None
  1224. if fields:
  1225. field_list = [f.strip() for f in fields.split(",")]
  1226. # Parse dates
  1227. date_from_dt = None
  1228. date_to_dt = None
  1229. if date_from:
  1230. try:
  1231. # A date on its own is a local calendar day (DB-C12).
  1232. if len(date_from) == 10:
  1233. date_from_dt = to_naive_utc(local_date_start(date.fromisoformat(date_from), day_zone(tz)))
  1234. else:
  1235. date_from_dt = to_naive_utc(datetime.fromisoformat(date_from))
  1236. except (ValueError, OverflowError):
  1237. raise HTTPException(400, "Invalid date_from format")
  1238. if date_to:
  1239. try:
  1240. # A date on its own ends with that day, as every other date filter
  1241. # here reads it; read as midnight it left out the whole day.
  1242. if len(date_to) == 10:
  1243. date_to_dt = to_naive_utc(local_date_end(date.fromisoformat(date_to), day_zone(tz)))
  1244. else:
  1245. date_to_dt = to_naive_utc(datetime.fromisoformat(date_to))
  1246. except (ValueError, OverflowError):
  1247. raise HTTPException(400, "Invalid date_to format")
  1248. service = ExportService(db)
  1249. try:
  1250. file_bytes, filename, content_type = await service.export_archives(
  1251. format=format,
  1252. fields=field_list,
  1253. printer_id=printer_id,
  1254. project_id=project_id,
  1255. status=status,
  1256. date_from=date_from_dt,
  1257. date_to=date_to_dt,
  1258. search=search,
  1259. visible_to_user_id=visible_to_user_id,
  1260. printer_scope=printer_scope,
  1261. )
  1262. except ImportError as e:
  1263. raise HTTPException(500, str(e))
  1264. return StreamingResponse(
  1265. io.BytesIO(file_bytes),
  1266. media_type=content_type,
  1267. headers={"Content-Disposition": build_content_disposition(filename)},
  1268. )
  1269. @router.get("/stats/export")
  1270. async def export_stats(
  1271. format: str = Query("csv", description="Export format: csv or xlsx"),
  1272. days: int = 30,
  1273. printer_id: int | None = None,
  1274. project_id: int | None = None,
  1275. created_by_id: int | None = Query(None, description="Filter by user who created the print (-1 for no user)"),
  1276. db: AsyncSession = Depends(get_db, scope="function"),
  1277. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.STATS_READ),
  1278. printer_scope: PrinterScope = RequestPrinterScope,
  1279. actor: User | ApiKeyActor | None = RequestActor,
  1280. ):
  1281. """Export statistics summary to CSV or Excel format."""
  1282. _validate_user_filter_permission(actor, created_by_id)
  1283. from fastapi.responses import StreamingResponse
  1284. from backend.app.services.export import ExportService
  1285. if format not in ("csv", "xlsx"):
  1286. raise HTTPException(400, "Format must be 'csv' or 'xlsx'")
  1287. service = ExportService(db)
  1288. try:
  1289. file_bytes, filename, content_type = await service.export_stats(
  1290. format=format,
  1291. days=days,
  1292. printer_id=printer_id,
  1293. project_id=project_id,
  1294. created_by_id=created_by_id,
  1295. printer_scope=printer_scope,
  1296. )
  1297. except ImportError as e:
  1298. raise HTTPException(500, str(e))
  1299. return StreamingResponse(
  1300. io.BytesIO(file_bytes),
  1301. media_type=content_type,
  1302. headers={"Content-Disposition": build_content_disposition(filename)},
  1303. )
  1304. @router.get("/stats", response_model=ArchiveStats)
  1305. async def get_archive_stats(
  1306. date_from: date | None = Query(None, description="Start date (inclusive), YYYY-MM-DD"),
  1307. date_to: date | None = Query(None, description="End date (inclusive), YYYY-MM-DD"),
  1308. tz: str | None = Query(
  1309. None, max_length=64, description="IANA zone the dates are calendar days in; the server's when omitted"
  1310. ),
  1311. created_by_id: int | None = Query(None, description="Filter by user who created the print (-1 for no user)"),
  1312. db: AsyncSession = Depends(get_db, scope="function"),
  1313. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.STATS_READ),
  1314. printer_scope: PrinterScope = RequestPrinterScope,
  1315. actor: User | ApiKeyActor | None = RequestActor,
  1316. ):
  1317. """Get statistics across all archives.
  1318. Stats aggregate over PrintLogEntry (one row per print event), not over
  1319. PrintArchive (one row per file). A reprint contributes a new PrintLogEntry
  1320. so its filament/cost/time/energy add to the totals instead of overwriting
  1321. the source archive's first-run values (#1378).
  1322. """
  1323. from backend.app.models.print_log import PrintLogEntry
  1324. _validate_user_filter_permission(actor, created_by_id)
  1325. # Build date filter conditions scoped to PrintLogEntry (event-time).
  1326. base_conditions = []
  1327. if date_from:
  1328. dt_from = local_date_start(date_from, day_zone(tz))
  1329. base_conditions.append(PrintLogEntry.created_at >= dt_from)
  1330. if date_to:
  1331. dt_to = local_date_end(date_to, day_zone(tz))
  1332. base_conditions.append(PrintLogEntry.created_at <= dt_to)
  1333. _apply_run_user_filter(base_conditions, created_by_id)
  1334. # Only prints on printers the caller may see (#1727)
  1335. if (clause := printer_scope.where(PrintLogEntry.printer_id)) is not None:
  1336. base_conditions.append(clause)
  1337. # Total counts (one row per print event).
  1338. total_result = await db.execute(select(func.count(PrintLogEntry.id)).where(*base_conditions))
  1339. total_prints = total_result.scalar() or 0
  1340. successful_result = await db.execute(
  1341. select(func.count(PrintLogEntry.id)).where(PrintLogEntry.status == "completed", *base_conditions)
  1342. )
  1343. successful_prints = successful_result.scalar() or 0
  1344. failed_result = await db.execute(
  1345. select(func.count(PrintLogEntry.id)).where(PrintLogEntry.status.in_(("failed", "aborted")), *base_conditions)
  1346. )
  1347. failed_prints = failed_result.scalar() or 0
  1348. # User/system-stopped prints — stopped/cancelled/skipped are distinct from
  1349. # quality failures: the user (or the queue) interrupted them, the printer
  1350. # didn't detect a fault. Bucketed separately so the Success Rate gauge
  1351. # divides by completed + failed only (a cancelled print shouldn't drag
  1352. # the gauge down), while still being visible in the breakdown so they
  1353. # don't silently vanish from Total Prints (#1390).
  1354. cancelled_result = await db.execute(
  1355. select(func.count(PrintLogEntry.id)).where(
  1356. PrintLogEntry.status.in_(("stopped", "cancelled", "skipped")), *base_conditions
  1357. )
  1358. )
  1359. cancelled_prints = cancelled_result.scalar() or 0
  1360. # Total elapsed time — PrintLogEntry stores duration_seconds directly so we
  1361. # can sum it server-side. Rows missing duration fall back to the slicer
  1362. # estimate from the archive (joined for that case only).
  1363. time_rows = await db.execute(
  1364. select(
  1365. PrintLogEntry.duration_seconds,
  1366. PrintLogEntry.started_at,
  1367. PrintLogEntry.completed_at,
  1368. ).where(*base_conditions)
  1369. )
  1370. total_seconds = 0
  1371. for duration_seconds, started_at, completed_at in time_rows.all():
  1372. # Trust an explicitly stored duration, INCLUDING 0: a reconciled abort
  1373. # stores 0 on purpose because its real end time is unknown, and the
  1374. # started_at→completed_at fallback would otherwise bank the whole
  1375. # multi-day disconnect gap as print time (#2592). Only rows with a NULL
  1376. # duration (legacy entries that never recorded one) fall back.
  1377. if duration_seconds is not None:
  1378. total_seconds += duration_seconds
  1379. elif started_at and completed_at:
  1380. elapsed = (completed_at - started_at).total_seconds()
  1381. if elapsed > 0:
  1382. total_seconds += int(elapsed)
  1383. total_time = total_seconds / 3600 # Convert to hours
  1384. filament_result = await db.execute(
  1385. select(func.coalesce(func.sum(PrintLogEntry.filament_used_grams), 0)).where(*base_conditions)
  1386. )
  1387. total_filament = filament_result.scalar() or 0
  1388. cost_result = await db.execute(select(func.sum(PrintLogEntry.cost)).where(*base_conditions))
  1389. total_cost = cost_result.scalar() or 0
  1390. wear_result = await db.execute(select(func.sum(PrintLogEntry.wear_cost)).where(*base_conditions))
  1391. total_wear_cost = wear_result.scalar() or 0
  1392. # By filament type (split comma-separated values for multi-material prints)
  1393. filament_type_result = await db.execute(
  1394. select(PrintLogEntry.filament_type).where(PrintLogEntry.filament_type.isnot(None), *base_conditions)
  1395. )
  1396. prints_by_filament: dict[str, int] = {}
  1397. for (filament_types,) in filament_type_result.all():
  1398. for ftype in filament_types.split(","):
  1399. ftype = ftype.strip()
  1400. if ftype:
  1401. prints_by_filament[ftype] = prints_by_filament.get(ftype, 0) + 1
  1402. # By printer
  1403. printer_result = await db.execute(
  1404. select(PrintLogEntry.printer_id, func.count(PrintLogEntry.id))
  1405. .where(*base_conditions)
  1406. .group_by(PrintLogEntry.printer_id)
  1407. )
  1408. prints_by_printer = {str(k): v for k, v in printer_result.all()}
  1409. # Names for printers the client can no longer look up. The breakdowns above
  1410. # key on the id each run recorded, and deleting a printer while keeping its
  1411. # history leaves that id pointing at nothing, so a chart that used to read
  1412. # "Ultron" fell back to "Printer 1" (#2873). Every run also stored the name
  1413. # it printed on, so the last one recorded is what that id was called. The
  1414. # client still prefers a live printer's current name, which keeps a rename
  1415. # showing up straight away.
  1416. last_named_run = (
  1417. select(func.max(PrintLogEntry.id).label("entry_id"))
  1418. .where(
  1419. PrintLogEntry.printer_id.isnot(None),
  1420. PrintLogEntry.printer_name.isnot(None),
  1421. *base_conditions,
  1422. )
  1423. .group_by(PrintLogEntry.printer_id)
  1424. .subquery()
  1425. )
  1426. name_result = await db.execute(
  1427. select(PrintLogEntry.printer_id, PrintLogEntry.printer_name).join(
  1428. last_named_run, PrintLogEntry.id == last_named_run.c.entry_id
  1429. )
  1430. )
  1431. printer_names = {str(printer_id): name for printer_id, name in name_result.all()}
  1432. # Time accuracy — compare each completed run's actual duration to the
  1433. # slicer's estimate on the linked archive. Runs without a linked archive
  1434. # (NULL archive_id) or without an estimate are excluded.
  1435. accuracy_rows = await db.execute(
  1436. select(
  1437. PrintLogEntry.duration_seconds,
  1438. PrintLogEntry.started_at,
  1439. PrintLogEntry.completed_at,
  1440. PrintLogEntry.printer_id,
  1441. PrintArchive.print_time_seconds,
  1442. )
  1443. .join(PrintArchive, PrintArchive.id == PrintLogEntry.archive_id)
  1444. .where(
  1445. PrintLogEntry.status == "completed",
  1446. PrintArchive.print_time_seconds.isnot(None),
  1447. # A run whose times were not measured is no sample (#3310).
  1448. PrintLogEntry.duration_unmeasured.is_not(True),
  1449. *base_conditions,
  1450. )
  1451. )
  1452. # Accuracy is meaningful only when the estimate roughly describes the
  1453. # work the run actually performed. Two shapes produce wildly-off ratios
  1454. # that are pure noise:
  1455. # - multi-plate ``.gcode.3mf`` printed plate-by-plate: each run's
  1456. # actual is one plate, the archive's estimate is the sum across
  1457. # plates (post-#1593 parser fix), so the ratio is roughly N×100%
  1458. # for an N-plate file. Pre-fix this shape was also broken, just
  1459. # less dramatically — the estimate was plate-1-only so the ratio
  1460. # was meaningless rather than N×.
  1461. # - manual interventions / purge waste blowing the actual far past
  1462. # the estimate.
  1463. # Clamp to the [50%, 200%] band so the printer-level average reflects
  1464. # real slicer-vs-reality drift, not multi-plate accounting or one-off
  1465. # outliers. Single-plate archives — the case the metric is actually
  1466. # designed for — stay fully included.
  1467. _ACCURACY_BAND_LO = 50.0
  1468. _ACCURACY_BAND_HI = 200.0
  1469. average_accuracy = None
  1470. accuracy_by_printer: dict[str, float] = {}
  1471. accuracies: list[float] = []
  1472. printer_accuracies: dict[str, list[float]] = {}
  1473. for duration_seconds, started_at, completed_at, run_printer_id, estimate_seconds in accuracy_rows.all():
  1474. actual_seconds = duration_seconds
  1475. if not actual_seconds and started_at and completed_at:
  1476. elapsed = (completed_at - started_at).total_seconds()
  1477. actual_seconds = int(elapsed) if elapsed > 0 else None
  1478. if not actual_seconds or not estimate_seconds:
  1479. continue
  1480. accuracy = (estimate_seconds / actual_seconds) * 100
  1481. if accuracy < _ACCURACY_BAND_LO or accuracy > _ACCURACY_BAND_HI:
  1482. continue
  1483. accuracies.append(accuracy)
  1484. printer_key = str(run_printer_id) if run_printer_id else "unknown"
  1485. printer_accuracies.setdefault(printer_key, []).append(accuracy)
  1486. if accuracies:
  1487. average_accuracy = round(sum(accuracies) / len(accuracies), 1)
  1488. for printer_key, accs in printer_accuracies.items():
  1489. accuracy_by_printer[printer_key] = round(sum(accs) / len(accs), 1)
  1490. # Energy totals - check which mode to use
  1491. from backend.app.api.routes.settings import get_setting
  1492. from backend.app.services.energy_price import (
  1493. all_time_cost,
  1494. cost_at_average_price,
  1495. snapshot_cost,
  1496. stored_price,
  1497. )
  1498. energy_tracking_mode = await get_setting(db, "energy_tracking_mode") or "total"
  1499. # The last price known. Not read from Home Assistant here: the snapshot
  1500. # loop refreshes it hourly, and it only prices the energy since then.
  1501. energy_cost_per_kwh = await stored_price(db)
  1502. total_energy_kwh: float = 0.0
  1503. total_energy_cost: float = 0.0
  1504. energy_data_warming_up = False
  1505. if energy_tracking_mode == "total" and not date_from and not date_to:
  1506. # All-time total consumption — read live lifetime counters, costed at
  1507. # the price of each hour the snapshots cover (#1251).
  1508. total_energy_kwh = await _sum_live_plug_totals(db)
  1509. total_energy_cost = await all_time_cost(db, total_energy_kwh, energy_cost_per_kwh)
  1510. elif energy_tracking_mode == "total":
  1511. # Total consumption mode with a date filter (#941): use hourly snapshots
  1512. # to compute per-plug (endpoint - baseline) deltas.
  1513. dt_from = local_date_start(date_from, day_zone(tz)) if date_from else None
  1514. dt_to = local_date_end(date_to, day_zone(tz)) if date_to else None
  1515. total_energy_kwh, energy_data_warming_up = await _sum_snapshot_deltas(db, dt_from=dt_from, dt_to=dt_to)
  1516. costed = await snapshot_cost(db, fallback_price=energy_cost_per_kwh, dt_from=dt_from, dt_to=dt_to)
  1517. total_energy_cost = cost_at_average_price(total_energy_kwh, costed.kwh, costed.cost, energy_cost_per_kwh)
  1518. else:
  1519. # Per-print mode: sum the per-run energy column from PrintLogEntry.
  1520. energy_kwh_result = await db.execute(select(func.sum(PrintLogEntry.energy_kwh)).where(*base_conditions))
  1521. total_energy_kwh = energy_kwh_result.scalar() or 0
  1522. energy_cost_result = await db.execute(select(func.sum(PrintLogEntry.energy_cost)).where(*base_conditions))
  1523. total_energy_cost = energy_cost_result.scalar() or 0
  1524. return ArchiveStats(
  1525. total_prints=total_prints,
  1526. successful_prints=successful_prints,
  1527. failed_prints=failed_prints,
  1528. cancelled_prints=cancelled_prints,
  1529. total_print_time_hours=round(total_time, 1),
  1530. total_filament_grams=round(total_filament, 1),
  1531. total_cost=round(total_cost, 2),
  1532. prints_by_filament_type=prints_by_filament,
  1533. prints_by_printer=prints_by_printer,
  1534. printer_names=printer_names,
  1535. average_time_accuracy=average_accuracy,
  1536. time_accuracy_by_printer=accuracy_by_printer if accuracy_by_printer else None,
  1537. total_energy_kwh=round(total_energy_kwh, 3),
  1538. total_energy_cost=round(total_energy_cost, 3),
  1539. total_wear_cost=round(total_wear_cost, 2),
  1540. energy_data_warming_up=energy_data_warming_up,
  1541. )
  1542. async def _sum_live_plug_totals(db: AsyncSession) -> float:
  1543. """Sum the live lifetime counter from every smart plug.
  1544. Used for all-time "total consumption" mode. Only the current value is
  1545. available so this can't be date-filtered — use `_sum_snapshot_deltas` for
  1546. that case.
  1547. """
  1548. from backend.app.api.routes.settings import get_homeassistant_settings
  1549. from backend.app.models.smart_plug import SmartPlug
  1550. from backend.app.services.homeassistant import homeassistant_service
  1551. from backend.app.services.mqtt_relay import mqtt_relay
  1552. from backend.app.services.rest_smart_plug import rest_smart_plug_service
  1553. from backend.app.services.tasmota import tasmota_service
  1554. plugs_result = await db.execute(select(SmartPlug))
  1555. plugs = list(plugs_result.scalars().all())
  1556. ha_settings = await get_homeassistant_settings(db)
  1557. homeassistant_service.configure(ha_settings["ha_url"], ha_settings["ha_token"])
  1558. total = 0.0
  1559. for plug in plugs:
  1560. if plug.plug_type == "tasmota":
  1561. energy = await tasmota_service.get_energy(plug)
  1562. if energy and energy.get("total") is not None:
  1563. total += energy["total"]
  1564. elif plug.plug_type == "homeassistant":
  1565. energy = await homeassistant_service.get_energy(plug)
  1566. if energy and energy.get("total") is not None:
  1567. total += energy["total"]
  1568. elif plug.plug_type == "mqtt":
  1569. # MQTT plugs only expose today's counter, not lifetime.
  1570. mqtt_data = mqtt_relay.smart_plug_service.get_plug_data(plug.id)
  1571. if mqtt_data and mqtt_data.energy is not None:
  1572. total += mqtt_data.energy
  1573. elif plug.plug_type == "rest":
  1574. # A REST device that exposes only a lifetime counter (Shelly
  1575. # ``aenergy.total`` via ``rest_energy_total_path``) returns no
  1576. # ``today`` key, so read the lifetime total first, like the
  1577. # Tasmota/HA branches above. ``today`` stays as the fallback for
  1578. # devices that only have a daily counter.
  1579. energy = await rest_smart_plug_service.get_energy(plug)
  1580. if energy:
  1581. value = energy.get("total")
  1582. if value is None:
  1583. value = energy.get("today")
  1584. if value is not None:
  1585. total += value
  1586. return total
  1587. async def _sum_snapshot_deltas(
  1588. db: AsyncSession,
  1589. *,
  1590. dt_from: datetime | None,
  1591. dt_to: datetime | None,
  1592. ) -> tuple[float, bool]:
  1593. """Sum per-plug energy consumption over a date range using hourly snapshots.
  1594. For each plug:
  1595. * baseline = last snapshot at or before `dt_from` (ideal)
  1596. — if missing, fall back to the earliest snapshot ever
  1597. recorded for the plug and flag the result as warming up.
  1598. * endpoint = last snapshot at or before `dt_to` (or most recent overall)
  1599. * delta = max(0, endpoint - baseline) — clamp counter resets to 0.
  1600. Returns (total_kwh, warming_up). `warming_up = True` means at least one plug
  1601. had no baseline before `dt_from` (fresh install or fresh upgrade), so the
  1602. result undercounts the beginning of the range.
  1603. """
  1604. from backend.app.models.smart_plug import SmartPlug
  1605. from backend.app.models.smart_plug_energy_snapshot import SmartPlugEnergySnapshot
  1606. from backend.app.utils.local_time import to_naive_utc
  1607. # ``recorded_at`` is a naive column holding UTC. Binding an aware datetime
  1608. # against it raises DataError on asyncpg (SQLite silently drops the offset),
  1609. # which took the whole date-filtered energy figure down on Postgres.
  1610. dt_from = to_naive_utc(dt_from)
  1611. dt_to = to_naive_utc(dt_to)
  1612. plug_ids_result = await db.execute(select(SmartPlug.id))
  1613. plug_ids = [row[0] for row in plug_ids_result.all()]
  1614. if not plug_ids:
  1615. return 0.0, False
  1616. total = 0.0
  1617. warming_up = False
  1618. for plug_id in plug_ids:
  1619. baseline: float | None = None
  1620. if dt_from is not None:
  1621. baseline_q = await db.execute(
  1622. select(SmartPlugEnergySnapshot.lifetime_kwh)
  1623. .where(
  1624. SmartPlugEnergySnapshot.plug_id == plug_id,
  1625. SmartPlugEnergySnapshot.recorded_at <= dt_from,
  1626. )
  1627. .order_by(SmartPlugEnergySnapshot.recorded_at.desc())
  1628. .limit(1)
  1629. )
  1630. baseline = baseline_q.scalar()
  1631. if baseline is None:
  1632. # No snapshot before range start — fall back to the earliest
  1633. # snapshot ever recorded. Result undercounts the pre-first-snapshot
  1634. # portion of the range; signal that to the frontend.
  1635. earliest_q = await db.execute(
  1636. select(SmartPlugEnergySnapshot.lifetime_kwh)
  1637. .where(SmartPlugEnergySnapshot.plug_id == plug_id)
  1638. .order_by(SmartPlugEnergySnapshot.recorded_at.asc())
  1639. .limit(1)
  1640. )
  1641. baseline = earliest_q.scalar()
  1642. if baseline is None:
  1643. # No snapshots at all for this plug yet.
  1644. warming_up = True
  1645. continue
  1646. warming_up = True
  1647. endpoint_conditions = [SmartPlugEnergySnapshot.plug_id == plug_id]
  1648. if dt_to is not None:
  1649. endpoint_conditions.append(SmartPlugEnergySnapshot.recorded_at <= dt_to)
  1650. endpoint_q = await db.execute(
  1651. select(SmartPlugEnergySnapshot.lifetime_kwh)
  1652. .where(*endpoint_conditions)
  1653. .order_by(SmartPlugEnergySnapshot.recorded_at.desc())
  1654. .limit(1)
  1655. )
  1656. endpoint = endpoint_q.scalar()
  1657. if endpoint is None:
  1658. continue
  1659. total += max(0.0, endpoint - baseline)
  1660. return total, warming_up
  1661. @router.get("/tags")
  1662. async def get_all_tags(
  1663. db: AsyncSession = Depends(get_db, scope="function"),
  1664. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1665. require_ownership_permission(
  1666. Permission.ARCHIVES_READ_ALL,
  1667. Permission.ARCHIVES_READ_OWN,
  1668. )
  1669. ),
  1670. printer_scope: PrinterScope = RequestPrinterScope,
  1671. ):
  1672. """List all unique tags with usage counts.
  1673. Returns a list of tags sorted by count (descending), then by name.
  1674. """
  1675. user, can_read_all = auth_result
  1676. # Query all archives with non-null tags
  1677. tag_conditions: list[ColumnElement[bool]] = [PrintArchive.tags.isnot(None), PrintArchive.deleted_at.is_(None)]
  1678. if user is not None and not can_read_all:
  1679. tag_conditions.append(PrintArchive.created_by_id == user.id)
  1680. if (scope_clause := archive_scope_clause(printer_scope)) is not None:
  1681. tag_conditions.append(scope_clause)
  1682. result = await db.execute(select(PrintArchive.tags).where(*tag_conditions))
  1683. all_tags_rows = result.all()
  1684. # Count occurrences of each tag
  1685. tag_counts: dict[str, int] = {}
  1686. for (tags_str,) in all_tags_rows:
  1687. if tags_str:
  1688. for tag in tags_str.split(","):
  1689. tag = tag.strip()
  1690. if tag:
  1691. tag_counts[tag] = tag_counts.get(tag, 0) + 1
  1692. # Convert to list and sort by count (desc), then name (asc)
  1693. tags_list = [{"name": name, "count": count} for name, count in tag_counts.items()]
  1694. tags_list.sort(key=lambda x: (-x["count"], x["name"].lower()))
  1695. return tags_list
  1696. @router.put("/tags/{tag_name}")
  1697. async def rename_tag(
  1698. tag_name: str,
  1699. request: Request,
  1700. db: AsyncSession = Depends(get_db, scope="function"),
  1701. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  1702. printer_scope: PrinterScope = RequestPrinterScope,
  1703. ):
  1704. """Rename a tag across all archives.
  1705. Request body should contain {"new_name": "new tag name"}.
  1706. Returns the count of affected archives.
  1707. """
  1708. body = await request.json()
  1709. new_name = body.get("new_name", "").strip()
  1710. if not new_name:
  1711. raise HTTPException(400, "new_name is required")
  1712. if new_name == tag_name:
  1713. return {"affected": 0}
  1714. # Find all archives containing the old tag
  1715. result = await db.execute(
  1716. select(PrintArchive).where(
  1717. PrintArchive.tags.isnot(None),
  1718. PrintArchive.deleted_at.is_(None),
  1719. *_visible_archive_clauses(None, True, printer_scope),
  1720. )
  1721. )
  1722. archives = list(result.scalars().all())
  1723. affected = 0
  1724. for archive in archives:
  1725. if not archive.tags:
  1726. continue
  1727. tags = [t.strip() for t in archive.tags.split(",")]
  1728. if tag_name in tags:
  1729. # Replace old tag with new tag
  1730. new_tags = [new_name if t == tag_name else t for t in tags]
  1731. # Remove duplicates while preserving order
  1732. seen = set()
  1733. unique_tags = []
  1734. for t in new_tags:
  1735. if t not in seen:
  1736. seen.add(t)
  1737. unique_tags.append(t)
  1738. archive.tags = ", ".join(unique_tags)
  1739. affected += 1
  1740. await db.commit()
  1741. return {"affected": affected}
  1742. @router.delete("/tags/{tag_name}")
  1743. async def delete_tag(
  1744. tag_name: str,
  1745. db: AsyncSession = Depends(get_db, scope="function"),
  1746. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  1747. printer_scope: PrinterScope = RequestPrinterScope,
  1748. ):
  1749. """Delete a tag from all archives.
  1750. Returns the count of affected archives.
  1751. """
  1752. # Find all archives containing the tag
  1753. result = await db.execute(
  1754. select(PrintArchive).where(
  1755. PrintArchive.tags.isnot(None),
  1756. PrintArchive.deleted_at.is_(None),
  1757. *_visible_archive_clauses(None, True, printer_scope),
  1758. )
  1759. )
  1760. archives = list(result.scalars().all())
  1761. affected = 0
  1762. for archive in archives:
  1763. if not archive.tags:
  1764. continue
  1765. tags = [t.strip() for t in archive.tags.split(",")]
  1766. if tag_name in tags:
  1767. # Remove the tag
  1768. new_tags = [t for t in tags if t != tag_name]
  1769. archive.tags = ", ".join(new_tags) if new_tags else None
  1770. affected += 1
  1771. await db.commit()
  1772. return {"affected": affected}
  1773. @router.get("/{archive_id}", response_model=ArchiveResponse)
  1774. async def get_archive(
  1775. archive_id: int,
  1776. db: AsyncSession = Depends(get_db, scope="function"),
  1777. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1778. require_ownership_permission(
  1779. Permission.ARCHIVES_READ_ALL,
  1780. Permission.ARCHIVES_READ_OWN,
  1781. )
  1782. ),
  1783. printer_scope: PrinterScope = RequestPrinterScope,
  1784. run_id: int | None = Query(None, description="Show this run's outcome instead of the latest run's"),
  1785. ):
  1786. """Get a specific archive. With ``run_id``, as that run of it printed."""
  1787. from backend.app.services.print_confirmation import run_of_archive
  1788. user, can_read_all = auth_result
  1789. service = ArchiveService(db)
  1790. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  1791. run = await run_of_archive(db, archive.id, run_id) if run_id is not None else None
  1792. if run_id is not None and (run is None or not printer_scope.allows(run.printer_id)):
  1793. raise HTTPException(404, "Run not found")
  1794. # Find duplicates
  1795. makerworld_id = archive.extra_data.get("makerworld_model_id") if archive.extra_data else None
  1796. duplicates = await service.find_duplicates(
  1797. archive_id=archive.id,
  1798. content_hash=archive.content_hash,
  1799. print_name=archive.print_name,
  1800. makerworld_model_id=makerworld_id,
  1801. visible=_visible_archive_clauses(user, can_read_all, printer_scope),
  1802. )
  1803. run_aggregates = await _load_run_aggregates(db, [archive.id], printer_scope)
  1804. data = archive_to_response(archive, duplicates, run_aggregate=run_aggregates.get(archive.id))
  1805. view = await scoped_view(db, archive, printer_scope)
  1806. if view is None:
  1807. return _with_run_outcome(data, run) if run is not None else data
  1808. if run is not None:
  1809. # The run asked for, in the caller's scope (checked above).
  1810. view = replace(view, run=run, shown=run)
  1811. return _scoped(data, view)
  1812. @router.get("/{archive_id}/delete-impact")
  1813. async def get_archive_delete_impact(
  1814. archive_id: int,
  1815. db: AsyncSession = Depends(get_db, scope="function"),
  1816. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1817. require_ownership_permission(
  1818. Permission.ARCHIVES_READ_ALL,
  1819. Permission.ARCHIVES_READ_OWN,
  1820. )
  1821. ),
  1822. printer_scope: PrinterScope = RequestPrinterScope,
  1823. ):
  1824. """Pre-flight for the delete-confirm modal (#1734).
  1825. Returns the number of related queue items the user is about to remove
  1826. AND whether any of them are currently printing (which would block the
  1827. delete with a 409 — surfaced to the modal so it can disable the
  1828. confirm button instead of failing on submit). Cheap, single endpoint —
  1829. not folded into the archive GET response so the much larger list
  1830. endpoint isn't forced to run the same query per row.
  1831. """
  1832. user, can_read_all = auth_result
  1833. service = ArchiveService(db)
  1834. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  1835. from backend.app.services.archive import _count_related_queue_items
  1836. total, printing = await _count_related_queue_items(db, archive.id)
  1837. return {"related_queue_items": total, "currently_printing": printing}
  1838. @router.get("/{archive_id}/runs", response_model=PrintLogResponse)
  1839. async def list_archive_runs(
  1840. archive_id: int,
  1841. db: AsyncSession = Depends(get_db, scope="function"),
  1842. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1843. require_ownership_permission(
  1844. Permission.ARCHIVES_READ_ALL,
  1845. Permission.ARCHIVES_READ_OWN,
  1846. )
  1847. ),
  1848. printer_scope: PrinterScope = RequestPrinterScope,
  1849. ):
  1850. """List PrintLogEntry rows for this archive — one per print event.
  1851. Newest first. Drives the per-archive "Print Log" view (#1378).
  1852. """
  1853. from backend.app.models.print_log import PrintLogEntry
  1854. from backend.app.schemas.print_log import PrintLogEntrySchema
  1855. user, can_read_all = auth_result
  1856. await _ensure_archive_visible(await db.get(PrintArchive, archive_id), user, can_read_all, printer_scope)
  1857. # Only the prints on the caller's printers (#1727), as the print log.
  1858. scope_clause = printer_scope.where(PrintLogEntry.printer_id)
  1859. rows = await db.execute(
  1860. select(PrintLogEntry)
  1861. .where(PrintLogEntry.archive_id == archive_id, *([scope_clause] if scope_clause is not None else []))
  1862. .order_by(PrintLogEntry.started_at.desc().nulls_last(), PrintLogEntry.id.desc())
  1863. )
  1864. entries = list(rows.scalars().all())
  1865. items = [PrintLogEntrySchema.model_validate(e, from_attributes=True) for e in entries]
  1866. return PrintLogResponse(items=items, total=len(items))
  1867. @router.get("/{archive_id}/similar")
  1868. async def find_similar_archives(
  1869. archive_id: int,
  1870. limit: int = 10,
  1871. db: AsyncSession = Depends(get_db, scope="function"),
  1872. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1873. require_ownership_permission(
  1874. Permission.ARCHIVES_READ_ALL,
  1875. Permission.ARCHIVES_READ_OWN,
  1876. )
  1877. ),
  1878. printer_scope: PrinterScope = RequestPrinterScope,
  1879. ):
  1880. """Find archives with similar settings for comparison.
  1881. Returns archives that match by:
  1882. - Same print name (highest priority)
  1883. - Same file content hash
  1884. - Same filament type
  1885. """
  1886. from backend.app.services.archive_comparison import ArchiveComparisonService
  1887. user, can_read_all = auth_result
  1888. await _ensure_archive_visible(await db.get(PrintArchive, archive_id), user, can_read_all, printer_scope)
  1889. service = ArchiveComparisonService(db)
  1890. try:
  1891. return await service.find_similar_archives(
  1892. archive_id, limit=limit, visible=_visible_archive_clauses(user, can_read_all, printer_scope, by_runs=False)
  1893. )
  1894. except ValueError as e:
  1895. raise HTTPException(404, str(e))
  1896. @router.patch("/{archive_id}", response_model=ArchiveResponse)
  1897. async def update_archive(
  1898. archive_id: int,
  1899. update_data: ArchiveUpdate,
  1900. db: AsyncSession = Depends(get_db, scope="function"),
  1901. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1902. require_ownership_permission(
  1903. Permission.ARCHIVES_UPDATE_ALL,
  1904. Permission.ARCHIVES_UPDATE_OWN,
  1905. )
  1906. ),
  1907. printer_scope: PrinterScope = RequestPrinterScope,
  1908. ):
  1909. """Update archive metadata (tags, notes, cost, filament grams, is_favorite, project_id)."""
  1910. from sqlalchemy.orm import selectinload
  1911. user, can_modify_all = auth_result
  1912. result = await db.execute(
  1913. select(PrintArchive)
  1914. .options(selectinload(PrintArchive.project), selectinload(PrintArchive.created_by))
  1915. .where(PrintArchive.id == archive_id)
  1916. )
  1917. archive = result.scalar_one_or_none()
  1918. if not archive:
  1919. raise HTTPException(404, "Archive not found")
  1920. if not printer_scope.allows(archive.printer_id) and not await printed_in_scope(db, archive, printer_scope):
  1921. raise HTTPException(404, "Archive not found")
  1922. # Ownership check
  1923. if not can_modify_all:
  1924. if archive.created_by_id != user.id:
  1925. raise HTTPException(403, "You can only update your own archives")
  1926. # Read before the writes below: the mirror needs to know whether the run's
  1927. # figure was inherited from this archive or measured on its own (#1820).
  1928. previous_filament_grams = archive.filament_used_grams
  1929. previous_cost = archive.cost
  1930. previous_status = archive.status
  1931. update_payload = update_data.model_dump(exclude_unset=True)
  1932. # Anything but its file's own notes, tags, favourite and project, or one
  1933. # of the caller's runs' outcome (run_id, checked below), is the whole
  1934. # archive's: its outcome, cost and printer are its prints' (archive
  1935. # redesign).
  1936. if update_payload.get("run_id") is None and set(update_payload) - _FILE_FIELDS - {"run_id"}:
  1937. await _ensure_whole_archive(archive, printer_scope)
  1938. # Later runs of the archive are charged from its cost and weight: a caller
  1939. # limited to their own archives sets neither. The one exception is the
  1940. # weight of an archive without its 3MF (#1820), which has none and cannot
  1941. # be printed again.
  1942. if not can_modify_all:
  1943. if "cost" in update_payload:
  1944. raise HTTPException(403, "Only users who can edit all archives can set an archive's cost")
  1945. if "filament_used_grams" in update_payload and update_payload["filament_used_grams"] != previous_filament_grams:
  1946. if previous_filament_grams is not None or archive.file_path:
  1947. raise HTTPException(403, "Only users who can edit all archives can change an archive's filament weight")
  1948. # A new printer must be one the caller may use (as on upload), and both
  1949. # targets must exist. None clears either: a printer-less archive is in
  1950. # every scope, like one uploaded without a printer.
  1951. new_printer_id = update_payload.get("printer_id")
  1952. if new_printer_id is not None and new_printer_id != archive.printer_id:
  1953. printer_scope.ensure(new_printer_id)
  1954. if await db.get(Printer, new_printer_id) is None:
  1955. raise HTTPException(400, "Printer not found")
  1956. new_project_id = update_payload.get("project_id")
  1957. if new_project_id is not None and new_project_id != archive.project_id:
  1958. if await db.get(Project, new_project_id) is None:
  1959. raise HTTPException(400, "Project not found")
  1960. # #1898: how the verdict arrived is recorded with it, never on its own.
  1961. verdict_source = update_payload.pop("user_verdict_source", None)
  1962. verdict_given = "user_verdict" in update_payload
  1963. new_verdict = update_payload.pop("user_verdict", None)
  1964. # The outcome of one run of the archive (a plate printed on its own): that
  1965. # run's, and the archive's only when it is the archive's latest.
  1966. from backend.app.services.print_confirmation import latest_run, record_verdict, run_of_archive
  1967. target_run = None
  1968. run_outcome: dict = {}
  1969. if (run_id := update_payload.pop("run_id", None)) is not None:
  1970. if set(update_payload) - _RUN_OUTCOME_FIELDS:
  1971. raise HTTPException(400, "run_id applies to a run's outcome only: its verdict, status and failure reason")
  1972. target_run = await run_of_archive(db, archive_id, run_id)
  1973. # A run printed on a printer outside the caller's scope is not theirs
  1974. # to judge, as on the print log (#1727).
  1975. if target_run is None or not printer_scope.allows(target_run.printer_id):
  1976. raise HTTPException(404, "Run not found")
  1977. run_outcome = {field: update_payload.pop(field) for field in list(update_payload)}
  1978. for field, value in update_payload.items():
  1979. setattr(archive, field, value)
  1980. # New grams are a new price, unless the caller set the cost as well.
  1981. repriced = False
  1982. if (
  1983. update_payload.get("filament_used_grams")
  1984. and "cost" not in update_payload
  1985. and update_payload["filament_used_grams"] != previous_filament_grams
  1986. and (can_modify_all or archive.cost is None)
  1987. ):
  1988. archive.cost = await _one_print_cost(db, archive, previous_grams=previous_filament_grams)
  1989. repriced = True
  1990. # #1444: Mirror per-run classification fields to the most recent
  1991. # PrintLogEntry for this archive. PrintLogEntry.failure_reason is captured
  1992. # once at print-completion time from archive.failure_reason — which is
  1993. # NULL until the user classifies the failure via the Edit Archive modal.
  1994. # Without this mirror the Failure Analysis widget (which groups by
  1995. # print_log_entries.failure_reason) keeps showing "Unknown" forever.
  1996. # Same desync hits status: flipping it in the modal wouldn't update the
  1997. # entry either. Only the latest entry is touched because that's the run
  1998. # the modal is implicitly showing (archive.failure_reason / status are
  1999. # overwritten on each reprint to reflect the latest run's outcome).
  2000. # filament_used_grams rides along for the same reason (#1820): the filament
  2001. # totals on the Projects page and in the Prometheus metrics sum the LOG
  2002. # ENTRY's grams, not the archive's, so correcting only the archive would fix
  2003. # the card and leave every aggregate reading the old figure -- or, for a
  2004. # print that archived without its 3MF, no figure at all.
  2005. # While the archive is being reprinted its newest entry is the previous
  2006. # run's, whose outcome this edit is not about.
  2007. mirror_fields = {"failure_reason", "status", "filament_used_grams"}
  2008. to_mirror = {k: v for k, v in update_payload.items() if k in mirror_fields}
  2009. if to_mirror and previous_status != "printing":
  2010. from backend.app.models.print_log import PrintLogEntry
  2011. # The latest by its start, the run the archive shows: ids follow the
  2012. # starts too, but plates on two printers can end the other way round.
  2013. latest_entry = await db.scalar(
  2014. select(PrintLogEntry)
  2015. .where(PrintLogEntry.archive_id == archive_id)
  2016. .order_by(PrintLogEntry.started_at.desc().nulls_last(), PrintLogEntry.id.desc())
  2017. .limit(1)
  2018. )
  2019. if latest_entry is not None:
  2020. # ...but never over a figure the run measured for itself. A run's
  2021. # grams come from the tracked spool delta when there is one, and
  2022. # only fall back to copying the archive's estimate when there is
  2023. # not (see _compute_run_filament_grams). Overwriting a measurement
  2024. # with a typed estimate would lose the better number; the case this
  2025. # edit exists for -- a print archived with no 3MF -- leaves the run
  2026. # with nothing at all, so it is covered by the None arm.
  2027. if "filament_used_grams" in to_mirror and not (
  2028. latest_entry.filament_used_grams is None or latest_entry.filament_used_grams == previous_filament_grams
  2029. ):
  2030. del to_mirror["filament_used_grams"]
  2031. # The run's cost was inherited with its grams, so it follows them.
  2032. if "filament_used_grams" in to_mirror and repriced:
  2033. to_mirror["cost"] = archive.cost
  2034. for field, value in to_mirror.items():
  2035. setattr(latest_entry, field, value)
  2036. # A cost set by hand reaches the run that had the archive's (statistics,
  2037. # project totals and batch sums read the run's cost).
  2038. if "cost" in update_payload and previous_status != "printing":
  2039. from backend.app.services.print_log import mirror_archive_cost
  2040. await mirror_archive_cost(db, archive, previous_cost)
  2041. if target_run is not None:
  2042. for field, value in run_outcome.items():
  2043. setattr(target_run, field, value)
  2044. # #1898: the verdict lands on its run (the one asked about, else the
  2045. # archive's latest; not while the archive is being reprinted, its latest
  2046. # being the previous print) and retires that run's one-tap token: the links
  2047. # stop changing anything once someone decided, and report the recorded
  2048. # verdict instead. Clearing the verdict drops the provenance but leaves the
  2049. # token spent: it was used.
  2050. if verdict_given:
  2051. verdict_run = target_run
  2052. if verdict_run is None and previous_status != "printing":
  2053. verdict_run = await latest_run(db, archive_id)
  2054. await record_verdict(db, archive, new_verdict, source=verdict_source or "api", run=verdict_run)
  2055. if target_run is not None:
  2056. from backend.app.services.print_log import mirror_latest_run
  2057. await db.flush()
  2058. await mirror_latest_run(db, archive_id)
  2059. await db.commit()
  2060. # Re-fetch with relationships loaded after commit
  2061. result = await db.execute(
  2062. select(PrintArchive)
  2063. .options(selectinload(PrintArchive.project), selectinload(PrintArchive.created_by))
  2064. .where(PrintArchive.id == archive_id)
  2065. )
  2066. archive = result.scalar_one_or_none()
  2067. # Load run aggregate so the time/accuracy badge stays consistent with
  2068. # the list / detail endpoints when the frontend re-renders the card
  2069. # after a PATCH (#1608).
  2070. run_aggregates = await _load_run_aggregates(db, [archive.id], printer_scope) if archive else {}
  2071. data = archive_to_response(archive, run_aggregate=run_aggregates.get(archive.id) if archive else None)
  2072. return _scoped(data, await scoped_view(db, archive, printer_scope)) if archive else data
  2073. @router.post("/{archive_id}/favorite", response_model=ArchiveResponse)
  2074. async def toggle_favorite(
  2075. archive_id: int,
  2076. db: AsyncSession = Depends(get_db, scope="function"),
  2077. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2078. require_ownership_permission(
  2079. Permission.ARCHIVES_UPDATE_ALL,
  2080. Permission.ARCHIVES_UPDATE_OWN,
  2081. )
  2082. ),
  2083. printer_scope: PrinterScope = RequestPrinterScope,
  2084. ):
  2085. """Toggle favorite status for an archive."""
  2086. user, can_modify_all = auth_result
  2087. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  2088. archive = await _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  2089. archive.is_favorite = not archive.is_favorite
  2090. await db.commit()
  2091. await db.refresh(archive)
  2092. view = await scoped_view(db, archive, printer_scope)
  2093. if view is None:
  2094. return archive
  2095. # As the list shows it to a caller limited to some printers.
  2096. from sqlalchemy.orm import selectinload
  2097. loaded = await db.scalar(
  2098. select(PrintArchive)
  2099. .options(selectinload(PrintArchive.project), selectinload(PrintArchive.created_by))
  2100. .where(PrintArchive.id == archive_id)
  2101. )
  2102. if loaded is None:
  2103. raise HTTPException(404, "Archive not found")
  2104. run_aggregates = await _load_run_aggregates(db, [archive_id], printer_scope)
  2105. return _scoped(archive_to_response(loaded, run_aggregate=run_aggregates.get(archive_id)), view)
  2106. async def _spoolman_owns_cost(db: AsyncSession) -> bool:
  2107. """True when per-spool pricing lives in Spoolman rather than in our tables.
  2108. Both cost recalculations below rebuild a print's cost from
  2109. ``SpoolUsageHistory``, and fall back to the built-in Filament catalogue or
  2110. the global default rate when there are no rows for it. In Spoolman mode a
  2111. print has rows only since #3407, when Spoolman charges started being
  2112. recorded; for an older print, or one whose charges Spoolman refused, that
  2113. fallback is not a recalculation, it is a downgrade: it would overwrite the
  2114. Spoolman-priced figure ``spoolman_tracking`` recorded at completion with a
  2115. default-rate one, and the per-slot spool resolution it came from is
  2116. transient and cannot be rebuilt here (#2591).
  2117. """
  2118. from backend.app.api.routes.settings import get_setting
  2119. setting = await get_setting(db, "spoolman_enabled")
  2120. return bool(setting) and setting.lower() == "true"
  2121. def _priced_usage_totals():
  2122. """Priced spool usage per archive. A row recorded without a price is left
  2123. out of both sums, so its grams are priced at the default rate instead of
  2124. counting as free filament."""
  2125. priced = SpoolUsageHistory.cost.is_not(None)
  2126. return (
  2127. func.sum(SpoolUsageHistory.cost),
  2128. func.sum(case((priced, SpoolUsageHistory.weight_used), else_=0.0)),
  2129. )
  2130. async def _one_print_cost(db: AsyncSession, archive: PrintArchive, previous_grams: float | None = None) -> float | None:
  2131. """The archive's cost for one print of its grams (see ``price_one_print``).
  2132. A Spoolman-priced cost has no usage rows to rebuild it from (#2591); it is
  2133. kept, scaled to new grams when they were edited.
  2134. """
  2135. from backend.app.models.print_log import PrintLogEntry
  2136. grams = archive.filament_used_grams
  2137. default_cost = await _default_cost_per_kg(db)
  2138. usage_cost, tracked_grams = (
  2139. await db.execute(select(*_priced_usage_totals()).where(SpoolUsageHistory.archive_id == archive.id))
  2140. ).one()
  2141. if usage_cost is not None and usage_cost > 0:
  2142. printed_grams, runs = (
  2143. await db.execute(
  2144. select(func.sum(PrintLogEntry.filament_used_grams), func.count(PrintLogEntry.id)).where(
  2145. PrintLogEntry.archive_id == archive.id
  2146. )
  2147. )
  2148. ).one()
  2149. return price_one_print(
  2150. grams,
  2151. float(usage_cost),
  2152. float(tracked_grams or 0),
  2153. float(printed_grams or 0),
  2154. int(runs or 0),
  2155. default_cost,
  2156. default_cost,
  2157. )
  2158. if not grams:
  2159. return archive.cost
  2160. if archive.cost is not None and await _spoolman_owns_cost(db):
  2161. if previous_grams:
  2162. return round(archive.cost * grams / previous_grams, 2)
  2163. return archive.cost
  2164. fallback = default_cost
  2165. if archive.filament_type:
  2166. primary_type = archive.filament_type.split(",")[0].strip()
  2167. catalog = await db.scalar(select(Filament.cost_per_kg).where(Filament.type == primary_type).limit(1))
  2168. if catalog is not None:
  2169. fallback = catalog
  2170. return price_one_print(grams, None, 0.0, 0.0, 0, fallback, default_cost)
  2171. @router.post("/recalculate-costs")
  2172. async def recalculate_all_costs(
  2173. db: AsyncSession = Depends(get_db, scope="function"),
  2174. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  2175. printer_scope: PrinterScope = RequestPrinterScope,
  2176. ):
  2177. """Recalculate costs for all archives based on filament usage and prices."""
  2178. from backend.app.services.print_log import mirror_archive_cost
  2179. from backend.app.services.usage_repricing import reprice_usage
  2180. # Only archives every print of which is on the caller's printers: it
  2181. # reprices the archive and its runs (archive redesign).
  2182. query = select(PrintArchive)
  2183. scoped_ids = None
  2184. if (clause := whole_archive_scope_clause(printer_scope)) is not None:
  2185. query = query.where(clause)
  2186. scoped_ids = select(PrintArchive.id).where(clause)
  2187. result = await db.execute(query)
  2188. archives = list(result.scalars().all())
  2189. # Load all filaments for lookup
  2190. filament_result = await db.execute(select(Filament))
  2191. filaments = {f.type: f.cost_per_kg for f in filament_result.scalars().all()}
  2192. default_cost_per_kg = await _default_cost_per_kg(db)
  2193. spoolman_owns = await _spoolman_owns_cost(db)
  2194. # The usage first, at the spools' current prices: the sums below read it,
  2195. # and a price corrected since the print changed nothing otherwise (#3414).
  2196. # A caller limited to some printers leaves the usage of no archive alone.
  2197. repriced = await reprice_usage(
  2198. db, scoped_ids, default_cost_per_kg=default_cost_per_kg, spoolman_enabled=spoolman_owns
  2199. )
  2200. await db.flush()
  2201. # Pre-fetch all usage costs and tracked weight by archive_id.
  2202. # Tracked weight is used to top-up the cost at the default rate for any
  2203. # filament grams not covered by an inventory spool (#1344).
  2204. usage_costs_result = await db.execute(
  2205. select(SpoolUsageHistory.archive_id, *_priced_usage_totals()).group_by(SpoolUsageHistory.archive_id)
  2206. )
  2207. usage_costs = usage_costs_result.fetchall()
  2208. cost_map = {
  2209. row[0]: (row[1], float(row[2] or 0))
  2210. for row in usage_costs
  2211. if row[0] is not None and row[1] is not None and row[1] > 0
  2212. }
  2213. from backend.app.models.print_log import PrintLogEntry
  2214. run_totals = {
  2215. row[0]: (float(row[1] or 0), int(row[2] or 0))
  2216. for row in (
  2217. await db.execute(
  2218. select(
  2219. PrintLogEntry.archive_id,
  2220. func.sum(PrintLogEntry.filament_used_grams),
  2221. func.count(PrintLogEntry.id),
  2222. ).group_by(PrintLogEntry.archive_id)
  2223. )
  2224. ).all()
  2225. }
  2226. updated = 0
  2227. preserved = 0
  2228. for archive in archives:
  2229. usage = cost_map.get(archive.id)
  2230. if usage is not None:
  2231. usage_cost, tracked_grams = usage
  2232. printed_grams, runs = run_totals.get(archive.id, (0.0, 0))
  2233. new_cost = price_one_print(
  2234. archive.filament_used_grams,
  2235. usage_cost,
  2236. tracked_grams,
  2237. printed_grams,
  2238. runs,
  2239. default_cost_per_kg,
  2240. default_cost_per_kg,
  2241. )
  2242. else:
  2243. # No usage linked to this archive. Usage without an archive link
  2244. # (recorded before usage was linked, with archiving off, or of a
  2245. # deleted archive) is not matched by print name: that charged one
  2246. # print's spool cost to every archive of the same name.
  2247. if spoolman_owns and archive.cost is not None:
  2248. # Priced from the linked Spoolman spools at completion; there is
  2249. # nothing better to recompute it from here (#2591).
  2250. new_cost = None
  2251. preserved += 1
  2252. elif archive.filament_used_grams and archive.filament_type:
  2253. primary_type = archive.filament_type.split(",")[0].strip()
  2254. cost_per_kg = filaments.get(primary_type, default_cost_per_kg)
  2255. new_cost = price_one_print(
  2256. archive.filament_used_grams, None, 0.0, 0.0, 0, cost_per_kg, default_cost_per_kg
  2257. )
  2258. else:
  2259. new_cost = None
  2260. if new_cost is not None and archive.cost != new_cost:
  2261. previous_cost = archive.cost
  2262. archive.cost = new_cost
  2263. # Its run had the old figure: statistics read the run's cost. A
  2264. # run its own usage priced keeps that, repriced above.
  2265. await mirror_archive_cost(db, archive, previous_cost, skip_runs=repriced.priced_runs)
  2266. updated += 1
  2267. await db.commit()
  2268. message = f"Recalculated costs for {updated} archives"
  2269. if preserved:
  2270. message += f"; kept {preserved} priced from Spoolman"
  2271. if repriced.spoolman_unreachable:
  2272. message += f"; Spoolman not reachable, {repriced.spoolman_unreachable} usage records kept their price"
  2273. return {
  2274. "message": message,
  2275. "updated": updated,
  2276. "preserved": preserved,
  2277. "usage_repriced": repriced.repriced,
  2278. "spoolman_unreachable": repriced.spoolman_unreachable,
  2279. }
  2280. @router.get("/{archive_id}/duplicates")
  2281. async def get_archive_duplicates(
  2282. archive_id: int,
  2283. db: AsyncSession = Depends(get_db, scope="function"),
  2284. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2285. require_ownership_permission(
  2286. Permission.ARCHIVES_READ_ALL,
  2287. Permission.ARCHIVES_READ_OWN,
  2288. )
  2289. ),
  2290. printer_scope: PrinterScope = RequestPrinterScope,
  2291. ):
  2292. """Get duplicates for a specific archive."""
  2293. user, can_read_all = auth_result
  2294. service = ArchiveService(db)
  2295. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2296. makerworld_id = archive.extra_data.get("makerworld_model_id") if archive.extra_data else None
  2297. duplicates = await service.find_duplicates(
  2298. archive_id=archive.id,
  2299. content_hash=archive.content_hash,
  2300. print_name=archive.print_name,
  2301. makerworld_model_id=makerworld_id,
  2302. visible=_visible_archive_clauses(user, can_read_all, printer_scope),
  2303. )
  2304. return {"duplicates": duplicates, "count": len(duplicates)}
  2305. @router.post("/backfill-hashes")
  2306. async def backfill_content_hashes(
  2307. db: AsyncSession = Depends(get_db, scope="function"),
  2308. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  2309. printer_scope: PrinterScope = RequestPrinterScope,
  2310. ):
  2311. """Compute and store content hashes for all archives missing them."""
  2312. result = await db.execute(
  2313. select(PrintArchive).where(
  2314. PrintArchive.content_hash.is_(None), *_visible_archive_clauses(None, True, printer_scope)
  2315. )
  2316. )
  2317. archives = list(result.scalars().all())
  2318. updated = 0
  2319. errors = []
  2320. for archive in archives:
  2321. try:
  2322. file_path = settings.base_dir / archive.file_path
  2323. if not file_path.is_file():
  2324. errors.append({"id": archive.id, "error": "File not found"})
  2325. continue
  2326. archive.content_hash = ArchiveService.compute_file_hash(file_path)
  2327. updated += 1
  2328. except Exception as e:
  2329. logger.exception("Failed to compute hash for archive %s: %s", archive.id, e)
  2330. errors.append({"id": archive.id, "error": "Failed to compute hash"})
  2331. await db.commit()
  2332. return {"updated": updated, "errors": errors}
  2333. @router.delete("/{archive_id}")
  2334. async def delete_archive(
  2335. archive_id: int,
  2336. purge_stats: bool = Query(
  2337. False,
  2338. description=(
  2339. "When false (default) the archive is soft-deleted — files removed "
  2340. "from disk, row hidden from listings, but its filament / energy / "
  2341. "time / cost contribution stays in Quick Stats. Set true to also "
  2342. "drop the row from statistics (#1343)."
  2343. ),
  2344. ),
  2345. db: AsyncSession = Depends(get_db, scope="function"),
  2346. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2347. require_ownership_permission(
  2348. Permission.ARCHIVES_DELETE_ALL,
  2349. Permission.ARCHIVES_DELETE_OWN,
  2350. )
  2351. ),
  2352. printer_scope: PrinterScope = RequestPrinterScope,
  2353. ):
  2354. """Delete an archive (soft by default; ``?purge_stats=true`` to hard-delete).
  2355. Both delete paths now cascade to related ``print_queue`` rows (#1734) —
  2356. hard delete via the ``ON DELETE CASCADE`` FK, soft delete via the
  2357. ``_delete_related_queue_items`` helper. A 409 guard blocks the delete
  2358. when any related queue item is currently mid-print so the dispatcher
  2359. doesn't lose its metadata trail under the running print.
  2360. """
  2361. user, can_modify_all = auth_result
  2362. # Get archive first to check ownership
  2363. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  2364. archive = result.scalar_one_or_none()
  2365. if not archive:
  2366. raise HTTPException(404, "Archive not found")
  2367. # Its prints on other printers would go with it (archive redesign).
  2368. await _ensure_whole_archive(archive, printer_scope)
  2369. # Ownership check
  2370. if not can_modify_all:
  2371. if archive.created_by_id != user.id:
  2372. raise HTTPException(403, "You can only delete your own archives")
  2373. # #1734: block delete when any related queue item is currently printing.
  2374. # Both soft and hard delete are gated — an in-flight print needs its
  2375. # backing archive to stay around for the metadata trail (filament,
  2376. # plate, ams_mapping). The user can stop the print first, then retry.
  2377. from backend.app.services.archive import _count_related_queue_items
  2378. _related_total, related_printing = await _count_related_queue_items(db, archive_id)
  2379. if related_printing > 0:
  2380. raise HTTPException(
  2381. 409,
  2382. f"Cannot delete archive — {related_printing} related queue item(s) are "
  2383. f"currently printing. Stop the print first, then retry.",
  2384. )
  2385. service = ArchiveService(db)
  2386. if purge_stats:
  2387. # Hard-delete the linked PrintLogEntry rows first so their filament /
  2388. # cost / count contributions disappear from /archives/stats. The FK is
  2389. # ON DELETE SET NULL, so without this delete the runs would survive
  2390. # the archive row and keep showing up in totals (#1343 / #1378).
  2391. from sqlalchemy import delete as sa_delete
  2392. from backend.app.models.print_log import PrintLogEntry
  2393. # Its open run too (a print started on the printer, with no queue item
  2394. # to refuse the delete): it has no archive left to end on.
  2395. await db.execute(
  2396. sa_delete(PrintLogEntry)
  2397. .where(PrintLogEntry.archive_id == archive_id)
  2398. .execution_options(include_open_runs=True)
  2399. )
  2400. await db.commit()
  2401. if not await service.delete_archive(archive_id):
  2402. raise HTTPException(404, "Archive not found")
  2403. return {"status": "deleted", "purged_from_stats": True}
  2404. if not await service.soft_delete_archive(archive_id):
  2405. raise HTTPException(404, "Archive not found")
  2406. return {"status": "deleted", "purged_from_stats": False}
  2407. @router.get("/{archive_id}/download")
  2408. async def download_archive(
  2409. archive_id: int,
  2410. inline: bool = False,
  2411. db: AsyncSession = Depends(get_db, scope="function"),
  2412. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2413. require_ownership_permission(
  2414. Permission.ARCHIVES_READ_ALL,
  2415. Permission.ARCHIVES_READ_OWN,
  2416. )
  2417. ),
  2418. printer_scope: PrinterScope = RequestPrinterScope,
  2419. ):
  2420. """Download the 3MF file."""
  2421. user, can_read_all = auth_result
  2422. service = ArchiveService(db)
  2423. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2424. file_path = settings.base_dir / archive.file_path
  2425. if not file_path.is_file():
  2426. raise HTTPException(404, "File not found")
  2427. # Use inline disposition to let browser/OS handle file association
  2428. content_disposition = "inline" if inline else "attachment"
  2429. return FileResponse(
  2430. path=file_path,
  2431. filename=archive.filename,
  2432. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  2433. content_disposition_type=content_disposition,
  2434. )
  2435. @router.get("/{archive_id}/file/{filename}")
  2436. async def download_archive_with_filename(
  2437. archive_id: int,
  2438. filename: str,
  2439. db: AsyncSession = Depends(get_db, scope="function"),
  2440. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2441. require_ownership_permission(
  2442. Permission.ARCHIVES_READ_ALL,
  2443. Permission.ARCHIVES_READ_OWN,
  2444. )
  2445. ),
  2446. printer_scope: PrinterScope = RequestPrinterScope,
  2447. ):
  2448. """Download the 3MF file with filename in URL."""
  2449. user, can_read_all = auth_result
  2450. service = ArchiveService(db)
  2451. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2452. file_path = settings.base_dir / archive.file_path
  2453. if not file_path.is_file():
  2454. raise HTTPException(404, "File not found")
  2455. return FileResponse(
  2456. path=file_path,
  2457. filename=archive.filename,
  2458. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  2459. )
  2460. @router.post("/{archive_id}/slicer-token")
  2461. async def create_archive_slicer_token(
  2462. archive_id: int,
  2463. db: AsyncSession = Depends(get_db, scope="function"),
  2464. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2465. require_ownership_permission(
  2466. Permission.ARCHIVES_READ_ALL,
  2467. Permission.ARCHIVES_READ_OWN,
  2468. )
  2469. ),
  2470. printer_scope: PrinterScope = RequestPrinterScope,
  2471. ):
  2472. """Create a short-lived download token for opening files in slicer applications.
  2473. Slicer protocol handlers (bambustudioopen://, orcaslicer://) cannot send
  2474. auth headers, so they use this token in the URL path instead.
  2475. """
  2476. from backend.app.core.auth import create_slicer_download_token
  2477. user, can_read_all = auth_result
  2478. service = ArchiveService(db)
  2479. await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2480. token = await create_slicer_download_token("archive", archive_id)
  2481. return {"token": token}
  2482. @router.get("/{archive_id}/dl/{token}/{filename}")
  2483. async def download_archive_for_slicer(
  2484. archive_id: int,
  2485. token: str,
  2486. filename: str,
  2487. db: AsyncSession = Depends(get_db, scope="function"),
  2488. ):
  2489. """Download 3MF file using a slicer download token.
  2490. Token-authenticated (no auth headers needed). The token is short-lived and
  2491. archive-bound, created by POST /{archive_id}/slicer-token, and redeemable
  2492. for the rest of its TTL rather than exactly once -- the slicer is a separate
  2493. process that may fetch the URL more than once (#3029).
  2494. Filename is at the end of the URL so slicers can detect the file format.
  2495. """
  2496. from backend.app.core.auth import verify_slicer_download_token
  2497. if not await verify_slicer_download_token(token, "archive", archive_id, single_use=False):
  2498. raise HTTPException(403, "Invalid or expired download token")
  2499. service = ArchiveService(db)
  2500. archive = await service.get_archive(archive_id)
  2501. if not archive:
  2502. raise HTTPException(404, "Archive not found")
  2503. file_path = settings.base_dir / archive.file_path
  2504. if not file_path.is_file():
  2505. raise HTTPException(404, "File not found")
  2506. return FileResponse(
  2507. path=file_path,
  2508. filename=archive.filename,
  2509. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  2510. )
  2511. @router.get("/{archive_id}/thumbnail")
  2512. async def get_thumbnail(
  2513. archive_id: int,
  2514. db: AsyncSession = Depends(get_db, scope="function"),
  2515. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2516. require_media_token_ownership(
  2517. Permission.ARCHIVES_READ_ALL,
  2518. Permission.ARCHIVES_READ_OWN,
  2519. )
  2520. ),
  2521. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  2522. ):
  2523. """Get the thumbnail image.
  2524. Requires a media token query param (?token=xxx) when auth is enabled, and
  2525. returns 404 for an archive the caller may not read (#3025).
  2526. """
  2527. user, can_read_all = auth_result
  2528. service = ArchiveService(db)
  2529. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2530. if not archive.thumbnail_path:
  2531. raise HTTPException(404, "Thumbnail not found")
  2532. thumb_path = settings.base_dir / archive.thumbnail_path
  2533. if not thumb_path.exists():
  2534. raise HTTPException(404, "Thumbnail file not found")
  2535. # Use file modification time as ETag to bust cache
  2536. mtime = int(thumb_path.stat().st_mtime)
  2537. return FileResponse(
  2538. path=thumb_path,
  2539. media_type="image/png",
  2540. headers={
  2541. "Cache-Control": "no-cache, must-revalidate",
  2542. "ETag": f'"{mtime}"',
  2543. },
  2544. )
  2545. @router.get("/{archive_id}/printer-media")
  2546. async def get_archive_printer_media(
  2547. archive_id: int,
  2548. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2549. require_ownership_permission(
  2550. Permission.ARCHIVES_READ_ALL,
  2551. Permission.ARCHIVES_READ_OWN,
  2552. )
  2553. ),
  2554. can_list_printer_files: bool = Depends(probe_permissions_if_auth_enabled(Permission.PRINTERS_FILES)),
  2555. printer_scope: PrinterScope = RequestPrinterScope,
  2556. ):
  2557. """Find downloadable timelapse and `/ipcam` files for one print.
  2558. Local attached timelapses are returned without touching the printer.
  2559. Printer directories are listed only when the caller also has
  2560. ``printers:files``; otherwise the local result is returned with a warning.
  2561. Files are downloaded only after the user explicitly selects them in the UI.
  2562. """
  2563. user, can_read_all = auth_result
  2564. async with database.async_session() as db:
  2565. archive = await _ensure_archive_visible(
  2566. await ArchiveService(db).get_archive(archive_id), user, can_read_all, printer_scope
  2567. )
  2568. printer = None
  2569. claimed_timelapse_stems: set[str] = set()
  2570. if archive.printer_id is not None:
  2571. printer = (await db.execute(select(Printer).where(Printer.id == archive.printer_id))).scalar_one_or_none()
  2572. if printer is not None and archive.timelapse_path is None:
  2573. claimed_timelapse_stems = await _claimed_timelapse_stems(db, archive.printer_id, archive_id)
  2574. local_timelapse = None
  2575. if archive.timelapse_path and not await _media_hidden(archive, printer_scope):
  2576. local_path = settings.base_dir / archive.timelapse_path
  2577. if await asyncio.to_thread(local_path.is_file):
  2578. local_timelapse = {
  2579. "name": local_path.name,
  2580. "size": (await asyncio.to_thread(local_path.stat)).st_size,
  2581. }
  2582. response = {
  2583. "archive_id": archive.id,
  2584. # Not a printer outside the caller's scope, seen through a run of theirs.
  2585. "printer_id": archive.printer_id if printer_scope.allows(archive.printer_id) else None,
  2586. "local_timelapse": local_timelapse,
  2587. "remote_files": [],
  2588. "warnings": [],
  2589. }
  2590. if archive.printer_id is None or archive.started_at is None:
  2591. return response
  2592. if not can_list_printer_files:
  2593. response["warnings"].append("printer_files_forbidden")
  2594. return response
  2595. if printer is None or not printer_scope.allows(printer.id):
  2596. response["warnings"].append("printer_missing")
  2597. return response
  2598. if ftps_handshake_blocked(printer.ip_address):
  2599. if local_timelapse is None:
  2600. response["warnings"].append("timelapse_unavailable")
  2601. response["warnings"].append("ipcam_unavailable")
  2602. return response
  2603. remote_files: list[dict] = []
  2604. # If no copy was attached to the archive, offer the matching printer-side
  2605. # timelapse without mutating the archive or deleting anything from the SD.
  2606. if local_timelapse is None:
  2607. videos: list[dict] = []
  2608. any_timelapse_directory_available = False
  2609. for timelapse_dir in ("/timelapse", "/timelapse/video", "/record", "/recording"):
  2610. if ftps_handshake_blocked(printer.ip_address):
  2611. break
  2612. listing = await list_files_result_async(
  2613. printer.ip_address,
  2614. printer.access_code,
  2615. timelapse_dir,
  2616. timeout=_PRINTER_MEDIA_LIST_TIMEOUT_SECONDS,
  2617. printer_model=printer.model,
  2618. )
  2619. any_timelapse_directory_available |= listing.available
  2620. candidates = [
  2621. file
  2622. for file in listing.files
  2623. if not file.get("is_directory") and str(file.get("name") or "").lower().endswith(VIDEO_SUFFIXES)
  2624. ]
  2625. if candidates:
  2626. videos = candidates
  2627. break
  2628. if not any_timelapse_directory_available:
  2629. response["warnings"].append("timelapse_unavailable")
  2630. if videos:
  2631. baseline = set(archive.timelapse_baseline or [])
  2632. eligible = [
  2633. file
  2634. for file in videos
  2635. if str(file.get("name") or "") not in baseline
  2636. and Path(str(file.get("name") or "")).stem not in claimed_timelapse_stems
  2637. ]
  2638. if archive.timelapse_baseline is not None:
  2639. candidate = eligible[0] if len(eligible) == 1 else None
  2640. else:
  2641. candidate, _ = _match_timelapse_by_timestamp(eligible, archive.started_at)
  2642. if candidate is not None:
  2643. remote_files.append(
  2644. {
  2645. "name": candidate.get("name"),
  2646. "path": candidate.get("path"),
  2647. "size": candidate.get("size") or 0,
  2648. "mtime": candidate.get("mtime"),
  2649. "kind": "timelapse",
  2650. }
  2651. )
  2652. if ftps_handshake_blocked(printer.ip_address):
  2653. response["warnings"].append("ipcam_unavailable")
  2654. response["remote_files"] = remote_files
  2655. return response
  2656. ipcam_listing = await list_files_result_async(
  2657. printer.ip_address,
  2658. printer.access_code,
  2659. "/ipcam",
  2660. timeout=_PRINTER_MEDIA_LIST_TIMEOUT_SECONDS,
  2661. printer_model=printer.model,
  2662. )
  2663. if ipcam_listing.available:
  2664. for file in match_ipcam_chunks(ipcam_listing.files, archive.started_at, archive.completed_at):
  2665. remote_files.append(
  2666. {
  2667. "name": file.get("name"),
  2668. "path": file.get("path") or f"/ipcam/{file.get('name')}",
  2669. "size": file.get("size") or 0,
  2670. "mtime": file.get("mtime"),
  2671. "kind": "ipcam",
  2672. }
  2673. )
  2674. else:
  2675. response["warnings"].append("ipcam_unavailable")
  2676. response["remote_files"] = remote_files
  2677. return response
  2678. @router.post("/{archive_id}/media-download-token")
  2679. async def create_archive_media_download_token(
  2680. archive_id: int,
  2681. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2682. require_ownership_permission(Permission.ARCHIVES_READ_ALL, Permission.ARCHIVES_READ_OWN)
  2683. ),
  2684. printer_scope: PrinterScope = RequestPrinterScope,
  2685. ):
  2686. """Mint a single-use token bound to an archive's attached timelapse."""
  2687. from backend.app.core.auth import create_slicer_download_token
  2688. user, can_read_all = auth_result
  2689. async with database.async_session() as db:
  2690. archive = await _ensure_archive_visible(
  2691. await ArchiveService(db).get_archive(archive_id), user, can_read_all, printer_scope
  2692. )
  2693. if not archive.timelapse_path:
  2694. raise HTTPException(404, "Timelapse not found")
  2695. await _ensure_media_visible(archive, printer_scope)
  2696. timelapse_path = settings.base_dir / archive.timelapse_path
  2697. if not await asyncio.to_thread(timelapse_path.is_file):
  2698. raise HTTPException(404, "Timelapse file not found")
  2699. return {
  2700. "token": await create_slicer_download_token("archive-timelapse", archive_id),
  2701. "filename": timelapse_path.name,
  2702. }
  2703. @router.get("/{archive_id}/media/dl/{token}/{filename}")
  2704. async def download_archive_media_with_token(
  2705. archive_id: int,
  2706. token: str,
  2707. filename: str,
  2708. ):
  2709. """Consume a resource-bound token and stream an attached timelapse."""
  2710. from backend.app.core.auth import verify_slicer_download_token
  2711. if not await verify_slicer_download_token(token, "archive-timelapse", archive_id):
  2712. return download_error_response(403, "This download link has already been used or has expired.")
  2713. async with database.async_session() as db:
  2714. archive = await ArchiveService(db).get_archive(archive_id)
  2715. if not archive or not archive.timelapse_path:
  2716. return download_error_response(404, "This print has no attached timelapse.")
  2717. timelapse_path = settings.base_dir / archive.timelapse_path
  2718. if not await asyncio.to_thread(timelapse_path.is_file):
  2719. return download_error_response(404, "The attached timelapse is no longer on disk.")
  2720. safe_filename = safe_download_filename(filename, fallback=timelapse_path.name)
  2721. return FileResponse(
  2722. path=timelapse_path,
  2723. filename=safe_filename,
  2724. headers={"Content-Disposition": build_content_disposition(safe_filename)},
  2725. )
  2726. @router.get("/{archive_id}/timelapse")
  2727. async def get_timelapse(
  2728. archive_id: int,
  2729. db: AsyncSession = Depends(get_db, scope="function"),
  2730. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2731. require_media_token_ownership(
  2732. Permission.ARCHIVES_READ_ALL,
  2733. Permission.ARCHIVES_READ_OWN,
  2734. )
  2735. ),
  2736. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  2737. ):
  2738. """Get the timelapse video.
  2739. Requires a media token query param (?token=xxx) when auth is enabled, and
  2740. returns 404 for an archive the caller may not read (#3025).
  2741. """
  2742. user, can_read_all = auth_result
  2743. service = ArchiveService(db)
  2744. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2745. if not archive.timelapse_path:
  2746. raise HTTPException(404, "Timelapse not found")
  2747. await _ensure_media_visible(archive, printer_scope)
  2748. timelapse_path = settings.base_dir / archive.timelapse_path
  2749. if not timelapse_path.exists():
  2750. raise HTTPException(404, "Timelapse file not found")
  2751. # Use file modification time as ETag to bust cache after processing
  2752. mtime = int(timelapse_path.stat().st_mtime)
  2753. # Detect media type from file extension (AVI from P1S before background conversion)
  2754. suffix = timelapse_path.suffix.lower()
  2755. media_type = {".mp4": "video/mp4", ".avi": "video/x-msvideo", ".mkv": "video/x-matroska"}.get(suffix, "video/mp4")
  2756. ext = suffix if suffix in (".mp4", ".avi", ".mkv") else ".mp4"
  2757. return FileResponse(
  2758. path=timelapse_path,
  2759. media_type=media_type,
  2760. filename=f"{archive.print_name or 'timelapse'}{ext}",
  2761. headers={
  2762. "Cache-Control": "no-cache, must-revalidate",
  2763. "ETag": f'"{mtime}"',
  2764. },
  2765. )
  2766. @router.delete("/{archive_id}/timelapse")
  2767. async def delete_timelapse(
  2768. archive_id: int,
  2769. db: AsyncSession = Depends(get_db, scope="function"),
  2770. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2771. require_ownership_permission(
  2772. Permission.ARCHIVES_DELETE_ALL,
  2773. Permission.ARCHIVES_DELETE_OWN,
  2774. )
  2775. ),
  2776. printer_scope: PrinterScope = RequestPrinterScope,
  2777. ):
  2778. """Remove the timelapse video from an archive."""
  2779. user, can_modify_all = auth_result
  2780. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  2781. archive = await _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  2782. await _ensure_whole_archive(archive, printer_scope)
  2783. if not archive.timelapse_path:
  2784. raise HTTPException(404, "No timelapse attached to this archive")
  2785. # Delete the file
  2786. timelapse_path = settings.base_dir / archive.timelapse_path
  2787. if timelapse_path.exists():
  2788. timelapse_path.unlink()
  2789. # Clear the path in database, on the runs that held it too
  2790. for run in await runs_with_timelapse(db, archive.id, archive.timelapse_path):
  2791. run.timelapse_path = None
  2792. archive.timelapse_path = None
  2793. archive.timelapse_plate_id = None
  2794. await db.commit()
  2795. from backend.app.services.archive import _announce_timelapse
  2796. await _announce_timelapse(archive_id, attached=False)
  2797. return {"status": "deleted"}
  2798. @router.post("/{archive_id}/timelapse/scan")
  2799. async def scan_timelapse(
  2800. archive_id: int,
  2801. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  2802. printer_scope: PrinterScope = RequestPrinterScope,
  2803. ):
  2804. """Scan printer for timelapse matching this archive and attach it."""
  2805. from backend.app.core.database import async_session
  2806. from backend.app.models.printer import Printer
  2807. from backend.app.services.bambu_ftp import (
  2808. DownloadDeadlineExceeded,
  2809. delete_archived_timelapse,
  2810. download_file_bytes_async,
  2811. ftps_handshake_blocked,
  2812. get_ftp_retry_settings,
  2813. list_files_async,
  2814. remote_file_settled,
  2815. with_ftp_retry,
  2816. )
  2817. # Read the archive + printer in a short session and release the pooled DB
  2818. # connection BEFORE the FTP scan/download below — a timelapse pull walks
  2819. # several directories and fetches a 100MB+ video, so holding Depends(get_db, scope="function")
  2820. # across it pinned one connection idle-in-transaction for minutes (#2572).
  2821. # Scalar columns stay readable on the detached rows (expire_on_commit=False);
  2822. # the attach at the end runs in its own fresh short session.
  2823. async with async_session() as db:
  2824. archive = await ArchiveService(db).get_archive(archive_id)
  2825. if not archive:
  2826. raise HTTPException(404, "Archive not found")
  2827. if archive.timelapse_path:
  2828. return {"status": "exists", "message": "Timelapse already attached"}
  2829. if not archive.printer_id:
  2830. raise HTTPException(400, "Archive has no associated printer")
  2831. printer_scope.ensure(archive.printer_id)
  2832. await _ensure_whole_archive(archive, printer_scope)
  2833. result = await db.execute(select(Printer).where(Printer.id == archive.printer_id))
  2834. printer = result.scalar_one_or_none()
  2835. if not printer:
  2836. raise HTTPException(404, "Printer not found")
  2837. # Get base name from archive filename (without .3mf extension)
  2838. base_name = Path(archive.filename).stem
  2839. # Scan timelapse directory on printer
  2840. # Different printer models use different paths
  2841. files = []
  2842. for timelapse_path in ["/timelapse", "/timelapse/video", "/record", "/recording"]:
  2843. if ftps_handshake_blocked(printer.ip_address):
  2844. break
  2845. try:
  2846. files = await list_files_async(
  2847. printer.ip_address, printer.access_code, timelapse_path, printer_model=printer.model
  2848. )
  2849. if files:
  2850. break
  2851. except Exception:
  2852. continue
  2853. if not files:
  2854. # "Couldn't reach the printer" and "the printer has no timelapse
  2855. # directory" are different problems with different fixes, and both used
  2856. # to come back as one 500 (#2780). Nothing here will work while the
  2857. # printer's file service is not answering over TLS, so say that rather
  2858. # than reporting an empty directory.
  2859. if ftps_handshake_blocked(printer.ip_address):
  2860. raise HTTPException(
  2861. 503,
  2862. f"Printer {printer.ip_address} is not answering its file service over TLS. "
  2863. "Bambuddy will try again shortly.",
  2864. )
  2865. raise HTTPException(404, "No timelapse directory found on the printer")
  2866. # Look for matching timelapse
  2867. matching_file = None
  2868. video_files = [
  2869. f for f in files if not f.get("is_directory") and f.get("name", "").lower().endswith((".mp4", ".avi"))
  2870. ]
  2871. # Strategy 0: snapshot diff against the baseline captured at print start
  2872. # (#2704). This is the same comparison the automatic scan makes, and the
  2873. # only one here that doesn't depend on the printer's clock — a printer in
  2874. # LAN-only mode can't reach Bambu's NTP server, so the timestamps in both
  2875. # the filename and the FTP mtime can be days out. One reporter's P1S was
  2876. # six and a half days off, which defeats every strategy below.
  2877. #
  2878. # When a baseline exists it is authoritative and the clock-based strategies
  2879. # are skipped entirely: they can only turn an honest "pick one yourself"
  2880. # into a confident wrong answer. Those strategies stay for archives created
  2881. # before the baseline was persisted.
  2882. used_baseline = archive.timelapse_baseline is not None
  2883. # A video another archive of this printer holds is that print's, whichever
  2884. # strategy below would pick it (a short name like "1" is in every video's
  2885. # timestamp). Picking by hand still offers it: that is how a wrong match
  2886. # is corrected.
  2887. offered = video_files
  2888. async with async_session() as db:
  2889. claimed = await _claimed_timelapse_stems(db, archive.printer_id, archive_id)
  2890. video_files = [f for f in video_files if Path(f.get("name", "")).stem not in claimed]
  2891. if used_baseline:
  2892. baseline = set(archive.timelapse_baseline)
  2893. candidates = [f for f in video_files if f.get("name", "") not in baseline]
  2894. if len(candidates) == 1:
  2895. matching_file = candidates[0]
  2896. logger.info("Matched timelapse by print-start baseline: %s", matching_file.get("name"))
  2897. elif candidates:
  2898. # Ambiguous — offer only the plausible files instead of guessing.
  2899. offered = candidates
  2900. logger.info("Baseline left %s unclaimed candidates for archive %s", len(candidates), archive_id)
  2901. else:
  2902. logger.info("Baseline shows no unclaimed new video on the printer for archive %s", archive_id)
  2903. # Strategy 1: Match by print name in filename
  2904. if not used_baseline:
  2905. for f in video_files:
  2906. fname = f.get("name", "")
  2907. if base_name.lower() in fname.lower():
  2908. matching_file = f
  2909. break
  2910. # Strategy 2: Match by timestamp proximity against print START time.
  2911. # Bambu timelapse filename embeds the print start time in printer-local clock.
  2912. # See _match_timelapse_by_timestamp for the offset-search rationale and why we
  2913. # intentionally don't try to match filename against end time here.
  2914. if not used_baseline and not matching_file and archive.started_at:
  2915. candidate, diff = _match_timelapse_by_timestamp(video_files, archive.started_at)
  2916. if candidate is not None:
  2917. matching_file = candidate
  2918. logger.info("Matched timelapse by timestamp: %s (diff: %s)", candidate.get("name"), diff)
  2919. # Strategy 3: Use file modification time from FTP listing
  2920. # This handles cases where printer's filename timestamp is wrong but file mtime is correct
  2921. if not used_baseline and not matching_file and (archive.started_at or archive.completed_at or archive.created_at):
  2922. from datetime import datetime, timedelta
  2923. _archive_start = archive.started_at
  2924. archive_end = archive.completed_at or archive.created_at
  2925. best_match = None
  2926. best_diff = timedelta(hours=24)
  2927. for f in video_files:
  2928. mtime = f.get("mtime")
  2929. if mtime:
  2930. # Timelapse file should be modified during or shortly after the print
  2931. # The mtime should be close to completion time (video finishes when print ends)
  2932. if archive_end:
  2933. diff = abs(mtime - archive_end)
  2934. if diff < best_diff:
  2935. best_diff = diff
  2936. best_match = f
  2937. logger.debug(
  2938. f"Timelapse mtime match candidate: {f.get('name')}, mtime: {mtime}, diff from end: {diff}"
  2939. )
  2940. if best_match and best_diff < timedelta(hours=2):
  2941. matching_file = best_match
  2942. logger.info("Matched timelapse by file mtime: %s (diff: %s)", best_match.get("name"), best_diff)
  2943. # Strategy 4: If only one timelapse exists and archive was recently completed, use it
  2944. # This handles cases where printer clock is wrong or timezone issues exist
  2945. if not used_baseline and not matching_file and len(video_files) == 1:
  2946. from datetime import datetime, timedelta, timezone
  2947. archive_completed = archive.completed_at or archive.created_at
  2948. if archive_completed:
  2949. if archive_completed.tzinfo is None:
  2950. archive_completed = archive_completed.replace(tzinfo=timezone.utc)
  2951. time_since_completion = datetime.now(timezone.utc) - archive_completed
  2952. # If archive was completed within the last hour, assume the single timelapse is for it
  2953. if time_since_completion < timedelta(hours=1):
  2954. matching_file = video_files[0]
  2955. logger.info("Using single timelapse file as fallback: %s", video_files[0].get("name"))
  2956. # Note: We intentionally don't use a "most recent file" fallback because
  2957. # we can't verify if timelapse was actually enabled for this print.
  2958. # Instead, return the list of available files for manual selection.
  2959. if not matching_file:
  2960. # Return available files for manual selection
  2961. available_files = [
  2962. {
  2963. "name": f.get("name"),
  2964. "path": f.get("path"),
  2965. "size": f.get("size"),
  2966. "mtime": f.get("mtime").isoformat() if f.get("mtime") else None,
  2967. }
  2968. for f in offered
  2969. ]
  2970. # Sort by mtime descending (most recent first)
  2971. available_files.sort(key=lambda x: x.get("mtime") or "", reverse=True)
  2972. return {
  2973. "status": "not_found",
  2974. "message": "No matching timelapse found - please select manually",
  2975. "available_files": available_files,
  2976. }
  2977. # Download the timelapse - use the full path from the file listing
  2978. remote_path = matching_file.get("path") or f"/timelapse/{matching_file['name']}"
  2979. # Get FTP retry settings
  2980. ftp_retry_enabled, ftp_retry_count, ftp_retry_delay, ftp_timeout = await get_ftp_retry_settings()
  2981. try:
  2982. if ftp_retry_enabled:
  2983. timelapse_data = await with_ftp_retry(
  2984. download_file_bytes_async,
  2985. printer.ip_address,
  2986. printer.access_code,
  2987. remote_path,
  2988. socket_timeout=ftp_timeout,
  2989. printer_model=printer.model,
  2990. expected_size=matching_file.get("size"),
  2991. max_retries=ftp_retry_count,
  2992. retry_delay=ftp_retry_delay,
  2993. operation_name=f"Download timelapse {matching_file['name']}",
  2994. cooloff_ip=printer.ip_address,
  2995. )
  2996. else:
  2997. timelapse_data = await download_file_bytes_async(
  2998. printer.ip_address,
  2999. printer.access_code,
  3000. remote_path,
  3001. socket_timeout=ftp_timeout,
  3002. printer_model=printer.model,
  3003. expected_size=matching_file.get("size"),
  3004. )
  3005. except DownloadDeadlineExceeded as exc:
  3006. # Not retried: the deadline already fitted the file's size.
  3007. raise HTTPException(504, "The timelapse download took longer than its size allows") from exc
  3008. if not timelapse_data:
  3009. raise HTTPException(502, "Could not download the timelapse from the printer")
  3010. # Confirm the printer has finished writing before we commit to this file and
  3011. # delete the original: matching the listing's size proves we got what it
  3012. # said, not that the file was complete (#2704).
  3013. if not await remote_file_settled(
  3014. printer.ip_address,
  3015. printer.access_code,
  3016. remote_path,
  3017. len(timelapse_data),
  3018. printer_model=printer.model,
  3019. ):
  3020. raise HTTPException(409, "The printer is still writing this video — try again in a moment")
  3021. # Attach in a fresh short session (the read session was released before FTP).
  3022. async with async_session() as db:
  3023. success = await ArchiveService(db).attach_timelapse(archive_id, timelapse_data, matching_file["name"])
  3024. if not success:
  3025. raise HTTPException(500, "Failed to attach timelapse")
  3026. # Safe now, and only now: the transfer matched the size the listing reported
  3027. # and the bytes are committed to the archive (#2704).
  3028. await delete_archived_timelapse(
  3029. printer.ip_address,
  3030. printer.access_code,
  3031. remote_path,
  3032. verified=matching_file.get("size") is not None,
  3033. printer_model=printer.model,
  3034. printer_name=printer.name,
  3035. )
  3036. return {
  3037. "status": "attached",
  3038. "message": f"Timelapse '{matching_file['name']}' attached successfully",
  3039. "filename": matching_file["name"],
  3040. }
  3041. @router.post("/{archive_id}/timelapse/select")
  3042. async def select_timelapse(
  3043. archive_id: int,
  3044. filename: str = Query(..., description="Timelapse filename to attach"),
  3045. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  3046. printer_scope: PrinterScope = RequestPrinterScope,
  3047. ):
  3048. """Manually select a timelapse from the printer to attach."""
  3049. from backend.app.core.database import async_session
  3050. from backend.app.models.printer import Printer
  3051. from backend.app.services.bambu_ftp import (
  3052. DownloadDeadlineExceeded,
  3053. delete_archived_timelapse,
  3054. download_file_bytes_async,
  3055. get_ftp_retry_settings,
  3056. list_files_async,
  3057. remote_file_settled,
  3058. with_ftp_retry,
  3059. )
  3060. # Read the archive + printer in a short session and release the pooled DB
  3061. # connection BEFORE the FTP scan/download below (#2572); scalars stay
  3062. # readable after close (expire_on_commit=False), the attach reopens one.
  3063. async with async_session() as db:
  3064. archive = await ArchiveService(db).get_archive(archive_id)
  3065. if not archive:
  3066. raise HTTPException(404, "Archive not found")
  3067. if not archive.printer_id:
  3068. raise HTTPException(400, "Archive has no associated printer")
  3069. printer_scope.ensure(archive.printer_id)
  3070. await _ensure_whole_archive(archive, printer_scope)
  3071. result = await db.execute(select(Printer).where(Printer.id == archive.printer_id))
  3072. printer = result.scalar_one_or_none()
  3073. if not printer:
  3074. raise HTTPException(404, "Printer not found")
  3075. # Find the file on the printer
  3076. files = []
  3077. remote_path = None
  3078. expected_size = None
  3079. for timelapse_dir in ["/timelapse", "/timelapse/video", "/record", "/recording"]:
  3080. try:
  3081. files = await list_files_async(
  3082. printer.ip_address, printer.access_code, timelapse_dir, printer_model=printer.model
  3083. )
  3084. for f in files:
  3085. if f.get("name") == filename:
  3086. remote_path = f.get("path") or f"{timelapse_dir}/{filename}"
  3087. expected_size = f.get("size")
  3088. break
  3089. if remote_path:
  3090. break
  3091. except Exception:
  3092. continue
  3093. if not remote_path:
  3094. raise HTTPException(404, f"Timelapse '{filename}' not found on printer")
  3095. # Download and attach
  3096. ftp_retry_enabled, ftp_retry_count, ftp_retry_delay, ftp_timeout = await get_ftp_retry_settings()
  3097. try:
  3098. if ftp_retry_enabled:
  3099. timelapse_data = await with_ftp_retry(
  3100. download_file_bytes_async,
  3101. printer.ip_address,
  3102. printer.access_code,
  3103. remote_path,
  3104. socket_timeout=ftp_timeout,
  3105. printer_model=printer.model,
  3106. expected_size=expected_size,
  3107. max_retries=ftp_retry_count,
  3108. retry_delay=ftp_retry_delay,
  3109. operation_name=f"Download timelapse {filename}",
  3110. cooloff_ip=printer.ip_address,
  3111. )
  3112. else:
  3113. timelapse_data = await download_file_bytes_async(
  3114. printer.ip_address,
  3115. printer.access_code,
  3116. remote_path,
  3117. socket_timeout=ftp_timeout,
  3118. printer_model=printer.model,
  3119. expected_size=expected_size,
  3120. )
  3121. except DownloadDeadlineExceeded as exc:
  3122. # Not retried: the deadline already fitted the file's size.
  3123. raise HTTPException(504, "The timelapse download took longer than its size allows") from exc
  3124. if not timelapse_data:
  3125. raise HTTPException(502, "Could not download the timelapse from the printer")
  3126. # Confirm the printer has finished writing before we commit to this file and
  3127. # delete the original: matching the listing's size proves we got what it
  3128. # said, not that the file was complete (#2704).
  3129. if not await remote_file_settled(
  3130. printer.ip_address,
  3131. printer.access_code,
  3132. remote_path,
  3133. len(timelapse_data),
  3134. printer_model=printer.model,
  3135. ):
  3136. raise HTTPException(409, "The printer is still writing this video — try again in a moment")
  3137. # Attach in a fresh short session (the read session was released before FTP).
  3138. async with async_session() as db:
  3139. success = await ArchiveService(db).attach_timelapse(archive_id, timelapse_data, filename)
  3140. if not success:
  3141. raise HTTPException(500, "Failed to attach timelapse")
  3142. # Safe now, and only now: the transfer matched the size the listing reported
  3143. # and the bytes are committed to the archive (#2704).
  3144. await delete_archived_timelapse(
  3145. printer.ip_address,
  3146. printer.access_code,
  3147. remote_path,
  3148. verified=expected_size is not None,
  3149. printer_model=printer.model,
  3150. printer_name=printer.name,
  3151. )
  3152. return {
  3153. "status": "attached",
  3154. "message": f"Timelapse '{filename}' attached successfully",
  3155. "filename": filename,
  3156. }
  3157. @router.post("/{archive_id}/timelapse/upload")
  3158. async def upload_timelapse(
  3159. archive_id: int,
  3160. file: UploadFile = File(...),
  3161. db: AsyncSession = Depends(get_db, scope="function"),
  3162. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  3163. printer_scope: PrinterScope = RequestPrinterScope,
  3164. ):
  3165. """Manually upload a timelapse video to an archive."""
  3166. service = ArchiveService(db)
  3167. archive = await service.get_archive(archive_id)
  3168. if not archive:
  3169. raise HTTPException(404, "Archive not found")
  3170. await _ensure_whole_archive(archive, printer_scope)
  3171. if not file.filename or not file.filename.endswith((".mp4", ".avi", ".mkv")):
  3172. raise HTTPException(400, "File must be a video file (.mp4, .avi, .mkv)")
  3173. content = await file.read()
  3174. safe_filename = _safe_filename(file.filename)
  3175. success = await service.attach_timelapse(archive_id, content, safe_filename)
  3176. if not success:
  3177. raise HTTPException(500, "Failed to attach timelapse")
  3178. return {"status": "attached", "filename": safe_filename}
  3179. @router.get("/{archive_id}/timelapse/info")
  3180. async def get_timelapse_info(
  3181. archive_id: int,
  3182. db: AsyncSession = Depends(get_db, scope="function"),
  3183. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  3184. require_ownership_permission(
  3185. Permission.ARCHIVES_READ_ALL,
  3186. Permission.ARCHIVES_READ_OWN,
  3187. )
  3188. ),
  3189. printer_scope: PrinterScope = RequestPrinterScope,
  3190. ):
  3191. """Get timelapse video metadata for editor."""
  3192. from backend.app.schemas.timelapse import TimelapseInfoResponse
  3193. from backend.app.services.timelapse_processor import TimelapseProcessor
  3194. user, can_read_all = auth_result
  3195. service = ArchiveService(db)
  3196. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  3197. if not archive.timelapse_path:
  3198. raise HTTPException(404, "Timelapse not found")
  3199. await _ensure_media_visible(archive, printer_scope)
  3200. timelapse_path = settings.base_dir / archive.timelapse_path
  3201. if not timelapse_path.exists():
  3202. raise HTTPException(404, "Timelapse file not found")
  3203. try:
  3204. processor = TimelapseProcessor(timelapse_path)
  3205. info = await processor.get_info()
  3206. return TimelapseInfoResponse(**info)
  3207. except Exception as e:
  3208. logger.error("Failed to get timelapse info: %s", e)
  3209. raise HTTPException(500, f"Failed to get video info: {str(e)}")
  3210. @router.get("/{archive_id}/timelapse/thumbnails")
  3211. async def get_timelapse_thumbnails(
  3212. archive_id: int,
  3213. count: int = Query(10, ge=1, le=30),
  3214. width: int = Query(160, ge=80, le=320),
  3215. db: AsyncSession = Depends(get_db, scope="function"),
  3216. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  3217. require_ownership_permission(
  3218. Permission.ARCHIVES_READ_ALL,
  3219. Permission.ARCHIVES_READ_OWN,
  3220. )
  3221. ),
  3222. printer_scope: PrinterScope = RequestPrinterScope,
  3223. ):
  3224. """Generate timeline thumbnail frames for visual scrubbing."""
  3225. import base64
  3226. from backend.app.schemas.timelapse import ThumbnailResponse
  3227. from backend.app.services.timelapse_processor import TimelapseProcessor
  3228. user, can_read_all = auth_result
  3229. service = ArchiveService(db)
  3230. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  3231. if not archive.timelapse_path:
  3232. raise HTTPException(404, "Timelapse not found")
  3233. await _ensure_media_visible(archive, printer_scope)
  3234. timelapse_path = settings.base_dir / archive.timelapse_path
  3235. if not timelapse_path.exists():
  3236. raise HTTPException(404, "Timelapse file not found")
  3237. try:
  3238. processor = TimelapseProcessor(timelapse_path)
  3239. thumbnails = await processor.generate_thumbnails(count, width)
  3240. return ThumbnailResponse(
  3241. thumbnails=[base64.b64encode(data).decode() for _, data in thumbnails],
  3242. timestamps=[ts for ts, _ in thumbnails],
  3243. )
  3244. except Exception as e:
  3245. logger.error("Failed to generate thumbnails: %s", e)
  3246. raise HTTPException(500, f"Failed to generate thumbnails: {str(e)}")
  3247. @router.post("/{archive_id}/timelapse/process", status_code=202)
  3248. async def process_timelapse(
  3249. archive_id: int,
  3250. trim_start: float = Form(0),
  3251. trim_end: float = Form(None),
  3252. speed: float = Form(1.0),
  3253. audio_volume: float = Form(1.0),
  3254. save_mode: str = Form("new"),
  3255. output_filename: str = Form(None),
  3256. audio: UploadFile = File(None),
  3257. db: AsyncSession = Depends(get_db, scope="function"),
  3258. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  3259. printer_scope: PrinterScope = RequestPrinterScope,
  3260. ):
  3261. """Start re-encoding the timelapse with trim, speed and optional audio.
  3262. Runs as a background job: poll ``GET .../timelapse/job`` for progress and
  3263. the result, ``DELETE .../timelapse/job`` cancels it.
  3264. """
  3265. import shutil
  3266. import tempfile
  3267. import uuid
  3268. from backend.app.schemas.timelapse import TimelapseJobResponse
  3269. from backend.app.services import timelapse_jobs
  3270. from backend.app.services.timelapse_processor import TimelapseProcessor
  3271. # Validate speed
  3272. if not 0.25 <= speed <= 4.0:
  3273. raise HTTPException(400, "Speed must be between 0.25 and 4.0")
  3274. if not 0.0 <= audio_volume <= 1.0:
  3275. raise HTTPException(400, "audio_volume must be between 0 and 1")
  3276. if save_mode not in ("replace", "new"):
  3277. raise HTTPException(400, "save_mode must be 'replace' or 'new'")
  3278. service = ArchiveService(db)
  3279. archive = await service.get_archive(archive_id)
  3280. if not archive or not archive.timelapse_path:
  3281. raise HTTPException(404, "Timelapse not found")
  3282. await _ensure_whole_archive(archive, printer_scope)
  3283. timelapse_path = settings.base_dir / archive.timelapse_path
  3284. if not timelapse_path.exists():
  3285. raise HTTPException(404, "Timelapse file not found")
  3286. current = timelapse_jobs.get_job(archive_id)
  3287. if current is not None and current.status == timelapse_jobs.RUNNING:
  3288. raise HTTPException(409, "This timelapse is already being processed")
  3289. try:
  3290. processor = TimelapseProcessor(timelapse_path)
  3291. except RuntimeError as e:
  3292. raise HTTPException(500, f"Processing failed: {e}") from None
  3293. archive_dir = timelapse_path.parent
  3294. stored_timelapse_path = archive.timelapse_path
  3295. # What is being edited, so a replace does not overwrite a timelapse removed
  3296. # or swapped for another one while the job ran.
  3297. source = timelapse_path.stat()
  3298. source_signature = (source.st_size, source.st_mtime_ns, source.st_ino)
  3299. # Determine output path. Unique temp names: two archives' jobs run at once.
  3300. if save_mode == "replace":
  3301. # Process to temp file first, then replace
  3302. output_path = Path(tempfile.gettempdir()) / f"processed_{archive_id}_{uuid.uuid4().hex}.mp4"
  3303. else:
  3304. # Save as new file alongside original
  3305. filename = output_filename or f"{archive.print_name or 'timelapse'}_edited.mp4"
  3306. # Sanitize filename - remove path separators and traversal sequences
  3307. filename = "".join(c for c in filename if c.isalnum() or c in "._- ")
  3308. # Prevent path traversal
  3309. if ".." in filename or not filename or filename.startswith("."):
  3310. filename = f"timelapse_{archive_id}_edited"
  3311. if not filename.endswith(".mp4"):
  3312. filename += ".mp4"
  3313. output_path = archive_dir / filename # SEC-PATH-OK: filename alnum-filtered + .. rejected above
  3314. # Handle audio file
  3315. audio_temp_path = None
  3316. if audio and audio.filename:
  3317. # Validate audio file extension
  3318. if not audio.filename.lower().endswith((".mp3", ".wav", ".m4a", ".aac", ".ogg")):
  3319. raise HTTPException(400, "Audio must be .mp3, .wav, .m4a, .aac, or .ogg")
  3320. audio_content = await audio.read()
  3321. # Extract and validate suffix to prevent path injection
  3322. suffix = Path(audio.filename).suffix.lower()
  3323. if suffix not in (".mp3", ".wav", ".m4a", ".aac", ".ogg"):
  3324. raise HTTPException(400, "Invalid audio file extension")
  3325. audio_temp_path = Path(tempfile.gettempdir()) / f"audio_{archive_id}_{uuid.uuid4().hex}{suffix}"
  3326. audio_temp_path.write_bytes(audio_content)
  3327. async def run(job: timelapse_jobs.TimelapseJob) -> tuple[str, str]:
  3328. try:
  3329. success = await processor.process(
  3330. output_path=output_path,
  3331. trim_start=trim_start,
  3332. trim_end=trim_end,
  3333. speed=speed,
  3334. audio_path=audio_temp_path,
  3335. audio_volume=audio_volume,
  3336. on_progress=job.set_progress,
  3337. )
  3338. if not success:
  3339. raise RuntimeError("Video processing failed")
  3340. if save_mode == "replace":
  3341. await _ensure_timelapse_unchanged(archive_id, stored_timelapse_path, timelapse_path, source_signature)
  3342. shutil.move(str(output_path), str(timelapse_path))
  3343. return stored_timelapse_path, "Timelapse replaced successfully"
  3344. return str(output_path.relative_to(settings.base_dir)), f"Saved as {output_path.name}"
  3345. finally:
  3346. if save_mode == "replace":
  3347. output_path.unlink(missing_ok=True)
  3348. if audio_temp_path:
  3349. audio_temp_path.unlink(missing_ok=True)
  3350. try:
  3351. job = timelapse_jobs.start_job(archive_id, run)
  3352. except timelapse_jobs.JobAlreadyRunning:
  3353. # Started by another request while this one read its audio upload.
  3354. if audio_temp_path:
  3355. audio_temp_path.unlink(missing_ok=True)
  3356. raise HTTPException(409, "This timelapse is already being processed") from None
  3357. return TimelapseJobResponse.from_job(job)
  3358. async def _ensure_timelapse_unchanged(
  3359. archive_id: int, stored_path: str, timelapse_path: Path, signature: tuple[int, int, int]
  3360. ) -> None:
  3361. async with database.async_session() as db:
  3362. current = (
  3363. await db.execute(select(PrintArchive.timelapse_path).where(PrintArchive.id == archive_id))
  3364. ).scalar_one_or_none()
  3365. try:
  3366. now = timelapse_path.stat()
  3367. except FileNotFoundError:
  3368. now = None
  3369. if current != stored_path or now is None or (now.st_size, now.st_mtime_ns, now.st_ino) != signature:
  3370. raise RuntimeError("The timelapse was removed or replaced while it was being edited; the edit was not saved")
  3371. async def _timelapse_job_archive(archive_id: int, db: AsyncSession, printer_scope: PrinterScope) -> None:
  3372. archive = await ArchiveService(db).get_archive(archive_id)
  3373. if not archive:
  3374. raise HTTPException(404, "Timelapse not found")
  3375. # The job edits the archive's video, which is the whole archive's.
  3376. await _ensure_whole_archive(archive, printer_scope)
  3377. @router.get("/{archive_id}/timelapse/job")
  3378. async def get_timelapse_job(
  3379. archive_id: int,
  3380. db: AsyncSession = Depends(get_db, scope="function"),
  3381. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  3382. printer_scope: PrinterScope = RequestPrinterScope,
  3383. ):
  3384. """The archive's running or last timelapse job: status, progress, result."""
  3385. from backend.app.schemas.timelapse import TimelapseJobResponse
  3386. from backend.app.services import timelapse_jobs
  3387. await _timelapse_job_archive(archive_id, db, printer_scope)
  3388. job = timelapse_jobs.get_job(archive_id)
  3389. if job is None:
  3390. raise HTTPException(404, "No timelapse job")
  3391. return TimelapseJobResponse.from_job(job)
  3392. @router.delete("/{archive_id}/timelapse/job")
  3393. async def cancel_timelapse_job(
  3394. archive_id: int,
  3395. db: AsyncSession = Depends(get_db, scope="function"),
  3396. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  3397. printer_scope: PrinterScope = RequestPrinterScope,
  3398. ):
  3399. """Cancel the running timelapse job; stops ffmpeg and drops its output."""
  3400. from backend.app.schemas.timelapse import TimelapseJobResponse
  3401. from backend.app.services import timelapse_jobs
  3402. await _timelapse_job_archive(archive_id, db, printer_scope)
  3403. if not await timelapse_jobs.cancel_job(archive_id):
  3404. raise HTTPException(404, "No timelapse job is running")
  3405. job = timelapse_jobs.get_job(archive_id)
  3406. assert job is not None
  3407. return TimelapseJobResponse.from_job(job)
  3408. # ============================================
  3409. # Photo Endpoints
  3410. # ============================================
  3411. @router.post("/{archive_id}/photos")
  3412. async def upload_photo(
  3413. archive_id: int,
  3414. file: UploadFile = File(...),
  3415. db: AsyncSession = Depends(get_db, scope="function"),
  3416. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  3417. require_ownership_permission(
  3418. Permission.ARCHIVES_UPDATE_ALL,
  3419. Permission.ARCHIVES_UPDATE_OWN,
  3420. )
  3421. ),
  3422. printer_scope: PrinterScope = RequestPrinterScope,
  3423. ):
  3424. """Upload a photo of the printed result."""
  3425. user, can_modify_all = auth_result
  3426. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  3427. archive = await _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  3428. if not file.filename or not file.filename.lower().endswith((".jpg", ".jpeg", ".png", ".webp")):
  3429. raise HTTPException(400, "File must be an image (.jpg, .jpeg, .png, .webp)")
  3430. # Get archive directory. parents=True because an archive with no 3MF owns
  3431. # <archive_dir>/<id>/, which nothing else has necessarily created yet.
  3432. photos_dir = archive_photos_dir(archive)
  3433. photos_dir.mkdir(parents=True, exist_ok=True)
  3434. # Generate unique filename
  3435. import uuid
  3436. ext = Path(file.filename).suffix.lower()
  3437. photo_filename = f"{uuid.uuid4().hex[:8]}{ext}"
  3438. photo_path = photos_dir / photo_filename # SEC-PATH-OK: photo_filename = uuid.uuid4().hex[:8] + ext
  3439. # Save file
  3440. content = await file.read()
  3441. photo_path.write_bytes(content)
  3442. # Update archive photos list (create new list to trigger SQLAlchemy change detection)
  3443. photos = list(archive.photos or [])
  3444. photos.append(photo_filename)
  3445. archive.photos = photos
  3446. await db.commit()
  3447. await db.refresh(archive)
  3448. return {"status": "uploaded", "filename": photo_filename, "photos": await _visible_photos(archive, printer_scope)}
  3449. @router.get("/{archive_id}/photos/{filename}")
  3450. async def get_photo(
  3451. archive_id: int,
  3452. filename: str,
  3453. db: AsyncSession = Depends(get_db, scope="function"),
  3454. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  3455. require_media_token_ownership(
  3456. Permission.ARCHIVES_READ_ALL,
  3457. Permission.ARCHIVES_READ_OWN,
  3458. )
  3459. ),
  3460. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  3461. ):
  3462. """Get a specific photo.
  3463. Requires a media token query param (?token=xxx) when auth is enabled, and
  3464. returns 404 for an archive the caller may not read (#3025).
  3465. """
  3466. user, can_read_all = auth_result
  3467. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  3468. archive = await _ensure_archive_visible(result.scalar_one_or_none(), user, can_read_all, printer_scope)
  3469. await _ensure_media_visible(archive, printer_scope, filename)
  3470. # Membership check first — UUID-generated names on upload mean any URL
  3471. # filename that doesn't appear here is by definition not a real photo.
  3472. # Mirrors the delete handler below; previously this endpoint had no
  3473. # membership check at all and joined `filename` straight to disk.
  3474. if not archive.photos or filename not in archive.photos:
  3475. raise HTTPException(404, "Photo not found")
  3476. # Defence-in-depth: even though the membership check above already
  3477. # constrains `filename` to UUID-generated names from upload,
  3478. # find_archive_photo resolves and containment-checks each candidate,
  3479. # guarding against future code paths that might populate
  3480. # `archive.photos` from a less-trusted source.
  3481. photo_path = find_archive_photo(archive, filename)
  3482. if photo_path is None:
  3483. raise HTTPException(404, "Photo not found")
  3484. # Determine media type
  3485. ext = Path(filename).suffix.lower()
  3486. media_types = {
  3487. ".jpg": "image/jpeg",
  3488. ".jpeg": "image/jpeg",
  3489. ".png": "image/png",
  3490. ".webp": "image/webp",
  3491. }
  3492. media_type = media_types.get(ext, "image/jpeg")
  3493. return FileResponse(path=photo_path, media_type=media_type)
  3494. @router.delete("/{archive_id}/photos/{filename}")
  3495. async def delete_photo(
  3496. archive_id: int,
  3497. filename: str,
  3498. db: AsyncSession = Depends(get_db, scope="function"),
  3499. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  3500. require_ownership_permission(
  3501. Permission.ARCHIVES_DELETE_ALL,
  3502. Permission.ARCHIVES_DELETE_OWN,
  3503. )
  3504. ),
  3505. printer_scope: PrinterScope = RequestPrinterScope,
  3506. ):
  3507. """Delete a photo."""
  3508. user, can_modify_all = auth_result
  3509. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  3510. archive = await _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  3511. await _ensure_media_visible(archive, printer_scope, filename)
  3512. if not archive.photos or filename not in archive.photos:
  3513. raise HTTPException(404, "Photo not found")
  3514. # Delete file — same lookup as get_photo above, so a photo that is
  3515. # readable is also deletable. Removing the name while leaving the file is
  3516. # how a no-3MF archive accumulated photos nobody could see or remove.
  3517. photo_path = find_archive_photo(archive, filename)
  3518. if photo_path is not None:
  3519. photo_path.unlink()
  3520. # Update archive photos list, and the list of the run it was taken for
  3521. photos = [p for p in archive.photos if p != filename]
  3522. archive.photos = photos if photos else None
  3523. runs = select(PrintLogEntry).where(PrintLogEntry.archive_id == archive.id)
  3524. for run in await db.scalars(runs.execution_options(**{INCLUDE_OPEN_RUNS: True})):
  3525. if run.photos and filename in run.photos:
  3526. run.photos = [p for p in run.photos if p != filename]
  3527. await db.commit()
  3528. return {"status": "deleted", "photos": await _visible_photos(archive, printer_scope)}
  3529. # ============================================
  3530. # Post-print outcome confirmation (#1898)
  3531. # ============================================
  3532. # English-only on purpose: this page is rendered by the backend for a phone
  3533. # browser that carries no session and therefore no language preference.
  3534. _VERDICT_LABELS = {"good": "Good part", "reject": "Rejected"}
  3535. _VERDICT_SOURCE_PHRASES = {
  3536. "dialog": "in the app",
  3537. "link": "with a one-tap link",
  3538. "plate_clear": "automatically when the print plate was cleared",
  3539. "printer_card": "from the printer card",
  3540. "api": "through the API",
  3541. "reaction": "with a reaction in chat",
  3542. }
  3543. def _confirm_page(glyph: str, heading: str, body: str) -> str:
  3544. """The small HTML page every one-tap outcome link renders."""
  3545. return (
  3546. "<!doctype html><html><head><meta name='viewport' content='width=device-width, initial-scale=1'>"
  3547. "<title>Bambuddy</title></head>"
  3548. "<body style='font-family: system-ui, sans-serif; background:#1a1d21; color:#fff; display:flex;"
  3549. " align-items:center; justify-content:center; min-height:90vh; margin:0'>"
  3550. f"<div style='text-align:center; padding:0 1.5rem; max-width:32rem'>"
  3551. f"<div style='font-size:3rem'>{glyph}</div><h2>{heading}</h2>{body}</div></body></html>"
  3552. )
  3553. async def _render_already_answered_page(
  3554. db: AsyncSession, archive: PrintArchive, run: PrintLogEntry | None = None
  3555. ) -> str:
  3556. """Explain a spent one-tap link instead of calling it invalid.
  3557. The plate-clear default, the app and the other link all answer the same
  3558. prompt, so the button in a push notification is routinely tapped after the
  3559. question is settled. Report the verdict on file, when and how it landed,
  3560. and where to change it: of the run the link asked about, when it did.
  3561. """
  3562. from backend.app.api.routes.settings import get_external_base_url
  3563. name = html_escape(archive.print_name or archive.filename or "")
  3564. answered = run if run is not None else archive
  3565. # The verdict's own timestamp, not the moment the token was spent: a
  3566. # verdict changed later in the app would otherwise be dated by the older
  3567. # event. Pre-#1898 rows have neither, and then the page omits the date.
  3568. used_at = answered.user_verdict_at or answered.confirm_token_used_at
  3569. if used_at is not None and used_at.tzinfo is not None:
  3570. used_at = used_at.astimezone(timezone.utc)
  3571. when = used_at.strftime("%Y-%m-%d %H:%M UTC") if used_at else None
  3572. how = _VERDICT_SOURCE_PHRASES.get(answered.user_verdict_source or "")
  3573. label = _VERDICT_LABELS.get(answered.user_verdict or "")
  3574. if label:
  3575. recorded = f"Recorded as <strong>{label}</strong>"
  3576. if how:
  3577. recorded += f" {how}"
  3578. if when:
  3579. recorded += f" on {when}"
  3580. recorded += "."
  3581. else:
  3582. # The verdict was cleared again in the app; the link stays spent.
  3583. recorded = "This prompt was already answered and the verdict has since been cleared."
  3584. base = await get_external_base_url(db)
  3585. run_param = f"&run={run.id}" if run is not None else ""
  3586. link = html_escape(f"{base}/archives?confirm={archive.id}{run_param}", quote=True)
  3587. glyph = "&#10003;" if answered.user_verdict == "good" else "&#10007;" if answered.user_verdict else "&#8505;"
  3588. return _confirm_page(
  3589. glyph,
  3590. "Already answered",
  3591. f"<p style='color:#9ca3af'>{name}</p>"
  3592. f"<p style='color:#9ca3af'>{recorded}</p>"
  3593. f"<p><a style='color:#00ae42' href='{link}'>Open this print in Bambuddy</a> to change it.</p>",
  3594. )
  3595. def _render_confirm_prompt_page(request: Request, archive: PrintArchive, verdict: str) -> str:
  3596. """The page a one-tap verdict link opens. It has recorded nothing yet.
  3597. GET is where link unfurlers, mail-security scanners and browser prefetchers
  3598. arrive, uninvited and within seconds of the message being sent, so GET
  3599. writes nothing at all -- the verdict is recorded by the form below, over
  3600. POST, which none of them issue.
  3601. The form submits itself only for a page opened from a notification button,
  3602. which is what keeps the operator at one tap. The marker for that
  3603. (``?tap=1``) is put on the Telegram inline keyboard's URLs and nowhere else
  3604. -- never on a URL that travels in message text -- so a mail-security
  3605. sandbox that renders HTML and runs JavaScript cannot press the button for
  3606. the operator: it only ever sees the unmarked URL out of the body. The
  3607. User-Agent heuristic still runs on top of that, but it is no longer the
  3608. only thing between a scanner and the write; it cannot be, because such a
  3609. sandbox sends an ordinary Chrome string.
  3610. Every other arrival -- an unmarked link somebody typed or mailed, a browser
  3611. with JavaScript off -- gets the same page and presses the button.
  3612. """
  3613. name = html_escape(archive.print_name or archive.filename or "")
  3614. # Escaped although the route only lets good/reject through: the page must
  3615. # not depend on a check made in another function.
  3616. label = html_escape(_VERDICT_LABELS.get(verdict, verdict))
  3617. # No action attribute: the form posts back to the URL the page was loaded
  3618. # from, so it works behind a reverse proxy and on a host external_url does
  3619. # not name.
  3620. form = (
  3621. "<form method='post' id='confirm-form'>"
  3622. "<button type='submit' style='font: inherit; font-size:1.1rem; padding:0.9rem 2rem;"
  3623. " border:0; border-radius:0.5rem; background:#00ae42; color:#fff'>Yes, record it</button>"
  3624. "</form>"
  3625. )
  3626. # The SPA's CSP allows inline scripts only with the per-request nonce the
  3627. # security-headers middleware mints (main.py). Without one -- no middleware,
  3628. # an unmarked URL, or an unattended-looking caller -- the page simply waits
  3629. # for the button.
  3630. nonce = getattr(request.state, "csp_nonce", None)
  3631. script = ""
  3632. if nonce and is_one_tap_request(request.query_params) and not is_unattended_fetch(request.method, request.headers):
  3633. script = (
  3634. f"<script nonce='{html_escape(nonce, quote=True)}'>"
  3635. "document.getElementById('confirm-form').submit();</script>"
  3636. )
  3637. return _confirm_page(
  3638. "&#63;",
  3639. "Confirm this outcome",
  3640. f"<p style='color:#9ca3af'>{name}</p>"
  3641. f"<p style='color:#9ca3af'>Record this print as <strong>{label}</strong>?</p>"
  3642. f"{form}{script}",
  3643. )
  3644. def _confirm_response(html: str):
  3645. """The one-tap pages, never cached.
  3646. A proxy holding on to "Saved" or to the prompt would answer a later tap
  3647. from its cache, and the prompt page is a capability URL either way.
  3648. """
  3649. from fastapi.responses import HTMLResponse
  3650. return HTMLResponse(html, headers={"Cache-Control": "no-store"})
  3651. async def _load_confirmable_archive(
  3652. db: AsyncSession, token: str, verdict: str
  3653. ) -> tuple[PrintArchive, PrintLogEntry | None]:
  3654. """Resolve a one-tap capability token, or raise the route's 400/404: the
  3655. archive with the run the token asks about (None for an archive's own
  3656. token, minted before runs had theirs)."""
  3657. from backend.app.services.print_confirmation import confirm_token_holder
  3658. if verdict not in ("good", "reject"):
  3659. raise HTTPException(400, "Verdict must be 'good' or 'reject'")
  3660. found = await confirm_token_holder(db, token)
  3661. if found is None:
  3662. raise HTTPException(404, "Confirmation link is invalid or was already used")
  3663. return found
  3664. @router.get("/confirm/{token}/{verdict}")
  3665. async def confirm_outcome_page(
  3666. request: Request,
  3667. token: str,
  3668. verdict: str,
  3669. db: AsyncSession = Depends(get_db, scope="function"),
  3670. ):
  3671. """Open a one-tap verdict link. Reads only — the verdict is recorded by POST.
  3672. This used to be the route that recorded, and that was the defect: a GET
  3673. that changes state is answered by everything that walks a URL. Telegram
  3674. and Slack fetch the links in a message body to build a preview card, mail
  3675. gateways detonate them before delivery, browsers prefetch them — any one
  3676. of those spent the single-use token and settled the outcome before the
  3677. operator had read the question, always in the "good" direction because
  3678. good_url came first. Suppressing previews per channel does not cover the
  3679. proxies and scanners in between; only removing the write from GET does.
  3680. So GET now hands back the prompt page and nothing else. The page's form
  3681. POSTs to this same URL, and :func:`confirm_outcome_by_token` records it.
  3682. The human cost is zero for the path the feature is built around: a URL
  3683. opened from a notification button carries ``?tap=1`` and the page submits
  3684. itself, so that tap is still the only tap. Nothing else gets that script,
  3685. including a scanner that runs JavaScript -- the marker is on the buttons,
  3686. not in the message text a scanner reads.
  3687. """
  3688. archive, run = await _load_confirmable_archive(db, token, verdict)
  3689. if (run or archive).confirm_token_used_at is not None:
  3690. # Answered already — by hand, by the other link, by the plate-clear
  3691. # default or by a reaction. Report what is on file and change nothing.
  3692. return _confirm_response(await _render_already_answered_page(db, archive, run))
  3693. return _confirm_response(_render_confirm_prompt_page(request, archive, verdict))
  3694. @router.post("/confirm/{token}/{verdict}")
  3695. async def confirm_outcome_by_token(
  3696. token: str,
  3697. verdict: str,
  3698. db: AsyncSession = Depends(get_db, scope="function"),
  3699. ):
  3700. """Record a print-outcome verdict via the capability token from a push notification.
  3701. Deliberately unauthenticated: the token IS the credential. It is a 256-bit
  3702. per-archive capability, minted when the confirmation prompt fires, and it
  3703. only ever grants writing good/reject on that one archive, exactly once.
  3704. "Exactly once" is enforced by ``confirm_token_used_at`` rather than by
  3705. dropping the token value, so a link for a print that was already answered
  3706. can be recognised and explained instead of looking broken.
  3707. POST is what keeps the capability the operator's: unfurlers, scanners and
  3708. prefetchers issue GET, and the GET route above writes nothing. The two
  3709. callers that reach here are the prompt page's form and the ntfy action
  3710. button, which performs its own request from the phone and is configured
  3711. with ``method=POST``. Returns a small HTML page for the phone browser.
  3712. """
  3713. from backend.app.services.print_confirmation import latest_run, record_verdict
  3714. archive, run = await _load_confirmable_archive(db, token, verdict)
  3715. if (run or archive).confirm_token_used_at is not None:
  3716. return _confirm_response(await _render_already_answered_page(db, archive, run))
  3717. # The run the link asks about; an archive's own token, sent before runs
  3718. # had theirs, answers its latest. Verdict-aware statistics read the run.
  3719. await record_verdict(db, archive, verdict, source="link", run=run or await latest_run(db, archive.id))
  3720. await db.commit()
  3721. label = "Good part" if verdict == "good" else "Rejected"
  3722. name = archive.print_name or archive.filename
  3723. return _confirm_response(
  3724. _confirm_page(
  3725. "&#10003;" if verdict == "good" else "&#10007;",
  3726. label,
  3727. f"<p style='color:#9ca3af'>{html_escape(name)}</p>"
  3728. "<p style='color:#9ca3af'>Saved &mdash; you can close this page.</p>",
  3729. )
  3730. )
  3731. # ============================================
  3732. # QR Code Endpoint
  3733. # ============================================
  3734. @router.get("/{archive_id}/qrcode")
  3735. async def get_qrcode(
  3736. archive_id: int,
  3737. request: Request,
  3738. size: int = 200,
  3739. db: AsyncSession = Depends(get_db, scope="function"),
  3740. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  3741. require_media_token_ownership(
  3742. Permission.ARCHIVES_READ_ALL,
  3743. Permission.ARCHIVES_READ_OWN,
  3744. )
  3745. ),
  3746. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  3747. ):
  3748. """Generate a QR code that links to this archive.
  3749. Requires a media token query param (?token=xxx) when auth is enabled, and
  3750. returns 404 for an archive the caller may not read (#3025).
  3751. """
  3752. user, can_read_all = auth_result
  3753. try:
  3754. import qrcode
  3755. from PIL import Image as PILImage
  3756. except ImportError:
  3757. raise HTTPException(500, "QR code generation not available - qrcode package not installed")
  3758. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  3759. archive = await _ensure_archive_visible(result.scalar_one_or_none(), user, can_read_all, printer_scope)
  3760. # Build URL to archive download
  3761. base_url = str(request.base_url).rstrip("/")
  3762. archive_url = f"{base_url}/api/v1/archives/{archive_id}/download"
  3763. # Generate QR code
  3764. qr = qrcode.QRCode(
  3765. version=1,
  3766. error_correction=qrcode.constants.ERROR_CORRECT_M,
  3767. box_size=10,
  3768. border=2,
  3769. )
  3770. qr.add_data(archive_url)
  3771. qr.make(fit=True)
  3772. img = qr.make_image(fill_color="black", back_color="white")
  3773. # Convert to PIL Image for resizing
  3774. pil_img = img.get_image()
  3775. # Resize if needed
  3776. if size != 200:
  3777. pil_img = pil_img.resize((size, size), PILImage.Resampling.LANCZOS)
  3778. # Convert to bytes
  3779. buffer = io.BytesIO()
  3780. pil_img.save(buffer, format="PNG")
  3781. buffer.seek(0)
  3782. qr_filename = f"qr_{archive.print_name or archive_id}.png"
  3783. return Response(
  3784. content=buffer.getvalue(),
  3785. media_type="image/png",
  3786. headers={"Content-Disposition": build_content_disposition(qr_filename, disposition="inline")},
  3787. )
  3788. @router.get("/{archive_id}/capabilities")
  3789. async def get_archive_capabilities(
  3790. archive_id: int,
  3791. db: AsyncSession = Depends(get_db, scope="function"),
  3792. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  3793. require_ownership_permission(
  3794. Permission.ARCHIVES_READ_ALL,
  3795. Permission.ARCHIVES_READ_OWN,
  3796. )
  3797. ),
  3798. printer_scope: PrinterScope = RequestPrinterScope,
  3799. ):
  3800. """Check what viewing capabilities are available for this 3MF file."""
  3801. import defusedxml.ElementTree as ET
  3802. user, can_read_all = auth_result
  3803. service = ArchiveService(db)
  3804. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  3805. file_path = settings.base_dir / archive.file_path
  3806. if not file_path.is_file():
  3807. raise HTTPException(404, "File not found")
  3808. has_model = False
  3809. has_gcode = False
  3810. has_source = False
  3811. build_volume = {"x": 256, "y": 256, "z": 256} # Default to X1/P1 size
  3812. filament_colors: list[str] = []
  3813. # Check if source 3MF exists - this is where actual mesh data typically lives
  3814. source_path = None
  3815. if archive.source_3mf_path:
  3816. source_path = settings.base_dir / archive.source_3mf_path
  3817. if source_path.exists():
  3818. has_source = True
  3819. # Helper function to check for mesh data and extract colors from a 3MF file
  3820. def extract_3mf_info(zf_path: Path) -> tuple[bool, list[str], dict]:
  3821. """Extract mesh presence, colors, and build volume from a 3MF file."""
  3822. found_mesh = False
  3823. colors: list[str] = []
  3824. volume = {"x": 256, "y": 256, "z": 256}
  3825. try:
  3826. with zipfile.ZipFile(zf_path, "r") as zf:
  3827. names = zf.namelist()
  3828. # Check for 3D model - look for actual mesh data
  3829. for name in names:
  3830. if name.endswith(".model"):
  3831. try:
  3832. content = zf.read(name).decode("utf-8")
  3833. if "<vertex" in content or "<mesh" in content:
  3834. found_mesh = True
  3835. break
  3836. except Exception:
  3837. pass # Skip unreadable .model entries in archive
  3838. # Extract filament colors from project_settings.config
  3839. if "Metadata/project_settings.config" in names:
  3840. try:
  3841. config_content = zf.read("Metadata/project_settings.config").decode("utf-8")
  3842. config_data = json.loads(config_content)
  3843. # Parse printable_area: ['0x0', '256x0', '256x256', '0x256']
  3844. printable_area = config_data.get("printable_area", [])
  3845. if printable_area and len(printable_area) >= 3:
  3846. max_x = 0
  3847. max_y = 0
  3848. for coord in printable_area:
  3849. if "x" in coord:
  3850. parts = coord.split("x")
  3851. if len(parts) == 2:
  3852. try:
  3853. x, y = int(parts[0]), int(parts[1])
  3854. max_x = max(max_x, x)
  3855. max_y = max(max_y, y)
  3856. except ValueError:
  3857. pass # Skip non-numeric printable_area coordinate
  3858. if max_x > 0 and max_y > 0:
  3859. volume["x"] = max_x
  3860. volume["y"] = max_y
  3861. # Parse printable_height
  3862. printable_height = config_data.get("printable_height")
  3863. if printable_height:
  3864. try:
  3865. volume["z"] = int(printable_height)
  3866. except (ValueError, TypeError):
  3867. pass # Skip unparseable printable_height value
  3868. # Extract filament colors
  3869. raw_colors = config_data.get("filament_colour", [])
  3870. if raw_colors:
  3871. for color in raw_colors:
  3872. if color and isinstance(color, str):
  3873. colors.append(color)
  3874. except Exception:
  3875. pass # Skip malformed project_settings.config
  3876. except zipfile.BadZipFile:
  3877. pass # File is not a valid zip/3MF archive
  3878. return found_mesh, colors, volume
  3879. # First check source 3MF for mesh data and colors (preferred for 3D model viewing)
  3880. if has_source and source_path:
  3881. source_has_mesh, source_colors, source_volume = extract_3mf_info(source_path)
  3882. if source_has_mesh:
  3883. has_model = True
  3884. if source_colors:
  3885. filament_colors = source_colors
  3886. if source_volume["x"] != 256 or source_volume["y"] != 256 or source_volume["z"] != 256:
  3887. build_volume = source_volume
  3888. try:
  3889. with zipfile.ZipFile(file_path, "r") as zf:
  3890. names = zf.namelist()
  3891. # Check for G-code in the sliced file. Shared with the library's
  3892. # file-type classification so the card's badge and what the File
  3893. # Manager makes of the same file cannot disagree (#2993).
  3894. has_gcode = names_carry_gcode(names)
  3895. # Check for 3D model in sliced file (fallback if no source)
  3896. if not has_model:
  3897. for name in names:
  3898. if name.endswith(".model"):
  3899. try:
  3900. content = zf.read(name).decode("utf-8")
  3901. if "<vertex" in content or "<mesh" in content:
  3902. has_model = True
  3903. break
  3904. except Exception:
  3905. pass # Skip unreadable .model entries in archive
  3906. # Extract filament colors from slice_info.config (for gcode preview)
  3907. # These are the actual filaments used in the print, indexed by tool/extruder
  3908. slice_colors: list[str] = []
  3909. if "Metadata/slice_info.config" in names:
  3910. try:
  3911. slice_content = zf.read("Metadata/slice_info.config").decode("utf-8")
  3912. root = ET.fromstring(slice_content)
  3913. filaments = root.findall(".//filament")
  3914. filament_map: dict[int, str] = {}
  3915. for f in filaments:
  3916. fid = f.get("id")
  3917. fcolor = f.get("color")
  3918. used_g = f.get("used_g", "0")
  3919. try:
  3920. used_amount = float(used_g)
  3921. except (ValueError, TypeError):
  3922. used_amount = 0
  3923. if fid is not None and fcolor:
  3924. try:
  3925. tool_id = int(fid) - 1
  3926. if tool_id >= 0 and used_amount > 0:
  3927. filament_map[tool_id] = fcolor
  3928. except ValueError:
  3929. pass # Skip filament entry with non-numeric ID
  3930. if filament_map:
  3931. max_tool = max(filament_map.keys())
  3932. for i in range(max_tool + 1):
  3933. slice_colors.append(filament_map.get(i, "#00AE42"))
  3934. except Exception:
  3935. pass # Skip malformed slice_info.config XML
  3936. # Use slice_info colors if we don't have colors from source yet
  3937. if not filament_colors and slice_colors:
  3938. filament_colors = slice_colors
  3939. # Extract build volume from sliced file if not already set from source
  3940. if build_volume["x"] == 256 and build_volume["y"] == 256:
  3941. if "Metadata/project_settings.config" in names:
  3942. try:
  3943. config_content = zf.read("Metadata/project_settings.config").decode("utf-8")
  3944. config_data = json.loads(config_content)
  3945. printable_area = config_data.get("printable_area", [])
  3946. if printable_area and len(printable_area) >= 3:
  3947. max_x = 0
  3948. max_y = 0
  3949. for coord in printable_area:
  3950. if "x" in coord:
  3951. parts = coord.split("x")
  3952. if len(parts) == 2:
  3953. try:
  3954. x, y = int(parts[0]), int(parts[1])
  3955. max_x = max(max_x, x)
  3956. max_y = max(max_y, y)
  3957. except ValueError:
  3958. pass # Skip non-numeric printable_area coordinate
  3959. if max_x > 0 and max_y > 0:
  3960. build_volume["x"] = max_x
  3961. build_volume["y"] = max_y
  3962. printable_height = config_data.get("printable_height")
  3963. if printable_height:
  3964. try:
  3965. build_volume["z"] = int(printable_height)
  3966. except (ValueError, TypeError):
  3967. pass # Skip unparseable printable_height value
  3968. # Fallback colors from project_settings if still empty
  3969. if not filament_colors:
  3970. raw_colors = config_data.get("filament_colour", [])
  3971. if raw_colors:
  3972. for color in raw_colors:
  3973. if color and isinstance(color, str):
  3974. filament_colors.append(color)
  3975. except Exception:
  3976. pass # Skip malformed project_settings.config
  3977. except zipfile.BadZipFile:
  3978. raise HTTPException(400, "Invalid 3MF file")
  3979. return {
  3980. "has_model": has_model,
  3981. "has_gcode": has_gcode,
  3982. "has_source": has_source,
  3983. "build_volume": build_volume,
  3984. "filament_colors": filament_colors,
  3985. }
  3986. @router.get("/{archive_id}/gcode")
  3987. async def get_gcode(
  3988. archive_id: int,
  3989. plate: int | None = None,
  3990. db: AsyncSession = Depends(get_db, scope="function"),
  3991. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  3992. require_ownership_permission(
  3993. Permission.ARCHIVES_READ_ALL,
  3994. Permission.ARCHIVES_READ_OWN,
  3995. )
  3996. ),
  3997. printer_scope: PrinterScope = RequestPrinterScope,
  3998. ):
  3999. """Extract and return G-code from the 3MF file.
  4000. When *plate* is provided, returns the G-code for that specific plate
  4001. (e.g. ``?plate=2`` returns ``Metadata/plate_2.gcode``). If omitted, falls
  4002. back to the archive's lowest-numbered plate — not the first member in the
  4003. zip, which is whatever order the slicer wrote and routinely puts plate 2
  4004. ahead of plate 1.
  4005. """
  4006. user, can_read_all = auth_result
  4007. service = ArchiveService(db)
  4008. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  4009. file_path = settings.base_dir / archive.file_path
  4010. if not file_path.is_file():
  4011. raise HTTPException(404, "File not found")
  4012. if plate is not None and plate < 1:
  4013. raise HTTPException(400, "Plate index must be >= 1")
  4014. try:
  4015. with zipfile.ZipFile(file_path, "r") as zf:
  4016. # Bambu 3MF files store G-code in Metadata/plate_X.gcode
  4017. gcode_files = [n for n in zf.namelist() if n.startswith("Metadata/") and n.endswith(".gcode")]
  4018. if not gcode_files:
  4019. raise HTTPException(
  4020. 404,
  4021. "No G-code found. This file hasn't been sliced yet - G-code is only available after slicing in Bambu Studio.",
  4022. )
  4023. if plate is not None:
  4024. selected = select_plate_gcode_name(gcode_files, plate)
  4025. if selected is None:
  4026. raise HTTPException(404, f"Plate {plate} not found in this archive")
  4027. else:
  4028. selected = default_plate_gcode_name(gcode_files)
  4029. gcode_content = zf.read(selected).decode("utf-8")
  4030. return Response(content=gcode_content, media_type="text/plain")
  4031. except zipfile.BadZipFile:
  4032. raise HTTPException(400, "Invalid 3MF file")
  4033. except HTTPException:
  4034. raise
  4035. except Exception as e:
  4036. raise HTTPException(500, f"Error extracting G-code: {str(e)}")
  4037. @router.get("/{archive_id}/plate-preview")
  4038. async def get_plate_preview(
  4039. archive_id: int,
  4040. db: AsyncSession = Depends(get_db, scope="function"),
  4041. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4042. require_media_token_ownership(
  4043. Permission.ARCHIVES_READ_ALL,
  4044. Permission.ARCHIVES_READ_OWN,
  4045. )
  4046. ),
  4047. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  4048. ):
  4049. """Get the plate preview image from the 3MF file.
  4050. Returns the slicer-generated plate thumbnail which shows the model
  4051. with correct colors and positioning.
  4052. Requires a media token query param (?token=xxx) when auth is enabled, and
  4053. returns 404 for an archive the caller may not read (#3025).
  4054. """
  4055. user, can_read_all = auth_result
  4056. service = ArchiveService(db)
  4057. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  4058. file_path = settings.base_dir / archive.file_path
  4059. if not file_path.is_file():
  4060. raise HTTPException(404, "File not found")
  4061. try:
  4062. with zipfile.ZipFile(file_path, "r") as zf:
  4063. names = zf.namelist()
  4064. # Try to find plate preview images in order of preference
  4065. # First look for the specific plate being printed (check slice_info for plate index)
  4066. plate_num = 1
  4067. if "Metadata/slice_info.config" in names:
  4068. try:
  4069. import defusedxml.ElementTree as ET
  4070. slice_content = zf.read("Metadata/slice_info.config").decode("utf-8")
  4071. root = ET.fromstring(slice_content)
  4072. plate_elem = root.find(".//plate/metadata[@key='index']")
  4073. if plate_elem is not None:
  4074. plate_num = int(plate_elem.get("value", "1"))
  4075. except Exception:
  4076. pass # Default plate_num=1 if slice_info is missing or malformed
  4077. # Try plate-specific image first, then fall back to plate_1
  4078. preview_paths = [
  4079. f"Metadata/plate_{plate_num}.png",
  4080. "Metadata/plate_1.png",
  4081. "Metadata/thumbnail.png",
  4082. ]
  4083. for preview_path in preview_paths:
  4084. if preview_path in names:
  4085. image_data = zf.read(preview_path)
  4086. return Response(content=image_data, media_type="image/png")
  4087. # If no plate image, try any PNG in Metadata
  4088. for name in names:
  4089. if name.startswith("Metadata/plate_") and name.endswith(".png") and "_small" not in name:
  4090. image_data = zf.read(name)
  4091. return Response(content=image_data, media_type="image/png")
  4092. raise HTTPException(404, "No plate preview found in 3MF file")
  4093. except zipfile.BadZipFile:
  4094. raise HTTPException(400, "Invalid 3MF file")
  4095. except HTTPException:
  4096. raise
  4097. except Exception as e:
  4098. raise HTTPException(500, f"Error extracting plate preview: {str(e)}")
  4099. @router.post("/upload")
  4100. async def upload_archive(
  4101. file: UploadFile = File(...),
  4102. printer_id: int | None = None,
  4103. prefer_filename_for_name: bool = Query(
  4104. False,
  4105. description=(
  4106. "Name the archive after the uploaded filename instead of the print_name "
  4107. "embedded in the 3MF's metadata. Off by default, which keeps the embedded "
  4108. "name. Turn it on when the filename you send is the meaningful one — an "
  4109. "integration naming files after its own jobs, or a file whose embedded "
  4110. "title is a stale name from whoever originally sliced it."
  4111. ),
  4112. ),
  4113. db: AsyncSession = Depends(get_db, scope="function"),
  4114. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_CREATE),
  4115. printer_scope: PrinterScope = RequestPrinterScope,
  4116. ):
  4117. """Manually upload a 3MF file to archive.
  4118. prefer_filename_for_name is the same flag the FTP review flow and
  4119. virtual-printer dispatch already pass to ArchiveService.archive_print —
  4120. this endpoint just didn't expose it (#1152 follow-up). Those callers derive
  4121. it from the VP-scoped `virtual_printer_archive_name_source` setting; here it
  4122. is per-request, because the caller is an API client that knows whether the
  4123. filename it sent is the meaningful one (#2609).
  4124. """
  4125. printer_scope.ensure(printer_id)
  4126. if not file.filename or not file.filename.endswith(".3mf"):
  4127. raise HTTPException(400, "File must be a .3mf file")
  4128. # Save uploaded file temporarily — strip directory components to prevent path traversal
  4129. safe_filename = _safe_filename(file.filename)
  4130. temp_path = (
  4131. settings.archive_dir / "temp" / safe_filename
  4132. ) # SEC-PATH-OK: safe_filename = _safe_filename(...) basename-stripped above
  4133. temp_path.parent.mkdir(parents=True, exist_ok=True)
  4134. try:
  4135. content = await file.read()
  4136. # #1401: same content validation as library upload — catches
  4137. # raw-gcode-renamed-to-.3mf and other unprintable shapes before
  4138. # archiving them and offering them up for print.
  4139. from backend.app.api.routes.library import validate_print_file_upload
  4140. validate_print_file_upload(file.filename, content)
  4141. temp_path.write_bytes(content)
  4142. service = ArchiveService(db)
  4143. archive = await service.archive_print(
  4144. printer_id=printer_id,
  4145. source_file=temp_path,
  4146. created_by_id=current_user.id if current_user else None,
  4147. prefer_filename_for_name=prefer_filename_for_name,
  4148. )
  4149. if not archive:
  4150. raise HTTPException(400, "Failed to archive file")
  4151. return ArchiveResponse.model_validate(archive)
  4152. finally:
  4153. if temp_path.exists():
  4154. temp_path.unlink()
  4155. @router.post("/upload-bulk")
  4156. async def upload_archives_bulk(
  4157. files: list[UploadFile] = File(...),
  4158. printer_id: int | None = None,
  4159. prefer_filename_for_name: bool = Query(
  4160. False,
  4161. description=(
  4162. "Name each archive after its uploaded filename instead of the print_name "
  4163. "embedded in the 3MF's metadata. Applies to every file in the batch. Off "
  4164. "by default, which keeps the embedded name."
  4165. ),
  4166. ),
  4167. db: AsyncSession = Depends(get_db, scope="function"),
  4168. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_CREATE),
  4169. printer_scope: PrinterScope = RequestPrinterScope,
  4170. ):
  4171. """Bulk upload multiple 3MF files to archive.
  4172. prefer_filename_for_name applies to every file in the batch. See
  4173. upload_archive for the flag's lineage.
  4174. """
  4175. printer_scope.ensure(printer_id)
  4176. from backend.app.api.routes.library import validate_print_file_upload
  4177. # Each outcome carries its file's position in the request: two files of
  4178. # the same name cannot be told apart by name.
  4179. results = []
  4180. errors = []
  4181. for index, file in enumerate(files):
  4182. if not file.filename or not file.filename.endswith(".3mf"):
  4183. errors.append({"index": index, "filename": file.filename or "unknown", "error": "Not a .3mf file"})
  4184. continue
  4185. safe_filename = _safe_filename(file.filename)
  4186. temp_path = (
  4187. settings.archive_dir / "temp" / safe_filename
  4188. ) # SEC-PATH-OK: safe_filename = _safe_filename(...) basename-stripped above
  4189. temp_path.parent.mkdir(parents=True, exist_ok=True)
  4190. try:
  4191. content = await file.read()
  4192. # #1401: bulk-upload variant of the library validation. Collect
  4193. # the rejection per-file rather than aborting the whole batch
  4194. # so one bad file in a 10-file drag-drop doesn't lose the
  4195. # other nine.
  4196. try:
  4197. validate_print_file_upload(file.filename, content)
  4198. except HTTPException as exc:
  4199. errors.append({"index": index, "filename": file.filename, "error": exc.detail})
  4200. continue
  4201. temp_path.write_bytes(content)
  4202. service = ArchiveService(db)
  4203. archive = await service.archive_print(
  4204. printer_id=printer_id,
  4205. source_file=temp_path,
  4206. created_by_id=current_user.id if current_user else None,
  4207. prefer_filename_for_name=prefer_filename_for_name,
  4208. )
  4209. if archive:
  4210. results.append(
  4211. {
  4212. "index": index,
  4213. "filename": file.filename,
  4214. "id": archive.id,
  4215. "status": "success",
  4216. }
  4217. )
  4218. else:
  4219. errors.append({"index": index, "filename": file.filename, "error": "Failed to process"})
  4220. except Exception as e:
  4221. logger.exception("Failed to upload archive %s: %s", file.filename, e)
  4222. errors.append({"index": index, "filename": file.filename, "error": "Failed to process file"})
  4223. finally:
  4224. if temp_path.exists():
  4225. temp_path.unlink()
  4226. return {
  4227. "uploaded": len(results),
  4228. "failed": len(errors),
  4229. "results": results,
  4230. "errors": errors,
  4231. }
  4232. @router.get("/{archive_id}/plates")
  4233. async def get_archive_plates(
  4234. archive_id: int,
  4235. db: AsyncSession = Depends(get_db, scope="function"),
  4236. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4237. require_ownership_permission(
  4238. Permission.ARCHIVES_READ_ALL,
  4239. Permission.ARCHIVES_READ_OWN,
  4240. )
  4241. ),
  4242. printer_scope: PrinterScope = RequestPrinterScope,
  4243. ):
  4244. """Get available plates from a multi-plate 3MF archive.
  4245. Returns a list of plates with their index, name, thumbnail availability,
  4246. and filament requirements. For single-plate exports, returns a single plate.
  4247. """
  4248. import re
  4249. import defusedxml.ElementTree as ET
  4250. user, can_read_all = auth_result
  4251. service = ArchiveService(db)
  4252. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  4253. file_path = settings.base_dir / archive.file_path
  4254. if not file_path.is_file():
  4255. raise HTTPException(404, "Archive file not found")
  4256. plates = []
  4257. # Initialize so the `has_gcode = bool(gcode_files)` after the try/except
  4258. # never raises NameError when the archive isn't a valid zip (e.g. plain
  4259. # .gcode file from a sliced-archive flow that didn't request 3MF output).
  4260. gcode_files: list[str] = []
  4261. # Printer / process preset names the 3MF was prepared with — used by the
  4262. # SliceModal to default its dropdowns (#1325).
  4263. embedded_presets: dict[str, str | None] = {"printer": None, "process": None}
  4264. # Process settings the designer changed away from the stock preset (#2622),
  4265. # offered in the SliceModal for a cross-printer re-slice. Same payload the
  4266. # library plates endpoint returns — SliceModal reads one shape for both.
  4267. design_overrides: list[dict] = []
  4268. try:
  4269. with zipfile.ZipFile(file_path, "r") as zf:
  4270. namelist = zf.namelist()
  4271. embedded_presets = extract_embedded_presets_from_3mf(zf)
  4272. if _PROJECT_SETTINGS_PATH in namelist:
  4273. try:
  4274. design_overrides = [
  4275. o._asdict()
  4276. for o in overrides_from_config(json.loads(zf.read(_PROJECT_SETTINGS_PATH).decode("utf-8")))
  4277. ]
  4278. except (ValueError, OSError, KeyError):
  4279. design_overrides = []
  4280. # Find all plate gcode files to determine available plates
  4281. gcode_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".gcode")]
  4282. # If no gcode is present (source-only or unsliced), fall back to plate JSON/PNG
  4283. plate_indices: list[int] = []
  4284. if gcode_files:
  4285. # Extract plate indices from gcode filenames
  4286. for gf in gcode_files:
  4287. # "Metadata/plate_5.gcode" -> 5
  4288. try:
  4289. # Remove "Metadata/plate_" and ".gcode"
  4290. plate_str = gf[15:-6]
  4291. plate_indices.append(int(plate_str))
  4292. except ValueError:
  4293. pass # Skip gcode file with non-numeric plate index
  4294. else:
  4295. plate_json_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".json")]
  4296. plate_png_files = [
  4297. n
  4298. for n in namelist
  4299. if n.startswith("Metadata/plate_")
  4300. and n.endswith(".png")
  4301. and "_small" not in n
  4302. and "no_light" not in n
  4303. ]
  4304. plate_name_candidates = plate_json_files + plate_png_files
  4305. plate_re = re.compile(r"^Metadata/plate_(\d+)\.(json|png)$")
  4306. seen_indices: set[int] = set()
  4307. for name in plate_name_candidates:
  4308. match = plate_re.match(name)
  4309. if match:
  4310. try:
  4311. index = int(match.group(1))
  4312. except ValueError:
  4313. continue
  4314. if index in seen_indices:
  4315. continue
  4316. seen_indices.add(index)
  4317. plate_indices.append(index)
  4318. if not plate_indices:
  4319. # No plate metadata found
  4320. return {
  4321. "archive_id": archive_id,
  4322. "filename": archive.filename,
  4323. "plates": [],
  4324. "is_multi_plate": False,
  4325. }
  4326. plate_indices.sort()
  4327. # Parse model_settings.config for plate names + object assignments
  4328. # Plate names are stored with plater_id and plater_name keys
  4329. plate_names = {} # plater_id -> name
  4330. plate_object_ids: dict[int, list[str]] = {}
  4331. object_names_by_id: dict[str, str] = {}
  4332. if "Metadata/model_settings.config" in namelist:
  4333. try:
  4334. model_content = zf.read("Metadata/model_settings.config").decode()
  4335. model_root = ET.fromstring(model_content)
  4336. # Build object ID -> name map
  4337. for obj_elem in model_root.findall(".//object"):
  4338. obj_id = obj_elem.get("id")
  4339. if not obj_id:
  4340. continue
  4341. name_meta = obj_elem.find("metadata[@key='name']")
  4342. obj_name = name_meta.get("value") if name_meta is not None else None
  4343. if obj_name:
  4344. object_names_by_id[obj_id] = obj_name
  4345. for plate_elem in model_root.findall(".//plate"):
  4346. plater_id = None
  4347. plater_name = None
  4348. for meta in plate_elem.findall("metadata"):
  4349. key = meta.get("key")
  4350. value = meta.get("value")
  4351. if key == "plater_id" and value:
  4352. try:
  4353. plater_id = int(value)
  4354. except ValueError:
  4355. pass # Skip plate with non-numeric plater_id
  4356. elif key == "plater_name" and value:
  4357. plater_name = value.strip()
  4358. if plater_id is not None and plater_name:
  4359. plate_names[plater_id] = plater_name
  4360. if plater_id is not None:
  4361. for instance_elem in plate_elem.findall("model_instance"):
  4362. for inst_meta in instance_elem.findall("metadata"):
  4363. if inst_meta.get("key") == "object_id":
  4364. obj_id = inst_meta.get("value")
  4365. if not obj_id:
  4366. continue
  4367. plate_object_ids.setdefault(plater_id, [])
  4368. if obj_id not in plate_object_ids[plater_id]:
  4369. plate_object_ids[plater_id].append(obj_id)
  4370. except Exception:
  4371. pass # model_settings.config parsing is optional
  4372. # Parse slice_info.config for plate metadata
  4373. plate_metadata = {} # plate_index -> {filaments, prediction, weight, name, objects}
  4374. if "Metadata/slice_info.config" in namelist:
  4375. content = zf.read("Metadata/slice_info.config").decode()
  4376. root = ET.fromstring(content)
  4377. for plate_elem in root.findall(".//plate"):
  4378. plate_info = {
  4379. "filaments": [],
  4380. "prediction": None,
  4381. "weight": None,
  4382. "name": None,
  4383. "objects": [],
  4384. "bed_type": None,
  4385. }
  4386. # Get plate index from metadata
  4387. plate_index = None
  4388. for meta in plate_elem.findall("metadata"):
  4389. key = meta.get("key")
  4390. value = meta.get("value")
  4391. if key == "index" and value:
  4392. try:
  4393. plate_index = int(value)
  4394. except ValueError:
  4395. pass # Skip plate with non-numeric index
  4396. elif key == "prediction" and value:
  4397. try:
  4398. plate_info["prediction"] = int(value)
  4399. except ValueError:
  4400. pass # Skip non-numeric print time prediction
  4401. elif key == "weight" and value:
  4402. try:
  4403. plate_info["weight"] = float(value)
  4404. except ValueError:
  4405. pass # Skip non-numeric filament weight
  4406. elif key == "curr_bed_type" and value:
  4407. # Per-plate bed type so the PrintModal can show the
  4408. # right plate alongside each option (#1281).
  4409. plate_info["bed_type"] = value.strip()
  4410. # Get filaments used in this plate
  4411. for filament_elem in plate_elem.findall("filament"):
  4412. filament_id = filament_elem.get("id")
  4413. filament_type = filament_elem.get("type", "")
  4414. filament_color = filament_elem.get("color", "")
  4415. used_g = filament_elem.get("used_g", "0")
  4416. used_m = filament_elem.get("used_m", "0")
  4417. try:
  4418. used_grams = float(used_g)
  4419. except (ValueError, TypeError):
  4420. used_grams = 0
  4421. if used_grams > 0 and filament_id:
  4422. plate_info["filaments"].append(
  4423. {
  4424. "slot_id": int(filament_id),
  4425. "type": filament_type,
  4426. "color": filament_color,
  4427. # The preset the slicer assigned (GFA01, ...): names the product line
  4428. "tray_info_idx": filament_elem.get("tray_info_idx", "") or "",
  4429. "used_grams": round(used_grams, 1),
  4430. "used_meters": float(used_m) if used_m else 0,
  4431. }
  4432. )
  4433. # Sort filaments by slot ID
  4434. plate_info["filaments"].sort(key=lambda x: x["slot_id"])
  4435. # Collect all object names on this plate
  4436. for obj_elem in plate_elem.findall("object"):
  4437. obj_name = obj_elem.get("name")
  4438. if obj_name and obj_name not in plate_info["objects"]:
  4439. plate_info["objects"].append(obj_name)
  4440. # Set plate name: prefer custom name from model_settings.config,
  4441. # fall back to first object name if no custom name was set
  4442. if plate_index is not None:
  4443. custom_name = plate_names.get(plate_index)
  4444. if custom_name:
  4445. plate_info["name"] = custom_name
  4446. else:
  4447. # Fall back to first object name as hint
  4448. if plate_info["objects"]:
  4449. plate_info["name"] = plate_info["objects"][0]
  4450. plate_metadata[plate_index] = plate_info
  4451. # Parse plate_*.json for object lists when slice_info is missing
  4452. plate_json_objects: dict[int, list[str]] = {}
  4453. for name in namelist:
  4454. match = re.match(r"^Metadata/plate_(\d+)\.json$", name)
  4455. if not match:
  4456. continue
  4457. try:
  4458. plate_index = int(match.group(1))
  4459. except ValueError:
  4460. continue
  4461. try:
  4462. payload = json.loads(zf.read(name).decode())
  4463. bbox_objects = payload.get("bbox_objects", [])
  4464. names = []
  4465. for obj in bbox_objects:
  4466. obj_name = obj.get("name") if isinstance(obj, dict) else None
  4467. if obj_name and obj_name not in names:
  4468. names.append(obj_name)
  4469. if names:
  4470. plate_json_objects[plate_index] = names
  4471. except Exception:
  4472. continue
  4473. # Build plate list
  4474. for idx in plate_indices:
  4475. meta = plate_metadata.get(idx, {})
  4476. has_thumbnail = f"Metadata/plate_{idx}.png" in namelist
  4477. objects = meta.get("objects", [])
  4478. if not objects:
  4479. objects = plate_json_objects.get(idx, [])
  4480. if not objects and plate_object_ids.get(idx):
  4481. objects = [
  4482. object_names_by_id.get(obj_id, f"Object {obj_id}") for obj_id in plate_object_ids.get(idx, [])
  4483. ]
  4484. plate_name = meta.get("name")
  4485. if not plate_name:
  4486. plate_name = plate_names.get(idx)
  4487. if not plate_name and objects:
  4488. plate_name = objects[0]
  4489. plates.append(
  4490. {
  4491. "index": idx,
  4492. "name": plate_name,
  4493. "objects": objects,
  4494. "object_count": len(objects),
  4495. "has_thumbnail": has_thumbnail,
  4496. "thumbnail_url": f"/api/v1/archives/{archive_id}/plate-thumbnail/{idx}"
  4497. if has_thumbnail
  4498. else None,
  4499. "print_time_seconds": meta.get("prediction"),
  4500. "filament_used_grams": meta.get("weight"),
  4501. "filaments": meta.get("filaments", []),
  4502. "bed_type": meta.get("bed_type"),
  4503. }
  4504. )
  4505. except Exception as e:
  4506. logger.warning("Failed to parse plates from archive %s: %s", archive_id, e)
  4507. # Has gcode iff the plate list was built from .gcode filenames (as opposed
  4508. # to the JSON/PNG fallback for source-only 3MF projects). Callers that need
  4509. # to preview gcode — the viewer, skip-objects — can gate on this instead of
  4510. # 404-ing on every plate request.
  4511. has_gcode = bool(gcode_files)
  4512. return {
  4513. "archive_id": archive_id,
  4514. "filename": archive.filename,
  4515. "plates": plates,
  4516. "is_multi_plate": len(plates) > 1,
  4517. "has_gcode": has_gcode,
  4518. "embedded_printer": embedded_presets["printer"],
  4519. "embedded_process": embedded_presets["process"],
  4520. "design_overrides": design_overrides,
  4521. }
  4522. @router.get("/{archive_id}/plate-thumbnail/{plate_index}")
  4523. async def get_plate_thumbnail(
  4524. archive_id: int,
  4525. plate_index: int,
  4526. db: AsyncSession = Depends(get_db, scope="function"),
  4527. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4528. require_media_token_ownership(
  4529. Permission.ARCHIVES_READ_ALL,
  4530. Permission.ARCHIVES_READ_OWN,
  4531. )
  4532. ),
  4533. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  4534. ):
  4535. """Get the thumbnail image for a specific plate.
  4536. Requires a media token query param (?token=xxx) when auth is enabled, and
  4537. returns 404 for an archive the caller may not read (#3025).
  4538. """
  4539. user, can_read_all = auth_result
  4540. service = ArchiveService(db)
  4541. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  4542. file_path = settings.base_dir / archive.file_path
  4543. if not file_path.is_file():
  4544. raise HTTPException(404, "Archive file not found")
  4545. try:
  4546. with zipfile.ZipFile(file_path, "r") as zf:
  4547. thumb_path = f"Metadata/plate_{plate_index}.png"
  4548. if thumb_path in zf.namelist():
  4549. data = zf.read(thumb_path)
  4550. return Response(content=data, media_type="image/png")
  4551. except Exception:
  4552. pass # Fall through to 404 if archive is unreadable or thumbnail missing
  4553. raise HTTPException(404, f"Thumbnail for plate {plate_index} not found")
  4554. async def _try_preview_slice_filaments(
  4555. db: AsyncSession,
  4556. *,
  4557. kind: str,
  4558. source_id: int,
  4559. plate_id: int,
  4560. file_path: Path,
  4561. request_id: str | None = None,
  4562. ) -> list[dict] | None:
  4563. """Run a preview slice via the user's configured sidecar so the filament
  4564. list endpoint can return real per-plate filaments for unsliced project
  4565. files. Returns ``None`` on any failure — the caller falls back to the
  4566. painted-face heuristic. ``request_id`` flows through to the sidecar
  4567. for live progress on the SliceModal's inline spinner + toast.
  4568. """
  4569. from backend.app.api.routes.settings import get_setting
  4570. from backend.app.services.slice_preview import get_preview_filaments
  4571. from backend.app.services.slicer_api import get_stall_timeout_seconds
  4572. preferred = (await get_setting(db, "preferred_slicer")) or "bambu_studio"
  4573. if preferred == "orcaslicer":
  4574. configured = await get_setting(db, "orcaslicer_api_url")
  4575. api_url = (configured or settings.slicer_api_url).strip()
  4576. elif preferred == "bambu_studio":
  4577. configured = await get_setting(db, "bambu_studio_api_url")
  4578. api_url = (configured or settings.bambu_studio_api_url).strip()
  4579. else:
  4580. return None
  4581. if not api_url:
  4582. return None
  4583. try:
  4584. file_bytes = file_path.read_bytes()
  4585. except OSError:
  4586. return None
  4587. return await get_preview_filaments(
  4588. kind=kind,
  4589. source_id=source_id,
  4590. plate_id=plate_id,
  4591. file_bytes=file_bytes,
  4592. file_name=file_path.name,
  4593. api_url=api_url,
  4594. request_id=request_id,
  4595. timeout_seconds=await get_stall_timeout_seconds(db),
  4596. )
  4597. @router.get("/{archive_id}/filament-requirements")
  4598. async def get_filament_requirements(
  4599. archive_id: int,
  4600. plate_id: int | None = None,
  4601. request_id: str | None = None,
  4602. full_slots: bool = False,
  4603. db: AsyncSession = Depends(get_db, scope="function"),
  4604. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4605. require_ownership_permission(
  4606. Permission.ARCHIVES_READ_ALL,
  4607. Permission.ARCHIVES_READ_OWN,
  4608. )
  4609. ),
  4610. printer_scope: PrinterScope = RequestPrinterScope,
  4611. ):
  4612. """Get filament requirements from the archived 3MF file.
  4613. Returns the filaments used in this print with their slot IDs, types, colors,
  4614. and usage amounts. This can be compared with current AMS state before reprinting.
  4615. Args:
  4616. archive_id: The archive ID
  4617. plate_id: Optional plate index to filter filaments for (for multi-plate files)
  4618. """
  4619. import defusedxml.ElementTree as ET
  4620. user, can_read_all = auth_result
  4621. service = ArchiveService(db)
  4622. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  4623. file_path = settings.base_dir / archive.file_path
  4624. if not file_path.is_file():
  4625. raise HTTPException(404, "Archive file not found")
  4626. filaments = []
  4627. try:
  4628. with zipfile.ZipFile(file_path, "r") as zf:
  4629. # Parse slice_info.config for filament requirements
  4630. if "Metadata/slice_info.config" in zf.namelist():
  4631. content = zf.read("Metadata/slice_info.config").decode()
  4632. root = ET.fromstring(content)
  4633. # If plate_id is specified, find filaments for that specific plate
  4634. if plate_id is not None:
  4635. # Find the plate element with matching index
  4636. for plate_elem in root.findall(".//plate"):
  4637. plate_index = None
  4638. for meta in plate_elem.findall("metadata"):
  4639. if meta.get("key") == "index":
  4640. try:
  4641. plate_index = int(meta.get("value", "0"))
  4642. except ValueError:
  4643. pass # Skip plate with non-numeric index metadata
  4644. break
  4645. if plate_index == plate_id:
  4646. # Extract filaments from this plate element
  4647. for filament_elem in plate_elem.findall("filament"):
  4648. filament_id = filament_elem.get("id")
  4649. filament_type = filament_elem.get("type", "")
  4650. filament_color = filament_elem.get("color", "")
  4651. used_g = filament_elem.get("used_g", "0")
  4652. used_m = filament_elem.get("used_m", "0")
  4653. tray_info_idx = filament_elem.get("tray_info_idx", "")
  4654. try:
  4655. used_grams = float(used_g)
  4656. except (ValueError, TypeError):
  4657. used_grams = 0
  4658. if used_grams > 0 and filament_id:
  4659. filaments.append(
  4660. {
  4661. "slot_id": int(filament_id),
  4662. "type": filament_type,
  4663. "color": filament_color,
  4664. "used_grams": round(used_grams, 1),
  4665. "used_meters": float(used_m) if used_m else 0,
  4666. "tray_info_idx": tray_info_idx,
  4667. "used_in_plate": True,
  4668. }
  4669. )
  4670. break
  4671. else:
  4672. # No plate_id specified - extract all filaments with used_g > 0
  4673. # This is the legacy behavior for single-plate files
  4674. for filament_elem in root.findall(".//filament"):
  4675. filament_id = filament_elem.get("id")
  4676. filament_type = filament_elem.get("type", "")
  4677. filament_color = filament_elem.get("color", "")
  4678. used_g = filament_elem.get("used_g", "0")
  4679. used_m = filament_elem.get("used_m", "0")
  4680. tray_info_idx = filament_elem.get("tray_info_idx", "")
  4681. # Only include filaments that are actually used
  4682. try:
  4683. used_grams = float(used_g)
  4684. except (ValueError, TypeError):
  4685. used_grams = 0
  4686. if used_grams > 0 and filament_id:
  4687. filaments.append(
  4688. {
  4689. "slot_id": int(filament_id),
  4690. "type": filament_type,
  4691. "color": filament_color,
  4692. "used_grams": round(used_grams, 1),
  4693. "used_meters": float(used_m) if used_m else 0,
  4694. "tray_info_idx": tray_info_idx,
  4695. "used_in_plate": True,
  4696. }
  4697. )
  4698. # Re-slicing a source that already carries slice_info (#2712).
  4699. # See library.py for the full rationale: the slice modal's list is
  4700. # positional, so a source using only slot 4 must still present
  4701. # four slots or the pick lands on slot 1. The print path keeps the
  4702. # used-only list it depends on.
  4703. if full_slots and filaments:
  4704. filaments = expand_to_project_slots(zf, filaments)
  4705. # Unsliced project files: see library.py for full rationale.
  4706. # Return the FULL project_settings.config slot list with a
  4707. # used_in_plate flag derived from the preview slice; the
  4708. # CLI needs every slot pre-filled to avoid silent default
  4709. # substitution.
  4710. if not filaments:
  4711. project_filaments = extract_project_filaments_from_3mf(zf)
  4712. used_slot_ids: set[int] = set()
  4713. if project_filaments and plate_id is not None:
  4714. preview = await _try_preview_slice_filaments(
  4715. db,
  4716. kind="archive",
  4717. source_id=archive_id,
  4718. plate_id=plate_id,
  4719. file_path=file_path,
  4720. request_id=request_id,
  4721. )
  4722. if preview is not None:
  4723. used_slot_ids = {f["slot_id"] for f in preview}
  4724. fallback_all_used = not used_slot_ids
  4725. for f in project_filaments:
  4726. f["used_in_plate"] = fallback_all_used or f["slot_id"] in used_slot_ids
  4727. filaments = project_filaments
  4728. # Sort by slot ID
  4729. filaments.sort(key=lambda x: x["slot_id"])
  4730. # Enrich with nozzle mapping for dual-nozzle printers
  4731. nozzle_mapping = extract_nozzle_mapping_from_3mf(zf, plate_id=plate_id)
  4732. if nozzle_mapping:
  4733. for filament in filaments:
  4734. filament["nozzle_id"] = nozzle_mapping.get(filament["slot_id"])
  4735. # Nozzle-rack machines (#1784): the print dialog offers a rack
  4736. # position per filament group, which needs the group table as well
  4737. # as the carriage above.
  4738. annotate_rack_groups(filaments, file_path, plate_id)
  4739. except Exception as e:
  4740. logger.warning("Failed to parse filament requirements from archive %s: %s", archive_id, e)
  4741. return {
  4742. "archive_id": archive_id,
  4743. "filename": archive.filename,
  4744. "plate_id": plate_id,
  4745. "filaments": filaments,
  4746. }
  4747. @router.post("/{archive_id}/slice", status_code=202)
  4748. async def slice_archive(
  4749. archive_id: int,
  4750. request: SliceRequest,
  4751. db: AsyncSession = Depends(get_db, scope="function"),
  4752. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.LIBRARY_UPLOAD),
  4753. printer_scope: PrinterScope = RequestPrinterScope,
  4754. actor: User | ApiKeyActor | None = RequestActor,
  4755. ):
  4756. """Enqueue a slice job for an archive's source. Returns 202 + job_id;
  4757. the slice runs in the background, the caller polls `GET /slice-jobs/{id}`.
  4758. Source preference: ``source_3mf_path`` (the un-sliced project file the
  4759. user originally sent to slice) → ``file_path`` (the sliced 3MF/gcode that
  4760. actually printed).
  4761. """
  4762. from backend.app.api.routes.library import guard_nozzle_class_reslice, slice_and_persist_as_archive
  4763. from backend.app.core.database import async_session
  4764. from backend.app.services.slice_dispatch import (
  4765. http_exception_to_job_error,
  4766. slice_dispatch,
  4767. )
  4768. archive = await db.get(PrintArchive, archive_id)
  4769. # Per-row ownership gate — mirror the archive read routes. LIBRARY_UPLOAD
  4770. # alone let a READ_OWN caller slice another user's archive by raw id even
  4771. # though GET on that id returned 404. An API key is checked as its owner
  4772. # (RequestActor); only auth off keeps can_read_all=True.
  4773. can_read_all = actor is None or actor.has_permission(Permission.ARCHIVES_READ_ALL.value)
  4774. archive = await _ensure_archive_visible(archive, actor, can_read_all, printer_scope)
  4775. # The re-sliced copy belongs to the caller and can be queued as their own,
  4776. # so slicing an archive needs the same right as printing it again.
  4777. from backend.app.api.routes.print_queue import _assert_can_queue_archive
  4778. await _assert_can_queue_archive(archive, actor, printer_scope)
  4779. src_relative = archive.source_3mf_path or archive.file_path
  4780. if not src_relative:
  4781. raise HTTPException(
  4782. status_code=400,
  4783. detail="Archive has no source file to slice",
  4784. )
  4785. src_path = (
  4786. Path(settings.base_dir) / src_relative
  4787. ) # SEC-PATH-OK: src_relative is archive.source_3mf_path from DB, set by _resolve_source_3mf_path which already does resolve+relative_to containment
  4788. if not src_path.exists():
  4789. raise HTTPException(status_code=404, detail="Archive source file missing on disk")
  4790. raw_filename = archive.filename or src_path.name
  4791. src_lower = raw_filename.lower()
  4792. if not (
  4793. src_lower.endswith(".stl")
  4794. or src_lower.endswith(".3mf")
  4795. or src_lower.endswith(".step")
  4796. or src_lower.endswith(".stp")
  4797. ):
  4798. raise HTTPException(
  4799. status_code=400,
  4800. detail="Archive's source file must be STL, 3MF, or STEP to slice",
  4801. )
  4802. # Match the library route: derive the sliced output's filename from
  4803. # `print_name` when set, so the new archive row's display name lines
  4804. # up with the source's display.
  4805. src_ext = Path(raw_filename).suffix.lower() or ".3mf"
  4806. src_filename = (
  4807. f"{archive.print_name.strip()}{src_ext}" if archive.print_name and archive.print_name.strip() else raw_filename
  4808. )
  4809. model_bytes = src_path.read_bytes()
  4810. archive_id_local = archive.id
  4811. user_id = current_user.id if current_user else None
  4812. # The new archive is its owner's also when a key, which resolves no cloud
  4813. # presets, slices it.
  4814. owner_id = user_id if user_id is not None else (actor.id if isinstance(actor, ApiKeyActor) else None)
  4815. # Block a cross-nozzle-class re-slice (single-nozzle <-> H2D) up front —
  4816. # BambuStudio's multi-extruder validator would otherwise reject it with a
  4817. # cryptic error. No-op for same-class or un-sliced sources.
  4818. await guard_nozzle_class_reslice(db, current_user, request, archive.sliced_for_model)
  4819. async def _run(job_id: int):
  4820. async with async_session() as task_db:
  4821. # Re-fetch the source archive on the background-task session.
  4822. src_archive = await task_db.get(PrintArchive, archive_id_local)
  4823. if src_archive is None:
  4824. raise http_exception_to_job_error(
  4825. HTTPException(status_code=404, detail="Archive disappeared during slice")
  4826. )
  4827. try:
  4828. response = await slice_and_persist_as_archive(
  4829. task_db,
  4830. model_bytes=model_bytes,
  4831. model_filename=src_filename,
  4832. request=request,
  4833. source_archive=src_archive,
  4834. current_user_id=user_id,
  4835. job_id=job_id,
  4836. owner_id=owner_id,
  4837. )
  4838. except HTTPException as exc:
  4839. raise http_exception_to_job_error(exc) from exc
  4840. return response.model_dump()
  4841. job = await slice_dispatch.enqueue(
  4842. kind="archive",
  4843. source_id=archive.id,
  4844. source_name=archive.print_name or archive.filename or f"archive {archive.id}",
  4845. owner_id=owner_id,
  4846. run=_run,
  4847. )
  4848. return {
  4849. "job_id": job.id,
  4850. "status": job.status,
  4851. "status_url": f"/api/v1/slice-jobs/{job.id}",
  4852. }
  4853. @router.post("/{archive_id}/reprint")
  4854. async def reprint_archive(
  4855. archive_id: int,
  4856. printer_id: int,
  4857. # SECURITY.md SEC-AUTH-1: every route either has an explicit auth dep or
  4858. # is in the route-auth-coverage allowlist. Gating the deprecation stub on
  4859. # QUEUE_CREATE matches the replacement route (POST /queue/) and means
  4860. # anonymous callers bounce at auth instead of seeing the deprecation
  4861. # message — leaking "this route exists" to unauthenticated callers is
  4862. # exactly the shape the backstop guards against.
  4863. _: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_CREATE),
  4864. ):
  4865. """Legacy direct reprint endpoint. Use POST /queue/ instead."""
  4866. logger.warning(
  4867. "Gone API used: POST /archives/%s/reprint?printer_id=%s; use POST /queue/ instead",
  4868. archive_id,
  4869. printer_id,
  4870. )
  4871. raise HTTPException(
  4872. status_code=410,
  4873. detail="Direct archive reprint has been removed. Create a print queue item with POST /queue/.",
  4874. )
  4875. # =============================================================================
  4876. # Project Page API
  4877. # =============================================================================
  4878. @router.get("/{archive_id}/project-page")
  4879. async def get_project_page(
  4880. archive_id: int,
  4881. db: AsyncSession = Depends(get_db, scope="function"),
  4882. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4883. require_ownership_permission(
  4884. Permission.ARCHIVES_READ_ALL,
  4885. Permission.ARCHIVES_READ_OWN,
  4886. )
  4887. ),
  4888. printer_scope: PrinterScope = RequestPrinterScope,
  4889. ):
  4890. """Get the project page data from the 3MF file."""
  4891. from backend.app.schemas.archive import ProjectPageResponse
  4892. from backend.app.services.archive import ProjectPageParser
  4893. user, can_read_all = auth_result
  4894. service = ArchiveService(db)
  4895. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  4896. file_path = settings.base_dir / archive.file_path
  4897. if not file_path.is_file():
  4898. raise HTTPException(404, "Archive file not found")
  4899. parser = ProjectPageParser(file_path)
  4900. data = parser.parse(archive_id)
  4901. return ProjectPageResponse(**data)
  4902. @router.patch("/{archive_id}/project-page")
  4903. async def update_project_page(
  4904. archive_id: int,
  4905. update_data: dict,
  4906. db: AsyncSession = Depends(get_db, scope="function"),
  4907. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4908. require_ownership_permission(
  4909. Permission.ARCHIVES_UPDATE_ALL,
  4910. Permission.ARCHIVES_UPDATE_OWN,
  4911. )
  4912. ),
  4913. printer_scope: PrinterScope = RequestPrinterScope,
  4914. ):
  4915. """Update project page metadata in the 3MF file."""
  4916. from backend.app.services.archive import ProjectPageParser
  4917. user, can_modify_all = auth_result
  4918. service = ArchiveService(db)
  4919. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_modify_all, printer_scope)
  4920. # It rewrites the archive's 3MF, the file of every print of it.
  4921. await _ensure_whole_archive(archive, printer_scope)
  4922. file_path = settings.base_dir / archive.file_path
  4923. if not file_path.is_file():
  4924. raise HTTPException(404, "Archive file not found")
  4925. parser = ProjectPageParser(file_path)
  4926. success = parser.update_metadata(update_data)
  4927. if not success:
  4928. raise HTTPException(500, "Failed to update project page")
  4929. # Return updated data
  4930. data = parser.parse(archive_id)
  4931. return data
  4932. @router.get("/{archive_id}/project-image/{image_path:path}")
  4933. async def get_project_image(
  4934. archive_id: int,
  4935. image_path: str,
  4936. db: AsyncSession = Depends(get_db, scope="function"),
  4937. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4938. require_media_token_ownership(
  4939. Permission.ARCHIVES_READ_ALL,
  4940. Permission.ARCHIVES_READ_OWN,
  4941. )
  4942. ),
  4943. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  4944. ):
  4945. """Get an image from the 3MF project page.
  4946. Requires a media token query param (?token=xxx) when auth is enabled, and
  4947. returns 404 for an archive the caller may not read (#3025).
  4948. """
  4949. user, can_read_all = auth_result
  4950. from backend.app.services.archive import ProjectPageParser
  4951. service = ArchiveService(db)
  4952. archive = await _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  4953. file_path = settings.base_dir / archive.file_path
  4954. if not file_path.is_file():
  4955. raise HTTPException(404, "Archive file not found")
  4956. parser = ProjectPageParser(file_path)
  4957. result = parser.get_image(image_path)
  4958. if not result:
  4959. raise HTTPException(404, "Image not found in 3MF file")
  4960. image_data, content_type = result
  4961. return Response(
  4962. content=image_data,
  4963. media_type=content_type,
  4964. headers={"Cache-Control": "max-age=3600"},
  4965. )
  4966. # =============================================================================
  4967. # Source 3MF API (Original Project Files)
  4968. # =============================================================================
  4969. def _resolve_source_3mf_path(archive: PrintArchive, source_filename: str) -> Path:
  4970. """Resolve where to write a source 3MF for ``archive``.
  4971. Normal archives nest the source under ``<archive_file_dir>/source/``.
  4972. "Fallback" archives (created in main.py when MQTT reports a print start
  4973. but Bambuddy never saw the source 3MF — cloud / Handy / pre-existing
  4974. SD-card prints) carry ``file_path=""``. Joining that with ``base_dir``
  4975. via the ``/`` operator silently yields ``base_dir`` itself, whose parent
  4976. is ``base_dir.parent`` — which sent the upload to ``/app/source/`` and
  4977. raised a 500 on the final ``relative_to`` (#1531). Fallback archives
  4978. now land under ``<base_dir>/archive/no_source/<archive_id>/`` instead,
  4979. which stays inside the data volume and remains addressable by every
  4980. read site that does ``base_dir / archive.source_3mf_path``.
  4981. The resolved directory is asserted to be inside ``base_dir`` even when
  4982. ``archive.file_path`` is populated, so a row corrupted by an old import
  4983. or manual SQL edit fails with a clear 500 instead of writing outside
  4984. the data volume.
  4985. """
  4986. if archive.file_path:
  4987. archive_file = settings.base_dir / archive.file_path
  4988. source_dir = archive_file.parent / "source"
  4989. else:
  4990. source_dir = settings.base_dir / "archive" / "no_source" / str(archive.id)
  4991. # Containment check via resolve() — catches absolute file_path, `..`
  4992. # traversal, and any other shape that escapes the data volume — but we
  4993. # return the *literal* source_dir below. Resolving the returned path
  4994. # would canonicalise away a symlinked DATA_DIR (legitimate on TrueNAS /
  4995. # QNAP / Synology storage pools, and any `-v /symlink:/app/data`
  4996. # mount), which would then make the caller's
  4997. # ``source_path.relative_to(settings.base_dir)`` raise because the
  4998. # left side is canonical and the right is the symlink path.
  4999. try:
  5000. source_dir.resolve().relative_to(settings.base_dir.resolve())
  5001. except ValueError as exc:
  5002. raise HTTPException(
  5003. 500,
  5004. f"Archive {archive.id} resolves to a path outside the data directory; cannot attach source.",
  5005. ) from exc
  5006. source_dir.mkdir(parents=True, exist_ok=True)
  5007. return (
  5008. source_dir / source_filename
  5009. ) # SEC-PATH-OK: callers pass _safe_filename(...) basename-stripped; source_dir resolve+relative_to checked above
  5010. @router.post("/{archive_id}/source")
  5011. async def upload_source_3mf(
  5012. archive_id: int,
  5013. file: UploadFile = File(...),
  5014. db: AsyncSession = Depends(get_db, scope="function"),
  5015. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  5016. require_ownership_permission(
  5017. Permission.ARCHIVES_UPDATE_ALL,
  5018. Permission.ARCHIVES_UPDATE_OWN,
  5019. )
  5020. ),
  5021. printer_scope: PrinterScope = RequestPrinterScope,
  5022. ):
  5023. """Upload the original source 3MF project file for an archive."""
  5024. user, can_modify_all = auth_result
  5025. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  5026. archive = await _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  5027. await _ensure_whole_archive(archive, printer_scope)
  5028. if not file.filename or not file.filename.endswith(".3mf"):
  5029. raise HTTPException(400, "File must be a .3mf file")
  5030. # Save the source 3MF file - preserve original filename, strip directory components
  5031. source_filename = _safe_filename(file.filename)
  5032. source_path = _resolve_source_3mf_path(archive, source_filename)
  5033. # Delete old source file if exists
  5034. if archive.source_3mf_path:
  5035. old_source_path = settings.base_dir / archive.source_3mf_path
  5036. if old_source_path.exists():
  5037. old_source_path.unlink()
  5038. content = await file.read()
  5039. # #1401: validate zip header on source 3MF uploads too — source files
  5040. # are uploaded for reprint and slicing, so an invalid one breaks the
  5041. # same downstream paths as a bad sliced file.
  5042. from backend.app.api.routes.library import validate_print_file_upload
  5043. validate_print_file_upload(file.filename, content)
  5044. source_path.write_bytes(content)
  5045. # Update archive with source path (relative to base_dir)
  5046. archive.source_3mf_path = str(source_path.relative_to(settings.base_dir))
  5047. await db.commit()
  5048. await db.refresh(archive)
  5049. return {
  5050. "status": "uploaded",
  5051. "source_3mf_path": archive.source_3mf_path,
  5052. "filename": source_filename,
  5053. }
  5054. @router.get("/{archive_id}/source")
  5055. async def download_source_3mf(
  5056. archive_id: int,
  5057. db: AsyncSession = Depends(get_db, scope="function"),
  5058. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  5059. require_ownership_permission(
  5060. Permission.ARCHIVES_READ_ALL,
  5061. Permission.ARCHIVES_READ_OWN,
  5062. )
  5063. ),
  5064. printer_scope: PrinterScope = RequestPrinterScope,
  5065. ):
  5066. """Download the source 3MF project file."""
  5067. user, can_read_all = auth_result
  5068. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  5069. archive = await _ensure_archive_visible(result.scalar_one_or_none(), user, can_read_all, printer_scope)
  5070. if not archive.source_3mf_path:
  5071. raise HTTPException(404, "No source 3MF attached to this archive")
  5072. source_path = settings.base_dir / archive.source_3mf_path
  5073. if not source_path.exists():
  5074. raise HTTPException(404, "Source 3MF file not found on disk")
  5075. # Use the actual filename from the path
  5076. filename = source_path.name
  5077. return FileResponse(
  5078. path=source_path,
  5079. filename=filename,
  5080. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  5081. )
  5082. @router.get("/{archive_id}/source/{filename}")
  5083. async def download_source_3mf_for_slicer(
  5084. archive_id: int,
  5085. filename: str,
  5086. db: AsyncSession = Depends(get_db, scope="function"),
  5087. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  5088. require_ownership_permission(
  5089. Permission.ARCHIVES_READ_ALL,
  5090. Permission.ARCHIVES_READ_OWN,
  5091. )
  5092. ),
  5093. printer_scope: PrinterScope = RequestPrinterScope,
  5094. ):
  5095. """Download source 3MF with filename in URL."""
  5096. user, can_read_all = auth_result
  5097. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  5098. archive = await _ensure_archive_visible(result.scalar_one_or_none(), user, can_read_all, printer_scope)
  5099. if not archive.source_3mf_path:
  5100. raise HTTPException(404, "No source 3MF attached to this archive")
  5101. source_path = settings.base_dir / archive.source_3mf_path
  5102. if not source_path.exists():
  5103. raise HTTPException(404, "Source 3MF file not found on disk")
  5104. return FileResponse(
  5105. path=source_path,
  5106. filename=filename if filename.endswith(".3mf") else f"{filename}.3mf",
  5107. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  5108. )
  5109. @router.post("/{archive_id}/source-slicer-token")
  5110. async def create_source_slicer_token(
  5111. archive_id: int,
  5112. db: AsyncSession = Depends(get_db, scope="function"),
  5113. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  5114. require_ownership_permission(
  5115. Permission.ARCHIVES_READ_ALL,
  5116. Permission.ARCHIVES_READ_OWN,
  5117. )
  5118. ),
  5119. printer_scope: PrinterScope = RequestPrinterScope,
  5120. ):
  5121. """Create a short-lived download token for opening source 3MF in slicer."""
  5122. from backend.app.core.auth import create_slicer_download_token
  5123. user, can_read_all = auth_result
  5124. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  5125. archive = await _ensure_archive_visible(result.scalar_one_or_none(), user, can_read_all, printer_scope)
  5126. if not archive.source_3mf_path:
  5127. raise HTTPException(404, "No source 3MF attached to this archive")
  5128. token = await create_slicer_download_token("source", archive_id)
  5129. return {"token": token}
  5130. @router.get("/{archive_id}/source-dl/{token}/{filename}")
  5131. async def download_source_3mf_for_slicer_with_token(
  5132. archive_id: int,
  5133. token: str,
  5134. filename: str,
  5135. db: AsyncSession = Depends(get_db, scope="function"),
  5136. ):
  5137. """Download source 3MF using a slicer download token.
  5138. Token-authenticated (no auth headers needed). The token is short-lived and
  5139. archive-bound, created by POST /{archive_id}/source-slicer-token, and
  5140. redeemable for the rest of its TTL rather than exactly once (#3029).
  5141. """
  5142. from backend.app.core.auth import verify_slicer_download_token
  5143. if not await verify_slicer_download_token(token, "source", archive_id, single_use=False):
  5144. raise HTTPException(403, "Invalid or expired download token")
  5145. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  5146. archive = result.scalar_one_or_none()
  5147. if not archive:
  5148. raise HTTPException(404, "Archive not found")
  5149. if not archive.source_3mf_path:
  5150. raise HTTPException(404, "No source 3MF attached to this archive")
  5151. source_path = settings.base_dir / archive.source_3mf_path
  5152. if not source_path.exists():
  5153. raise HTTPException(404, "Source 3MF file not found on disk")
  5154. return FileResponse(
  5155. path=source_path,
  5156. filename=filename if filename.endswith(".3mf") else f"{filename}.3mf",
  5157. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  5158. )
  5159. @router.post("/upload-source")
  5160. async def upload_source_3mf_by_name(
  5161. file: UploadFile = File(...),
  5162. print_name: str = Query(None, description="Match archive by print name"),
  5163. db: AsyncSession = Depends(get_db, scope="function"),
  5164. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  5165. printer_scope: PrinterScope = RequestPrinterScope,
  5166. ):
  5167. """Upload source 3MF and match to archive by print name.
  5168. This endpoint is designed for slicer post-processing scripts.
  5169. It finds the most recent archive matching the print name and attaches the source.
  5170. """
  5171. if not file.filename or not file.filename.endswith(".3mf"):
  5172. raise HTTPException(400, "File must be a .3mf file")
  5173. safe_filename = _safe_filename(file.filename)
  5174. # Derive print name from filename if not provided
  5175. if not print_name:
  5176. # Remove .3mf extension and common suffixes
  5177. print_name = safe_filename.rsplit(".3mf", 1)[0]
  5178. # Remove _source suffix if present
  5179. if print_name.endswith("_source"):
  5180. print_name = print_name[:-7]
  5181. # Find matching archive - try exact match first, then fuzzy
  5182. # Only an archive every print of which is on the caller's printers: its
  5183. # file is the whole archive's (archive redesign).
  5184. visible = _visible_archive_clauses(None, True, printer_scope, whole=True)
  5185. result = await db.execute(
  5186. select(PrintArchive)
  5187. .where(PrintArchive.print_name == print_name, *visible)
  5188. .order_by(PrintArchive.created_at.desc())
  5189. .limit(1)
  5190. )
  5191. archive = result.scalar_one_or_none()
  5192. if not archive:
  5193. # Try matching filename without .gcode.3mf
  5194. result = await db.execute(
  5195. select(PrintArchive)
  5196. .where(PrintArchive.filename.like(f"{print_name}%"), *visible)
  5197. .order_by(PrintArchive.created_at.desc())
  5198. .limit(1)
  5199. )
  5200. archive = result.scalar_one_or_none()
  5201. if not archive:
  5202. # Try case-insensitive partial match on print_name
  5203. result = await db.execute(
  5204. select(PrintArchive)
  5205. .where(PrintArchive.print_name.ilike(f"%{print_name}%"), *visible)
  5206. .order_by(PrintArchive.created_at.desc())
  5207. .limit(1)
  5208. )
  5209. archive = result.scalar_one_or_none()
  5210. if not archive:
  5211. raise HTTPException(404, f"No archive found matching '{print_name}'")
  5212. # Save the source 3MF file - preserve original filename, strip directory components
  5213. source_filename = safe_filename
  5214. source_path = _resolve_source_3mf_path(archive, source_filename)
  5215. # Delete old source file if exists
  5216. if archive.source_3mf_path:
  5217. old_source_path = settings.base_dir / archive.source_3mf_path
  5218. if old_source_path.exists():
  5219. old_source_path.unlink()
  5220. content = await file.read()
  5221. # #1401: same zip-header check as the other upload routes — the
  5222. # match-by-name endpoint is used by slicer post-processing scripts,
  5223. # so a misconfigured script is exactly how a bad 3MF would slip in.
  5224. from backend.app.api.routes.library import validate_print_file_upload
  5225. validate_print_file_upload(file.filename, content)
  5226. source_path.write_bytes(content)
  5227. # Update archive with source path
  5228. archive.source_3mf_path = str(source_path.relative_to(settings.base_dir))
  5229. await db.commit()
  5230. await db.refresh(archive)
  5231. return {
  5232. "status": "uploaded",
  5233. "archive_id": archive.id,
  5234. "archive_name": archive.print_name or archive.filename,
  5235. "source_3mf_path": archive.source_3mf_path,
  5236. "filename": source_filename,
  5237. }
  5238. @router.delete("/{archive_id}/source")
  5239. async def delete_source_3mf(
  5240. archive_id: int,
  5241. db: AsyncSession = Depends(get_db, scope="function"),
  5242. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  5243. require_ownership_permission(
  5244. Permission.ARCHIVES_DELETE_ALL,
  5245. Permission.ARCHIVES_DELETE_OWN,
  5246. )
  5247. ),
  5248. printer_scope: PrinterScope = RequestPrinterScope,
  5249. ):
  5250. """Delete the source 3MF project file from an archive."""
  5251. user, can_modify_all = auth_result
  5252. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  5253. archive = await _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  5254. await _ensure_whole_archive(archive, printer_scope)
  5255. if not archive.source_3mf_path:
  5256. raise HTTPException(404, "No source 3MF attached to this archive")
  5257. # Delete the file
  5258. source_path = settings.base_dir / archive.source_3mf_path
  5259. if source_path.exists():
  5260. source_path.unlink()
  5261. # Clear the path in database
  5262. archive.source_3mf_path = None
  5263. await db.commit()
  5264. return {"status": "deleted"}
  5265. # =============================================================================
  5266. # F3D API (Fusion 360 Design Files)
  5267. # =============================================================================
  5268. @router.post("/{archive_id}/f3d")
  5269. async def upload_f3d(
  5270. archive_id: int,
  5271. file: UploadFile = File(...),
  5272. db: AsyncSession = Depends(get_db, scope="function"),
  5273. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  5274. require_ownership_permission(
  5275. Permission.ARCHIVES_UPDATE_ALL,
  5276. Permission.ARCHIVES_UPDATE_OWN,
  5277. )
  5278. ),
  5279. printer_scope: PrinterScope = RequestPrinterScope,
  5280. ):
  5281. """Upload a Fusion 360 design file for an archive."""
  5282. user, can_modify_all = auth_result
  5283. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  5284. archive = await _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  5285. await _ensure_whole_archive(archive, printer_scope)
  5286. if not file.filename or not file.filename.endswith(".f3d"):
  5287. raise HTTPException(400, "File must be a .f3d file")
  5288. # Get archive directory and create f3d subdirectory
  5289. file_path = settings.base_dir / archive.file_path
  5290. archive_dir = file_path.parent
  5291. f3d_dir = archive_dir / "f3d"
  5292. f3d_dir.mkdir(exist_ok=True)
  5293. # Delete old F3D file if exists
  5294. if archive.f3d_path:
  5295. old_f3d_path = settings.base_dir / archive.f3d_path
  5296. if old_f3d_path.exists():
  5297. old_f3d_path.unlink()
  5298. # Save the F3D file - preserve original filename, strip directory components
  5299. f3d_filename = _safe_filename(file.filename)
  5300. f3d_path = f3d_dir / f3d_filename # SEC-PATH-OK: f3d_filename = _safe_filename(...) basename-stripped above
  5301. content = await file.read()
  5302. f3d_path.write_bytes(content)
  5303. # Update archive with F3D path (relative to base_dir)
  5304. archive.f3d_path = str(f3d_path.relative_to(settings.base_dir))
  5305. await db.commit()
  5306. await db.refresh(archive)
  5307. return {
  5308. "status": "uploaded",
  5309. "f3d_path": archive.f3d_path,
  5310. "filename": f3d_filename,
  5311. }
  5312. @router.get("/{archive_id}/f3d")
  5313. async def download_f3d(
  5314. archive_id: int,
  5315. db: AsyncSession = Depends(get_db, scope="function"),
  5316. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  5317. require_ownership_permission(
  5318. Permission.ARCHIVES_READ_ALL,
  5319. Permission.ARCHIVES_READ_OWN,
  5320. )
  5321. ),
  5322. printer_scope: PrinterScope = RequestPrinterScope,
  5323. ):
  5324. """Download the Fusion 360 design file."""
  5325. user, can_read_all = auth_result
  5326. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  5327. archive = await _ensure_archive_visible(result.scalar_one_or_none(), user, can_read_all, printer_scope)
  5328. if not archive.f3d_path:
  5329. raise HTTPException(404, "No F3D file attached to this archive")
  5330. f3d_path = settings.base_dir / archive.f3d_path
  5331. if not f3d_path.exists():
  5332. raise HTTPException(404, "F3D file not found on disk")
  5333. # Use the actual filename from the path
  5334. filename = f3d_path.name
  5335. return FileResponse(
  5336. path=f3d_path,
  5337. filename=filename,
  5338. media_type="application/octet-stream",
  5339. )
  5340. @router.delete("/{archive_id}/f3d")
  5341. async def delete_f3d(
  5342. archive_id: int,
  5343. db: AsyncSession = Depends(get_db, scope="function"),
  5344. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  5345. require_ownership_permission(
  5346. Permission.ARCHIVES_DELETE_ALL,
  5347. Permission.ARCHIVES_DELETE_OWN,
  5348. )
  5349. ),
  5350. printer_scope: PrinterScope = RequestPrinterScope,
  5351. ):
  5352. """Delete the Fusion 360 design file from an archive."""
  5353. user, can_modify_all = auth_result
  5354. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  5355. archive = await _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  5356. await _ensure_whole_archive(archive, printer_scope)
  5357. if not archive.f3d_path:
  5358. raise HTTPException(404, "No F3D file attached to this archive")
  5359. # Delete the file
  5360. f3d_path = settings.base_dir / archive.f3d_path
  5361. if f3d_path.exists():
  5362. f3d_path.unlink()
  5363. # Clear the path in database
  5364. archive.f3d_path = None
  5365. await db.commit()
  5366. return {"status": "deleted"}