archives.py 239 KB

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