archives.py 238 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832
  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. update_payload = update_data.model_dump(exclude_unset=True)
  1718. # Later runs of the archive are charged from its cost and weight: a caller
  1719. # limited to their own archives sets neither. The one exception is the
  1720. # weight of an archive without its 3MF (#1820), which has none and cannot
  1721. # be printed again.
  1722. if not can_modify_all:
  1723. if "cost" in update_payload:
  1724. raise HTTPException(403, "Only users who can edit all archives can set an archive's cost")
  1725. if "filament_used_grams" in update_payload and update_payload["filament_used_grams"] != previous_filament_grams:
  1726. if previous_filament_grams is not None or archive.file_path:
  1727. raise HTTPException(403, "Only users who can edit all archives can change an archive's filament weight")
  1728. # A new printer must be one the caller may use (as on upload), and both
  1729. # targets must exist. None clears either: a printer-less archive is in
  1730. # every scope, like one uploaded without a printer.
  1731. new_printer_id = update_payload.get("printer_id")
  1732. if new_printer_id is not None and new_printer_id != archive.printer_id:
  1733. printer_scope.ensure(new_printer_id)
  1734. if await db.get(Printer, new_printer_id) is None:
  1735. raise HTTPException(400, "Printer not found")
  1736. new_project_id = update_payload.get("project_id")
  1737. if new_project_id is not None and new_project_id != archive.project_id:
  1738. if await db.get(Project, new_project_id) is None:
  1739. raise HTTPException(400, "Project not found")
  1740. # #1898: how the verdict arrived is recorded with it, never on its own.
  1741. verdict_source = update_payload.pop("user_verdict_source", None)
  1742. for field, value in update_payload.items():
  1743. setattr(archive, field, value)
  1744. # New grams are a new price, unless the caller set the cost as well.
  1745. repriced = False
  1746. if (
  1747. update_payload.get("filament_used_grams")
  1748. and "cost" not in update_payload
  1749. and update_payload["filament_used_grams"] != previous_filament_grams
  1750. and (can_modify_all or archive.cost is None)
  1751. ):
  1752. archive.cost = await _one_print_cost(db, archive, previous_grams=previous_filament_grams)
  1753. repriced = True
  1754. # #1898: a landed verdict retires the one-tap capability token from the
  1755. # push notification — the links stop changing anything once someone
  1756. # decided, and report the recorded verdict instead. Clearing the verdict
  1757. # drops the provenance but leaves the token spent: it was used.
  1758. if "user_verdict" in update_payload:
  1759. if update_payload["user_verdict"] is None:
  1760. archive.user_verdict_source = None
  1761. archive.user_verdict_at = None
  1762. else:
  1763. stamp_verdict(archive, verdict_source or "api")
  1764. retire_confirm_token(archive)
  1765. # #1444: Mirror per-run classification fields to the most recent
  1766. # PrintLogEntry for this archive. PrintLogEntry.failure_reason is captured
  1767. # once at print-completion time from archive.failure_reason — which is
  1768. # NULL until the user classifies the failure via the Edit Archive modal.
  1769. # Without this mirror the Failure Analysis widget (which groups by
  1770. # print_log_entries.failure_reason) keeps showing "Unknown" forever.
  1771. # Same desync hits status: flipping it in the modal wouldn't update the
  1772. # entry either. Only the latest entry is touched because that's the run
  1773. # the modal is implicitly showing (archive.failure_reason / status are
  1774. # overwritten on each reprint to reflect the latest run's outcome).
  1775. # filament_used_grams rides along for the same reason (#1820): the filament
  1776. # totals on the Projects page and in the Prometheus metrics sum the LOG
  1777. # ENTRY's grams, not the archive's, so correcting only the archive would fix
  1778. # the card and leave every aggregate reading the old figure -- or, for a
  1779. # print that archived without its 3MF, no figure at all.
  1780. mirror_fields = {"failure_reason", "status", "filament_used_grams", "user_verdict"}
  1781. to_mirror = {k: v for k, v in update_payload.items() if k in mirror_fields}
  1782. if to_mirror:
  1783. from backend.app.models.print_log import PrintLogEntry
  1784. latest_entry = await db.scalar(
  1785. select(PrintLogEntry)
  1786. .where(PrintLogEntry.archive_id == archive_id)
  1787. .order_by(PrintLogEntry.id.desc())
  1788. .limit(1)
  1789. )
  1790. if latest_entry is not None:
  1791. # ...but never over a figure the run measured for itself. A run's
  1792. # grams come from the tracked spool delta when there is one, and
  1793. # only fall back to copying the archive's estimate when there is
  1794. # not (see _compute_run_filament_grams). Overwriting a measurement
  1795. # with a typed estimate would lose the better number; the case this
  1796. # edit exists for -- a print archived with no 3MF -- leaves the run
  1797. # with nothing at all, so it is covered by the None arm.
  1798. if "filament_used_grams" in to_mirror and not (
  1799. latest_entry.filament_used_grams is None or latest_entry.filament_used_grams == previous_filament_grams
  1800. ):
  1801. del to_mirror["filament_used_grams"]
  1802. # The run's cost was inherited with its grams, so it follows them.
  1803. if "filament_used_grams" in to_mirror and repriced:
  1804. to_mirror["cost"] = archive.cost
  1805. for field, value in to_mirror.items():
  1806. setattr(latest_entry, field, value)
  1807. await db.commit()
  1808. # Re-fetch with relationships loaded after commit
  1809. result = await db.execute(
  1810. select(PrintArchive)
  1811. .options(selectinload(PrintArchive.project), selectinload(PrintArchive.created_by))
  1812. .where(PrintArchive.id == archive_id)
  1813. )
  1814. archive = result.scalar_one_or_none()
  1815. # Load run aggregate so the time/accuracy badge stays consistent with
  1816. # the list / detail endpoints when the frontend re-renders the card
  1817. # after a PATCH (#1608).
  1818. run_aggregates = await _load_run_aggregates(db, [archive.id]) if archive else {}
  1819. return archive_to_response(archive, run_aggregate=run_aggregates.get(archive.id) if archive else None)
  1820. @router.post("/{archive_id}/favorite", response_model=ArchiveResponse)
  1821. async def toggle_favorite(
  1822. archive_id: int,
  1823. db: AsyncSession = Depends(get_db),
  1824. auth_result: tuple[User | None, bool] = Depends(
  1825. require_ownership_permission(
  1826. Permission.ARCHIVES_UPDATE_ALL,
  1827. Permission.ARCHIVES_UPDATE_OWN,
  1828. )
  1829. ),
  1830. printer_scope: PrinterScope = RequestPrinterScope,
  1831. ):
  1832. """Toggle favorite status for an archive."""
  1833. user, can_modify_all = auth_result
  1834. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  1835. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  1836. archive.is_favorite = not archive.is_favorite
  1837. await db.commit()
  1838. await db.refresh(archive)
  1839. return archive
  1840. async def _spoolman_owns_cost(db: AsyncSession) -> bool:
  1841. """True when per-spool pricing lives in Spoolman rather than in our tables.
  1842. Both cost recalculations below rebuild a print's cost from
  1843. ``SpoolUsageHistory``, and fall back to the built-in Filament catalogue or
  1844. the global default rate when there are no rows for it. In Spoolman mode
  1845. there are never any rows -- the built-in usage tracker is handed
  1846. ``spoolman_owns_usage`` at print start and writes none -- so that fallback
  1847. is not a recalculation, it is a downgrade: it would overwrite the
  1848. Spoolman-priced figure ``spoolman_tracking`` recorded at completion with a
  1849. default-rate one, and the per-slot spool resolution it came from is
  1850. transient and cannot be rebuilt here (#2591).
  1851. """
  1852. from backend.app.api.routes.settings import get_setting
  1853. setting = await get_setting(db, "spoolman_enabled")
  1854. return bool(setting) and setting.lower() == "true"
  1855. def _priced_usage_totals():
  1856. """Priced spool usage per archive. A row recorded without a price is left
  1857. out of both sums, so its grams are priced at the default rate instead of
  1858. counting as free filament."""
  1859. priced = SpoolUsageHistory.cost.is_not(None)
  1860. return (
  1861. func.sum(SpoolUsageHistory.cost),
  1862. func.sum(case((priced, SpoolUsageHistory.weight_used), else_=0.0)),
  1863. )
  1864. async def _one_print_cost(db: AsyncSession, archive: PrintArchive, previous_grams: float | None = None) -> float | None:
  1865. """The archive's cost for one print of its grams (see ``price_one_print``).
  1866. A Spoolman-priced cost has no usage rows to rebuild it from (#2591); it is
  1867. kept, scaled to new grams when they were edited.
  1868. """
  1869. from backend.app.models.print_log import PrintLogEntry
  1870. grams = archive.filament_used_grams
  1871. default_cost = await _default_cost_per_kg(db)
  1872. usage_cost, tracked_grams = (
  1873. await db.execute(select(*_priced_usage_totals()).where(SpoolUsageHistory.archive_id == archive.id))
  1874. ).one()
  1875. if usage_cost is not None and usage_cost > 0:
  1876. printed_grams, runs = (
  1877. await db.execute(
  1878. select(func.sum(PrintLogEntry.filament_used_grams), func.count(PrintLogEntry.id)).where(
  1879. PrintLogEntry.archive_id == archive.id
  1880. )
  1881. )
  1882. ).one()
  1883. return price_one_print(
  1884. grams,
  1885. float(usage_cost),
  1886. float(tracked_grams or 0),
  1887. float(printed_grams or 0),
  1888. int(runs or 0),
  1889. default_cost,
  1890. default_cost,
  1891. )
  1892. if not grams:
  1893. return archive.cost
  1894. if archive.cost is not None and await _spoolman_owns_cost(db):
  1895. if previous_grams:
  1896. return round(archive.cost * grams / previous_grams, 2)
  1897. return archive.cost
  1898. fallback = default_cost
  1899. if archive.filament_type:
  1900. primary_type = archive.filament_type.split(",")[0].strip()
  1901. catalog = await db.scalar(select(Filament.cost_per_kg).where(Filament.type == primary_type).limit(1))
  1902. if catalog is not None:
  1903. fallback = catalog
  1904. return price_one_print(grams, None, 0.0, 0.0, 0, fallback, default_cost)
  1905. @router.post("/{archive_id}/rescan", response_model=ArchiveResponse)
  1906. async def rescan_archive(
  1907. archive_id: int,
  1908. db: AsyncSession = Depends(get_db),
  1909. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  1910. printer_scope: PrinterScope = RequestPrinterScope,
  1911. ):
  1912. """Rescan the 3MF file and update metadata."""
  1913. from backend.app.services.archive import ThreeMFParser
  1914. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  1915. archive = result.scalar_one_or_none()
  1916. if not archive or not printer_scope.allows(archive.printer_id):
  1917. raise HTTPException(404, "Archive not found")
  1918. file_path = settings.base_dir / archive.file_path
  1919. if not file_path.is_file():
  1920. raise HTTPException(404, "Archive file not found")
  1921. # Parse the 3MF file
  1922. parser = ThreeMFParser(file_path)
  1923. metadata = parser.parse()
  1924. # Update fields from metadata
  1925. if metadata.get("filament_type"):
  1926. archive.filament_type = metadata["filament_type"]
  1927. if metadata.get("filament_color"):
  1928. archive.filament_color = metadata["filament_color"]
  1929. if metadata.get("print_time_seconds"):
  1930. archive.print_time_seconds = metadata["print_time_seconds"]
  1931. if metadata.get("filament_used_grams"):
  1932. archive.filament_used_grams = metadata["filament_used_grams"]
  1933. if metadata.get("layer_height"):
  1934. archive.layer_height = metadata["layer_height"]
  1935. if metadata.get("nozzle_diameter"):
  1936. archive.nozzle_diameter = metadata["nozzle_diameter"]
  1937. if metadata.get("bed_temperature"):
  1938. archive.bed_temperature = metadata["bed_temperature"]
  1939. if metadata.get("bed_type"):
  1940. archive.bed_type = metadata["bed_type"]
  1941. if metadata.get("nozzle_temperature"):
  1942. archive.nozzle_temperature = metadata["nozzle_temperature"]
  1943. if metadata.get("makerworld_url"):
  1944. archive.makerworld_url = metadata["makerworld_url"]
  1945. if metadata.get("designer"):
  1946. archive.designer = metadata["designer"]
  1947. # Calculate cost: prefer spool-based cost if available, else catalog-based.
  1948. # When spool-based costs exist but don't cover every filament gram used
  1949. # (#1344), fall back to the global default rate for the untracked weight
  1950. # so the displayed cost still reflects the whole print.
  1951. if archive.filament_used_grams and archive.filament_type:
  1952. archive.cost = await _one_print_cost(db, archive)
  1953. await db.commit()
  1954. await db.refresh(archive)
  1955. return archive
  1956. @router.post("/recalculate-costs")
  1957. async def recalculate_all_costs(
  1958. db: AsyncSession = Depends(get_db),
  1959. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  1960. printer_scope: PrinterScope = RequestPrinterScope,
  1961. ):
  1962. """Recalculate costs for all archives based on filament usage and prices."""
  1963. # Only the archives of the caller's printers, as the archive list shows them.
  1964. query = select(PrintArchive)
  1965. if (clause := printer_scope.where(PrintArchive.printer_id)) is not None:
  1966. query = query.where(clause)
  1967. result = await db.execute(query)
  1968. archives = list(result.scalars().all())
  1969. # Load all filaments for lookup
  1970. filament_result = await db.execute(select(Filament))
  1971. filaments = {f.type: f.cost_per_kg for f in filament_result.scalars().all()}
  1972. default_cost_per_kg = await _default_cost_per_kg(db)
  1973. # Pre-fetch all usage costs and tracked weight by archive_id.
  1974. # Tracked weight is used to top-up the cost at the default rate for any
  1975. # filament grams not covered by an inventory spool (#1344).
  1976. usage_costs_result = await db.execute(
  1977. select(SpoolUsageHistory.archive_id, *_priced_usage_totals()).group_by(SpoolUsageHistory.archive_id)
  1978. )
  1979. usage_costs = usage_costs_result.fetchall()
  1980. cost_map = {
  1981. row[0]: (row[1], float(row[2] or 0))
  1982. for row in usage_costs
  1983. if row[0] is not None and row[1] is not None and row[1] > 0
  1984. }
  1985. from backend.app.models.print_log import PrintLogEntry
  1986. run_totals = {
  1987. row[0]: (float(row[1] or 0), int(row[2] or 0))
  1988. for row in (
  1989. await db.execute(
  1990. select(
  1991. PrintLogEntry.archive_id,
  1992. func.sum(PrintLogEntry.filament_used_grams),
  1993. func.count(PrintLogEntry.id),
  1994. ).group_by(PrintLogEntry.archive_id)
  1995. )
  1996. ).all()
  1997. }
  1998. spoolman_owns = await _spoolman_owns_cost(db)
  1999. updated = 0
  2000. preserved = 0
  2001. for archive in archives:
  2002. usage = cost_map.get(archive.id)
  2003. if usage is not None:
  2004. usage_cost, tracked_grams = usage
  2005. printed_grams, runs = run_totals.get(archive.id, (0.0, 0))
  2006. new_cost = price_one_print(
  2007. archive.filament_used_grams,
  2008. usage_cost,
  2009. tracked_grams,
  2010. printed_grams,
  2011. runs,
  2012. default_cost_per_kg,
  2013. default_cost_per_kg,
  2014. )
  2015. else:
  2016. # No usage linked to this archive. Usage without an archive link
  2017. # (recorded before usage was linked, with archiving off, or of a
  2018. # deleted archive) is not matched by print name: that charged one
  2019. # print's spool cost to every archive of the same name.
  2020. if spoolman_owns and archive.cost is not None:
  2021. # Priced from the linked Spoolman spools at completion; there is
  2022. # nothing better to recompute it from here (#2591).
  2023. new_cost = None
  2024. preserved += 1
  2025. elif archive.filament_used_grams and archive.filament_type:
  2026. primary_type = archive.filament_type.split(",")[0].strip()
  2027. cost_per_kg = filaments.get(primary_type, default_cost_per_kg)
  2028. new_cost = price_one_print(
  2029. archive.filament_used_grams, None, 0.0, 0.0, 0, cost_per_kg, default_cost_per_kg
  2030. )
  2031. else:
  2032. new_cost = None
  2033. if new_cost is not None and archive.cost != new_cost:
  2034. archive.cost = new_cost
  2035. updated += 1
  2036. await db.commit()
  2037. message = f"Recalculated costs for {updated} archives"
  2038. if preserved:
  2039. message += f"; kept {preserved} priced from Spoolman"
  2040. return {"message": message, "updated": updated, "preserved": preserved}
  2041. @router.post("/rescan-all")
  2042. async def rescan_all_archives(
  2043. db: AsyncSession = Depends(get_db),
  2044. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  2045. printer_scope: PrinterScope = RequestPrinterScope,
  2046. ):
  2047. """Rescan all archives and update their metadata."""
  2048. from backend.app.services.archive import ThreeMFParser
  2049. # Only the archives of the caller's printers, as the archive list shows them.
  2050. query = select(PrintArchive)
  2051. if (clause := printer_scope.where(PrintArchive.printer_id)) is not None:
  2052. query = query.where(clause)
  2053. result = await db.execute(query)
  2054. archives = list(result.scalars().all())
  2055. updated = 0
  2056. errors = []
  2057. for archive in archives:
  2058. try:
  2059. file_path = settings.base_dir / archive.file_path
  2060. if not file_path.is_file():
  2061. errors.append({"id": archive.id, "error": "File not found"})
  2062. continue
  2063. parser = ThreeMFParser(file_path)
  2064. metadata = parser.parse()
  2065. if metadata.get("filament_type"):
  2066. archive.filament_type = metadata["filament_type"]
  2067. if metadata.get("filament_color"):
  2068. archive.filament_color = metadata["filament_color"]
  2069. if metadata.get("print_time_seconds"):
  2070. archive.print_time_seconds = metadata["print_time_seconds"]
  2071. if metadata.get("filament_used_grams"):
  2072. archive.filament_used_grams = metadata["filament_used_grams"]
  2073. if metadata.get("layer_height"):
  2074. archive.layer_height = metadata["layer_height"]
  2075. if metadata.get("nozzle_diameter"):
  2076. archive.nozzle_diameter = metadata["nozzle_diameter"]
  2077. if metadata.get("makerworld_url"):
  2078. archive.makerworld_url = metadata["makerworld_url"]
  2079. if metadata.get("designer"):
  2080. archive.designer = metadata["designer"]
  2081. updated += 1
  2082. except Exception as e:
  2083. logger.exception("Failed to rescan archive %s: %s", archive.id, e)
  2084. errors.append({"id": archive.id, "error": "Failed to parse 3MF file"})
  2085. await db.commit()
  2086. return {"updated": updated, "errors": errors}
  2087. @router.get("/{archive_id}/duplicates")
  2088. async def get_archive_duplicates(
  2089. archive_id: int,
  2090. db: AsyncSession = Depends(get_db),
  2091. auth_result: tuple[User | None, bool] = Depends(
  2092. require_ownership_permission(
  2093. Permission.ARCHIVES_READ_ALL,
  2094. Permission.ARCHIVES_READ_OWN,
  2095. )
  2096. ),
  2097. printer_scope: PrinterScope = RequestPrinterScope,
  2098. ):
  2099. """Get duplicates for a specific archive."""
  2100. user, can_read_all = auth_result
  2101. service = ArchiveService(db)
  2102. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2103. makerworld_id = archive.extra_data.get("makerworld_model_id") if archive.extra_data else None
  2104. duplicates = await service.find_duplicates(
  2105. archive_id=archive.id,
  2106. content_hash=archive.content_hash,
  2107. print_name=archive.print_name,
  2108. makerworld_model_id=makerworld_id,
  2109. visible=_visible_archive_clauses(user, can_read_all, printer_scope),
  2110. )
  2111. return {"duplicates": duplicates, "count": len(duplicates)}
  2112. @router.post("/backfill-hashes")
  2113. async def backfill_content_hashes(
  2114. db: AsyncSession = Depends(get_db),
  2115. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  2116. printer_scope: PrinterScope = RequestPrinterScope,
  2117. ):
  2118. """Compute and store content hashes for all archives missing them."""
  2119. result = await db.execute(
  2120. select(PrintArchive).where(
  2121. PrintArchive.content_hash.is_(None), *_visible_archive_clauses(None, True, printer_scope)
  2122. )
  2123. )
  2124. archives = list(result.scalars().all())
  2125. updated = 0
  2126. errors = []
  2127. for archive in archives:
  2128. try:
  2129. file_path = settings.base_dir / archive.file_path
  2130. if not file_path.is_file():
  2131. errors.append({"id": archive.id, "error": "File not found"})
  2132. continue
  2133. archive.content_hash = ArchiveService.compute_file_hash(file_path)
  2134. updated += 1
  2135. except Exception as e:
  2136. logger.exception("Failed to compute hash for archive %s: %s", archive.id, e)
  2137. errors.append({"id": archive.id, "error": "Failed to compute hash"})
  2138. await db.commit()
  2139. return {"updated": updated, "errors": errors}
  2140. @router.delete("/{archive_id}")
  2141. async def delete_archive(
  2142. archive_id: int,
  2143. purge_stats: bool = Query(
  2144. False,
  2145. description=(
  2146. "When false (default) the archive is soft-deleted — files removed "
  2147. "from disk, row hidden from listings, but its filament / energy / "
  2148. "time / cost contribution stays in Quick Stats. Set true to also "
  2149. "drop the row from statistics (#1343)."
  2150. ),
  2151. ),
  2152. db: AsyncSession = Depends(get_db),
  2153. auth_result: tuple[User | None, bool] = Depends(
  2154. require_ownership_permission(
  2155. Permission.ARCHIVES_DELETE_ALL,
  2156. Permission.ARCHIVES_DELETE_OWN,
  2157. )
  2158. ),
  2159. printer_scope: PrinterScope = RequestPrinterScope,
  2160. ):
  2161. """Delete an archive (soft by default; ``?purge_stats=true`` to hard-delete).
  2162. Both delete paths now cascade to related ``print_queue`` rows (#1734) —
  2163. hard delete via the ``ON DELETE CASCADE`` FK, soft delete via the
  2164. ``_delete_related_queue_items`` helper. A 409 guard blocks the delete
  2165. when any related queue item is currently mid-print so the dispatcher
  2166. doesn't lose its metadata trail under the running print.
  2167. """
  2168. user, can_modify_all = auth_result
  2169. # Get archive first to check ownership
  2170. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  2171. archive = result.scalar_one_or_none()
  2172. if not archive or not printer_scope.allows(archive.printer_id):
  2173. raise HTTPException(404, "Archive not found")
  2174. # Ownership check
  2175. if not can_modify_all:
  2176. if archive.created_by_id != user.id:
  2177. raise HTTPException(403, "You can only delete your own archives")
  2178. # #1734: block delete when any related queue item is currently printing.
  2179. # Both soft and hard delete are gated — an in-flight print needs its
  2180. # backing archive to stay around for the metadata trail (filament,
  2181. # plate, ams_mapping). The user can stop the print first, then retry.
  2182. from backend.app.services.archive import _count_related_queue_items
  2183. _related_total, related_printing = await _count_related_queue_items(db, archive_id)
  2184. if related_printing > 0:
  2185. raise HTTPException(
  2186. 409,
  2187. f"Cannot delete archive — {related_printing} related queue item(s) are "
  2188. f"currently printing. Stop the print first, then retry.",
  2189. )
  2190. service = ArchiveService(db)
  2191. if purge_stats:
  2192. # Hard-delete the linked PrintLogEntry rows first so their filament /
  2193. # cost / count contributions disappear from /archives/stats. The FK is
  2194. # ON DELETE SET NULL, so without this delete the runs would survive
  2195. # the archive row and keep showing up in totals (#1343 / #1378).
  2196. from sqlalchemy import delete as sa_delete
  2197. from backend.app.models.print_log import PrintLogEntry
  2198. await db.execute(sa_delete(PrintLogEntry).where(PrintLogEntry.archive_id == archive_id))
  2199. await db.commit()
  2200. if not await service.delete_archive(archive_id):
  2201. raise HTTPException(404, "Archive not found")
  2202. return {"status": "deleted", "purged_from_stats": True}
  2203. if not await service.soft_delete_archive(archive_id):
  2204. raise HTTPException(404, "Archive not found")
  2205. return {"status": "deleted", "purged_from_stats": False}
  2206. @router.get("/{archive_id}/download")
  2207. async def download_archive(
  2208. archive_id: int,
  2209. inline: bool = False,
  2210. db: AsyncSession = Depends(get_db),
  2211. auth_result: tuple[User | None, bool] = Depends(
  2212. require_ownership_permission(
  2213. Permission.ARCHIVES_READ_ALL,
  2214. Permission.ARCHIVES_READ_OWN,
  2215. )
  2216. ),
  2217. printer_scope: PrinterScope = RequestPrinterScope,
  2218. ):
  2219. """Download the 3MF file."""
  2220. user, can_read_all = auth_result
  2221. service = ArchiveService(db)
  2222. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2223. file_path = settings.base_dir / archive.file_path
  2224. if not file_path.is_file():
  2225. raise HTTPException(404, "File not found")
  2226. # Use inline disposition to let browser/OS handle file association
  2227. content_disposition = "inline" if inline else "attachment"
  2228. return FileResponse(
  2229. path=file_path,
  2230. filename=archive.filename,
  2231. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  2232. content_disposition_type=content_disposition,
  2233. )
  2234. @router.get("/{archive_id}/file/{filename}")
  2235. async def download_archive_with_filename(
  2236. archive_id: int,
  2237. filename: str,
  2238. db: AsyncSession = Depends(get_db),
  2239. auth_result: tuple[User | None, bool] = Depends(
  2240. require_ownership_permission(
  2241. Permission.ARCHIVES_READ_ALL,
  2242. Permission.ARCHIVES_READ_OWN,
  2243. )
  2244. ),
  2245. printer_scope: PrinterScope = RequestPrinterScope,
  2246. ):
  2247. """Download the 3MF file with filename in URL."""
  2248. user, can_read_all = auth_result
  2249. service = ArchiveService(db)
  2250. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2251. file_path = settings.base_dir / archive.file_path
  2252. if not file_path.is_file():
  2253. raise HTTPException(404, "File not found")
  2254. return FileResponse(
  2255. path=file_path,
  2256. filename=archive.filename,
  2257. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  2258. )
  2259. @router.post("/{archive_id}/slicer-token")
  2260. async def create_archive_slicer_token(
  2261. archive_id: int,
  2262. db: AsyncSession = Depends(get_db),
  2263. auth_result: tuple[User | None, bool] = Depends(
  2264. require_ownership_permission(
  2265. Permission.ARCHIVES_READ_ALL,
  2266. Permission.ARCHIVES_READ_OWN,
  2267. )
  2268. ),
  2269. printer_scope: PrinterScope = RequestPrinterScope,
  2270. ):
  2271. """Create a short-lived download token for opening files in slicer applications.
  2272. Slicer protocol handlers (bambustudioopen://, orcaslicer://) cannot send
  2273. auth headers, so they use this token in the URL path instead.
  2274. """
  2275. from backend.app.core.auth import create_slicer_download_token
  2276. user, can_read_all = auth_result
  2277. service = ArchiveService(db)
  2278. _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2279. token = await create_slicer_download_token("archive", archive_id)
  2280. return {"token": token}
  2281. @router.get("/{archive_id}/dl/{token}/{filename}")
  2282. async def download_archive_for_slicer(
  2283. archive_id: int,
  2284. token: str,
  2285. filename: str,
  2286. db: AsyncSession = Depends(get_db),
  2287. ):
  2288. """Download 3MF file using a slicer download token.
  2289. Token-authenticated (no auth headers needed). The token is short-lived and
  2290. archive-bound, created by POST /{archive_id}/slicer-token, and redeemable
  2291. for the rest of its TTL rather than exactly once -- the slicer is a separate
  2292. process that may fetch the URL more than once (#3029).
  2293. Filename is at the end of the URL so slicers can detect the file format.
  2294. """
  2295. from backend.app.core.auth import verify_slicer_download_token
  2296. if not await verify_slicer_download_token(token, "archive", archive_id, single_use=False):
  2297. raise HTTPException(403, "Invalid or expired download token")
  2298. service = ArchiveService(db)
  2299. archive = await service.get_archive(archive_id)
  2300. if not archive:
  2301. raise HTTPException(404, "Archive not found")
  2302. file_path = settings.base_dir / archive.file_path
  2303. if not file_path.is_file():
  2304. raise HTTPException(404, "File not found")
  2305. return FileResponse(
  2306. path=file_path,
  2307. filename=archive.filename,
  2308. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  2309. )
  2310. @router.get("/{archive_id}/thumbnail")
  2311. async def get_thumbnail(
  2312. archive_id: int,
  2313. db: AsyncSession = Depends(get_db),
  2314. auth_result: tuple[User | None, bool] = Depends(
  2315. require_media_token_ownership(
  2316. Permission.ARCHIVES_READ_ALL,
  2317. Permission.ARCHIVES_READ_OWN,
  2318. )
  2319. ),
  2320. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  2321. ):
  2322. """Get the thumbnail image.
  2323. Requires a media token query param (?token=xxx) when auth is enabled, and
  2324. returns 404 for an archive the caller may not read (#3025).
  2325. """
  2326. user, can_read_all = auth_result
  2327. service = ArchiveService(db)
  2328. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2329. if not archive.thumbnail_path:
  2330. raise HTTPException(404, "Thumbnail not found")
  2331. thumb_path = settings.base_dir / archive.thumbnail_path
  2332. if not thumb_path.exists():
  2333. raise HTTPException(404, "Thumbnail file not found")
  2334. # Use file modification time as ETag to bust cache
  2335. mtime = int(thumb_path.stat().st_mtime)
  2336. return FileResponse(
  2337. path=thumb_path,
  2338. media_type="image/png",
  2339. headers={
  2340. "Cache-Control": "no-cache, must-revalidate",
  2341. "ETag": f'"{mtime}"',
  2342. },
  2343. )
  2344. @router.get("/{archive_id}/printer-media")
  2345. async def get_archive_printer_media(
  2346. archive_id: int,
  2347. auth_result: tuple[User | None, bool] = Depends(
  2348. require_ownership_permission(
  2349. Permission.ARCHIVES_READ_ALL,
  2350. Permission.ARCHIVES_READ_OWN,
  2351. )
  2352. ),
  2353. can_list_printer_files: bool = Depends(probe_permissions_if_auth_enabled(Permission.PRINTERS_FILES)),
  2354. printer_scope: PrinterScope = RequestPrinterScope,
  2355. ):
  2356. """Find downloadable timelapse and `/ipcam` files for one print.
  2357. Local attached timelapses are returned without touching the printer.
  2358. Printer directories are listed only when the caller also has
  2359. ``printers:files``; otherwise the local result is returned with a warning.
  2360. Files are downloaded only after the user explicitly selects them in the UI.
  2361. """
  2362. user, can_read_all = auth_result
  2363. async with database.async_session() as db:
  2364. archive = _ensure_archive_visible(
  2365. await ArchiveService(db).get_archive(archive_id), user, can_read_all, printer_scope
  2366. )
  2367. printer = None
  2368. claimed_timelapse_stems: set[str] = set()
  2369. if archive.printer_id is not None:
  2370. printer = (await db.execute(select(Printer).where(Printer.id == archive.printer_id))).scalar_one_or_none()
  2371. if printer is not None and archive.timelapse_path is None:
  2372. claimed_timelapse_stems = await _claimed_timelapse_stems(db, archive.printer_id, archive_id)
  2373. local_timelapse = None
  2374. if archive.timelapse_path:
  2375. local_path = settings.base_dir / archive.timelapse_path
  2376. if await asyncio.to_thread(local_path.is_file):
  2377. local_timelapse = {
  2378. "name": local_path.name,
  2379. "size": (await asyncio.to_thread(local_path.stat)).st_size,
  2380. }
  2381. response = {
  2382. "archive_id": archive.id,
  2383. "printer_id": archive.printer_id,
  2384. "local_timelapse": local_timelapse,
  2385. "remote_files": [],
  2386. "warnings": [],
  2387. }
  2388. if archive.printer_id is None or archive.started_at is None:
  2389. return response
  2390. if not can_list_printer_files:
  2391. response["warnings"].append("printer_files_forbidden")
  2392. return response
  2393. if printer is None or not printer_scope.allows(printer.id):
  2394. response["warnings"].append("printer_missing")
  2395. return response
  2396. if ftps_handshake_blocked(printer.ip_address):
  2397. if local_timelapse is None:
  2398. response["warnings"].append("timelapse_unavailable")
  2399. response["warnings"].append("ipcam_unavailable")
  2400. return response
  2401. remote_files: list[dict] = []
  2402. # If no copy was attached to the archive, offer the matching printer-side
  2403. # timelapse without mutating the archive or deleting anything from the SD.
  2404. if local_timelapse is None:
  2405. videos: list[dict] = []
  2406. any_timelapse_directory_available = False
  2407. for timelapse_dir in ("/timelapse", "/timelapse/video", "/record", "/recording"):
  2408. if ftps_handshake_blocked(printer.ip_address):
  2409. break
  2410. listing = await list_files_result_async(
  2411. printer.ip_address,
  2412. printer.access_code,
  2413. timelapse_dir,
  2414. timeout=_PRINTER_MEDIA_LIST_TIMEOUT_SECONDS,
  2415. printer_model=printer.model,
  2416. )
  2417. any_timelapse_directory_available |= listing.available
  2418. candidates = [
  2419. file
  2420. for file in listing.files
  2421. if not file.get("is_directory") and str(file.get("name") or "").lower().endswith(VIDEO_SUFFIXES)
  2422. ]
  2423. if candidates:
  2424. videos = candidates
  2425. break
  2426. if not any_timelapse_directory_available:
  2427. response["warnings"].append("timelapse_unavailable")
  2428. if videos:
  2429. baseline = set(archive.timelapse_baseline or [])
  2430. eligible = [
  2431. file
  2432. for file in videos
  2433. if str(file.get("name") or "") not in baseline
  2434. and Path(str(file.get("name") or "")).stem not in claimed_timelapse_stems
  2435. ]
  2436. if archive.timelapse_baseline is not None:
  2437. candidate = eligible[0] if len(eligible) == 1 else None
  2438. else:
  2439. candidate, _ = _match_timelapse_by_timestamp(eligible, archive.started_at)
  2440. if candidate is not None:
  2441. remote_files.append(
  2442. {
  2443. "name": candidate.get("name"),
  2444. "path": candidate.get("path"),
  2445. "size": candidate.get("size") or 0,
  2446. "mtime": candidate.get("mtime"),
  2447. "kind": "timelapse",
  2448. }
  2449. )
  2450. if ftps_handshake_blocked(printer.ip_address):
  2451. response["warnings"].append("ipcam_unavailable")
  2452. response["remote_files"] = remote_files
  2453. return response
  2454. ipcam_listing = await list_files_result_async(
  2455. printer.ip_address,
  2456. printer.access_code,
  2457. "/ipcam",
  2458. timeout=_PRINTER_MEDIA_LIST_TIMEOUT_SECONDS,
  2459. printer_model=printer.model,
  2460. )
  2461. if ipcam_listing.available:
  2462. for file in match_ipcam_chunks(ipcam_listing.files, archive.started_at, archive.completed_at):
  2463. remote_files.append(
  2464. {
  2465. "name": file.get("name"),
  2466. "path": file.get("path") or f"/ipcam/{file.get('name')}",
  2467. "size": file.get("size") or 0,
  2468. "mtime": file.get("mtime"),
  2469. "kind": "ipcam",
  2470. }
  2471. )
  2472. else:
  2473. response["warnings"].append("ipcam_unavailable")
  2474. response["remote_files"] = remote_files
  2475. return response
  2476. @router.post("/{archive_id}/media-download-token")
  2477. async def create_archive_media_download_token(
  2478. archive_id: int,
  2479. auth_result: tuple[User | None, bool] = Depends(
  2480. require_ownership_permission(Permission.ARCHIVES_READ_ALL, Permission.ARCHIVES_READ_OWN)
  2481. ),
  2482. printer_scope: PrinterScope = RequestPrinterScope,
  2483. ):
  2484. """Mint a single-use token bound to an archive's attached timelapse."""
  2485. from backend.app.core.auth import create_slicer_download_token
  2486. user, can_read_all = auth_result
  2487. async with database.async_session() as db:
  2488. archive = _ensure_archive_visible(
  2489. await ArchiveService(db).get_archive(archive_id), user, can_read_all, printer_scope
  2490. )
  2491. if not archive.timelapse_path:
  2492. raise HTTPException(404, "Timelapse not found")
  2493. timelapse_path = settings.base_dir / archive.timelapse_path
  2494. if not await asyncio.to_thread(timelapse_path.is_file):
  2495. raise HTTPException(404, "Timelapse file not found")
  2496. return {
  2497. "token": await create_slicer_download_token("archive-timelapse", archive_id),
  2498. "filename": timelapse_path.name,
  2499. }
  2500. @router.get("/{archive_id}/media/dl/{token}/{filename}")
  2501. async def download_archive_media_with_token(
  2502. archive_id: int,
  2503. token: str,
  2504. filename: str,
  2505. ):
  2506. """Consume a resource-bound token and stream an attached timelapse."""
  2507. from backend.app.core.auth import verify_slicer_download_token
  2508. if not await verify_slicer_download_token(token, "archive-timelapse", archive_id):
  2509. return download_error_response(403, "This download link has already been used or has expired.")
  2510. async with database.async_session() as db:
  2511. archive = await ArchiveService(db).get_archive(archive_id)
  2512. if not archive or not archive.timelapse_path:
  2513. return download_error_response(404, "This print has no attached timelapse.")
  2514. timelapse_path = settings.base_dir / archive.timelapse_path
  2515. if not await asyncio.to_thread(timelapse_path.is_file):
  2516. return download_error_response(404, "The attached timelapse is no longer on disk.")
  2517. safe_filename = safe_download_filename(filename, fallback=timelapse_path.name)
  2518. return FileResponse(
  2519. path=timelapse_path,
  2520. filename=safe_filename,
  2521. headers={"Content-Disposition": build_content_disposition(safe_filename)},
  2522. )
  2523. @router.get("/{archive_id}/timelapse")
  2524. async def get_timelapse(
  2525. archive_id: int,
  2526. db: AsyncSession = Depends(get_db),
  2527. auth_result: tuple[User | None, bool] = Depends(
  2528. require_media_token_ownership(
  2529. Permission.ARCHIVES_READ_ALL,
  2530. Permission.ARCHIVES_READ_OWN,
  2531. )
  2532. ),
  2533. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  2534. ):
  2535. """Get the timelapse video.
  2536. Requires a media token query param (?token=xxx) when auth is enabled, and
  2537. returns 404 for an archive the caller may not read (#3025).
  2538. """
  2539. user, can_read_all = auth_result
  2540. service = ArchiveService(db)
  2541. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2542. if not archive.timelapse_path:
  2543. raise HTTPException(404, "Timelapse not found")
  2544. timelapse_path = settings.base_dir / archive.timelapse_path
  2545. if not timelapse_path.exists():
  2546. raise HTTPException(404, "Timelapse file not found")
  2547. # Use file modification time as ETag to bust cache after processing
  2548. mtime = int(timelapse_path.stat().st_mtime)
  2549. # Detect media type from file extension (AVI from P1S before background conversion)
  2550. suffix = timelapse_path.suffix.lower()
  2551. media_type = {".mp4": "video/mp4", ".avi": "video/x-msvideo", ".mkv": "video/x-matroska"}.get(suffix, "video/mp4")
  2552. ext = suffix if suffix in (".mp4", ".avi", ".mkv") else ".mp4"
  2553. return FileResponse(
  2554. path=timelapse_path,
  2555. media_type=media_type,
  2556. filename=f"{archive.print_name or 'timelapse'}{ext}",
  2557. headers={
  2558. "Cache-Control": "no-cache, must-revalidate",
  2559. "ETag": f'"{mtime}"',
  2560. },
  2561. )
  2562. @router.delete("/{archive_id}/timelapse")
  2563. async def delete_timelapse(
  2564. archive_id: int,
  2565. db: AsyncSession = Depends(get_db),
  2566. auth_result: tuple[User | None, bool] = Depends(
  2567. require_ownership_permission(
  2568. Permission.ARCHIVES_DELETE_ALL,
  2569. Permission.ARCHIVES_DELETE_OWN,
  2570. )
  2571. ),
  2572. printer_scope: PrinterScope = RequestPrinterScope,
  2573. ):
  2574. """Remove the timelapse video from an archive."""
  2575. user, can_modify_all = auth_result
  2576. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  2577. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  2578. if not archive.timelapse_path:
  2579. raise HTTPException(404, "No timelapse attached to this archive")
  2580. # Delete the file
  2581. timelapse_path = settings.base_dir / archive.timelapse_path
  2582. if timelapse_path.exists():
  2583. timelapse_path.unlink()
  2584. # Clear the path in database
  2585. archive.timelapse_path = None
  2586. archive.timelapse_plate_id = None
  2587. await db.commit()
  2588. return {"status": "deleted"}
  2589. @router.post("/{archive_id}/timelapse/scan")
  2590. async def scan_timelapse(
  2591. archive_id: int,
  2592. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  2593. printer_scope: PrinterScope = RequestPrinterScope,
  2594. ):
  2595. """Scan printer for timelapse matching this archive and attach it."""
  2596. from backend.app.core.database import async_session
  2597. from backend.app.models.printer import Printer
  2598. from backend.app.services.bambu_ftp import (
  2599. DownloadDeadlineExceeded,
  2600. delete_archived_timelapse,
  2601. download_file_bytes_async,
  2602. ftps_handshake_blocked,
  2603. get_ftp_retry_settings,
  2604. list_files_async,
  2605. remote_file_settled,
  2606. with_ftp_retry,
  2607. )
  2608. # Read the archive + printer in a short session and release the pooled DB
  2609. # connection BEFORE the FTP scan/download below — a timelapse pull walks
  2610. # several directories and fetches a 100MB+ video, so holding Depends(get_db)
  2611. # across it pinned one connection idle-in-transaction for minutes (#2572).
  2612. # Scalar columns stay readable on the detached rows (expire_on_commit=False);
  2613. # the attach at the end runs in its own fresh short session.
  2614. async with async_session() as db:
  2615. archive = await ArchiveService(db).get_archive(archive_id)
  2616. if not archive:
  2617. raise HTTPException(404, "Archive not found")
  2618. if archive.timelapse_path:
  2619. return {"status": "exists", "message": "Timelapse already attached"}
  2620. if not archive.printer_id:
  2621. raise HTTPException(400, "Archive has no associated printer")
  2622. printer_scope.ensure(archive.printer_id)
  2623. result = await db.execute(select(Printer).where(Printer.id == archive.printer_id))
  2624. printer = result.scalar_one_or_none()
  2625. if not printer:
  2626. raise HTTPException(404, "Printer not found")
  2627. # Get base name from archive filename (without .3mf extension)
  2628. base_name = Path(archive.filename).stem
  2629. # Scan timelapse directory on printer
  2630. # Different printer models use different paths
  2631. files = []
  2632. for timelapse_path in ["/timelapse", "/timelapse/video", "/record", "/recording"]:
  2633. if ftps_handshake_blocked(printer.ip_address):
  2634. break
  2635. try:
  2636. files = await list_files_async(
  2637. printer.ip_address, printer.access_code, timelapse_path, printer_model=printer.model
  2638. )
  2639. if files:
  2640. break
  2641. except Exception:
  2642. continue
  2643. if not files:
  2644. # "Couldn't reach the printer" and "the printer has no timelapse
  2645. # directory" are different problems with different fixes, and both used
  2646. # to come back as one 500 (#2780). Nothing here will work while the
  2647. # printer's file service is not answering over TLS, so say that rather
  2648. # than reporting an empty directory.
  2649. if ftps_handshake_blocked(printer.ip_address):
  2650. raise HTTPException(
  2651. 503,
  2652. f"Printer {printer.ip_address} is not answering its file service over TLS. "
  2653. "Bambuddy will try again shortly.",
  2654. )
  2655. raise HTTPException(404, "No timelapse directory found on the printer")
  2656. # Look for matching timelapse
  2657. matching_file = None
  2658. video_files = [
  2659. f for f in files if not f.get("is_directory") and f.get("name", "").lower().endswith((".mp4", ".avi"))
  2660. ]
  2661. # Strategy 0: snapshot diff against the baseline captured at print start
  2662. # (#2704). This is the same comparison the automatic scan makes, and the
  2663. # only one here that doesn't depend on the printer's clock — a printer in
  2664. # LAN-only mode can't reach Bambu's NTP server, so the timestamps in both
  2665. # the filename and the FTP mtime can be days out. One reporter's P1S was
  2666. # six and a half days off, which defeats every strategy below.
  2667. #
  2668. # When a baseline exists it is authoritative and the clock-based strategies
  2669. # are skipped entirely: they can only turn an honest "pick one yourself"
  2670. # into a confident wrong answer. Those strategies stay for archives created
  2671. # before the baseline was persisted.
  2672. used_baseline = archive.timelapse_baseline is not None
  2673. if used_baseline:
  2674. baseline = set(archive.timelapse_baseline)
  2675. async with async_session() as db:
  2676. claimed = await _claimed_timelapse_stems(db, archive.printer_id, archive_id)
  2677. candidates = [
  2678. f for f in video_files if f.get("name", "") not in baseline and Path(f.get("name", "")).stem not in claimed
  2679. ]
  2680. if len(candidates) == 1:
  2681. matching_file = candidates[0]
  2682. logger.info("Matched timelapse by print-start baseline: %s", matching_file.get("name"))
  2683. elif candidates:
  2684. # Ambiguous — offer only the plausible files instead of guessing.
  2685. video_files = candidates
  2686. logger.info("Baseline left %s unclaimed candidates for archive %s", len(candidates), archive_id)
  2687. else:
  2688. logger.info("Baseline shows no unclaimed new video on the printer for archive %s", archive_id)
  2689. # Strategy 1: Match by print name in filename
  2690. if not used_baseline:
  2691. for f in video_files:
  2692. fname = f.get("name", "")
  2693. if base_name.lower() in fname.lower():
  2694. matching_file = f
  2695. break
  2696. # Strategy 2: Match by timestamp proximity against print START time.
  2697. # Bambu timelapse filename embeds the print start time in printer-local clock.
  2698. # See _match_timelapse_by_timestamp for the offset-search rationale and why we
  2699. # intentionally don't try to match filename against end time here.
  2700. if not used_baseline and not matching_file and archive.started_at:
  2701. candidate, diff = _match_timelapse_by_timestamp(video_files, archive.started_at)
  2702. if candidate is not None:
  2703. matching_file = candidate
  2704. logger.info("Matched timelapse by timestamp: %s (diff: %s)", candidate.get("name"), diff)
  2705. # Strategy 3: Use file modification time from FTP listing
  2706. # This handles cases where printer's filename timestamp is wrong but file mtime is correct
  2707. if not used_baseline and not matching_file and (archive.started_at or archive.completed_at or archive.created_at):
  2708. from datetime import datetime, timedelta
  2709. _archive_start = archive.started_at
  2710. archive_end = archive.completed_at or archive.created_at
  2711. best_match = None
  2712. best_diff = timedelta(hours=24)
  2713. for f in video_files:
  2714. mtime = f.get("mtime")
  2715. if mtime:
  2716. # Timelapse file should be modified during or shortly after the print
  2717. # The mtime should be close to completion time (video finishes when print ends)
  2718. if archive_end:
  2719. diff = abs(mtime - archive_end)
  2720. if diff < best_diff:
  2721. best_diff = diff
  2722. best_match = f
  2723. logger.debug(
  2724. f"Timelapse mtime match candidate: {f.get('name')}, mtime: {mtime}, diff from end: {diff}"
  2725. )
  2726. if best_match and best_diff < timedelta(hours=2):
  2727. matching_file = best_match
  2728. logger.info("Matched timelapse by file mtime: %s (diff: %s)", best_match.get("name"), best_diff)
  2729. # Strategy 4: If only one timelapse exists and archive was recently completed, use it
  2730. # This handles cases where printer clock is wrong or timezone issues exist
  2731. if not used_baseline and not matching_file and len(video_files) == 1:
  2732. from datetime import datetime, timedelta, timezone
  2733. archive_completed = archive.completed_at or archive.created_at
  2734. if archive_completed:
  2735. if archive_completed.tzinfo is None:
  2736. archive_completed = archive_completed.replace(tzinfo=timezone.utc)
  2737. time_since_completion = datetime.now(timezone.utc) - archive_completed
  2738. # If archive was completed within the last hour, assume the single timelapse is for it
  2739. if time_since_completion < timedelta(hours=1):
  2740. matching_file = video_files[0]
  2741. logger.info("Using single timelapse file as fallback: %s", video_files[0].get("name"))
  2742. # Note: We intentionally don't use a "most recent file" fallback because
  2743. # we can't verify if timelapse was actually enabled for this print.
  2744. # Instead, return the list of available files for manual selection.
  2745. if not matching_file:
  2746. # Return available files for manual selection
  2747. available_files = [
  2748. {
  2749. "name": f.get("name"),
  2750. "path": f.get("path"),
  2751. "size": f.get("size"),
  2752. "mtime": f.get("mtime").isoformat() if f.get("mtime") else None,
  2753. }
  2754. for f in video_files
  2755. ]
  2756. # Sort by mtime descending (most recent first)
  2757. available_files.sort(key=lambda x: x.get("mtime") or "", reverse=True)
  2758. return {
  2759. "status": "not_found",
  2760. "message": "No matching timelapse found - please select manually",
  2761. "available_files": available_files,
  2762. }
  2763. # Download the timelapse - use the full path from the file listing
  2764. remote_path = matching_file.get("path") or f"/timelapse/{matching_file['name']}"
  2765. # Get FTP retry settings
  2766. ftp_retry_enabled, ftp_retry_count, ftp_retry_delay, ftp_timeout = await get_ftp_retry_settings()
  2767. try:
  2768. if ftp_retry_enabled:
  2769. timelapse_data = await with_ftp_retry(
  2770. download_file_bytes_async,
  2771. printer.ip_address,
  2772. printer.access_code,
  2773. remote_path,
  2774. socket_timeout=ftp_timeout,
  2775. printer_model=printer.model,
  2776. expected_size=matching_file.get("size"),
  2777. max_retries=ftp_retry_count,
  2778. retry_delay=ftp_retry_delay,
  2779. operation_name=f"Download timelapse {matching_file['name']}",
  2780. cooloff_ip=printer.ip_address,
  2781. )
  2782. else:
  2783. timelapse_data = await 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. )
  2791. except DownloadDeadlineExceeded as exc:
  2792. # Not retried: the deadline already fitted the file's size.
  2793. raise HTTPException(504, "The timelapse download took longer than its size allows") from exc
  2794. if not timelapse_data:
  2795. raise HTTPException(502, "Could not download the timelapse from the printer")
  2796. # Confirm the printer has finished writing before we commit to this file and
  2797. # delete the original: matching the listing's size proves we got what it
  2798. # said, not that the file was complete (#2704).
  2799. if not await remote_file_settled(
  2800. printer.ip_address,
  2801. printer.access_code,
  2802. remote_path,
  2803. len(timelapse_data),
  2804. printer_model=printer.model,
  2805. ):
  2806. raise HTTPException(409, "The printer is still writing this video — try again in a moment")
  2807. # Attach in a fresh short session (the read session was released before FTP).
  2808. async with async_session() as db:
  2809. success = await ArchiveService(db).attach_timelapse(archive_id, timelapse_data, matching_file["name"])
  2810. if not success:
  2811. raise HTTPException(500, "Failed to attach timelapse")
  2812. # Safe now, and only now: the transfer matched the size the listing reported
  2813. # and the bytes are committed to the archive (#2704).
  2814. await delete_archived_timelapse(
  2815. printer.ip_address,
  2816. printer.access_code,
  2817. remote_path,
  2818. verified=matching_file.get("size") is not None,
  2819. printer_model=printer.model,
  2820. printer_name=printer.name,
  2821. )
  2822. return {
  2823. "status": "attached",
  2824. "message": f"Timelapse '{matching_file['name']}' attached successfully",
  2825. "filename": matching_file["name"],
  2826. }
  2827. @router.post("/{archive_id}/timelapse/select")
  2828. async def select_timelapse(
  2829. archive_id: int,
  2830. filename: str = Query(..., description="Timelapse filename to attach"),
  2831. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  2832. printer_scope: PrinterScope = RequestPrinterScope,
  2833. ):
  2834. """Manually select a timelapse from the printer to attach."""
  2835. from backend.app.core.database import async_session
  2836. from backend.app.models.printer import Printer
  2837. from backend.app.services.bambu_ftp import (
  2838. DownloadDeadlineExceeded,
  2839. delete_archived_timelapse,
  2840. download_file_bytes_async,
  2841. get_ftp_retry_settings,
  2842. list_files_async,
  2843. remote_file_settled,
  2844. with_ftp_retry,
  2845. )
  2846. # Read the archive + printer in a short session and release the pooled DB
  2847. # connection BEFORE the FTP scan/download below (#2572); scalars stay
  2848. # readable after close (expire_on_commit=False), the attach reopens one.
  2849. async with async_session() as db:
  2850. archive = await ArchiveService(db).get_archive(archive_id)
  2851. if not archive:
  2852. raise HTTPException(404, "Archive not found")
  2853. if not archive.printer_id:
  2854. raise HTTPException(400, "Archive has no associated printer")
  2855. printer_scope.ensure(archive.printer_id)
  2856. result = await db.execute(select(Printer).where(Printer.id == archive.printer_id))
  2857. printer = result.scalar_one_or_none()
  2858. if not printer:
  2859. raise HTTPException(404, "Printer not found")
  2860. # Find the file on the printer
  2861. files = []
  2862. remote_path = None
  2863. expected_size = None
  2864. for timelapse_dir in ["/timelapse", "/timelapse/video", "/record", "/recording"]:
  2865. try:
  2866. files = await list_files_async(
  2867. printer.ip_address, printer.access_code, timelapse_dir, printer_model=printer.model
  2868. )
  2869. for f in files:
  2870. if f.get("name") == filename:
  2871. remote_path = f.get("path") or f"{timelapse_dir}/{filename}"
  2872. expected_size = f.get("size")
  2873. break
  2874. if remote_path:
  2875. break
  2876. except Exception:
  2877. continue
  2878. if not remote_path:
  2879. raise HTTPException(404, f"Timelapse '{filename}' not found on printer")
  2880. # Download and attach
  2881. ftp_retry_enabled, ftp_retry_count, ftp_retry_delay, ftp_timeout = await get_ftp_retry_settings()
  2882. try:
  2883. if ftp_retry_enabled:
  2884. timelapse_data = await with_ftp_retry(
  2885. download_file_bytes_async,
  2886. printer.ip_address,
  2887. printer.access_code,
  2888. remote_path,
  2889. socket_timeout=ftp_timeout,
  2890. printer_model=printer.model,
  2891. expected_size=expected_size,
  2892. max_retries=ftp_retry_count,
  2893. retry_delay=ftp_retry_delay,
  2894. operation_name=f"Download timelapse {filename}",
  2895. cooloff_ip=printer.ip_address,
  2896. )
  2897. else:
  2898. timelapse_data = await 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. )
  2906. except DownloadDeadlineExceeded as exc:
  2907. # Not retried: the deadline already fitted the file's size.
  2908. raise HTTPException(504, "The timelapse download took longer than its size allows") from exc
  2909. if not timelapse_data:
  2910. raise HTTPException(502, "Could not download the timelapse from the printer")
  2911. # Confirm the printer has finished writing before we commit to this file and
  2912. # delete the original: matching the listing's size proves we got what it
  2913. # said, not that the file was complete (#2704).
  2914. if not await remote_file_settled(
  2915. printer.ip_address,
  2916. printer.access_code,
  2917. remote_path,
  2918. len(timelapse_data),
  2919. printer_model=printer.model,
  2920. ):
  2921. raise HTTPException(409, "The printer is still writing this video — try again in a moment")
  2922. # Attach in a fresh short session (the read session was released before FTP).
  2923. async with async_session() as db:
  2924. success = await ArchiveService(db).attach_timelapse(archive_id, timelapse_data, filename)
  2925. if not success:
  2926. raise HTTPException(500, "Failed to attach timelapse")
  2927. # Safe now, and only now: the transfer matched the size the listing reported
  2928. # and the bytes are committed to the archive (#2704).
  2929. await delete_archived_timelapse(
  2930. printer.ip_address,
  2931. printer.access_code,
  2932. remote_path,
  2933. verified=expected_size is not None,
  2934. printer_model=printer.model,
  2935. printer_name=printer.name,
  2936. )
  2937. return {
  2938. "status": "attached",
  2939. "message": f"Timelapse '{filename}' attached successfully",
  2940. "filename": filename,
  2941. }
  2942. @router.post("/{archive_id}/timelapse/upload")
  2943. async def upload_timelapse(
  2944. archive_id: int,
  2945. file: UploadFile = File(...),
  2946. db: AsyncSession = Depends(get_db),
  2947. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  2948. printer_scope: PrinterScope = RequestPrinterScope,
  2949. ):
  2950. """Manually upload a timelapse video to an archive."""
  2951. service = ArchiveService(db)
  2952. archive = await service.get_archive(archive_id)
  2953. if not archive or not printer_scope.allows(archive.printer_id):
  2954. raise HTTPException(404, "Archive not found")
  2955. if not file.filename or not file.filename.endswith((".mp4", ".avi", ".mkv")):
  2956. raise HTTPException(400, "File must be a video file (.mp4, .avi, .mkv)")
  2957. content = await file.read()
  2958. safe_filename = _safe_filename(file.filename)
  2959. success = await service.attach_timelapse(archive_id, content, safe_filename)
  2960. if not success:
  2961. raise HTTPException(500, "Failed to attach timelapse")
  2962. return {"status": "attached", "filename": safe_filename}
  2963. @router.get("/{archive_id}/timelapse/info")
  2964. async def get_timelapse_info(
  2965. archive_id: int,
  2966. db: AsyncSession = Depends(get_db),
  2967. auth_result: tuple[User | None, bool] = Depends(
  2968. require_ownership_permission(
  2969. Permission.ARCHIVES_READ_ALL,
  2970. Permission.ARCHIVES_READ_OWN,
  2971. )
  2972. ),
  2973. printer_scope: PrinterScope = RequestPrinterScope,
  2974. ):
  2975. """Get timelapse video metadata for editor."""
  2976. from backend.app.schemas.timelapse import TimelapseInfoResponse
  2977. from backend.app.services.timelapse_processor import TimelapseProcessor
  2978. user, can_read_all = auth_result
  2979. service = ArchiveService(db)
  2980. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2981. if not archive.timelapse_path:
  2982. raise HTTPException(404, "Timelapse not found")
  2983. timelapse_path = settings.base_dir / archive.timelapse_path
  2984. if not timelapse_path.exists():
  2985. raise HTTPException(404, "Timelapse file not found")
  2986. try:
  2987. processor = TimelapseProcessor(timelapse_path)
  2988. info = await processor.get_info()
  2989. return TimelapseInfoResponse(**info)
  2990. except Exception as e:
  2991. logger.error("Failed to get timelapse info: %s", e)
  2992. raise HTTPException(500, f"Failed to get video info: {str(e)}")
  2993. @router.get("/{archive_id}/timelapse/thumbnails")
  2994. async def get_timelapse_thumbnails(
  2995. archive_id: int,
  2996. count: int = Query(10, ge=1, le=30),
  2997. width: int = Query(160, ge=80, le=320),
  2998. db: AsyncSession = Depends(get_db),
  2999. auth_result: tuple[User | None, bool] = Depends(
  3000. require_ownership_permission(
  3001. Permission.ARCHIVES_READ_ALL,
  3002. Permission.ARCHIVES_READ_OWN,
  3003. )
  3004. ),
  3005. printer_scope: PrinterScope = RequestPrinterScope,
  3006. ):
  3007. """Generate timeline thumbnail frames for visual scrubbing."""
  3008. import base64
  3009. from backend.app.schemas.timelapse import ThumbnailResponse
  3010. from backend.app.services.timelapse_processor import TimelapseProcessor
  3011. user, can_read_all = auth_result
  3012. service = ArchiveService(db)
  3013. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  3014. if not archive.timelapse_path:
  3015. raise HTTPException(404, "Timelapse not found")
  3016. timelapse_path = settings.base_dir / archive.timelapse_path
  3017. if not timelapse_path.exists():
  3018. raise HTTPException(404, "Timelapse file not found")
  3019. try:
  3020. processor = TimelapseProcessor(timelapse_path)
  3021. thumbnails = await processor.generate_thumbnails(count, width)
  3022. return ThumbnailResponse(
  3023. thumbnails=[base64.b64encode(data).decode() for _, data in thumbnails],
  3024. timestamps=[ts for ts, _ in thumbnails],
  3025. )
  3026. except Exception as e:
  3027. logger.error("Failed to generate thumbnails: %s", e)
  3028. raise HTTPException(500, f"Failed to generate thumbnails: {str(e)}")
  3029. @router.post("/{archive_id}/timelapse/process")
  3030. async def process_timelapse(
  3031. archive_id: int,
  3032. trim_start: float = Form(0),
  3033. trim_end: float = Form(None),
  3034. speed: float = Form(1.0),
  3035. save_mode: str = Form("new"),
  3036. output_filename: str = Form(None),
  3037. audio: UploadFile = File(None),
  3038. db: AsyncSession = Depends(get_db),
  3039. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  3040. printer_scope: PrinterScope = RequestPrinterScope,
  3041. ):
  3042. """Process timelapse with trim, speed, and optional audio overlay."""
  3043. import shutil
  3044. import tempfile
  3045. from backend.app.schemas.timelapse import ProcessResponse
  3046. from backend.app.services.timelapse_processor import TimelapseProcessor
  3047. # Validate speed
  3048. if not 0.25 <= speed <= 4.0:
  3049. raise HTTPException(400, "Speed must be between 0.25 and 4.0")
  3050. if save_mode not in ("replace", "new"):
  3051. raise HTTPException(400, "save_mode must be 'replace' or 'new'")
  3052. service = ArchiveService(db)
  3053. archive = await service.get_archive(archive_id)
  3054. if not archive or not archive.timelapse_path or not printer_scope.allows(archive.printer_id):
  3055. raise HTTPException(404, "Timelapse not found")
  3056. timelapse_path = settings.base_dir / archive.timelapse_path
  3057. if not timelapse_path.exists():
  3058. raise HTTPException(404, "Timelapse file not found")
  3059. archive_dir = timelapse_path.parent
  3060. # Handle audio file
  3061. audio_temp_path = None
  3062. if audio and audio.filename:
  3063. # Validate audio file extension
  3064. if not audio.filename.lower().endswith((".mp3", ".wav", ".m4a", ".aac", ".ogg")):
  3065. raise HTTPException(400, "Audio must be .mp3, .wav, .m4a, .aac, or .ogg")
  3066. audio_content = await audio.read()
  3067. # Extract and validate suffix to prevent path injection
  3068. suffix = Path(audio.filename).suffix.lower()
  3069. if suffix not in (".mp3", ".wav", ".m4a", ".aac", ".ogg"):
  3070. raise HTTPException(400, "Invalid audio file extension")
  3071. audio_temp_path = Path(tempfile.gettempdir()) / f"audio_{archive_id}{suffix}"
  3072. audio_temp_path.write_bytes(audio_content)
  3073. try:
  3074. processor = TimelapseProcessor(timelapse_path)
  3075. # Determine output path
  3076. if save_mode == "replace":
  3077. # Process to temp file first, then replace
  3078. temp_output = Path(tempfile.gettempdir()) / f"processed_{archive_id}.mp4"
  3079. output_path = temp_output
  3080. else:
  3081. # Save as new file alongside original
  3082. filename = output_filename or f"{archive.print_name or 'timelapse'}_edited.mp4"
  3083. # Sanitize filename - remove path separators and traversal sequences
  3084. filename = "".join(c for c in filename if c.isalnum() or c in "._- ")
  3085. # Prevent path traversal
  3086. if ".." in filename or not filename or filename.startswith("."):
  3087. filename = f"timelapse_{archive_id}_edited"
  3088. if not filename.endswith(".mp4"):
  3089. filename += ".mp4"
  3090. output_path = archive_dir / filename # SEC-PATH-OK: filename alnum-filtered + .. rejected above
  3091. success = await processor.process(
  3092. output_path=output_path,
  3093. trim_start=trim_start,
  3094. trim_end=trim_end,
  3095. speed=speed,
  3096. audio_path=audio_temp_path,
  3097. )
  3098. if not success:
  3099. raise HTTPException(500, "Video processing failed")
  3100. # Handle save mode
  3101. if save_mode == "replace":
  3102. # Replace original file
  3103. shutil.move(str(output_path), str(timelapse_path))
  3104. final_path = archive.timelapse_path
  3105. message = "Timelapse replaced successfully"
  3106. else:
  3107. final_path = str(output_path.relative_to(settings.base_dir))
  3108. message = f"Saved as {output_path.name}"
  3109. return ProcessResponse(
  3110. status="completed",
  3111. output_path=final_path,
  3112. message=message,
  3113. )
  3114. except HTTPException:
  3115. raise
  3116. except Exception as e:
  3117. logger.error("Timelapse processing failed: %s", e)
  3118. raise HTTPException(500, f"Processing failed: {str(e)}")
  3119. finally:
  3120. # Cleanup temp audio file
  3121. if audio_temp_path and audio_temp_path.exists():
  3122. audio_temp_path.unlink()
  3123. # ============================================
  3124. # Photo Endpoints
  3125. # ============================================
  3126. @router.post("/{archive_id}/photos")
  3127. async def upload_photo(
  3128. archive_id: int,
  3129. file: UploadFile = File(...),
  3130. db: AsyncSession = Depends(get_db),
  3131. auth_result: tuple[User | None, bool] = Depends(
  3132. require_ownership_permission(
  3133. Permission.ARCHIVES_UPDATE_ALL,
  3134. Permission.ARCHIVES_UPDATE_OWN,
  3135. )
  3136. ),
  3137. printer_scope: PrinterScope = RequestPrinterScope,
  3138. ):
  3139. """Upload a photo of the printed result."""
  3140. user, can_modify_all = auth_result
  3141. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  3142. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  3143. if not file.filename or not file.filename.lower().endswith((".jpg", ".jpeg", ".png", ".webp")):
  3144. raise HTTPException(400, "File must be an image (.jpg, .jpeg, .png, .webp)")
  3145. # Get archive directory. parents=True because an archive with no 3MF owns
  3146. # <archive_dir>/<id>/, which nothing else has necessarily created yet.
  3147. photos_dir = archive_photos_dir(archive)
  3148. photos_dir.mkdir(parents=True, exist_ok=True)
  3149. # Generate unique filename
  3150. import uuid
  3151. ext = Path(file.filename).suffix.lower()
  3152. photo_filename = f"{uuid.uuid4().hex[:8]}{ext}"
  3153. photo_path = photos_dir / photo_filename # SEC-PATH-OK: photo_filename = uuid.uuid4().hex[:8] + ext
  3154. # Save file
  3155. content = await file.read()
  3156. photo_path.write_bytes(content)
  3157. # Update archive photos list (create new list to trigger SQLAlchemy change detection)
  3158. photos = list(archive.photos or [])
  3159. photos.append(photo_filename)
  3160. archive.photos = photos
  3161. await db.commit()
  3162. await db.refresh(archive)
  3163. return {"status": "uploaded", "filename": photo_filename, "photos": archive.photos}
  3164. @router.get("/{archive_id}/photos/{filename}")
  3165. async def get_photo(
  3166. archive_id: int,
  3167. filename: str,
  3168. db: AsyncSession = Depends(get_db),
  3169. auth_result: tuple[User | None, bool] = Depends(
  3170. require_media_token_ownership(
  3171. Permission.ARCHIVES_READ_ALL,
  3172. Permission.ARCHIVES_READ_OWN,
  3173. )
  3174. ),
  3175. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  3176. ):
  3177. """Get a specific photo.
  3178. Requires a media token query param (?token=xxx) when auth is enabled, and
  3179. returns 404 for an archive the caller may not read (#3025).
  3180. """
  3181. user, can_read_all = auth_result
  3182. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  3183. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_read_all, printer_scope)
  3184. # Membership check first — UUID-generated names on upload mean any URL
  3185. # filename that doesn't appear here is by definition not a real photo.
  3186. # Mirrors the delete handler below; previously this endpoint had no
  3187. # membership check at all and joined `filename` straight to disk.
  3188. if not archive.photos or filename not in archive.photos:
  3189. raise HTTPException(404, "Photo not found")
  3190. # Defence-in-depth: even though the membership check above already
  3191. # constrains `filename` to UUID-generated names from upload,
  3192. # find_archive_photo resolves and containment-checks each candidate,
  3193. # guarding against future code paths that might populate
  3194. # `archive.photos` from a less-trusted source.
  3195. photo_path = find_archive_photo(archive, filename)
  3196. if photo_path is None:
  3197. raise HTTPException(404, "Photo not found")
  3198. # Determine media type
  3199. ext = Path(filename).suffix.lower()
  3200. media_types = {
  3201. ".jpg": "image/jpeg",
  3202. ".jpeg": "image/jpeg",
  3203. ".png": "image/png",
  3204. ".webp": "image/webp",
  3205. }
  3206. media_type = media_types.get(ext, "image/jpeg")
  3207. return FileResponse(path=photo_path, media_type=media_type)
  3208. @router.delete("/{archive_id}/photos/{filename}")
  3209. async def delete_photo(
  3210. archive_id: int,
  3211. filename: str,
  3212. db: AsyncSession = Depends(get_db),
  3213. auth_result: tuple[User | None, bool] = Depends(
  3214. require_ownership_permission(
  3215. Permission.ARCHIVES_DELETE_ALL,
  3216. Permission.ARCHIVES_DELETE_OWN,
  3217. )
  3218. ),
  3219. printer_scope: PrinterScope = RequestPrinterScope,
  3220. ):
  3221. """Delete a photo."""
  3222. user, can_modify_all = auth_result
  3223. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  3224. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  3225. if not archive.photos or filename not in archive.photos:
  3226. raise HTTPException(404, "Photo not found")
  3227. # Delete file — same lookup as get_photo above, so a photo that is
  3228. # readable is also deletable. Removing the name while leaving the file is
  3229. # how a no-3MF archive accumulated photos nobody could see or remove.
  3230. photo_path = find_archive_photo(archive, filename)
  3231. if photo_path is not None:
  3232. photo_path.unlink()
  3233. # Update archive photos list
  3234. photos = [p for p in archive.photos if p != filename]
  3235. archive.photos = photos if photos else None
  3236. await db.commit()
  3237. return {"status": "deleted", "photos": archive.photos}
  3238. # ============================================
  3239. # Post-print outcome confirmation (#1898)
  3240. # ============================================
  3241. # English-only on purpose: this page is rendered by the backend for a phone
  3242. # browser that carries no session and therefore no language preference.
  3243. _VERDICT_LABELS = {"good": "Good part", "reject": "Rejected"}
  3244. _VERDICT_SOURCE_PHRASES = {
  3245. "dialog": "in the app",
  3246. "link": "with a one-tap link",
  3247. "plate_clear": "automatically when the print plate was cleared",
  3248. "printer_card": "from the printer card",
  3249. "api": "through the API",
  3250. "reaction": "with a reaction in chat",
  3251. }
  3252. def _confirm_page(glyph: str, heading: str, body: str) -> str:
  3253. """The small HTML page every one-tap outcome link renders."""
  3254. return (
  3255. "<!doctype html><html><head><meta name='viewport' content='width=device-width, initial-scale=1'>"
  3256. "<title>Bambuddy</title></head>"
  3257. "<body style='font-family: system-ui, sans-serif; background:#1a1d21; color:#fff; display:flex;"
  3258. " align-items:center; justify-content:center; min-height:90vh; margin:0'>"
  3259. f"<div style='text-align:center; padding:0 1.5rem; max-width:32rem'>"
  3260. f"<div style='font-size:3rem'>{glyph}</div><h2>{heading}</h2>{body}</div></body></html>"
  3261. )
  3262. async def _render_already_answered_page(db: AsyncSession, archive: PrintArchive) -> str:
  3263. """Explain a spent one-tap link instead of calling it invalid.
  3264. The plate-clear default, the app and the other link all answer the same
  3265. prompt, so the button in a push notification is routinely tapped after the
  3266. question is settled. Report the verdict on file, when and how it landed,
  3267. and where to change it.
  3268. """
  3269. from backend.app.api.routes.settings import get_external_base_url
  3270. name = html_escape(archive.print_name or archive.filename or "")
  3271. # The verdict's own timestamp, not the moment the token was spent: a
  3272. # verdict changed later in the app would otherwise be dated by the older
  3273. # event. Pre-#1898 rows have neither, and then the page omits the date.
  3274. used_at = archive.user_verdict_at or archive.confirm_token_used_at
  3275. if used_at is not None and used_at.tzinfo is not None:
  3276. used_at = used_at.astimezone(timezone.utc)
  3277. when = used_at.strftime("%Y-%m-%d %H:%M UTC") if used_at else None
  3278. how = _VERDICT_SOURCE_PHRASES.get(archive.user_verdict_source or "")
  3279. label = _VERDICT_LABELS.get(archive.user_verdict or "")
  3280. if label:
  3281. recorded = f"Recorded as <strong>{label}</strong>"
  3282. if how:
  3283. recorded += f" {how}"
  3284. if when:
  3285. recorded += f" on {when}"
  3286. recorded += "."
  3287. else:
  3288. # The verdict was cleared again in the app; the link stays spent.
  3289. recorded = "This prompt was already answered and the verdict has since been cleared."
  3290. base = await get_external_base_url(db)
  3291. link = html_escape(f"{base}/archives?confirm={archive.id}", quote=True)
  3292. glyph = "&#10003;" if archive.user_verdict == "good" else "&#10007;" if archive.user_verdict else "&#8505;"
  3293. return _confirm_page(
  3294. glyph,
  3295. "Already answered",
  3296. f"<p style='color:#9ca3af'>{name}</p>"
  3297. f"<p style='color:#9ca3af'>{recorded}</p>"
  3298. f"<p><a style='color:#00ae42' href='{link}'>Open this print in Bambuddy</a> to change it.</p>",
  3299. )
  3300. def _render_confirm_prompt_page(request: Request, archive: PrintArchive, verdict: str) -> str:
  3301. """The page a one-tap verdict link opens. It has recorded nothing yet.
  3302. GET is where link unfurlers, mail-security scanners and browser prefetchers
  3303. arrive, uninvited and within seconds of the message being sent, so GET
  3304. writes nothing at all -- the verdict is recorded by the form below, over
  3305. POST, which none of them issue.
  3306. The form submits itself only for a page opened from a notification button,
  3307. which is what keeps the operator at one tap. The marker for that
  3308. (``?tap=1``) is put on the Telegram inline keyboard's URLs and nowhere else
  3309. -- never on a URL that travels in message text -- so a mail-security
  3310. sandbox that renders HTML and runs JavaScript cannot press the button for
  3311. the operator: it only ever sees the unmarked URL out of the body. The
  3312. User-Agent heuristic still runs on top of that, but it is no longer the
  3313. only thing between a scanner and the write; it cannot be, because such a
  3314. sandbox sends an ordinary Chrome string.
  3315. Every other arrival -- an unmarked link somebody typed or mailed, a browser
  3316. with JavaScript off -- gets the same page and presses the button.
  3317. """
  3318. name = html_escape(archive.print_name or archive.filename or "")
  3319. # Escaped although the route only lets good/reject through: the page must
  3320. # not depend on a check made in another function.
  3321. label = html_escape(_VERDICT_LABELS.get(verdict, verdict))
  3322. # No action attribute: the form posts back to the URL the page was loaded
  3323. # from, so it works behind a reverse proxy and on a host external_url does
  3324. # not name.
  3325. form = (
  3326. "<form method='post' id='confirm-form'>"
  3327. "<button type='submit' style='font: inherit; font-size:1.1rem; padding:0.9rem 2rem;"
  3328. " border:0; border-radius:0.5rem; background:#00ae42; color:#fff'>Yes, record it</button>"
  3329. "</form>"
  3330. )
  3331. # The SPA's CSP allows inline scripts only with the per-request nonce the
  3332. # security-headers middleware mints (main.py). Without one -- no middleware,
  3333. # an unmarked URL, or an unattended-looking caller -- the page simply waits
  3334. # for the button.
  3335. nonce = getattr(request.state, "csp_nonce", None)
  3336. script = ""
  3337. if nonce and is_one_tap_request(request.query_params) and not is_unattended_fetch(request.method, request.headers):
  3338. script = (
  3339. f"<script nonce='{html_escape(nonce, quote=True)}'>"
  3340. "document.getElementById('confirm-form').submit();</script>"
  3341. )
  3342. return _confirm_page(
  3343. "&#63;",
  3344. "Confirm this outcome",
  3345. f"<p style='color:#9ca3af'>{name}</p>"
  3346. f"<p style='color:#9ca3af'>Record this print as <strong>{label}</strong>?</p>"
  3347. f"{form}{script}",
  3348. )
  3349. def _confirm_response(html: str):
  3350. """The one-tap pages, never cached.
  3351. A proxy holding on to "Saved" or to the prompt would answer a later tap
  3352. from its cache, and the prompt page is a capability URL either way.
  3353. """
  3354. from fastapi.responses import HTMLResponse
  3355. return HTMLResponse(html, headers={"Cache-Control": "no-store"})
  3356. async def _load_confirmable_archive(db: AsyncSession, token: str, verdict: str) -> PrintArchive:
  3357. """Resolve a one-tap capability token, or raise the route's 400/404."""
  3358. if verdict not in ("good", "reject"):
  3359. raise HTTPException(400, "Verdict must be 'good' or 'reject'")
  3360. result = await db.execute(
  3361. select(PrintArchive).where(PrintArchive.confirm_token == token, PrintArchive.confirm_token.isnot(None))
  3362. )
  3363. archive = result.scalar_one_or_none()
  3364. if not archive:
  3365. raise HTTPException(404, "Confirmation link is invalid or was already used")
  3366. return archive
  3367. @router.get("/confirm/{token}/{verdict}")
  3368. async def confirm_outcome_page(
  3369. request: Request,
  3370. token: str,
  3371. verdict: str,
  3372. db: AsyncSession = Depends(get_db),
  3373. ):
  3374. """Open a one-tap verdict link. Reads only — the verdict is recorded by POST.
  3375. This used to be the route that recorded, and that was the defect: a GET
  3376. that changes state is answered by everything that walks a URL. Telegram
  3377. and Slack fetch the links in a message body to build a preview card, mail
  3378. gateways detonate them before delivery, browsers prefetch them — any one
  3379. of those spent the single-use token and settled the outcome before the
  3380. operator had read the question, always in the "good" direction because
  3381. good_url came first. Suppressing previews per channel does not cover the
  3382. proxies and scanners in between; only removing the write from GET does.
  3383. So GET now hands back the prompt page and nothing else. The page's form
  3384. POSTs to this same URL, and :func:`confirm_outcome_by_token` records it.
  3385. The human cost is zero for the path the feature is built around: a URL
  3386. opened from a notification button carries ``?tap=1`` and the page submits
  3387. itself, so that tap is still the only tap. Nothing else gets that script,
  3388. including a scanner that runs JavaScript -- the marker is on the buttons,
  3389. not in the message text a scanner reads.
  3390. """
  3391. archive = await _load_confirmable_archive(db, token, verdict)
  3392. if archive.confirm_token_used_at is not None:
  3393. # Answered already — by hand, by the other link, by the plate-clear
  3394. # default or by a reaction. Report what is on file and change nothing.
  3395. return _confirm_response(await _render_already_answered_page(db, archive))
  3396. return _confirm_response(_render_confirm_prompt_page(request, archive, verdict))
  3397. @router.post("/confirm/{token}/{verdict}")
  3398. async def confirm_outcome_by_token(
  3399. token: str,
  3400. verdict: str,
  3401. db: AsyncSession = Depends(get_db),
  3402. ):
  3403. """Record a print-outcome verdict via the capability token from a push notification.
  3404. Deliberately unauthenticated: the token IS the credential. It is a 256-bit
  3405. per-archive capability, minted when the confirmation prompt fires, and it
  3406. only ever grants writing good/reject on that one archive, exactly once.
  3407. "Exactly once" is enforced by ``confirm_token_used_at`` rather than by
  3408. dropping the token value, so a link for a print that was already answered
  3409. can be recognised and explained instead of looking broken.
  3410. POST is what keeps the capability the operator's: unfurlers, scanners and
  3411. prefetchers issue GET, and the GET route above writes nothing. The two
  3412. callers that reach here are the prompt page's form and the ntfy action
  3413. button, which performs its own request from the phone and is configured
  3414. with ``method=POST``. Returns a small HTML page for the phone browser.
  3415. """
  3416. archive = await _load_confirmable_archive(db, token, verdict)
  3417. if archive.confirm_token_used_at is not None:
  3418. return _confirm_response(await _render_already_answered_page(db, archive))
  3419. archive.user_verdict = verdict
  3420. stamp_verdict(archive, "link")
  3421. retire_confirm_token(archive)
  3422. # Same mirror as the PATCH route (#1444): verdict-aware statistics read
  3423. # print_log_entries, so the latest run must carry the verdict too.
  3424. from backend.app.models.print_log import PrintLogEntry
  3425. latest_entry = await db.scalar(
  3426. select(PrintLogEntry).where(PrintLogEntry.archive_id == archive.id).order_by(PrintLogEntry.id.desc()).limit(1)
  3427. )
  3428. if latest_entry is not None:
  3429. latest_entry.user_verdict = verdict
  3430. await db.commit()
  3431. label = "Good part" if verdict == "good" else "Rejected"
  3432. name = archive.print_name or archive.filename
  3433. return _confirm_response(
  3434. _confirm_page(
  3435. "&#10003;" if verdict == "good" else "&#10007;",
  3436. label,
  3437. f"<p style='color:#9ca3af'>{html_escape(name)}</p>"
  3438. "<p style='color:#9ca3af'>Saved &mdash; you can close this page.</p>",
  3439. )
  3440. )
  3441. # ============================================
  3442. # QR Code Endpoint
  3443. # ============================================
  3444. @router.get("/{archive_id}/qrcode")
  3445. async def get_qrcode(
  3446. archive_id: int,
  3447. request: Request,
  3448. size: int = 200,
  3449. db: AsyncSession = Depends(get_db),
  3450. auth_result: tuple[User | None, bool] = Depends(
  3451. require_media_token_ownership(
  3452. Permission.ARCHIVES_READ_ALL,
  3453. Permission.ARCHIVES_READ_OWN,
  3454. )
  3455. ),
  3456. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  3457. ):
  3458. """Generate a QR code that links to this archive.
  3459. Requires a media token query param (?token=xxx) when auth is enabled, and
  3460. returns 404 for an archive the caller may not read (#3025).
  3461. """
  3462. user, can_read_all = auth_result
  3463. try:
  3464. import qrcode
  3465. from PIL import Image as PILImage
  3466. except ImportError:
  3467. raise HTTPException(500, "QR code generation not available - qrcode package not installed")
  3468. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  3469. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_read_all, printer_scope)
  3470. # Build URL to archive download
  3471. base_url = str(request.base_url).rstrip("/")
  3472. archive_url = f"{base_url}/api/v1/archives/{archive_id}/download"
  3473. # Generate QR code
  3474. qr = qrcode.QRCode(
  3475. version=1,
  3476. error_correction=qrcode.constants.ERROR_CORRECT_M,
  3477. box_size=10,
  3478. border=2,
  3479. )
  3480. qr.add_data(archive_url)
  3481. qr.make(fit=True)
  3482. img = qr.make_image(fill_color="black", back_color="white")
  3483. # Convert to PIL Image for resizing
  3484. pil_img = img.get_image()
  3485. # Resize if needed
  3486. if size != 200:
  3487. pil_img = pil_img.resize((size, size), PILImage.Resampling.LANCZOS)
  3488. # Convert to bytes
  3489. buffer = io.BytesIO()
  3490. pil_img.save(buffer, format="PNG")
  3491. buffer.seek(0)
  3492. qr_filename = f"qr_{archive.print_name or archive_id}.png"
  3493. return Response(
  3494. content=buffer.getvalue(),
  3495. media_type="image/png",
  3496. headers={"Content-Disposition": build_content_disposition(qr_filename, disposition="inline")},
  3497. )
  3498. @router.get("/{archive_id}/capabilities")
  3499. async def get_archive_capabilities(
  3500. archive_id: int,
  3501. db: AsyncSession = Depends(get_db),
  3502. auth_result: tuple[User | None, bool] = Depends(
  3503. require_ownership_permission(
  3504. Permission.ARCHIVES_READ_ALL,
  3505. Permission.ARCHIVES_READ_OWN,
  3506. )
  3507. ),
  3508. printer_scope: PrinterScope = RequestPrinterScope,
  3509. ):
  3510. """Check what viewing capabilities are available for this 3MF file."""
  3511. import defusedxml.ElementTree as ET
  3512. user, can_read_all = auth_result
  3513. service = ArchiveService(db)
  3514. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  3515. file_path = settings.base_dir / archive.file_path
  3516. if not file_path.is_file():
  3517. raise HTTPException(404, "File not found")
  3518. has_model = False
  3519. has_gcode = False
  3520. has_source = False
  3521. build_volume = {"x": 256, "y": 256, "z": 256} # Default to X1/P1 size
  3522. filament_colors: list[str] = []
  3523. # Check if source 3MF exists - this is where actual mesh data typically lives
  3524. source_path = None
  3525. if archive.source_3mf_path:
  3526. source_path = settings.base_dir / archive.source_3mf_path
  3527. if source_path.exists():
  3528. has_source = True
  3529. # Helper function to check for mesh data and extract colors from a 3MF file
  3530. def extract_3mf_info(zf_path: Path) -> tuple[bool, list[str], dict]:
  3531. """Extract mesh presence, colors, and build volume from a 3MF file."""
  3532. found_mesh = False
  3533. colors: list[str] = []
  3534. volume = {"x": 256, "y": 256, "z": 256}
  3535. try:
  3536. with zipfile.ZipFile(zf_path, "r") as zf:
  3537. names = zf.namelist()
  3538. # Check for 3D model - look for actual mesh data
  3539. for name in names:
  3540. if name.endswith(".model"):
  3541. try:
  3542. content = zf.read(name).decode("utf-8")
  3543. if "<vertex" in content or "<mesh" in content:
  3544. found_mesh = True
  3545. break
  3546. except Exception:
  3547. pass # Skip unreadable .model entries in archive
  3548. # Extract filament colors from project_settings.config
  3549. if "Metadata/project_settings.config" in names:
  3550. try:
  3551. config_content = zf.read("Metadata/project_settings.config").decode("utf-8")
  3552. config_data = json.loads(config_content)
  3553. # Parse printable_area: ['0x0', '256x0', '256x256', '0x256']
  3554. printable_area = config_data.get("printable_area", [])
  3555. if printable_area and len(printable_area) >= 3:
  3556. max_x = 0
  3557. max_y = 0
  3558. for coord in printable_area:
  3559. if "x" in coord:
  3560. parts = coord.split("x")
  3561. if len(parts) == 2:
  3562. try:
  3563. x, y = int(parts[0]), int(parts[1])
  3564. max_x = max(max_x, x)
  3565. max_y = max(max_y, y)
  3566. except ValueError:
  3567. pass # Skip non-numeric printable_area coordinate
  3568. if max_x > 0 and max_y > 0:
  3569. volume["x"] = max_x
  3570. volume["y"] = max_y
  3571. # Parse printable_height
  3572. printable_height = config_data.get("printable_height")
  3573. if printable_height:
  3574. try:
  3575. volume["z"] = int(printable_height)
  3576. except (ValueError, TypeError):
  3577. pass # Skip unparseable printable_height value
  3578. # Extract filament colors
  3579. raw_colors = config_data.get("filament_colour", [])
  3580. if raw_colors:
  3581. for color in raw_colors:
  3582. if color and isinstance(color, str):
  3583. colors.append(color)
  3584. except Exception:
  3585. pass # Skip malformed project_settings.config
  3586. except zipfile.BadZipFile:
  3587. pass # File is not a valid zip/3MF archive
  3588. return found_mesh, colors, volume
  3589. # First check source 3MF for mesh data and colors (preferred for 3D model viewing)
  3590. if has_source and source_path:
  3591. source_has_mesh, source_colors, source_volume = extract_3mf_info(source_path)
  3592. if source_has_mesh:
  3593. has_model = True
  3594. if source_colors:
  3595. filament_colors = source_colors
  3596. if source_volume["x"] != 256 or source_volume["y"] != 256 or source_volume["z"] != 256:
  3597. build_volume = source_volume
  3598. try:
  3599. with zipfile.ZipFile(file_path, "r") as zf:
  3600. names = zf.namelist()
  3601. # Check for G-code in the sliced file. Shared with the library's
  3602. # file-type classification so the card's badge and what the File
  3603. # Manager makes of the same file cannot disagree (#2993).
  3604. has_gcode = names_carry_gcode(names)
  3605. # Check for 3D model in sliced file (fallback if no source)
  3606. if not has_model:
  3607. for name in names:
  3608. if name.endswith(".model"):
  3609. try:
  3610. content = zf.read(name).decode("utf-8")
  3611. if "<vertex" in content or "<mesh" in content:
  3612. has_model = True
  3613. break
  3614. except Exception:
  3615. pass # Skip unreadable .model entries in archive
  3616. # Extract filament colors from slice_info.config (for gcode preview)
  3617. # These are the actual filaments used in the print, indexed by tool/extruder
  3618. slice_colors: list[str] = []
  3619. if "Metadata/slice_info.config" in names:
  3620. try:
  3621. slice_content = zf.read("Metadata/slice_info.config").decode("utf-8")
  3622. root = ET.fromstring(slice_content)
  3623. filaments = root.findall(".//filament")
  3624. filament_map: dict[int, str] = {}
  3625. for f in filaments:
  3626. fid = f.get("id")
  3627. fcolor = f.get("color")
  3628. used_g = f.get("used_g", "0")
  3629. try:
  3630. used_amount = float(used_g)
  3631. except (ValueError, TypeError):
  3632. used_amount = 0
  3633. if fid is not None and fcolor:
  3634. try:
  3635. tool_id = int(fid) - 1
  3636. if tool_id >= 0 and used_amount > 0:
  3637. filament_map[tool_id] = fcolor
  3638. except ValueError:
  3639. pass # Skip filament entry with non-numeric ID
  3640. if filament_map:
  3641. max_tool = max(filament_map.keys())
  3642. for i in range(max_tool + 1):
  3643. slice_colors.append(filament_map.get(i, "#00AE42"))
  3644. except Exception:
  3645. pass # Skip malformed slice_info.config XML
  3646. # Use slice_info colors if we don't have colors from source yet
  3647. if not filament_colors and slice_colors:
  3648. filament_colors = slice_colors
  3649. # Extract build volume from sliced file if not already set from source
  3650. if build_volume["x"] == 256 and build_volume["y"] == 256:
  3651. if "Metadata/project_settings.config" in names:
  3652. try:
  3653. config_content = zf.read("Metadata/project_settings.config").decode("utf-8")
  3654. config_data = json.loads(config_content)
  3655. printable_area = config_data.get("printable_area", [])
  3656. if printable_area and len(printable_area) >= 3:
  3657. max_x = 0
  3658. max_y = 0
  3659. for coord in printable_area:
  3660. if "x" in coord:
  3661. parts = coord.split("x")
  3662. if len(parts) == 2:
  3663. try:
  3664. x, y = int(parts[0]), int(parts[1])
  3665. max_x = max(max_x, x)
  3666. max_y = max(max_y, y)
  3667. except ValueError:
  3668. pass # Skip non-numeric printable_area coordinate
  3669. if max_x > 0 and max_y > 0:
  3670. build_volume["x"] = max_x
  3671. build_volume["y"] = max_y
  3672. printable_height = config_data.get("printable_height")
  3673. if printable_height:
  3674. try:
  3675. build_volume["z"] = int(printable_height)
  3676. except (ValueError, TypeError):
  3677. pass # Skip unparseable printable_height value
  3678. # Fallback colors from project_settings if still empty
  3679. if not filament_colors:
  3680. raw_colors = config_data.get("filament_colour", [])
  3681. if raw_colors:
  3682. for color in raw_colors:
  3683. if color and isinstance(color, str):
  3684. filament_colors.append(color)
  3685. except Exception:
  3686. pass # Skip malformed project_settings.config
  3687. except zipfile.BadZipFile:
  3688. raise HTTPException(400, "Invalid 3MF file")
  3689. return {
  3690. "has_model": has_model,
  3691. "has_gcode": has_gcode,
  3692. "has_source": has_source,
  3693. "build_volume": build_volume,
  3694. "filament_colors": filament_colors,
  3695. }
  3696. @router.get("/{archive_id}/gcode")
  3697. async def get_gcode(
  3698. archive_id: int,
  3699. plate: int | None = None,
  3700. db: AsyncSession = Depends(get_db),
  3701. auth_result: tuple[User | None, bool] = Depends(
  3702. require_ownership_permission(
  3703. Permission.ARCHIVES_READ_ALL,
  3704. Permission.ARCHIVES_READ_OWN,
  3705. )
  3706. ),
  3707. printer_scope: PrinterScope = RequestPrinterScope,
  3708. ):
  3709. """Extract and return G-code from the 3MF file.
  3710. When *plate* is provided, returns the G-code for that specific plate
  3711. (e.g. ``?plate=2`` returns ``Metadata/plate_2.gcode``). If omitted, falls
  3712. back to the archive's lowest-numbered plate — not the first member in the
  3713. zip, which is whatever order the slicer wrote and routinely puts plate 2
  3714. ahead of plate 1.
  3715. """
  3716. user, can_read_all = auth_result
  3717. service = ArchiveService(db)
  3718. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  3719. file_path = settings.base_dir / archive.file_path
  3720. if not file_path.is_file():
  3721. raise HTTPException(404, "File not found")
  3722. if plate is not None and plate < 1:
  3723. raise HTTPException(400, "Plate index must be >= 1")
  3724. try:
  3725. with zipfile.ZipFile(file_path, "r") as zf:
  3726. # Bambu 3MF files store G-code in Metadata/plate_X.gcode
  3727. gcode_files = [n for n in zf.namelist() if n.startswith("Metadata/") and n.endswith(".gcode")]
  3728. if not gcode_files:
  3729. raise HTTPException(
  3730. 404,
  3731. "No G-code found. This file hasn't been sliced yet - G-code is only available after slicing in Bambu Studio.",
  3732. )
  3733. if plate is not None:
  3734. selected = select_plate_gcode_name(gcode_files, plate)
  3735. if selected is None:
  3736. raise HTTPException(404, f"Plate {plate} not found in this archive")
  3737. else:
  3738. selected = default_plate_gcode_name(gcode_files)
  3739. gcode_content = zf.read(selected).decode("utf-8")
  3740. return Response(content=gcode_content, media_type="text/plain")
  3741. except zipfile.BadZipFile:
  3742. raise HTTPException(400, "Invalid 3MF file")
  3743. except HTTPException:
  3744. raise
  3745. except Exception as e:
  3746. raise HTTPException(500, f"Error extracting G-code: {str(e)}")
  3747. @router.get("/{archive_id}/plate-preview")
  3748. async def get_plate_preview(
  3749. archive_id: int,
  3750. db: AsyncSession = Depends(get_db),
  3751. auth_result: tuple[User | None, bool] = Depends(
  3752. require_media_token_ownership(
  3753. Permission.ARCHIVES_READ_ALL,
  3754. Permission.ARCHIVES_READ_OWN,
  3755. )
  3756. ),
  3757. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  3758. ):
  3759. """Get the plate preview image from the 3MF file.
  3760. Returns the slicer-generated plate thumbnail which shows the model
  3761. with correct colors and positioning.
  3762. Requires a media token query param (?token=xxx) when auth is enabled, and
  3763. returns 404 for an archive the caller may not read (#3025).
  3764. """
  3765. user, can_read_all = auth_result
  3766. service = ArchiveService(db)
  3767. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  3768. file_path = settings.base_dir / archive.file_path
  3769. if not file_path.is_file():
  3770. raise HTTPException(404, "File not found")
  3771. try:
  3772. with zipfile.ZipFile(file_path, "r") as zf:
  3773. names = zf.namelist()
  3774. # Try to find plate preview images in order of preference
  3775. # First look for the specific plate being printed (check slice_info for plate index)
  3776. plate_num = 1
  3777. if "Metadata/slice_info.config" in names:
  3778. try:
  3779. import defusedxml.ElementTree as ET
  3780. slice_content = zf.read("Metadata/slice_info.config").decode("utf-8")
  3781. root = ET.fromstring(slice_content)
  3782. plate_elem = root.find(".//plate/metadata[@key='index']")
  3783. if plate_elem is not None:
  3784. plate_num = int(plate_elem.get("value", "1"))
  3785. except Exception:
  3786. pass # Default plate_num=1 if slice_info is missing or malformed
  3787. # Try plate-specific image first, then fall back to plate_1
  3788. preview_paths = [
  3789. f"Metadata/plate_{plate_num}.png",
  3790. "Metadata/plate_1.png",
  3791. "Metadata/thumbnail.png",
  3792. ]
  3793. for preview_path in preview_paths:
  3794. if preview_path in names:
  3795. image_data = zf.read(preview_path)
  3796. return Response(content=image_data, media_type="image/png")
  3797. # If no plate image, try any PNG in Metadata
  3798. for name in names:
  3799. if name.startswith("Metadata/plate_") and name.endswith(".png") and "_small" not in name:
  3800. image_data = zf.read(name)
  3801. return Response(content=image_data, media_type="image/png")
  3802. raise HTTPException(404, "No plate preview found in 3MF file")
  3803. except zipfile.BadZipFile:
  3804. raise HTTPException(400, "Invalid 3MF file")
  3805. except HTTPException:
  3806. raise
  3807. except Exception as e:
  3808. raise HTTPException(500, f"Error extracting plate preview: {str(e)}")
  3809. @router.post("/upload")
  3810. async def upload_archive(
  3811. file: UploadFile = File(...),
  3812. printer_id: int | None = None,
  3813. prefer_filename_for_name: bool = Query(
  3814. False,
  3815. description=(
  3816. "Name the archive after the uploaded filename instead of the print_name "
  3817. "embedded in the 3MF's metadata. Off by default, which keeps the embedded "
  3818. "name. Turn it on when the filename you send is the meaningful one — an "
  3819. "integration naming files after its own jobs, or a file whose embedded "
  3820. "title is a stale name from whoever originally sliced it."
  3821. ),
  3822. ),
  3823. db: AsyncSession = Depends(get_db),
  3824. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_CREATE),
  3825. printer_scope: PrinterScope = RequestPrinterScope,
  3826. ):
  3827. """Manually upload a 3MF file to archive.
  3828. prefer_filename_for_name is the same flag the FTP review flow and
  3829. virtual-printer dispatch already pass to ArchiveService.archive_print —
  3830. this endpoint just didn't expose it (#1152 follow-up). Those callers derive
  3831. it from the VP-scoped `virtual_printer_archive_name_source` setting; here it
  3832. is per-request, because the caller is an API client that knows whether the
  3833. filename it sent is the meaningful one (#2609).
  3834. """
  3835. printer_scope.ensure(printer_id)
  3836. if not file.filename or not file.filename.endswith(".3mf"):
  3837. raise HTTPException(400, "File must be a .3mf file")
  3838. # Save uploaded file temporarily — strip directory components to prevent path traversal
  3839. safe_filename = _safe_filename(file.filename)
  3840. temp_path = (
  3841. settings.archive_dir / "temp" / safe_filename
  3842. ) # SEC-PATH-OK: safe_filename = _safe_filename(...) basename-stripped above
  3843. temp_path.parent.mkdir(parents=True, exist_ok=True)
  3844. try:
  3845. content = await file.read()
  3846. # #1401: same content validation as library upload — catches
  3847. # raw-gcode-renamed-to-.3mf and other unprintable shapes before
  3848. # archiving them and offering them up for print.
  3849. from backend.app.api.routes.library import validate_print_file_upload
  3850. validate_print_file_upload(file.filename, content)
  3851. temp_path.write_bytes(content)
  3852. service = ArchiveService(db)
  3853. archive = await service.archive_print(
  3854. printer_id=printer_id,
  3855. source_file=temp_path,
  3856. created_by_id=current_user.id if current_user else None,
  3857. prefer_filename_for_name=prefer_filename_for_name,
  3858. )
  3859. if not archive:
  3860. raise HTTPException(400, "Failed to archive file")
  3861. return ArchiveResponse.model_validate(archive)
  3862. finally:
  3863. if temp_path.exists():
  3864. temp_path.unlink()
  3865. @router.post("/upload-bulk")
  3866. async def upload_archives_bulk(
  3867. files: list[UploadFile] = File(...),
  3868. printer_id: int | None = None,
  3869. prefer_filename_for_name: bool = Query(
  3870. False,
  3871. description=(
  3872. "Name each archive after its uploaded filename instead of the print_name "
  3873. "embedded in the 3MF's metadata. Applies to every file in the batch. Off "
  3874. "by default, which keeps the embedded name."
  3875. ),
  3876. ),
  3877. db: AsyncSession = Depends(get_db),
  3878. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_CREATE),
  3879. printer_scope: PrinterScope = RequestPrinterScope,
  3880. ):
  3881. """Bulk upload multiple 3MF files to archive.
  3882. prefer_filename_for_name applies to every file in the batch. See
  3883. upload_archive for the flag's lineage.
  3884. """
  3885. printer_scope.ensure(printer_id)
  3886. from backend.app.api.routes.library import validate_print_file_upload
  3887. results = []
  3888. errors = []
  3889. for file in files:
  3890. if not file.filename or not file.filename.endswith(".3mf"):
  3891. errors.append({"filename": file.filename or "unknown", "error": "Not a .3mf file"})
  3892. continue
  3893. safe_filename = _safe_filename(file.filename)
  3894. temp_path = (
  3895. settings.archive_dir / "temp" / safe_filename
  3896. ) # SEC-PATH-OK: safe_filename = _safe_filename(...) basename-stripped above
  3897. temp_path.parent.mkdir(parents=True, exist_ok=True)
  3898. try:
  3899. content = await file.read()
  3900. # #1401: bulk-upload variant of the library validation. Collect
  3901. # the rejection per-file rather than aborting the whole batch
  3902. # so one bad file in a 10-file drag-drop doesn't lose the
  3903. # other nine.
  3904. try:
  3905. validate_print_file_upload(file.filename, content)
  3906. except HTTPException as exc:
  3907. errors.append({"filename": file.filename, "error": exc.detail})
  3908. continue
  3909. temp_path.write_bytes(content)
  3910. service = ArchiveService(db)
  3911. archive = await service.archive_print(
  3912. printer_id=printer_id,
  3913. source_file=temp_path,
  3914. created_by_id=current_user.id if current_user else None,
  3915. prefer_filename_for_name=prefer_filename_for_name,
  3916. )
  3917. if archive:
  3918. results.append(
  3919. {
  3920. "filename": file.filename,
  3921. "id": archive.id,
  3922. "status": "success",
  3923. }
  3924. )
  3925. else:
  3926. errors.append({"filename": file.filename, "error": "Failed to process"})
  3927. except Exception as e:
  3928. logger.exception("Failed to upload archive %s: %s", file.filename, e)
  3929. errors.append({"filename": file.filename, "error": "Failed to process file"})
  3930. finally:
  3931. if temp_path.exists():
  3932. temp_path.unlink()
  3933. return {
  3934. "uploaded": len(results),
  3935. "failed": len(errors),
  3936. "results": results,
  3937. "errors": errors,
  3938. }
  3939. @router.get("/{archive_id}/plates")
  3940. async def get_archive_plates(
  3941. archive_id: int,
  3942. db: AsyncSession = Depends(get_db),
  3943. auth_result: tuple[User | None, bool] = Depends(
  3944. require_ownership_permission(
  3945. Permission.ARCHIVES_READ_ALL,
  3946. Permission.ARCHIVES_READ_OWN,
  3947. )
  3948. ),
  3949. printer_scope: PrinterScope = RequestPrinterScope,
  3950. ):
  3951. """Get available plates from a multi-plate 3MF archive.
  3952. Returns a list of plates with their index, name, thumbnail availability,
  3953. and filament requirements. For single-plate exports, returns a single plate.
  3954. """
  3955. import re
  3956. import defusedxml.ElementTree as ET
  3957. user, can_read_all = auth_result
  3958. service = ArchiveService(db)
  3959. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  3960. file_path = settings.base_dir / archive.file_path
  3961. if not file_path.is_file():
  3962. raise HTTPException(404, "Archive file not found")
  3963. plates = []
  3964. # Initialize so the `has_gcode = bool(gcode_files)` after the try/except
  3965. # never raises NameError when the archive isn't a valid zip (e.g. plain
  3966. # .gcode file from a sliced-archive flow that didn't request 3MF output).
  3967. gcode_files: list[str] = []
  3968. # Printer / process preset names the 3MF was prepared with — used by the
  3969. # SliceModal to default its dropdowns (#1325).
  3970. embedded_presets: dict[str, str | None] = {"printer": None, "process": None}
  3971. # Process settings the designer changed away from the stock preset (#2622),
  3972. # offered in the SliceModal for a cross-printer re-slice. Same payload the
  3973. # library plates endpoint returns — SliceModal reads one shape for both.
  3974. design_overrides: list[dict] = []
  3975. try:
  3976. with zipfile.ZipFile(file_path, "r") as zf:
  3977. namelist = zf.namelist()
  3978. embedded_presets = extract_embedded_presets_from_3mf(zf)
  3979. if _PROJECT_SETTINGS_PATH in namelist:
  3980. try:
  3981. design_overrides = [
  3982. o._asdict()
  3983. for o in overrides_from_config(json.loads(zf.read(_PROJECT_SETTINGS_PATH).decode("utf-8")))
  3984. ]
  3985. except (ValueError, OSError, KeyError):
  3986. design_overrides = []
  3987. # Find all plate gcode files to determine available plates
  3988. gcode_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".gcode")]
  3989. # If no gcode is present (source-only or unsliced), fall back to plate JSON/PNG
  3990. plate_indices: list[int] = []
  3991. if gcode_files:
  3992. # Extract plate indices from gcode filenames
  3993. for gf in gcode_files:
  3994. # "Metadata/plate_5.gcode" -> 5
  3995. try:
  3996. # Remove "Metadata/plate_" and ".gcode"
  3997. plate_str = gf[15:-6]
  3998. plate_indices.append(int(plate_str))
  3999. except ValueError:
  4000. pass # Skip gcode file with non-numeric plate index
  4001. else:
  4002. plate_json_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".json")]
  4003. plate_png_files = [
  4004. n
  4005. for n in namelist
  4006. if n.startswith("Metadata/plate_")
  4007. and n.endswith(".png")
  4008. and "_small" not in n
  4009. and "no_light" not in n
  4010. ]
  4011. plate_name_candidates = plate_json_files + plate_png_files
  4012. plate_re = re.compile(r"^Metadata/plate_(\d+)\.(json|png)$")
  4013. seen_indices: set[int] = set()
  4014. for name in plate_name_candidates:
  4015. match = plate_re.match(name)
  4016. if match:
  4017. try:
  4018. index = int(match.group(1))
  4019. except ValueError:
  4020. continue
  4021. if index in seen_indices:
  4022. continue
  4023. seen_indices.add(index)
  4024. plate_indices.append(index)
  4025. if not plate_indices:
  4026. # No plate metadata found
  4027. return {
  4028. "archive_id": archive_id,
  4029. "filename": archive.filename,
  4030. "plates": [],
  4031. "is_multi_plate": False,
  4032. }
  4033. plate_indices.sort()
  4034. # Parse model_settings.config for plate names + object assignments
  4035. # Plate names are stored with plater_id and plater_name keys
  4036. plate_names = {} # plater_id -> name
  4037. plate_object_ids: dict[int, list[str]] = {}
  4038. object_names_by_id: dict[str, str] = {}
  4039. if "Metadata/model_settings.config" in namelist:
  4040. try:
  4041. model_content = zf.read("Metadata/model_settings.config").decode()
  4042. model_root = ET.fromstring(model_content)
  4043. # Build object ID -> name map
  4044. for obj_elem in model_root.findall(".//object"):
  4045. obj_id = obj_elem.get("id")
  4046. if not obj_id:
  4047. continue
  4048. name_meta = obj_elem.find("metadata[@key='name']")
  4049. obj_name = name_meta.get("value") if name_meta is not None else None
  4050. if obj_name:
  4051. object_names_by_id[obj_id] = obj_name
  4052. for plate_elem in model_root.findall(".//plate"):
  4053. plater_id = None
  4054. plater_name = None
  4055. for meta in plate_elem.findall("metadata"):
  4056. key = meta.get("key")
  4057. value = meta.get("value")
  4058. if key == "plater_id" and value:
  4059. try:
  4060. plater_id = int(value)
  4061. except ValueError:
  4062. pass # Skip plate with non-numeric plater_id
  4063. elif key == "plater_name" and value:
  4064. plater_name = value.strip()
  4065. if plater_id is not None and plater_name:
  4066. plate_names[plater_id] = plater_name
  4067. if plater_id is not None:
  4068. for instance_elem in plate_elem.findall("model_instance"):
  4069. for inst_meta in instance_elem.findall("metadata"):
  4070. if inst_meta.get("key") == "object_id":
  4071. obj_id = inst_meta.get("value")
  4072. if not obj_id:
  4073. continue
  4074. plate_object_ids.setdefault(plater_id, [])
  4075. if obj_id not in plate_object_ids[plater_id]:
  4076. plate_object_ids[plater_id].append(obj_id)
  4077. except Exception:
  4078. pass # model_settings.config parsing is optional
  4079. # Parse slice_info.config for plate metadata
  4080. plate_metadata = {} # plate_index -> {filaments, prediction, weight, name, objects}
  4081. if "Metadata/slice_info.config" in namelist:
  4082. content = zf.read("Metadata/slice_info.config").decode()
  4083. root = ET.fromstring(content)
  4084. for plate_elem in root.findall(".//plate"):
  4085. plate_info = {
  4086. "filaments": [],
  4087. "prediction": None,
  4088. "weight": None,
  4089. "name": None,
  4090. "objects": [],
  4091. "bed_type": None,
  4092. }
  4093. # Get plate index from metadata
  4094. plate_index = None
  4095. for meta in plate_elem.findall("metadata"):
  4096. key = meta.get("key")
  4097. value = meta.get("value")
  4098. if key == "index" and value:
  4099. try:
  4100. plate_index = int(value)
  4101. except ValueError:
  4102. pass # Skip plate with non-numeric index
  4103. elif key == "prediction" and value:
  4104. try:
  4105. plate_info["prediction"] = int(value)
  4106. except ValueError:
  4107. pass # Skip non-numeric print time prediction
  4108. elif key == "weight" and value:
  4109. try:
  4110. plate_info["weight"] = float(value)
  4111. except ValueError:
  4112. pass # Skip non-numeric filament weight
  4113. elif key == "curr_bed_type" and value:
  4114. # Per-plate bed type so the PrintModal can show the
  4115. # right plate alongside each option (#1281).
  4116. plate_info["bed_type"] = value.strip()
  4117. # Get filaments used in this plate
  4118. for filament_elem in plate_elem.findall("filament"):
  4119. filament_id = filament_elem.get("id")
  4120. filament_type = filament_elem.get("type", "")
  4121. filament_color = filament_elem.get("color", "")
  4122. used_g = filament_elem.get("used_g", "0")
  4123. used_m = filament_elem.get("used_m", "0")
  4124. try:
  4125. used_grams = float(used_g)
  4126. except (ValueError, TypeError):
  4127. used_grams = 0
  4128. if used_grams > 0 and filament_id:
  4129. plate_info["filaments"].append(
  4130. {
  4131. "slot_id": int(filament_id),
  4132. "type": filament_type,
  4133. "color": filament_color,
  4134. "used_grams": round(used_grams, 1),
  4135. "used_meters": float(used_m) if used_m else 0,
  4136. }
  4137. )
  4138. # Sort filaments by slot ID
  4139. plate_info["filaments"].sort(key=lambda x: x["slot_id"])
  4140. # Collect all object names on this plate
  4141. for obj_elem in plate_elem.findall("object"):
  4142. obj_name = obj_elem.get("name")
  4143. if obj_name and obj_name not in plate_info["objects"]:
  4144. plate_info["objects"].append(obj_name)
  4145. # Set plate name: prefer custom name from model_settings.config,
  4146. # fall back to first object name if no custom name was set
  4147. if plate_index is not None:
  4148. custom_name = plate_names.get(plate_index)
  4149. if custom_name:
  4150. plate_info["name"] = custom_name
  4151. else:
  4152. # Fall back to first object name as hint
  4153. if plate_info["objects"]:
  4154. plate_info["name"] = plate_info["objects"][0]
  4155. plate_metadata[plate_index] = plate_info
  4156. # Parse plate_*.json for object lists when slice_info is missing
  4157. plate_json_objects: dict[int, list[str]] = {}
  4158. for name in namelist:
  4159. match = re.match(r"^Metadata/plate_(\d+)\.json$", name)
  4160. if not match:
  4161. continue
  4162. try:
  4163. plate_index = int(match.group(1))
  4164. except ValueError:
  4165. continue
  4166. try:
  4167. payload = json.loads(zf.read(name).decode())
  4168. bbox_objects = payload.get("bbox_objects", [])
  4169. names = []
  4170. for obj in bbox_objects:
  4171. obj_name = obj.get("name") if isinstance(obj, dict) else None
  4172. if obj_name and obj_name not in names:
  4173. names.append(obj_name)
  4174. if names:
  4175. plate_json_objects[plate_index] = names
  4176. except Exception:
  4177. continue
  4178. # Build plate list
  4179. for idx in plate_indices:
  4180. meta = plate_metadata.get(idx, {})
  4181. has_thumbnail = f"Metadata/plate_{idx}.png" in namelist
  4182. objects = meta.get("objects", [])
  4183. if not objects:
  4184. objects = plate_json_objects.get(idx, [])
  4185. if not objects and plate_object_ids.get(idx):
  4186. objects = [
  4187. object_names_by_id.get(obj_id, f"Object {obj_id}") for obj_id in plate_object_ids.get(idx, [])
  4188. ]
  4189. plate_name = meta.get("name")
  4190. if not plate_name:
  4191. plate_name = plate_names.get(idx)
  4192. if not plate_name and objects:
  4193. plate_name = objects[0]
  4194. plates.append(
  4195. {
  4196. "index": idx,
  4197. "name": plate_name,
  4198. "objects": objects,
  4199. "object_count": len(objects),
  4200. "has_thumbnail": has_thumbnail,
  4201. "thumbnail_url": f"/api/v1/archives/{archive_id}/plate-thumbnail/{idx}"
  4202. if has_thumbnail
  4203. else None,
  4204. "print_time_seconds": meta.get("prediction"),
  4205. "filament_used_grams": meta.get("weight"),
  4206. "filaments": meta.get("filaments", []),
  4207. "bed_type": meta.get("bed_type"),
  4208. }
  4209. )
  4210. except Exception as e:
  4211. logger.warning("Failed to parse plates from archive %s: %s", archive_id, e)
  4212. # Has gcode iff the plate list was built from .gcode filenames (as opposed
  4213. # to the JSON/PNG fallback for source-only 3MF projects). Callers that need
  4214. # to preview gcode — the viewer, skip-objects — can gate on this instead of
  4215. # 404-ing on every plate request.
  4216. has_gcode = bool(gcode_files)
  4217. return {
  4218. "archive_id": archive_id,
  4219. "filename": archive.filename,
  4220. "plates": plates,
  4221. "is_multi_plate": len(plates) > 1,
  4222. "has_gcode": has_gcode,
  4223. "embedded_printer": embedded_presets["printer"],
  4224. "embedded_process": embedded_presets["process"],
  4225. "design_overrides": design_overrides,
  4226. }
  4227. @router.get("/{archive_id}/plate-thumbnail/{plate_index}")
  4228. async def get_plate_thumbnail(
  4229. archive_id: int,
  4230. plate_index: int,
  4231. db: AsyncSession = Depends(get_db),
  4232. auth_result: tuple[User | None, bool] = Depends(
  4233. require_media_token_ownership(
  4234. Permission.ARCHIVES_READ_ALL,
  4235. Permission.ARCHIVES_READ_OWN,
  4236. )
  4237. ),
  4238. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  4239. ):
  4240. """Get the thumbnail image for a specific plate.
  4241. Requires a media token query param (?token=xxx) when auth is enabled, and
  4242. returns 404 for an archive the caller may not read (#3025).
  4243. """
  4244. user, can_read_all = auth_result
  4245. service = ArchiveService(db)
  4246. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  4247. file_path = settings.base_dir / archive.file_path
  4248. if not file_path.is_file():
  4249. raise HTTPException(404, "Archive file not found")
  4250. try:
  4251. with zipfile.ZipFile(file_path, "r") as zf:
  4252. thumb_path = f"Metadata/plate_{plate_index}.png"
  4253. if thumb_path in zf.namelist():
  4254. data = zf.read(thumb_path)
  4255. return Response(content=data, media_type="image/png")
  4256. except Exception:
  4257. pass # Fall through to 404 if archive is unreadable or thumbnail missing
  4258. raise HTTPException(404, f"Thumbnail for plate {plate_index} not found")
  4259. async def _try_preview_slice_filaments(
  4260. db: AsyncSession,
  4261. *,
  4262. kind: str,
  4263. source_id: int,
  4264. plate_id: int,
  4265. file_path: Path,
  4266. request_id: str | None = None,
  4267. ) -> list[dict] | None:
  4268. """Run a preview slice via the user's configured sidecar so the filament
  4269. list endpoint can return real per-plate filaments for unsliced project
  4270. files. Returns ``None`` on any failure — the caller falls back to the
  4271. painted-face heuristic. ``request_id`` flows through to the sidecar
  4272. for live progress on the SliceModal's inline spinner + toast.
  4273. """
  4274. from backend.app.api.routes.settings import get_setting
  4275. from backend.app.services.slice_preview import get_preview_filaments
  4276. from backend.app.services.slicer_api import get_stall_timeout_seconds
  4277. preferred = (await get_setting(db, "preferred_slicer")) or "bambu_studio"
  4278. if preferred == "orcaslicer":
  4279. configured = await get_setting(db, "orcaslicer_api_url")
  4280. api_url = (configured or settings.slicer_api_url).strip()
  4281. elif preferred == "bambu_studio":
  4282. configured = await get_setting(db, "bambu_studio_api_url")
  4283. api_url = (configured or settings.bambu_studio_api_url).strip()
  4284. else:
  4285. return None
  4286. if not api_url:
  4287. return None
  4288. try:
  4289. file_bytes = file_path.read_bytes()
  4290. except OSError:
  4291. return None
  4292. return await get_preview_filaments(
  4293. kind=kind,
  4294. source_id=source_id,
  4295. plate_id=plate_id,
  4296. file_bytes=file_bytes,
  4297. file_name=file_path.name,
  4298. api_url=api_url,
  4299. request_id=request_id,
  4300. timeout_seconds=await get_stall_timeout_seconds(db),
  4301. )
  4302. @router.get("/{archive_id}/filament-requirements")
  4303. async def get_filament_requirements(
  4304. archive_id: int,
  4305. plate_id: int | None = None,
  4306. request_id: str | None = None,
  4307. full_slots: bool = False,
  4308. db: AsyncSession = Depends(get_db),
  4309. auth_result: tuple[User | None, bool] = Depends(
  4310. require_ownership_permission(
  4311. Permission.ARCHIVES_READ_ALL,
  4312. Permission.ARCHIVES_READ_OWN,
  4313. )
  4314. ),
  4315. printer_scope: PrinterScope = RequestPrinterScope,
  4316. ):
  4317. """Get filament requirements from the archived 3MF file.
  4318. Returns the filaments used in this print with their slot IDs, types, colors,
  4319. and usage amounts. This can be compared with current AMS state before reprinting.
  4320. Args:
  4321. archive_id: The archive ID
  4322. plate_id: Optional plate index to filter filaments for (for multi-plate files)
  4323. """
  4324. import defusedxml.ElementTree as ET
  4325. user, can_read_all = auth_result
  4326. service = ArchiveService(db)
  4327. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  4328. file_path = settings.base_dir / archive.file_path
  4329. if not file_path.is_file():
  4330. raise HTTPException(404, "Archive file not found")
  4331. filaments = []
  4332. try:
  4333. with zipfile.ZipFile(file_path, "r") as zf:
  4334. # Parse slice_info.config for filament requirements
  4335. if "Metadata/slice_info.config" in zf.namelist():
  4336. content = zf.read("Metadata/slice_info.config").decode()
  4337. root = ET.fromstring(content)
  4338. # If plate_id is specified, find filaments for that specific plate
  4339. if plate_id is not None:
  4340. # Find the plate element with matching index
  4341. for plate_elem in root.findall(".//plate"):
  4342. plate_index = None
  4343. for meta in plate_elem.findall("metadata"):
  4344. if meta.get("key") == "index":
  4345. try:
  4346. plate_index = int(meta.get("value", "0"))
  4347. except ValueError:
  4348. pass # Skip plate with non-numeric index metadata
  4349. break
  4350. if plate_index == plate_id:
  4351. # Extract filaments from this plate element
  4352. for filament_elem in plate_elem.findall("filament"):
  4353. filament_id = filament_elem.get("id")
  4354. filament_type = filament_elem.get("type", "")
  4355. filament_color = filament_elem.get("color", "")
  4356. used_g = filament_elem.get("used_g", "0")
  4357. used_m = filament_elem.get("used_m", "0")
  4358. tray_info_idx = filament_elem.get("tray_info_idx", "")
  4359. try:
  4360. used_grams = float(used_g)
  4361. except (ValueError, TypeError):
  4362. used_grams = 0
  4363. if used_grams > 0 and filament_id:
  4364. filaments.append(
  4365. {
  4366. "slot_id": int(filament_id),
  4367. "type": filament_type,
  4368. "color": filament_color,
  4369. "used_grams": round(used_grams, 1),
  4370. "used_meters": float(used_m) if used_m else 0,
  4371. "tray_info_idx": tray_info_idx,
  4372. "used_in_plate": True,
  4373. }
  4374. )
  4375. break
  4376. else:
  4377. # No plate_id specified - extract all filaments with used_g > 0
  4378. # This is the legacy behavior for single-plate files
  4379. for filament_elem in root.findall(".//filament"):
  4380. filament_id = filament_elem.get("id")
  4381. filament_type = filament_elem.get("type", "")
  4382. filament_color = filament_elem.get("color", "")
  4383. used_g = filament_elem.get("used_g", "0")
  4384. used_m = filament_elem.get("used_m", "0")
  4385. tray_info_idx = filament_elem.get("tray_info_idx", "")
  4386. # Only include filaments that are actually used
  4387. try:
  4388. used_grams = float(used_g)
  4389. except (ValueError, TypeError):
  4390. used_grams = 0
  4391. if used_grams > 0 and filament_id:
  4392. filaments.append(
  4393. {
  4394. "slot_id": int(filament_id),
  4395. "type": filament_type,
  4396. "color": filament_color,
  4397. "used_grams": round(used_grams, 1),
  4398. "used_meters": float(used_m) if used_m else 0,
  4399. "tray_info_idx": tray_info_idx,
  4400. "used_in_plate": True,
  4401. }
  4402. )
  4403. # Re-slicing a source that already carries slice_info (#2712).
  4404. # See library.py for the full rationale: the slice modal's list is
  4405. # positional, so a source using only slot 4 must still present
  4406. # four slots or the pick lands on slot 1. The print path keeps the
  4407. # used-only list it depends on.
  4408. if full_slots and filaments:
  4409. filaments = expand_to_project_slots(zf, filaments)
  4410. # Unsliced project files: see library.py for full rationale.
  4411. # Return the FULL project_settings.config slot list with a
  4412. # used_in_plate flag derived from the preview slice; the
  4413. # CLI needs every slot pre-filled to avoid silent default
  4414. # substitution.
  4415. if not filaments:
  4416. project_filaments = extract_project_filaments_from_3mf(zf)
  4417. used_slot_ids: set[int] = set()
  4418. if project_filaments and plate_id is not None:
  4419. preview = await _try_preview_slice_filaments(
  4420. db,
  4421. kind="archive",
  4422. source_id=archive_id,
  4423. plate_id=plate_id,
  4424. file_path=file_path,
  4425. request_id=request_id,
  4426. )
  4427. if preview is not None:
  4428. used_slot_ids = {f["slot_id"] for f in preview}
  4429. fallback_all_used = not used_slot_ids
  4430. for f in project_filaments:
  4431. f["used_in_plate"] = fallback_all_used or f["slot_id"] in used_slot_ids
  4432. filaments = project_filaments
  4433. # Sort by slot ID
  4434. filaments.sort(key=lambda x: x["slot_id"])
  4435. # Enrich with nozzle mapping for dual-nozzle printers
  4436. nozzle_mapping = extract_nozzle_mapping_from_3mf(zf, plate_id=plate_id)
  4437. if nozzle_mapping:
  4438. for filament in filaments:
  4439. filament["nozzle_id"] = nozzle_mapping.get(filament["slot_id"])
  4440. # Nozzle-rack machines (#1784): the print dialog offers a rack
  4441. # position per filament group, which needs the group table as well
  4442. # as the carriage above.
  4443. annotate_rack_groups(filaments, file_path, plate_id)
  4444. except Exception as e:
  4445. logger.warning("Failed to parse filament requirements from archive %s: %s", archive_id, e)
  4446. return {
  4447. "archive_id": archive_id,
  4448. "filename": archive.filename,
  4449. "plate_id": plate_id,
  4450. "filaments": filaments,
  4451. }
  4452. @router.post("/{archive_id}/slice", status_code=202)
  4453. async def slice_archive(
  4454. archive_id: int,
  4455. request: SliceRequest,
  4456. db: AsyncSession = Depends(get_db),
  4457. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.LIBRARY_UPLOAD),
  4458. printer_scope: PrinterScope = RequestPrinterScope,
  4459. actor: User | ApiKeyActor | None = RequestActor,
  4460. ):
  4461. """Enqueue a slice job for an archive's source. Returns 202 + job_id;
  4462. the slice runs in the background, the caller polls `GET /slice-jobs/{id}`.
  4463. Source preference: ``source_3mf_path`` (the un-sliced project file the
  4464. user originally sent to slice) → ``file_path`` (the sliced 3MF/gcode that
  4465. actually printed).
  4466. """
  4467. from backend.app.api.routes.library import guard_nozzle_class_reslice, slice_and_persist_as_archive
  4468. from backend.app.core.database import async_session
  4469. from backend.app.services.slice_dispatch import (
  4470. http_exception_to_job_error,
  4471. slice_dispatch,
  4472. )
  4473. archive = await db.get(PrintArchive, archive_id)
  4474. # Per-row ownership gate — mirror the archive read routes. LIBRARY_UPLOAD
  4475. # alone let a READ_OWN caller slice another user's archive by raw id even
  4476. # though GET on that id returned 404. An API key is checked as its owner
  4477. # (RequestActor); only auth off keeps can_read_all=True.
  4478. can_read_all = actor is None or actor.has_permission(Permission.ARCHIVES_READ_ALL.value)
  4479. archive = _ensure_archive_visible(archive, actor, can_read_all, printer_scope)
  4480. # The re-sliced copy belongs to the caller and can be queued as their own,
  4481. # so slicing an archive needs the same right as printing it again.
  4482. from backend.app.api.routes.print_queue import _assert_can_queue_archive
  4483. _assert_can_queue_archive(archive, actor, printer_scope)
  4484. src_relative = archive.source_3mf_path or archive.file_path
  4485. if not src_relative:
  4486. raise HTTPException(
  4487. status_code=400,
  4488. detail="Archive has no source file to slice",
  4489. )
  4490. src_path = (
  4491. Path(settings.base_dir) / src_relative
  4492. ) # 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
  4493. if not src_path.exists():
  4494. raise HTTPException(status_code=404, detail="Archive source file missing on disk")
  4495. raw_filename = archive.filename or src_path.name
  4496. src_lower = raw_filename.lower()
  4497. if not (
  4498. src_lower.endswith(".stl")
  4499. or src_lower.endswith(".3mf")
  4500. or src_lower.endswith(".step")
  4501. or src_lower.endswith(".stp")
  4502. ):
  4503. raise HTTPException(
  4504. status_code=400,
  4505. detail="Archive's source file must be STL, 3MF, or STEP to slice",
  4506. )
  4507. # Match the library route: derive the sliced output's filename from
  4508. # `print_name` when set, so the new archive row's display name lines
  4509. # up with the source's display.
  4510. src_ext = Path(raw_filename).suffix.lower() or ".3mf"
  4511. src_filename = (
  4512. f"{archive.print_name.strip()}{src_ext}" if archive.print_name and archive.print_name.strip() else raw_filename
  4513. )
  4514. model_bytes = src_path.read_bytes()
  4515. archive_id_local = archive.id
  4516. user_id = current_user.id if current_user else None
  4517. # Block a cross-nozzle-class re-slice (single-nozzle <-> H2D) up front —
  4518. # BambuStudio's multi-extruder validator would otherwise reject it with a
  4519. # cryptic error. No-op for same-class or un-sliced sources.
  4520. await guard_nozzle_class_reslice(db, current_user, request, archive.sliced_for_model)
  4521. async def _run(job_id: int):
  4522. async with async_session() as task_db:
  4523. # Re-fetch the source archive on the background-task session.
  4524. src_archive = await task_db.get(PrintArchive, archive_id_local)
  4525. if src_archive is None:
  4526. raise http_exception_to_job_error(
  4527. HTTPException(status_code=404, detail="Archive disappeared during slice")
  4528. )
  4529. try:
  4530. response = await slice_and_persist_as_archive(
  4531. task_db,
  4532. model_bytes=model_bytes,
  4533. model_filename=src_filename,
  4534. request=request,
  4535. source_archive=src_archive,
  4536. current_user_id=user_id,
  4537. job_id=job_id,
  4538. )
  4539. except HTTPException as exc:
  4540. raise http_exception_to_job_error(exc) from exc
  4541. return response.model_dump()
  4542. job = await slice_dispatch.enqueue(
  4543. kind="archive",
  4544. source_id=archive.id,
  4545. source_name=archive.print_name or archive.filename or f"archive {archive.id}",
  4546. owner_id=user_id,
  4547. run=_run,
  4548. )
  4549. return {
  4550. "job_id": job.id,
  4551. "status": job.status,
  4552. "status_url": f"/api/v1/slice-jobs/{job.id}",
  4553. }
  4554. @router.post("/{archive_id}/reprint")
  4555. async def reprint_archive(
  4556. archive_id: int,
  4557. printer_id: int,
  4558. # SECURITY.md SEC-AUTH-1: every route either has an explicit auth dep or
  4559. # is in the route-auth-coverage allowlist. Gating the deprecation stub on
  4560. # QUEUE_CREATE matches the replacement route (POST /queue/) and means
  4561. # anonymous callers bounce at auth instead of seeing the deprecation
  4562. # message — leaking "this route exists" to unauthenticated callers is
  4563. # exactly the shape the backstop guards against.
  4564. _: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_CREATE),
  4565. ):
  4566. """Legacy direct reprint endpoint. Use POST /queue/ instead."""
  4567. logger.warning(
  4568. "Gone API used: POST /archives/%s/reprint?printer_id=%s; use POST /queue/ instead",
  4569. archive_id,
  4570. printer_id,
  4571. )
  4572. raise HTTPException(
  4573. status_code=410,
  4574. detail="Direct archive reprint has been removed. Create a print queue item with POST /queue/.",
  4575. )
  4576. # =============================================================================
  4577. # Project Page API
  4578. # =============================================================================
  4579. @router.get("/{archive_id}/project-page")
  4580. async def get_project_page(
  4581. archive_id: int,
  4582. db: AsyncSession = Depends(get_db),
  4583. auth_result: tuple[User | None, bool] = Depends(
  4584. require_ownership_permission(
  4585. Permission.ARCHIVES_READ_ALL,
  4586. Permission.ARCHIVES_READ_OWN,
  4587. )
  4588. ),
  4589. printer_scope: PrinterScope = RequestPrinterScope,
  4590. ):
  4591. """Get the project page data from the 3MF file."""
  4592. from backend.app.schemas.archive import ProjectPageResponse
  4593. from backend.app.services.archive import ProjectPageParser
  4594. user, can_read_all = auth_result
  4595. service = ArchiveService(db)
  4596. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  4597. file_path = settings.base_dir / archive.file_path
  4598. if not file_path.is_file():
  4599. raise HTTPException(404, "Archive file not found")
  4600. parser = ProjectPageParser(file_path)
  4601. data = parser.parse(archive_id)
  4602. return ProjectPageResponse(**data)
  4603. @router.patch("/{archive_id}/project-page")
  4604. async def update_project_page(
  4605. archive_id: int,
  4606. update_data: dict,
  4607. db: AsyncSession = Depends(get_db),
  4608. auth_result: tuple[User | None, bool] = Depends(
  4609. require_ownership_permission(
  4610. Permission.ARCHIVES_UPDATE_ALL,
  4611. Permission.ARCHIVES_UPDATE_OWN,
  4612. )
  4613. ),
  4614. printer_scope: PrinterScope = RequestPrinterScope,
  4615. ):
  4616. """Update project page metadata in the 3MF file."""
  4617. from backend.app.services.archive import ProjectPageParser
  4618. user, can_modify_all = auth_result
  4619. service = ArchiveService(db)
  4620. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_modify_all, printer_scope)
  4621. file_path = settings.base_dir / archive.file_path
  4622. if not file_path.is_file():
  4623. raise HTTPException(404, "Archive file not found")
  4624. parser = ProjectPageParser(file_path)
  4625. success = parser.update_metadata(update_data)
  4626. if not success:
  4627. raise HTTPException(500, "Failed to update project page")
  4628. # Return updated data
  4629. data = parser.parse(archive_id)
  4630. return data
  4631. @router.get("/{archive_id}/project-image/{image_path:path}")
  4632. async def get_project_image(
  4633. archive_id: int,
  4634. image_path: str,
  4635. db: AsyncSession = Depends(get_db),
  4636. auth_result: tuple[User | None, bool] = Depends(
  4637. require_media_token_ownership(
  4638. Permission.ARCHIVES_READ_ALL,
  4639. Permission.ARCHIVES_READ_OWN,
  4640. )
  4641. ),
  4642. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  4643. ):
  4644. """Get an image from the 3MF project page.
  4645. Requires a media token query param (?token=xxx) when auth is enabled, and
  4646. returns 404 for an archive the caller may not read (#3025).
  4647. """
  4648. user, can_read_all = auth_result
  4649. from backend.app.services.archive import ProjectPageParser
  4650. service = ArchiveService(db)
  4651. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  4652. file_path = settings.base_dir / archive.file_path
  4653. if not file_path.is_file():
  4654. raise HTTPException(404, "Archive file not found")
  4655. parser = ProjectPageParser(file_path)
  4656. result = parser.get_image(image_path)
  4657. if not result:
  4658. raise HTTPException(404, "Image not found in 3MF file")
  4659. image_data, content_type = result
  4660. return Response(
  4661. content=image_data,
  4662. media_type=content_type,
  4663. headers={"Cache-Control": "max-age=3600"},
  4664. )
  4665. # =============================================================================
  4666. # Source 3MF API (Original Project Files)
  4667. # =============================================================================
  4668. def _resolve_source_3mf_path(archive: PrintArchive, source_filename: str) -> Path:
  4669. """Resolve where to write a source 3MF for ``archive``.
  4670. Normal archives nest the source under ``<archive_file_dir>/source/``.
  4671. "Fallback" archives (created in main.py when MQTT reports a print start
  4672. but Bambuddy never saw the source 3MF — cloud / Handy / pre-existing
  4673. SD-card prints) carry ``file_path=""``. Joining that with ``base_dir``
  4674. via the ``/`` operator silently yields ``base_dir`` itself, whose parent
  4675. is ``base_dir.parent`` — which sent the upload to ``/app/source/`` and
  4676. raised a 500 on the final ``relative_to`` (#1531). Fallback archives
  4677. now land under ``<base_dir>/archive/no_source/<archive_id>/`` instead,
  4678. which stays inside the data volume and remains addressable by every
  4679. read site that does ``base_dir / archive.source_3mf_path``.
  4680. The resolved directory is asserted to be inside ``base_dir`` even when
  4681. ``archive.file_path`` is populated, so a row corrupted by an old import
  4682. or manual SQL edit fails with a clear 500 instead of writing outside
  4683. the data volume.
  4684. """
  4685. if archive.file_path:
  4686. archive_file = settings.base_dir / archive.file_path
  4687. source_dir = archive_file.parent / "source"
  4688. else:
  4689. source_dir = settings.base_dir / "archive" / "no_source" / str(archive.id)
  4690. # Containment check via resolve() — catches absolute file_path, `..`
  4691. # traversal, and any other shape that escapes the data volume — but we
  4692. # return the *literal* source_dir below. Resolving the returned path
  4693. # would canonicalise away a symlinked DATA_DIR (legitimate on TrueNAS /
  4694. # QNAP / Synology storage pools, and any `-v /symlink:/app/data`
  4695. # mount), which would then make the caller's
  4696. # ``source_path.relative_to(settings.base_dir)`` raise because the
  4697. # left side is canonical and the right is the symlink path.
  4698. try:
  4699. source_dir.resolve().relative_to(settings.base_dir.resolve())
  4700. except ValueError as exc:
  4701. raise HTTPException(
  4702. 500,
  4703. f"Archive {archive.id} resolves to a path outside the data directory; cannot attach source.",
  4704. ) from exc
  4705. source_dir.mkdir(parents=True, exist_ok=True)
  4706. return (
  4707. source_dir / source_filename
  4708. ) # SEC-PATH-OK: callers pass _safe_filename(...) basename-stripped; source_dir resolve+relative_to checked above
  4709. @router.post("/{archive_id}/source")
  4710. async def upload_source_3mf(
  4711. archive_id: int,
  4712. file: UploadFile = File(...),
  4713. db: AsyncSession = Depends(get_db),
  4714. auth_result: tuple[User | None, bool] = Depends(
  4715. require_ownership_permission(
  4716. Permission.ARCHIVES_UPDATE_ALL,
  4717. Permission.ARCHIVES_UPDATE_OWN,
  4718. )
  4719. ),
  4720. printer_scope: PrinterScope = RequestPrinterScope,
  4721. ):
  4722. """Upload the original source 3MF project file for an archive."""
  4723. user, can_modify_all = auth_result
  4724. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  4725. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  4726. if not file.filename or not file.filename.endswith(".3mf"):
  4727. raise HTTPException(400, "File must be a .3mf file")
  4728. # Save the source 3MF file - preserve original filename, strip directory components
  4729. source_filename = _safe_filename(file.filename)
  4730. source_path = _resolve_source_3mf_path(archive, source_filename)
  4731. # Delete old source file if exists
  4732. if archive.source_3mf_path:
  4733. old_source_path = settings.base_dir / archive.source_3mf_path
  4734. if old_source_path.exists():
  4735. old_source_path.unlink()
  4736. content = await file.read()
  4737. # #1401: validate zip header on source 3MF uploads too — source files
  4738. # are uploaded for reprint and slicing, so an invalid one breaks the
  4739. # same downstream paths as a bad sliced file.
  4740. from backend.app.api.routes.library import validate_print_file_upload
  4741. validate_print_file_upload(file.filename, content)
  4742. source_path.write_bytes(content)
  4743. # Update archive with source path (relative to base_dir)
  4744. archive.source_3mf_path = str(source_path.relative_to(settings.base_dir))
  4745. await db.commit()
  4746. await db.refresh(archive)
  4747. return {
  4748. "status": "uploaded",
  4749. "source_3mf_path": archive.source_3mf_path,
  4750. "filename": source_filename,
  4751. }
  4752. @router.get("/{archive_id}/source")
  4753. async def download_source_3mf(
  4754. archive_id: int,
  4755. db: AsyncSession = Depends(get_db),
  4756. auth_result: tuple[User | None, bool] = Depends(
  4757. require_ownership_permission(
  4758. Permission.ARCHIVES_READ_ALL,
  4759. Permission.ARCHIVES_READ_OWN,
  4760. )
  4761. ),
  4762. printer_scope: PrinterScope = RequestPrinterScope,
  4763. ):
  4764. """Download the source 3MF project file."""
  4765. user, can_read_all = auth_result
  4766. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  4767. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_read_all, printer_scope)
  4768. if not archive.source_3mf_path:
  4769. raise HTTPException(404, "No source 3MF attached to this archive")
  4770. source_path = settings.base_dir / archive.source_3mf_path
  4771. if not source_path.exists():
  4772. raise HTTPException(404, "Source 3MF file not found on disk")
  4773. # Use the actual filename from the path
  4774. filename = source_path.name
  4775. return FileResponse(
  4776. path=source_path,
  4777. filename=filename,
  4778. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  4779. )
  4780. @router.get("/{archive_id}/source/{filename}")
  4781. async def download_source_3mf_for_slicer(
  4782. archive_id: int,
  4783. filename: str,
  4784. db: AsyncSession = Depends(get_db),
  4785. auth_result: tuple[User | None, bool] = Depends(
  4786. require_ownership_permission(
  4787. Permission.ARCHIVES_READ_ALL,
  4788. Permission.ARCHIVES_READ_OWN,
  4789. )
  4790. ),
  4791. printer_scope: PrinterScope = RequestPrinterScope,
  4792. ):
  4793. """Download source 3MF with filename in URL."""
  4794. user, can_read_all = auth_result
  4795. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  4796. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_read_all, printer_scope)
  4797. if not archive.source_3mf_path:
  4798. raise HTTPException(404, "No source 3MF attached to this archive")
  4799. source_path = settings.base_dir / archive.source_3mf_path
  4800. if not source_path.exists():
  4801. raise HTTPException(404, "Source 3MF file not found on disk")
  4802. return FileResponse(
  4803. path=source_path,
  4804. filename=filename if filename.endswith(".3mf") else f"{filename}.3mf",
  4805. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  4806. )
  4807. @router.post("/{archive_id}/source-slicer-token")
  4808. async def create_source_slicer_token(
  4809. archive_id: int,
  4810. db: AsyncSession = Depends(get_db),
  4811. auth_result: tuple[User | None, bool] = Depends(
  4812. require_ownership_permission(
  4813. Permission.ARCHIVES_READ_ALL,
  4814. Permission.ARCHIVES_READ_OWN,
  4815. )
  4816. ),
  4817. printer_scope: PrinterScope = RequestPrinterScope,
  4818. ):
  4819. """Create a short-lived download token for opening source 3MF in slicer."""
  4820. from backend.app.core.auth import create_slicer_download_token
  4821. user, can_read_all = auth_result
  4822. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  4823. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_read_all, printer_scope)
  4824. if not archive.source_3mf_path:
  4825. raise HTTPException(404, "No source 3MF attached to this archive")
  4826. token = await create_slicer_download_token("source", archive_id)
  4827. return {"token": token}
  4828. @router.get("/{archive_id}/source-dl/{token}/{filename}")
  4829. async def download_source_3mf_for_slicer_with_token(
  4830. archive_id: int,
  4831. token: str,
  4832. filename: str,
  4833. db: AsyncSession = Depends(get_db),
  4834. ):
  4835. """Download source 3MF using a slicer download token.
  4836. Token-authenticated (no auth headers needed). The token is short-lived and
  4837. archive-bound, created by POST /{archive_id}/source-slicer-token, and
  4838. redeemable for the rest of its TTL rather than exactly once (#3029).
  4839. """
  4840. from backend.app.core.auth import verify_slicer_download_token
  4841. if not await verify_slicer_download_token(token, "source", archive_id, single_use=False):
  4842. raise HTTPException(403, "Invalid or expired download token")
  4843. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  4844. archive = result.scalar_one_or_none()
  4845. if not archive:
  4846. raise HTTPException(404, "Archive not found")
  4847. if not archive.source_3mf_path:
  4848. raise HTTPException(404, "No source 3MF attached to this archive")
  4849. source_path = settings.base_dir / archive.source_3mf_path
  4850. if not source_path.exists():
  4851. raise HTTPException(404, "Source 3MF file not found on disk")
  4852. return FileResponse(
  4853. path=source_path,
  4854. filename=filename if filename.endswith(".3mf") else f"{filename}.3mf",
  4855. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  4856. )
  4857. @router.post("/upload-source")
  4858. async def upload_source_3mf_by_name(
  4859. file: UploadFile = File(...),
  4860. print_name: str = Query(None, description="Match archive by print name"),
  4861. db: AsyncSession = Depends(get_db),
  4862. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  4863. printer_scope: PrinterScope = RequestPrinterScope,
  4864. ):
  4865. """Upload source 3MF and match to archive by print name.
  4866. This endpoint is designed for slicer post-processing scripts.
  4867. It finds the most recent archive matching the print name and attaches the source.
  4868. """
  4869. if not file.filename or not file.filename.endswith(".3mf"):
  4870. raise HTTPException(400, "File must be a .3mf file")
  4871. safe_filename = _safe_filename(file.filename)
  4872. # Derive print name from filename if not provided
  4873. if not print_name:
  4874. # Remove .3mf extension and common suffixes
  4875. print_name = safe_filename.rsplit(".3mf", 1)[0]
  4876. # Remove _source suffix if present
  4877. if print_name.endswith("_source"):
  4878. print_name = print_name[:-7]
  4879. # Find matching archive - try exact match first, then fuzzy
  4880. # Only an archive on the caller's printers can be matched, as it is listed.
  4881. visible = _visible_archive_clauses(None, True, printer_scope)
  4882. result = await db.execute(
  4883. select(PrintArchive)
  4884. .where(PrintArchive.print_name == print_name, *visible)
  4885. .order_by(PrintArchive.created_at.desc())
  4886. .limit(1)
  4887. )
  4888. archive = result.scalar_one_or_none()
  4889. if not archive:
  4890. # Try matching filename without .gcode.3mf
  4891. result = await db.execute(
  4892. select(PrintArchive)
  4893. .where(PrintArchive.filename.like(f"{print_name}%"), *visible)
  4894. .order_by(PrintArchive.created_at.desc())
  4895. .limit(1)
  4896. )
  4897. archive = result.scalar_one_or_none()
  4898. if not archive:
  4899. # Try case-insensitive partial match on print_name
  4900. result = await db.execute(
  4901. select(PrintArchive)
  4902. .where(PrintArchive.print_name.ilike(f"%{print_name}%"), *visible)
  4903. .order_by(PrintArchive.created_at.desc())
  4904. .limit(1)
  4905. )
  4906. archive = result.scalar_one_or_none()
  4907. if not archive:
  4908. raise HTTPException(404, f"No archive found matching '{print_name}'")
  4909. # Save the source 3MF file - preserve original filename, strip directory components
  4910. source_filename = safe_filename
  4911. source_path = _resolve_source_3mf_path(archive, source_filename)
  4912. # Delete old source file if exists
  4913. if archive.source_3mf_path:
  4914. old_source_path = settings.base_dir / archive.source_3mf_path
  4915. if old_source_path.exists():
  4916. old_source_path.unlink()
  4917. content = await file.read()
  4918. # #1401: same zip-header check as the other upload routes — the
  4919. # match-by-name endpoint is used by slicer post-processing scripts,
  4920. # so a misconfigured script is exactly how a bad 3MF would slip in.
  4921. from backend.app.api.routes.library import validate_print_file_upload
  4922. validate_print_file_upload(file.filename, content)
  4923. source_path.write_bytes(content)
  4924. # Update archive with source path
  4925. archive.source_3mf_path = str(source_path.relative_to(settings.base_dir))
  4926. await db.commit()
  4927. await db.refresh(archive)
  4928. return {
  4929. "status": "uploaded",
  4930. "archive_id": archive.id,
  4931. "archive_name": archive.print_name or archive.filename,
  4932. "source_3mf_path": archive.source_3mf_path,
  4933. "filename": source_filename,
  4934. }
  4935. @router.delete("/{archive_id}/source")
  4936. async def delete_source_3mf(
  4937. archive_id: int,
  4938. db: AsyncSession = Depends(get_db),
  4939. auth_result: tuple[User | None, bool] = Depends(
  4940. require_ownership_permission(
  4941. Permission.ARCHIVES_DELETE_ALL,
  4942. Permission.ARCHIVES_DELETE_OWN,
  4943. )
  4944. ),
  4945. printer_scope: PrinterScope = RequestPrinterScope,
  4946. ):
  4947. """Delete the source 3MF project file from an archive."""
  4948. user, can_modify_all = auth_result
  4949. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  4950. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  4951. if not archive.source_3mf_path:
  4952. raise HTTPException(404, "No source 3MF attached to this archive")
  4953. # Delete the file
  4954. source_path = settings.base_dir / archive.source_3mf_path
  4955. if source_path.exists():
  4956. source_path.unlink()
  4957. # Clear the path in database
  4958. archive.source_3mf_path = None
  4959. await db.commit()
  4960. return {"status": "deleted"}
  4961. # =============================================================================
  4962. # F3D API (Fusion 360 Design Files)
  4963. # =============================================================================
  4964. @router.post("/{archive_id}/f3d")
  4965. async def upload_f3d(
  4966. archive_id: int,
  4967. file: UploadFile = File(...),
  4968. db: AsyncSession = Depends(get_db),
  4969. auth_result: tuple[User | None, bool] = Depends(
  4970. require_ownership_permission(
  4971. Permission.ARCHIVES_UPDATE_ALL,
  4972. Permission.ARCHIVES_UPDATE_OWN,
  4973. )
  4974. ),
  4975. printer_scope: PrinterScope = RequestPrinterScope,
  4976. ):
  4977. """Upload a Fusion 360 design file for an archive."""
  4978. user, can_modify_all = auth_result
  4979. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  4980. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  4981. if not file.filename or not file.filename.endswith(".f3d"):
  4982. raise HTTPException(400, "File must be a .f3d file")
  4983. # Get archive directory and create f3d subdirectory
  4984. file_path = settings.base_dir / archive.file_path
  4985. archive_dir = file_path.parent
  4986. f3d_dir = archive_dir / "f3d"
  4987. f3d_dir.mkdir(exist_ok=True)
  4988. # Delete old F3D file if exists
  4989. if archive.f3d_path:
  4990. old_f3d_path = settings.base_dir / archive.f3d_path
  4991. if old_f3d_path.exists():
  4992. old_f3d_path.unlink()
  4993. # Save the F3D file - preserve original filename, strip directory components
  4994. f3d_filename = _safe_filename(file.filename)
  4995. f3d_path = f3d_dir / f3d_filename # SEC-PATH-OK: f3d_filename = _safe_filename(...) basename-stripped above
  4996. content = await file.read()
  4997. f3d_path.write_bytes(content)
  4998. # Update archive with F3D path (relative to base_dir)
  4999. archive.f3d_path = str(f3d_path.relative_to(settings.base_dir))
  5000. await db.commit()
  5001. await db.refresh(archive)
  5002. return {
  5003. "status": "uploaded",
  5004. "f3d_path": archive.f3d_path,
  5005. "filename": f3d_filename,
  5006. }
  5007. @router.get("/{archive_id}/f3d")
  5008. async def download_f3d(
  5009. archive_id: int,
  5010. db: AsyncSession = Depends(get_db),
  5011. auth_result: tuple[User | None, bool] = Depends(
  5012. require_ownership_permission(
  5013. Permission.ARCHIVES_READ_ALL,
  5014. Permission.ARCHIVES_READ_OWN,
  5015. )
  5016. ),
  5017. printer_scope: PrinterScope = RequestPrinterScope,
  5018. ):
  5019. """Download the Fusion 360 design file."""
  5020. user, can_read_all = auth_result
  5021. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  5022. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_read_all, printer_scope)
  5023. if not archive.f3d_path:
  5024. raise HTTPException(404, "No F3D file attached to this archive")
  5025. f3d_path = settings.base_dir / archive.f3d_path
  5026. if not f3d_path.exists():
  5027. raise HTTPException(404, "F3D file not found on disk")
  5028. # Use the actual filename from the path
  5029. filename = f3d_path.name
  5030. return FileResponse(
  5031. path=f3d_path,
  5032. filename=filename,
  5033. media_type="application/octet-stream",
  5034. )
  5035. @router.delete("/{archive_id}/f3d")
  5036. async def delete_f3d(
  5037. archive_id: int,
  5038. db: AsyncSession = Depends(get_db),
  5039. auth_result: tuple[User | None, bool] = Depends(
  5040. require_ownership_permission(
  5041. Permission.ARCHIVES_DELETE_ALL,
  5042. Permission.ARCHIVES_DELETE_OWN,
  5043. )
  5044. ),
  5045. printer_scope: PrinterScope = RequestPrinterScope,
  5046. ):
  5047. """Delete the Fusion 360 design file from an archive."""
  5048. user, can_modify_all = auth_result
  5049. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  5050. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  5051. if not archive.f3d_path:
  5052. raise HTTPException(404, "No F3D file attached to this archive")
  5053. # Delete the file
  5054. f3d_path = settings.base_dir / archive.f3d_path
  5055. if f3d_path.exists():
  5056. f3d_path.unlink()
  5057. # Clear the path in database
  5058. archive.f3d_path = None
  5059. await db.commit()
  5060. return {"status": "deleted"}