archives.py 240 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831
  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, 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 day_zone, local_date_end, local_date_start, 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 | ApiKeyActor | 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_run_user_filter(conditions: list, created_by_id: int | None):
  214. """Append created_by_id filter scoped to PrintLogEntry rows."""
  215. from backend.app.models.print_log import PrintLogEntry
  216. if created_by_id is not None:
  217. if created_by_id == -1:
  218. conditions.append(PrintLogEntry.created_by_id.is_(None))
  219. else:
  220. conditions.append(PrintLogEntry.created_by_id == created_by_id)
  221. def compute_time_accuracy(archive: PrintArchive, run_aggregate: dict | None = None) -> dict:
  222. """Compute actual print time and accuracy for an archive.
  223. Returns dict with actual_time_seconds and time_accuracy.
  224. time_accuracy = (estimated / actual) * 100
  225. - 100% = perfect estimate
  226. - >100% = print was faster than estimated
  227. - <100% = print took longer than estimated
  228. When ``run_aggregate`` indicates the archive has more than one logged
  229. run (multi-plate file printed plate-by-plate, or reprints), both
  230. fields are suppressed: ``archive.started_at / completed_at`` reflect
  231. the LATEST run only, while ``archive.print_time_seconds`` is the
  232. whole-file estimate (post-#1593 the parser sums across plates), so
  233. comparing the two describes different scopes. The card-rendering
  234. frontend falls through to ``archive.print_time_seconds`` for the
  235. time display and hides the badge when ``time_accuracy`` is null —
  236. that's the desired "show estimate, no badge" presentation for
  237. multi-run archives (#1608). Single-run archives keep the original
  238. badge behaviour verbatim.
  239. """
  240. result: dict[str, int | float | None] = {"actual_time_seconds": None, "time_accuracy": None}
  241. # Multi-run archives: the per-run actual (started_at..completed_at on
  242. # the archive row) is incommensurable with the whole-file estimate.
  243. # Both fields are cleared so the card shows estimate + no badge.
  244. if run_aggregate and (run_aggregate.get("run_count") or 0) > 1:
  245. return result
  246. if archive.started_at and archive.completed_at and archive.status == "completed":
  247. actual_seconds = int((archive.completed_at - archive.started_at).total_seconds())
  248. if actual_seconds > 0:
  249. result["actual_time_seconds"] = actual_seconds
  250. if archive.print_time_seconds and archive.print_time_seconds > 0:
  251. # Calculate accuracy as percentage
  252. accuracy = (archive.print_time_seconds / actual_seconds) * 100
  253. # Sanity check: skip unreasonable values (e.g., manually changed status)
  254. # Valid range: 5% to 500% (print took 20x longer to 5x faster than estimated)
  255. if 5 <= accuracy <= 500:
  256. result["time_accuracy"] = round(accuracy, 1)
  257. return result
  258. async def _load_run_aggregates(db: AsyncSession, archive_ids: list[int]) -> dict[int, dict]:
  259. """Batch-load per-archive run aggregates from PrintLogEntry.
  260. Returns ``{archive_id: {run_count, last_run_at, total_filament_actual_grams,
  261. successful_run_count, failed_run_count}}``. Archives with no logged runs are
  262. absent from the map; callers should treat that as zero/none.
  263. """
  264. from backend.app.models.print_log import PrintLogEntry
  265. if not archive_ids:
  266. return {}
  267. rows = await db.execute(
  268. select(
  269. PrintLogEntry.archive_id,
  270. func.count(PrintLogEntry.id).label("run_count"),
  271. func.max(PrintLogEntry.started_at).label("last_run_at"),
  272. func.coalesce(func.sum(PrintLogEntry.filament_used_grams), 0).label("total_filament"),
  273. func.sum(case((PrintLogEntry.status == "completed", 1), else_=0)).label("successful"),
  274. func.sum(case((PrintLogEntry.status == "failed", 1), else_=0)).label("failed"),
  275. )
  276. .where(PrintLogEntry.archive_id.in_(archive_ids))
  277. .group_by(PrintLogEntry.archive_id)
  278. )
  279. aggregates: dict[int, dict] = {}
  280. for archive_id, run_count, last_run_at, total_filament, successful, failed in rows.all():
  281. aggregates[archive_id] = {
  282. "run_count": int(run_count or 0),
  283. "last_run_at": last_run_at,
  284. "total_filament_actual_grams": float(total_filament) if total_filament else None,
  285. "successful_run_count": int(successful or 0),
  286. "failed_run_count": int(failed or 0),
  287. }
  288. return aggregates
  289. def archive_to_response(
  290. archive: PrintArchive,
  291. duplicates: list[dict] | None = None,
  292. duplicate_count: int = 0,
  293. duplicate_sequence: int = 0,
  294. original_archive_id: int | None = None,
  295. run_aggregate: dict | None = None,
  296. ) -> dict:
  297. """Convert archive model to response dict with computed fields."""
  298. data = {
  299. "id": archive.id,
  300. "printer_id": archive.printer_id,
  301. "project_id": archive.project_id,
  302. "project_name": archive.project.name if archive.project else None,
  303. "filename": archive.filename,
  304. "file_path": archive.file_path,
  305. "file_size": archive.file_size,
  306. "content_hash": archive.content_hash,
  307. "thumbnail_path": archive.thumbnail_path,
  308. "timelapse_path": archive.timelapse_path,
  309. "source_3mf_path": archive.source_3mf_path,
  310. "f3d_path": archive.f3d_path,
  311. "duplicates": duplicates,
  312. "duplicate_count": duplicate_count if duplicates is None else len(duplicates),
  313. "duplicate_sequence": duplicate_sequence,
  314. "original_archive_id": original_archive_id,
  315. "print_name": archive.print_name,
  316. "plate_id": archive.plate_id,
  317. "print_time_seconds": archive.print_time_seconds,
  318. "filament_used_grams": archive.filament_used_grams,
  319. "filament_type": archive.filament_type,
  320. "filament_color": archive.filament_color,
  321. "layer_height": archive.layer_height,
  322. "total_layers": archive.total_layers,
  323. "nozzle_diameter": archive.nozzle_diameter,
  324. "bed_temperature": archive.bed_temperature,
  325. "bed_type": archive.bed_type,
  326. "nozzle_temperature": archive.nozzle_temperature,
  327. "sliced_for_model": archive.sliced_for_model,
  328. "status": archive.status,
  329. "started_at": archive.started_at,
  330. "completed_at": archive.completed_at,
  331. "extra_data": archive.extra_data,
  332. "makerworld_url": archive.makerworld_url,
  333. "designer": archive.designer,
  334. "external_url": archive.external_url,
  335. "is_favorite": archive.is_favorite,
  336. "tags": archive.tags,
  337. "notes": archive.notes,
  338. "cost": archive.cost,
  339. "photos": archive.photos,
  340. "failure_reason": archive.failure_reason,
  341. # Post-print outcome confirmation (#1898). confirm_token stays
  342. # server-side — it is a capability and never belongs in a response.
  343. "user_verdict": archive.user_verdict,
  344. "user_verdict_source": archive.user_verdict_source,
  345. "user_verdict_at": archive.user_verdict_at,
  346. # bool() because the column is nullable to match the migration; the
  347. # response contract stays a strict bool either way.
  348. "confirm_requested": bool(archive.confirm_requested),
  349. "quantity": archive.quantity,
  350. "energy_kwh": archive.energy_kwh,
  351. "energy_cost": archive.energy_cost,
  352. "wear_cost": archive.wear_cost,
  353. "created_at": archive.created_at,
  354. # User tracking (Issue #206)
  355. "created_by_id": archive.created_by_id,
  356. "created_by_username": archive.created_by.username if archive.created_by else None,
  357. }
  358. # Add computed time accuracy fields. ``run_aggregate`` lets
  359. # ``compute_time_accuracy`` suppress the badge for multi-run archives
  360. # where the per-run actual / whole-file estimate scopes don't match
  361. # (#1608).
  362. accuracy_data = compute_time_accuracy(archive, run_aggregate)
  363. data.update(accuracy_data)
  364. if run_aggregate:
  365. data["run_count"] = run_aggregate.get("run_count", 0)
  366. data["last_run_at"] = run_aggregate.get("last_run_at")
  367. data["total_filament_actual_grams"] = run_aggregate.get("total_filament_actual_grams")
  368. data["successful_run_count"] = run_aggregate.get("successful_run_count", 0)
  369. data["failed_run_count"] = run_aggregate.get("failed_run_count", 0)
  370. return data
  371. @router.get("/", response_model=list[ArchiveResponse])
  372. async def list_archives(
  373. printer_id: int | None = None,
  374. project_id: int | None = None,
  375. date_from: date | None = Query(None),
  376. date_to: date | None = Query(None),
  377. tz: str | None = Query(
  378. None, max_length=64, description="IANA zone the dates are calendar days in; the server's when omitted"
  379. ),
  380. limit: int = 50,
  381. offset: int = 0,
  382. db: AsyncSession = Depends(get_db),
  383. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  384. require_ownership_permission(
  385. Permission.ARCHIVES_READ_ALL,
  386. Permission.ARCHIVES_READ_OWN,
  387. )
  388. ),
  389. printer_scope: PrinterScope = RequestPrinterScope,
  390. ):
  391. """List archived prints."""
  392. user, can_read_all = auth_result
  393. visible_to_user_id = user.id if (user is not None and not can_read_all) else None
  394. service = ArchiveService(db)
  395. archives = await service.list_archives(
  396. printer_id=printer_id,
  397. project_id=project_id,
  398. date_from=date_from,
  399. date_to=date_to,
  400. limit=limit,
  401. offset=offset,
  402. visible_to_user_id=visible_to_user_id,
  403. printer_scope=printer_scope,
  404. day_zone=day_zone(tz),
  405. )
  406. # Get sets of duplicate hashes and duplicate (name, hash) pairs (efficient single queries),
  407. # counted among the archives this caller may see, as the duplicates view lists them.
  408. visible = _visible_archive_clauses(user, can_read_all, printer_scope)
  409. duplicate_hashes, duplicate_name_hash_pairs = await service.get_duplicate_hashes_and_names(visible)
  410. # Batch-load duplicate groups once for the current page keys.
  411. duplicate_hashes_in_page = {
  412. a.content_hash for a in archives if a.content_hash and a.content_hash in duplicate_hashes
  413. }
  414. duplicate_name_hash_keys_in_page = {
  415. (a.print_name.lower(), a.content_hash)
  416. for a in archives
  417. if a.print_name and a.content_hash and (a.print_name.lower(), a.content_hash) in duplicate_name_hash_pairs
  418. }
  419. duplicate_meta_by_archive_id: dict[int, tuple[int, int, int]] = {}
  420. if duplicate_hashes_in_page or duplicate_name_hash_keys_in_page:
  421. duplicate_group_conditions = []
  422. if duplicate_hashes_in_page:
  423. duplicate_group_conditions.append(PrintArchive.content_hash.in_(duplicate_hashes_in_page))
  424. if duplicate_name_hash_keys_in_page:
  425. name_hash_conditions = [
  426. and_(func.lower(PrintArchive.print_name) == name, PrintArchive.content_hash == hash_)
  427. for name, hash_ in duplicate_name_hash_keys_in_page
  428. ]
  429. duplicate_group_conditions.extend(name_hash_conditions)
  430. duplicate_group_rows = await db.execute(
  431. select(
  432. PrintArchive.id,
  433. PrintArchive.created_at,
  434. PrintArchive.content_hash,
  435. func.lower(PrintArchive.print_name).label("print_name_lower"),
  436. ).where(or_(*duplicate_group_conditions), PrintArchive.deleted_at.is_(None), *visible)
  437. )
  438. duplicate_groups_by_hash: dict[str, list[tuple[int, datetime]]] = defaultdict(list)
  439. duplicate_groups_by_name_hash: dict[tuple[str, str], list[tuple[int, datetime]]] = defaultdict(list)
  440. for archive_id, created_at, content_hash, print_name_lower in duplicate_group_rows.all():
  441. if content_hash and content_hash in duplicate_hashes_in_page:
  442. duplicate_groups_by_hash[content_hash].append((archive_id, created_at))
  443. if (
  444. print_name_lower
  445. and content_hash
  446. and (print_name_lower, content_hash) in duplicate_name_hash_keys_in_page
  447. ):
  448. duplicate_groups_by_name_hash[(print_name_lower, content_hash)].append((archive_id, created_at))
  449. for group in duplicate_groups_by_hash.values():
  450. if len(group) < 2:
  451. continue
  452. group.sort(key=lambda x: x[1])
  453. original_id = group[0][0]
  454. duplicate_count = len(group) - 1
  455. for sequence, (archive_id, _) in enumerate(group):
  456. duplicate_meta_by_archive_id[archive_id] = (sequence, original_id, duplicate_count)
  457. # Keep hash-based grouping precedence; name/hash groups only fill missing items.
  458. for group in duplicate_groups_by_name_hash.values():
  459. if len(group) < 2:
  460. continue
  461. group.sort(key=lambda x: x[1])
  462. original_id = group[0][0]
  463. duplicate_count = len(group) - 1
  464. for sequence, (archive_id, _) in enumerate(group):
  465. duplicate_meta_by_archive_id.setdefault(archive_id, (sequence, original_id, duplicate_count))
  466. run_aggregates = await _load_run_aggregates(db, [a.id for a in archives])
  467. # Build response with duplicate sequence and original archive ID pre-computed
  468. result = []
  469. for a in archives:
  470. has_hash_dup = a.content_hash in duplicate_hashes if a.content_hash else False
  471. has_name_dup = (
  472. bool(a.print_name and a.content_hash)
  473. and (a.print_name.lower(), a.content_hash) in duplicate_name_hash_pairs
  474. )
  475. has_duplicate = has_hash_dup or has_name_dup
  476. # Pre-compute duplicate sequence and original archive ID
  477. duplicate_sequence = 0
  478. original_archive_id: int | None = None
  479. duplicate_count = 1 if has_duplicate else 0
  480. if has_duplicate and a.id in duplicate_meta_by_archive_id:
  481. duplicate_sequence, original_archive_id, duplicate_count = duplicate_meta_by_archive_id[a.id]
  482. result.append(
  483. archive_to_response(
  484. a,
  485. duplicate_count=duplicate_count,
  486. duplicate_sequence=duplicate_sequence,
  487. original_archive_id=original_archive_id,
  488. run_aggregate=run_aggregates.get(a.id),
  489. )
  490. )
  491. return result
  492. @router.get("/last-per-printer", response_model=list[ArchiveResponse])
  493. async def list_last_archive_per_printer(
  494. db: AsyncSession = Depends(get_db),
  495. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  496. require_ownership_permission(
  497. Permission.ARCHIVES_READ_ALL,
  498. Permission.ARCHIVES_READ_OWN,
  499. )
  500. ),
  501. printer_scope: PrinterScope = RequestPrinterScope,
  502. ):
  503. """The most recent archive of every printer, for the printer cards.
  504. One query for the whole Printers page instead of one list request per card.
  505. Leaves out the duplicate detection the full listing does: the card shows a
  506. name and an outcome prompt, and on a farm that scan ran once per printer
  507. on every page load.
  508. """
  509. user, can_read_all = auth_result
  510. filters = [PrintArchive.deleted_at.is_(None), PrintArchive.printer_id.isnot(None)]
  511. if user is not None and not can_read_all:
  512. filters.append(PrintArchive.created_by_id == user.id)
  513. # Only printers the caller may see (#1727)
  514. if (clause := printer_scope.where(PrintArchive.printer_id)) is not None:
  515. filters.append(clause)
  516. ranked = (
  517. select(
  518. PrintArchive.id,
  519. func.row_number()
  520. .over(
  521. partition_by=PrintArchive.printer_id,
  522. # A reprint reuses its archive row, moving it to the printer it
  523. # runs on with a fresh started_at, so the latest run start, not
  524. # the row's age, tells which print a printer ran last.
  525. order_by=(
  526. func.coalesce(PrintArchive.started_at, PrintArchive.created_at).desc(),
  527. PrintArchive.id.desc(),
  528. ),
  529. )
  530. .label("rn"),
  531. )
  532. .where(*filters)
  533. .subquery()
  534. )
  535. from sqlalchemy.orm import selectinload
  536. result = await db.execute(
  537. select(PrintArchive)
  538. .options(selectinload(PrintArchive.project), selectinload(PrintArchive.created_by))
  539. .where(PrintArchive.id.in_(select(ranked.c.id).where(ranked.c.rn == 1)))
  540. .order_by(PrintArchive.printer_id)
  541. )
  542. archives = list(result.scalars().all())
  543. run_aggregates = await _load_run_aggregates(db, [a.id for a in archives])
  544. return [archive_to_response(a, run_aggregate=run_aggregates.get(a.id)) for a in archives]
  545. @router.get("/no-3mf-warning")
  546. async def no_3mf_warning(
  547. db: AsyncSession = Depends(get_db),
  548. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  549. require_ownership_permission(
  550. Permission.ARCHIVES_READ_ALL,
  551. Permission.ARCHIVES_READ_OWN,
  552. )
  553. ),
  554. printer_scope: PrinterScope = RequestPrinterScope,
  555. ):
  556. """Whether to nudge the user about a print that archived without its 3MF,
  557. and why. True iff any archive in the last 30 days was created via the
  558. no-3MF fallback path.
  559. Also returns ``reason``, because the advice differs and the original
  560. single-cause wording sent people the wrong way. Historically the only
  561. known cause was install step 4 ("Store sent files on external storage")
  562. being off in the slicer, so the banner said so unconditionally. On
  563. H2-series, P2S and X2D that advice is actively wrong: the setting is already
  564. on and turning it on again changes nothing, because the printer keeps the
  565. sliced file on internal storage that FTPS does not serve at all (#2780).
  566. ``reason`` is the slug from :mod:`print_storage` when we recorded one,
  567. else None for the original slicer-setting case. When archives disagree the
  568. most specific known reason wins — one printer storing internally is a real
  569. finding worth explaining, and it should not be masked by another printer's
  570. plain missing-file fallback.
  571. Complements the connection-diagnostic ``external_storage`` check, which
  572. only catches the printer-side variant of the setting. On older slicers
  573. where the toggle lives only in BambuStudio, the printer never reports it
  574. and the diagnostic passes — this endpoint surfaces the symptom instead.
  575. Dismissal is handled client-side via localStorage (one-shot): once the
  576. user has been told, no further nudge until they clear browser storage.
  577. The backend stays stateless.
  578. """
  579. user, can_read_all = auth_result
  580. cutoff = datetime.now(timezone.utc) - timedelta(days=30)
  581. conditions = [
  582. PrintArchive.created_at >= cutoff,
  583. PrintArchive.deleted_at.is_(None),
  584. PrintArchive.extra_data.isnot(None),
  585. ]
  586. if user is not None and not can_read_all:
  587. conditions.append(PrintArchive.created_by_id == user.id)
  588. if (scope_clause := printer_scope.where(PrintArchive.printer_id)) is not None:
  589. conditions.append(scope_clause)
  590. result = await db.execute(select(PrintArchive.extra_data).where(*conditions))
  591. reasons: set[str] = set()
  592. has_fallback = False
  593. for (extra_data,) in result.all():
  594. if not extra_data or not extra_data.get("no_3mf_available"):
  595. continue
  596. has_fallback = True
  597. reason = extra_data.get("no_3mf_reason")
  598. if reason:
  599. reasons.add(reason)
  600. if not has_fallback:
  601. return {"has_fallback": False, "reason": None}
  602. # Most specific first. Archives predating this field carry no reason at
  603. # all, so an install with one H2C and three older printers still gets the
  604. # H2C explanation rather than the generic one.
  605. #
  606. # REASON_FTPS_COOLOFF leads, and it is the only one of these that reports a
  607. # fault rather than a choice: the printer's file service refused a TLS
  608. # handshake, so the sweep never ran and nothing about where the file went
  609. # was ever tested. The other three describe an install working as
  610. # configured, and each ends in something the operator can change. This one
  611. # ends in "your printer is doing something we cannot yet explain", which is
  612. # both the more urgent thing to say and the thing that produces a useful
  613. # report. It also has to outrank them because the banner dismisses one-shot
  614. # into localStorage: a reason ranked below another is not merely deferred,
  615. # it is never shown to that user again (#2780).
  616. #
  617. # Ranking it first cannot mask a permanent cause, because a cool-off row is
  618. # not permanent. The retry #2957 schedules clears the row's markers when it
  619. # lands, so a row still carrying this slug is one where the retry failed too
  620. # -- a printer whose file service is still refusing, days later.
  621. #
  622. # REASON_FTP_TRANSFER_FAILED sits second for the same reasons and one more:
  623. # it is the only slug here whose remedy is a Bambuddy setting rather than a
  624. # slicer one or a card. It ranks below the cool-off because a printer that
  625. # will not complete a TLS handshake is the worse fault of the two, and its
  626. # own retry (#3063) clears the row the same way, so a row still carrying
  627. # this slug is one where three later attempts also ran out of time.
  628. #
  629. # REASON_INTERNAL_HISTORY comes last on purpose, even though it is the
  630. # narrowest: it is the one cause with no remedy at all -- the file was
  631. # already on the printer, in an area port 990 does not serve. The two ahead
  632. # of it each end in something the operator can do, so when an install has
  633. # both, the actionable explanation is the one worth the banner (#1820).
  634. for candidate in (
  635. REASON_FTPS_COOLOFF,
  636. REASON_FTP_TRANSFER_FAILED,
  637. REASON_INTERNAL_STORAGE,
  638. REASON_NO_EXTERNAL_STORAGE,
  639. REASON_INTERNAL_HISTORY,
  640. ):
  641. if candidate in reasons:
  642. return {"has_fallback": True, "reason": candidate}
  643. return {"has_fallback": True, "reason": None}
  644. @router.get("/slim", response_model=list[ArchiveSlim])
  645. async def list_archives_slim(
  646. date_from: date | None = Query(None),
  647. date_to: date | None = Query(None),
  648. tz: str | None = Query(
  649. None, max_length=64, description="IANA zone the dates are calendar days in; the server's when omitted"
  650. ),
  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 | ApiKeyActor | 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 = local_date_start(date_from, day_zone(tz))
  685. filters.append(PrintLogEntry.created_at >= dt_from)
  686. if date_to:
  687. dt_to = local_date_end(date_to, day_zone(tz))
  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 | ApiKeyActor | 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. tz: str | None = Query(
  919. None, max_length=64, description="IANA zone the dates are calendar days in; the server's when omitted"
  920. ),
  921. printer_id: int | None = None,
  922. project_id: int | None = None,
  923. created_by_id: int | None = Query(None, description="Filter by user who created the print (-1 for no user)"),
  924. db: AsyncSession = Depends(get_db),
  925. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  926. require_ownership_permission(
  927. Permission.ARCHIVES_READ_ALL,
  928. Permission.ARCHIVES_READ_OWN,
  929. )
  930. ),
  931. printer_scope: PrinterScope = RequestPrinterScope,
  932. ):
  933. """Analyze failure patterns across prints.
  934. Returns failure statistics including:
  935. - Overall failure rate
  936. - Failures by reason, filament type, printer
  937. - Time of day distribution
  938. - Recent failures
  939. - Weekly trend
  940. """
  941. current_user, can_read_all = auth_result
  942. _validate_user_filter_permission(current_user, created_by_id)
  943. # Callers without ARCHIVES_READ_ALL are scoped to their own runs (#2).
  944. if current_user is not None and not can_read_all:
  945. created_by_id = current_user.id
  946. from backend.app.services.failure_analysis import FailureAnalysisService
  947. service = FailureAnalysisService(db)
  948. return await service.analyze_failures(
  949. days=days,
  950. date_from=date_from,
  951. date_to=date_to,
  952. printer_id=printer_id,
  953. project_id=project_id,
  954. created_by_id=created_by_id,
  955. printer_scope=printer_scope,
  956. day_zone=day_zone(tz),
  957. )
  958. @router.get("/compare")
  959. async def compare_archives(
  960. archive_ids: str = Query(..., description="Comma-separated archive IDs (2-5)"),
  961. db: AsyncSession = Depends(get_db),
  962. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  963. require_ownership_permission(
  964. Permission.ARCHIVES_READ_ALL,
  965. Permission.ARCHIVES_READ_OWN,
  966. )
  967. ),
  968. printer_scope: PrinterScope = RequestPrinterScope,
  969. ):
  970. """Compare multiple archives side by side.
  971. Compares print settings, filament usage, and print times.
  972. Also analyzes correlation between settings and success/failure.
  973. Args:
  974. archive_ids: Comma-separated list of 2-5 archive IDs to compare
  975. """
  976. from backend.app.services.archive_comparison import ArchiveComparisonService
  977. user, can_read_all = auth_result
  978. # Parse and validate archive IDs
  979. try:
  980. ids = [int(id.strip()) for id in archive_ids.split(",")]
  981. except ValueError:
  982. raise HTTPException(400, "Invalid archive IDs format")
  983. if len(ids) < 2:
  984. raise HTTPException(400, "At least 2 archives required for comparison")
  985. if len(ids) > 5:
  986. raise HTTPException(400, "Maximum 5 archives can be compared at once")
  987. # Verify the caller is allowed to see every archive in the comparison —
  988. # one not-owned id in the list would otherwise leak its full detail block.
  989. # _ensure_archive_visible raises 404 on the first miss (same 404 the
  990. # single-archive endpoint would return).
  991. if user is not None and not can_read_all:
  992. existing = await db.execute(
  993. select(PrintArchive.id, PrintArchive.created_by_id, PrintArchive.deleted_at).where(PrintArchive.id.in_(ids))
  994. )
  995. owners_by_id = {row.id: row for row in existing.all()}
  996. for archive_id in ids:
  997. row = owners_by_id.get(archive_id)
  998. if row is None or row.deleted_at is not None or row.created_by_id != user.id:
  999. raise HTTPException(404, "Archive not found")
  1000. # Every compared archive must come from a printer the caller can see (#1727)
  1001. if not printer_scope.is_unrestricted:
  1002. printer_ids = await db.execute(select(PrintArchive.printer_id).where(PrintArchive.id.in_(ids)))
  1003. if not all(printer_scope.allows(pid) for (pid,) in printer_ids.all()):
  1004. raise HTTPException(404, "Archive not found")
  1005. service = ArchiveComparisonService(db)
  1006. try:
  1007. return await service.compare_archives(ids)
  1008. except ValueError as e:
  1009. raise HTTPException(400, str(e))
  1010. @router.get("/export")
  1011. async def export_archives(
  1012. format: str = Query("csv", description="Export format: csv or xlsx"),
  1013. fields: str | None = Query(None, description="Comma-separated field names"),
  1014. printer_id: int | None = None,
  1015. project_id: int | None = None,
  1016. status: str | None = None,
  1017. date_from: str | None = Query(None, description="Start date (ISO format)"),
  1018. date_to: str | None = Query(None, description="End date (ISO format)"),
  1019. tz: str | None = Query(
  1020. None, max_length=64, description="IANA zone the dates are calendar days in; the server's when omitted"
  1021. ),
  1022. search: str | None = None,
  1023. db: AsyncSession = Depends(get_db),
  1024. printer_scope: PrinterScope = RequestPrinterScope,
  1025. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1026. require_ownership_permission(
  1027. Permission.ARCHIVES_READ_ALL,
  1028. Permission.ARCHIVES_READ_OWN,
  1029. )
  1030. ),
  1031. ):
  1032. """Export archives to CSV or Excel format.
  1033. Returns a downloadable file with archive data.
  1034. """
  1035. from datetime import datetime
  1036. from fastapi.responses import StreamingResponse
  1037. from backend.app.services.export import ExportService
  1038. user, can_read_all = auth_result
  1039. visible_to_user_id = user.id if (user is not None and not can_read_all) else None
  1040. if format not in ("csv", "xlsx"):
  1041. raise HTTPException(400, "Format must be 'csv' or 'xlsx'")
  1042. # Parse fields
  1043. field_list = None
  1044. if fields:
  1045. field_list = [f.strip() for f in fields.split(",")]
  1046. # Parse dates
  1047. date_from_dt = None
  1048. date_to_dt = None
  1049. if date_from:
  1050. try:
  1051. # A date on its own is a local calendar day (DB-C12).
  1052. if len(date_from) == 10:
  1053. date_from_dt = to_naive_utc(local_date_start(date.fromisoformat(date_from), day_zone(tz)))
  1054. else:
  1055. date_from_dt = to_naive_utc(datetime.fromisoformat(date_from))
  1056. except (ValueError, OverflowError):
  1057. raise HTTPException(400, "Invalid date_from format")
  1058. if date_to:
  1059. try:
  1060. # A date on its own ends with that day, as every other date filter
  1061. # here reads it; read as midnight it left out the whole day.
  1062. if len(date_to) == 10:
  1063. date_to_dt = to_naive_utc(local_date_end(date.fromisoformat(date_to), day_zone(tz)))
  1064. else:
  1065. date_to_dt = to_naive_utc(datetime.fromisoformat(date_to))
  1066. except (ValueError, OverflowError):
  1067. raise HTTPException(400, "Invalid date_to format")
  1068. service = ExportService(db)
  1069. try:
  1070. file_bytes, filename, content_type = await service.export_archives(
  1071. format=format,
  1072. fields=field_list,
  1073. printer_id=printer_id,
  1074. project_id=project_id,
  1075. status=status,
  1076. date_from=date_from_dt,
  1077. date_to=date_to_dt,
  1078. search=search,
  1079. visible_to_user_id=visible_to_user_id,
  1080. printer_scope=printer_scope,
  1081. )
  1082. except ImportError as e:
  1083. raise HTTPException(500, str(e))
  1084. return StreamingResponse(
  1085. io.BytesIO(file_bytes),
  1086. media_type=content_type,
  1087. headers={"Content-Disposition": build_content_disposition(filename)},
  1088. )
  1089. @router.get("/stats/export")
  1090. async def export_stats(
  1091. format: str = Query("csv", description="Export format: csv or xlsx"),
  1092. days: int = 30,
  1093. printer_id: int | None = None,
  1094. project_id: int | None = None,
  1095. created_by_id: int | None = Query(None, description="Filter by user who created the print (-1 for no user)"),
  1096. db: AsyncSession = Depends(get_db),
  1097. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.STATS_READ),
  1098. printer_scope: PrinterScope = RequestPrinterScope,
  1099. actor: User | ApiKeyActor | None = RequestActor,
  1100. ):
  1101. """Export statistics summary to CSV or Excel format."""
  1102. _validate_user_filter_permission(actor, created_by_id)
  1103. from fastapi.responses import StreamingResponse
  1104. from backend.app.services.export import ExportService
  1105. if format not in ("csv", "xlsx"):
  1106. raise HTTPException(400, "Format must be 'csv' or 'xlsx'")
  1107. service = ExportService(db)
  1108. try:
  1109. file_bytes, filename, content_type = await service.export_stats(
  1110. format=format,
  1111. days=days,
  1112. printer_id=printer_id,
  1113. project_id=project_id,
  1114. created_by_id=created_by_id,
  1115. printer_scope=printer_scope,
  1116. )
  1117. except ImportError as e:
  1118. raise HTTPException(500, str(e))
  1119. return StreamingResponse(
  1120. io.BytesIO(file_bytes),
  1121. media_type=content_type,
  1122. headers={"Content-Disposition": build_content_disposition(filename)},
  1123. )
  1124. @router.get("/stats", response_model=ArchiveStats)
  1125. async def get_archive_stats(
  1126. date_from: date | None = Query(None, description="Start date (inclusive), YYYY-MM-DD"),
  1127. date_to: date | None = Query(None, description="End date (inclusive), YYYY-MM-DD"),
  1128. tz: str | None = Query(
  1129. None, max_length=64, description="IANA zone the dates are calendar days in; the server's when omitted"
  1130. ),
  1131. created_by_id: int | None = Query(None, description="Filter by user who created the print (-1 for no user)"),
  1132. db: AsyncSession = Depends(get_db),
  1133. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.STATS_READ),
  1134. printer_scope: PrinterScope = RequestPrinterScope,
  1135. actor: User | ApiKeyActor | None = RequestActor,
  1136. ):
  1137. """Get statistics across all archives.
  1138. Stats aggregate over PrintLogEntry (one row per print event), not over
  1139. PrintArchive (one row per file). A reprint contributes a new PrintLogEntry
  1140. so its filament/cost/time/energy add to the totals instead of overwriting
  1141. the source archive's first-run values (#1378).
  1142. """
  1143. from backend.app.models.print_log import PrintLogEntry
  1144. _validate_user_filter_permission(actor, created_by_id)
  1145. # Build date filter conditions scoped to PrintLogEntry (event-time).
  1146. base_conditions = []
  1147. if date_from:
  1148. dt_from = local_date_start(date_from, day_zone(tz))
  1149. base_conditions.append(PrintLogEntry.created_at >= dt_from)
  1150. if date_to:
  1151. dt_to = local_date_end(date_to, day_zone(tz))
  1152. base_conditions.append(PrintLogEntry.created_at <= dt_to)
  1153. _apply_run_user_filter(base_conditions, created_by_id)
  1154. # Only prints on printers the caller may see (#1727)
  1155. if (clause := printer_scope.where(PrintLogEntry.printer_id)) is not None:
  1156. base_conditions.append(clause)
  1157. # Total counts (one row per print event).
  1158. total_result = await db.execute(select(func.count(PrintLogEntry.id)).where(*base_conditions))
  1159. total_prints = total_result.scalar() or 0
  1160. successful_result = await db.execute(
  1161. select(func.count(PrintLogEntry.id)).where(PrintLogEntry.status == "completed", *base_conditions)
  1162. )
  1163. successful_prints = successful_result.scalar() or 0
  1164. failed_result = await db.execute(
  1165. select(func.count(PrintLogEntry.id)).where(PrintLogEntry.status.in_(("failed", "aborted")), *base_conditions)
  1166. )
  1167. failed_prints = failed_result.scalar() or 0
  1168. # User/system-stopped prints — stopped/cancelled/skipped are distinct from
  1169. # quality failures: the user (or the queue) interrupted them, the printer
  1170. # didn't detect a fault. Bucketed separately so the Success Rate gauge
  1171. # divides by completed + failed only (a cancelled print shouldn't drag
  1172. # the gauge down), while still being visible in the breakdown so they
  1173. # don't silently vanish from Total Prints (#1390).
  1174. cancelled_result = await db.execute(
  1175. select(func.count(PrintLogEntry.id)).where(
  1176. PrintLogEntry.status.in_(("stopped", "cancelled", "skipped")), *base_conditions
  1177. )
  1178. )
  1179. cancelled_prints = cancelled_result.scalar() or 0
  1180. # Total elapsed time — PrintLogEntry stores duration_seconds directly so we
  1181. # can sum it server-side. Rows missing duration fall back to the slicer
  1182. # estimate from the archive (joined for that case only).
  1183. time_rows = await db.execute(
  1184. select(
  1185. PrintLogEntry.duration_seconds,
  1186. PrintLogEntry.started_at,
  1187. PrintLogEntry.completed_at,
  1188. ).where(*base_conditions)
  1189. )
  1190. total_seconds = 0
  1191. for duration_seconds, started_at, completed_at in time_rows.all():
  1192. # Trust an explicitly stored duration, INCLUDING 0: a reconciled abort
  1193. # stores 0 on purpose because its real end time is unknown, and the
  1194. # started_at→completed_at fallback would otherwise bank the whole
  1195. # multi-day disconnect gap as print time (#2592). Only rows with a NULL
  1196. # duration (legacy entries that never recorded one) fall back.
  1197. if duration_seconds is not None:
  1198. total_seconds += duration_seconds
  1199. elif started_at and completed_at:
  1200. elapsed = (completed_at - started_at).total_seconds()
  1201. if elapsed > 0:
  1202. total_seconds += int(elapsed)
  1203. total_time = total_seconds / 3600 # Convert to hours
  1204. filament_result = await db.execute(
  1205. select(func.coalesce(func.sum(PrintLogEntry.filament_used_grams), 0)).where(*base_conditions)
  1206. )
  1207. total_filament = filament_result.scalar() or 0
  1208. cost_result = await db.execute(select(func.sum(PrintLogEntry.cost)).where(*base_conditions))
  1209. total_cost = cost_result.scalar() or 0
  1210. wear_result = await db.execute(select(func.sum(PrintLogEntry.wear_cost)).where(*base_conditions))
  1211. total_wear_cost = wear_result.scalar() or 0
  1212. # By filament type (split comma-separated values for multi-material prints)
  1213. filament_type_result = await db.execute(
  1214. select(PrintLogEntry.filament_type).where(PrintLogEntry.filament_type.isnot(None), *base_conditions)
  1215. )
  1216. prints_by_filament: dict[str, int] = {}
  1217. for (filament_types,) in filament_type_result.all():
  1218. for ftype in filament_types.split(","):
  1219. ftype = ftype.strip()
  1220. if ftype:
  1221. prints_by_filament[ftype] = prints_by_filament.get(ftype, 0) + 1
  1222. # By printer
  1223. printer_result = await db.execute(
  1224. select(PrintLogEntry.printer_id, func.count(PrintLogEntry.id))
  1225. .where(*base_conditions)
  1226. .group_by(PrintLogEntry.printer_id)
  1227. )
  1228. prints_by_printer = {str(k): v for k, v in printer_result.all()}
  1229. # Names for printers the client can no longer look up. The breakdowns above
  1230. # key on the id each run recorded, and deleting a printer while keeping its
  1231. # history leaves that id pointing at nothing, so a chart that used to read
  1232. # "Ultron" fell back to "Printer 1" (#2873). Every run also stored the name
  1233. # it printed on, so the last one recorded is what that id was called. The
  1234. # client still prefers a live printer's current name, which keeps a rename
  1235. # showing up straight away.
  1236. last_named_run = (
  1237. select(func.max(PrintLogEntry.id).label("entry_id"))
  1238. .where(
  1239. PrintLogEntry.printer_id.isnot(None),
  1240. PrintLogEntry.printer_name.isnot(None),
  1241. *base_conditions,
  1242. )
  1243. .group_by(PrintLogEntry.printer_id)
  1244. .subquery()
  1245. )
  1246. name_result = await db.execute(
  1247. select(PrintLogEntry.printer_id, PrintLogEntry.printer_name).join(
  1248. last_named_run, PrintLogEntry.id == last_named_run.c.entry_id
  1249. )
  1250. )
  1251. printer_names = {str(printer_id): name for printer_id, name in name_result.all()}
  1252. # Time accuracy — compare each completed run's actual duration to the
  1253. # slicer's estimate on the linked archive. Runs without a linked archive
  1254. # (NULL archive_id) or without an estimate are excluded.
  1255. accuracy_rows = await db.execute(
  1256. select(
  1257. PrintLogEntry.duration_seconds,
  1258. PrintLogEntry.started_at,
  1259. PrintLogEntry.completed_at,
  1260. PrintLogEntry.printer_id,
  1261. PrintArchive.print_time_seconds,
  1262. )
  1263. .join(PrintArchive, PrintArchive.id == PrintLogEntry.archive_id)
  1264. .where(
  1265. PrintLogEntry.status == "completed",
  1266. PrintArchive.print_time_seconds.isnot(None),
  1267. *base_conditions,
  1268. )
  1269. )
  1270. # Accuracy is meaningful only when the estimate roughly describes the
  1271. # work the run actually performed. Two shapes produce wildly-off ratios
  1272. # that are pure noise:
  1273. # - multi-plate ``.gcode.3mf`` printed plate-by-plate: each run's
  1274. # actual is one plate, the archive's estimate is the sum across
  1275. # plates (post-#1593 parser fix), so the ratio is roughly N×100%
  1276. # for an N-plate file. Pre-fix this shape was also broken, just
  1277. # less dramatically — the estimate was plate-1-only so the ratio
  1278. # was meaningless rather than N×.
  1279. # - manual interventions / purge waste blowing the actual far past
  1280. # the estimate.
  1281. # Clamp to the [50%, 200%] band so the printer-level average reflects
  1282. # real slicer-vs-reality drift, not multi-plate accounting or one-off
  1283. # outliers. Single-plate archives — the case the metric is actually
  1284. # designed for — stay fully included.
  1285. _ACCURACY_BAND_LO = 50.0
  1286. _ACCURACY_BAND_HI = 200.0
  1287. average_accuracy = None
  1288. accuracy_by_printer: dict[str, float] = {}
  1289. accuracies: list[float] = []
  1290. printer_accuracies: dict[str, list[float]] = {}
  1291. for duration_seconds, started_at, completed_at, run_printer_id, estimate_seconds in accuracy_rows.all():
  1292. actual_seconds = duration_seconds
  1293. if not actual_seconds and started_at and completed_at:
  1294. elapsed = (completed_at - started_at).total_seconds()
  1295. actual_seconds = int(elapsed) if elapsed > 0 else None
  1296. if not actual_seconds or not estimate_seconds:
  1297. continue
  1298. accuracy = (estimate_seconds / actual_seconds) * 100
  1299. if accuracy < _ACCURACY_BAND_LO or accuracy > _ACCURACY_BAND_HI:
  1300. continue
  1301. accuracies.append(accuracy)
  1302. printer_key = str(run_printer_id) if run_printer_id else "unknown"
  1303. printer_accuracies.setdefault(printer_key, []).append(accuracy)
  1304. if accuracies:
  1305. average_accuracy = round(sum(accuracies) / len(accuracies), 1)
  1306. for printer_key, accs in printer_accuracies.items():
  1307. accuracy_by_printer[printer_key] = round(sum(accs) / len(accs), 1)
  1308. # Energy totals - check which mode to use
  1309. from backend.app.api.routes.settings import get_setting
  1310. from backend.app.services.energy_price import (
  1311. all_time_cost,
  1312. cost_at_average_price,
  1313. snapshot_cost,
  1314. stored_price,
  1315. )
  1316. energy_tracking_mode = await get_setting(db, "energy_tracking_mode") or "total"
  1317. # The last price known. Not read from Home Assistant here: the snapshot
  1318. # loop refreshes it hourly, and it only prices the energy since then.
  1319. energy_cost_per_kwh = await stored_price(db)
  1320. total_energy_kwh: float = 0.0
  1321. total_energy_cost: float = 0.0
  1322. energy_data_warming_up = False
  1323. if energy_tracking_mode == "total" and not date_from and not date_to:
  1324. # All-time total consumption — read live lifetime counters, costed at
  1325. # the price of each hour the snapshots cover (#1251).
  1326. total_energy_kwh = await _sum_live_plug_totals(db)
  1327. total_energy_cost = await all_time_cost(db, total_energy_kwh, energy_cost_per_kwh)
  1328. elif energy_tracking_mode == "total":
  1329. # Total consumption mode with a date filter (#941): use hourly snapshots
  1330. # to compute per-plug (endpoint - baseline) deltas.
  1331. dt_from = local_date_start(date_from, day_zone(tz)) if date_from else None
  1332. dt_to = local_date_end(date_to, day_zone(tz)) if date_to else None
  1333. total_energy_kwh, energy_data_warming_up = await _sum_snapshot_deltas(db, dt_from=dt_from, dt_to=dt_to)
  1334. costed = await snapshot_cost(db, fallback_price=energy_cost_per_kwh, dt_from=dt_from, dt_to=dt_to)
  1335. total_energy_cost = cost_at_average_price(total_energy_kwh, costed.kwh, costed.cost, energy_cost_per_kwh)
  1336. else:
  1337. # Per-print mode: sum the per-run energy column from PrintLogEntry.
  1338. energy_kwh_result = await db.execute(select(func.sum(PrintLogEntry.energy_kwh)).where(*base_conditions))
  1339. total_energy_kwh = energy_kwh_result.scalar() or 0
  1340. energy_cost_result = await db.execute(select(func.sum(PrintLogEntry.energy_cost)).where(*base_conditions))
  1341. total_energy_cost = energy_cost_result.scalar() or 0
  1342. return ArchiveStats(
  1343. total_prints=total_prints,
  1344. successful_prints=successful_prints,
  1345. failed_prints=failed_prints,
  1346. cancelled_prints=cancelled_prints,
  1347. total_print_time_hours=round(total_time, 1),
  1348. total_filament_grams=round(total_filament, 1),
  1349. total_cost=round(total_cost, 2),
  1350. prints_by_filament_type=prints_by_filament,
  1351. prints_by_printer=prints_by_printer,
  1352. printer_names=printer_names,
  1353. average_time_accuracy=average_accuracy,
  1354. time_accuracy_by_printer=accuracy_by_printer if accuracy_by_printer else None,
  1355. total_energy_kwh=round(total_energy_kwh, 3),
  1356. total_energy_cost=round(total_energy_cost, 3),
  1357. total_wear_cost=round(total_wear_cost, 2),
  1358. energy_data_warming_up=energy_data_warming_up,
  1359. )
  1360. async def _sum_live_plug_totals(db: AsyncSession) -> float:
  1361. """Sum the live lifetime counter from every smart plug.
  1362. Used for all-time "total consumption" mode. Only the current value is
  1363. available so this can't be date-filtered — use `_sum_snapshot_deltas` for
  1364. that case.
  1365. """
  1366. from backend.app.api.routes.settings import get_homeassistant_settings
  1367. from backend.app.models.smart_plug import SmartPlug
  1368. from backend.app.services.homeassistant import homeassistant_service
  1369. from backend.app.services.mqtt_relay import mqtt_relay
  1370. from backend.app.services.rest_smart_plug import rest_smart_plug_service
  1371. from backend.app.services.tasmota import tasmota_service
  1372. plugs_result = await db.execute(select(SmartPlug))
  1373. plugs = list(plugs_result.scalars().all())
  1374. ha_settings = await get_homeassistant_settings(db)
  1375. homeassistant_service.configure(ha_settings["ha_url"], ha_settings["ha_token"])
  1376. total = 0.0
  1377. for plug in plugs:
  1378. if plug.plug_type == "tasmota":
  1379. energy = await tasmota_service.get_energy(plug)
  1380. if energy and energy.get("total") is not None:
  1381. total += energy["total"]
  1382. elif plug.plug_type == "homeassistant":
  1383. energy = await homeassistant_service.get_energy(plug)
  1384. if energy and energy.get("total") is not None:
  1385. total += energy["total"]
  1386. elif plug.plug_type == "mqtt":
  1387. # MQTT plugs only expose today's counter, not lifetime.
  1388. mqtt_data = mqtt_relay.smart_plug_service.get_plug_data(plug.id)
  1389. if mqtt_data and mqtt_data.energy is not None:
  1390. total += mqtt_data.energy
  1391. elif plug.plug_type == "rest":
  1392. # A REST device that exposes only a lifetime counter (Shelly
  1393. # ``aenergy.total`` via ``rest_energy_total_path``) returns no
  1394. # ``today`` key, so read the lifetime total first, like the
  1395. # Tasmota/HA branches above. ``today`` stays as the fallback for
  1396. # devices that only have a daily counter.
  1397. energy = await rest_smart_plug_service.get_energy(plug)
  1398. if energy:
  1399. value = energy.get("total")
  1400. if value is None:
  1401. value = energy.get("today")
  1402. if value is not None:
  1403. total += value
  1404. return total
  1405. async def _sum_snapshot_deltas(
  1406. db: AsyncSession,
  1407. *,
  1408. dt_from: datetime | None,
  1409. dt_to: datetime | None,
  1410. ) -> tuple[float, bool]:
  1411. """Sum per-plug energy consumption over a date range using hourly snapshots.
  1412. For each plug:
  1413. * baseline = last snapshot at or before `dt_from` (ideal)
  1414. — if missing, fall back to the earliest snapshot ever
  1415. recorded for the plug and flag the result as warming up.
  1416. * endpoint = last snapshot at or before `dt_to` (or most recent overall)
  1417. * delta = max(0, endpoint - baseline) — clamp counter resets to 0.
  1418. Returns (total_kwh, warming_up). `warming_up = True` means at least one plug
  1419. had no baseline before `dt_from` (fresh install or fresh upgrade), so the
  1420. result undercounts the beginning of the range.
  1421. """
  1422. from backend.app.models.smart_plug import SmartPlug
  1423. from backend.app.models.smart_plug_energy_snapshot import SmartPlugEnergySnapshot
  1424. from backend.app.utils.local_time import to_naive_utc
  1425. # ``recorded_at`` is a naive column holding UTC. Binding an aware datetime
  1426. # against it raises DataError on asyncpg (SQLite silently drops the offset),
  1427. # which took the whole date-filtered energy figure down on Postgres.
  1428. dt_from = to_naive_utc(dt_from)
  1429. dt_to = to_naive_utc(dt_to)
  1430. plug_ids_result = await db.execute(select(SmartPlug.id))
  1431. plug_ids = [row[0] for row in plug_ids_result.all()]
  1432. if not plug_ids:
  1433. return 0.0, False
  1434. total = 0.0
  1435. warming_up = False
  1436. for plug_id in plug_ids:
  1437. baseline: float | None = None
  1438. if dt_from is not None:
  1439. baseline_q = await db.execute(
  1440. select(SmartPlugEnergySnapshot.lifetime_kwh)
  1441. .where(
  1442. SmartPlugEnergySnapshot.plug_id == plug_id,
  1443. SmartPlugEnergySnapshot.recorded_at <= dt_from,
  1444. )
  1445. .order_by(SmartPlugEnergySnapshot.recorded_at.desc())
  1446. .limit(1)
  1447. )
  1448. baseline = baseline_q.scalar()
  1449. if baseline is None:
  1450. # No snapshot before range start — fall back to the earliest
  1451. # snapshot ever recorded. Result undercounts the pre-first-snapshot
  1452. # portion of the range; signal that to the frontend.
  1453. earliest_q = await db.execute(
  1454. select(SmartPlugEnergySnapshot.lifetime_kwh)
  1455. .where(SmartPlugEnergySnapshot.plug_id == plug_id)
  1456. .order_by(SmartPlugEnergySnapshot.recorded_at.asc())
  1457. .limit(1)
  1458. )
  1459. baseline = earliest_q.scalar()
  1460. if baseline is None:
  1461. # No snapshots at all for this plug yet.
  1462. warming_up = True
  1463. continue
  1464. warming_up = True
  1465. endpoint_conditions = [SmartPlugEnergySnapshot.plug_id == plug_id]
  1466. if dt_to is not None:
  1467. endpoint_conditions.append(SmartPlugEnergySnapshot.recorded_at <= dt_to)
  1468. endpoint_q = await db.execute(
  1469. select(SmartPlugEnergySnapshot.lifetime_kwh)
  1470. .where(*endpoint_conditions)
  1471. .order_by(SmartPlugEnergySnapshot.recorded_at.desc())
  1472. .limit(1)
  1473. )
  1474. endpoint = endpoint_q.scalar()
  1475. if endpoint is None:
  1476. continue
  1477. total += max(0.0, endpoint - baseline)
  1478. return total, warming_up
  1479. @router.get("/tags")
  1480. async def get_all_tags(
  1481. db: AsyncSession = Depends(get_db),
  1482. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1483. require_ownership_permission(
  1484. Permission.ARCHIVES_READ_ALL,
  1485. Permission.ARCHIVES_READ_OWN,
  1486. )
  1487. ),
  1488. printer_scope: PrinterScope = RequestPrinterScope,
  1489. ):
  1490. """List all unique tags with usage counts.
  1491. Returns a list of tags sorted by count (descending), then by name.
  1492. """
  1493. user, can_read_all = auth_result
  1494. # Query all archives with non-null tags
  1495. tag_conditions: list[ColumnElement[bool]] = [PrintArchive.tags.isnot(None), PrintArchive.deleted_at.is_(None)]
  1496. if user is not None and not can_read_all:
  1497. tag_conditions.append(PrintArchive.created_by_id == user.id)
  1498. if (scope_clause := printer_scope.where(PrintArchive.printer_id)) is not None:
  1499. tag_conditions.append(scope_clause)
  1500. result = await db.execute(select(PrintArchive.tags).where(*tag_conditions))
  1501. all_tags_rows = result.all()
  1502. # Count occurrences of each tag
  1503. tag_counts: dict[str, int] = {}
  1504. for (tags_str,) in all_tags_rows:
  1505. if tags_str:
  1506. for tag in tags_str.split(","):
  1507. tag = tag.strip()
  1508. if tag:
  1509. tag_counts[tag] = tag_counts.get(tag, 0) + 1
  1510. # Convert to list and sort by count (desc), then name (asc)
  1511. tags_list = [{"name": name, "count": count} for name, count in tag_counts.items()]
  1512. tags_list.sort(key=lambda x: (-x["count"], x["name"].lower()))
  1513. return tags_list
  1514. @router.put("/tags/{tag_name}")
  1515. async def rename_tag(
  1516. tag_name: str,
  1517. request: Request,
  1518. db: AsyncSession = Depends(get_db),
  1519. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  1520. printer_scope: PrinterScope = RequestPrinterScope,
  1521. ):
  1522. """Rename a tag across all archives.
  1523. Request body should contain {"new_name": "new tag name"}.
  1524. Returns the count of affected archives.
  1525. """
  1526. body = await request.json()
  1527. new_name = body.get("new_name", "").strip()
  1528. if not new_name:
  1529. raise HTTPException(400, "new_name is required")
  1530. if new_name == tag_name:
  1531. return {"affected": 0}
  1532. # Find all archives containing the old tag
  1533. result = await db.execute(
  1534. select(PrintArchive).where(
  1535. PrintArchive.tags.isnot(None),
  1536. PrintArchive.deleted_at.is_(None),
  1537. *_visible_archive_clauses(None, True, printer_scope),
  1538. )
  1539. )
  1540. archives = list(result.scalars().all())
  1541. affected = 0
  1542. for archive in archives:
  1543. if not archive.tags:
  1544. continue
  1545. tags = [t.strip() for t in archive.tags.split(",")]
  1546. if tag_name in tags:
  1547. # Replace old tag with new tag
  1548. new_tags = [new_name if t == tag_name else t for t in tags]
  1549. # Remove duplicates while preserving order
  1550. seen = set()
  1551. unique_tags = []
  1552. for t in new_tags:
  1553. if t not in seen:
  1554. seen.add(t)
  1555. unique_tags.append(t)
  1556. archive.tags = ", ".join(unique_tags)
  1557. affected += 1
  1558. await db.commit()
  1559. return {"affected": affected}
  1560. @router.delete("/tags/{tag_name}")
  1561. async def delete_tag(
  1562. tag_name: str,
  1563. db: AsyncSession = Depends(get_db),
  1564. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  1565. printer_scope: PrinterScope = RequestPrinterScope,
  1566. ):
  1567. """Delete a tag from all archives.
  1568. Returns the count of affected archives.
  1569. """
  1570. # Find all archives containing the tag
  1571. result = await db.execute(
  1572. select(PrintArchive).where(
  1573. PrintArchive.tags.isnot(None),
  1574. PrintArchive.deleted_at.is_(None),
  1575. *_visible_archive_clauses(None, True, printer_scope),
  1576. )
  1577. )
  1578. archives = list(result.scalars().all())
  1579. affected = 0
  1580. for archive in archives:
  1581. if not archive.tags:
  1582. continue
  1583. tags = [t.strip() for t in archive.tags.split(",")]
  1584. if tag_name in tags:
  1585. # Remove the tag
  1586. new_tags = [t for t in tags if t != tag_name]
  1587. archive.tags = ", ".join(new_tags) if new_tags else None
  1588. affected += 1
  1589. await db.commit()
  1590. return {"affected": affected}
  1591. @router.get("/{archive_id}", response_model=ArchiveResponse)
  1592. async def get_archive(
  1593. archive_id: int,
  1594. db: AsyncSession = Depends(get_db),
  1595. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1596. require_ownership_permission(
  1597. Permission.ARCHIVES_READ_ALL,
  1598. Permission.ARCHIVES_READ_OWN,
  1599. )
  1600. ),
  1601. printer_scope: PrinterScope = RequestPrinterScope,
  1602. ):
  1603. """Get a specific archive."""
  1604. user, can_read_all = auth_result
  1605. service = ArchiveService(db)
  1606. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  1607. # Find duplicates
  1608. makerworld_id = archive.extra_data.get("makerworld_model_id") if archive.extra_data else None
  1609. duplicates = await service.find_duplicates(
  1610. archive_id=archive.id,
  1611. content_hash=archive.content_hash,
  1612. print_name=archive.print_name,
  1613. makerworld_model_id=makerworld_id,
  1614. visible=_visible_archive_clauses(user, can_read_all, printer_scope),
  1615. )
  1616. run_aggregates = await _load_run_aggregates(db, [archive.id])
  1617. return archive_to_response(archive, duplicates, run_aggregate=run_aggregates.get(archive.id))
  1618. @router.get("/{archive_id}/delete-impact")
  1619. async def get_archive_delete_impact(
  1620. archive_id: int,
  1621. db: AsyncSession = Depends(get_db),
  1622. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1623. require_ownership_permission(
  1624. Permission.ARCHIVES_READ_ALL,
  1625. Permission.ARCHIVES_READ_OWN,
  1626. )
  1627. ),
  1628. printer_scope: PrinterScope = RequestPrinterScope,
  1629. ):
  1630. """Pre-flight for the delete-confirm modal (#1734).
  1631. Returns the number of related queue items the user is about to remove
  1632. AND whether any of them are currently printing (which would block the
  1633. delete with a 409 — surfaced to the modal so it can disable the
  1634. confirm button instead of failing on submit). Cheap, single endpoint —
  1635. not folded into the archive GET response so the much larger list
  1636. endpoint isn't forced to run the same query per row.
  1637. """
  1638. user, can_read_all = auth_result
  1639. service = ArchiveService(db)
  1640. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  1641. from backend.app.services.archive import _count_related_queue_items
  1642. total, printing = await _count_related_queue_items(db, archive.id)
  1643. return {"related_queue_items": total, "currently_printing": printing}
  1644. @router.get("/{archive_id}/runs", response_model=PrintLogResponse)
  1645. async def list_archive_runs(
  1646. archive_id: int,
  1647. db: AsyncSession = Depends(get_db),
  1648. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1649. require_ownership_permission(
  1650. Permission.ARCHIVES_READ_ALL,
  1651. Permission.ARCHIVES_READ_OWN,
  1652. )
  1653. ),
  1654. printer_scope: PrinterScope = RequestPrinterScope,
  1655. ):
  1656. """List PrintLogEntry rows for this archive — one per print event.
  1657. Newest first. Drives the per-archive "Print Log" view (#1378).
  1658. """
  1659. from backend.app.models.print_log import PrintLogEntry
  1660. from backend.app.schemas.print_log import PrintLogEntrySchema
  1661. user, can_read_all = auth_result
  1662. _ensure_archive_visible(await db.get(PrintArchive, archive_id), user, can_read_all, printer_scope)
  1663. rows = await db.execute(
  1664. select(PrintLogEntry)
  1665. .where(PrintLogEntry.archive_id == archive_id)
  1666. .order_by(PrintLogEntry.started_at.desc().nulls_last(), PrintLogEntry.id.desc())
  1667. )
  1668. entries = list(rows.scalars().all())
  1669. items = [PrintLogEntrySchema.model_validate(e, from_attributes=True) for e in entries]
  1670. return PrintLogResponse(items=items, total=len(items))
  1671. @router.get("/{archive_id}/similar")
  1672. async def find_similar_archives(
  1673. archive_id: int,
  1674. limit: int = 10,
  1675. db: AsyncSession = Depends(get_db),
  1676. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1677. require_ownership_permission(
  1678. Permission.ARCHIVES_READ_ALL,
  1679. Permission.ARCHIVES_READ_OWN,
  1680. )
  1681. ),
  1682. printer_scope: PrinterScope = RequestPrinterScope,
  1683. ):
  1684. """Find archives with similar settings for comparison.
  1685. Returns archives that match by:
  1686. - Same print name (highest priority)
  1687. - Same file content hash
  1688. - Same filament type
  1689. """
  1690. from backend.app.services.archive_comparison import ArchiveComparisonService
  1691. user, can_read_all = auth_result
  1692. _ensure_archive_visible(await db.get(PrintArchive, archive_id), user, can_read_all, printer_scope)
  1693. service = ArchiveComparisonService(db)
  1694. try:
  1695. return await service.find_similar_archives(
  1696. archive_id, limit=limit, visible=_visible_archive_clauses(user, can_read_all, printer_scope)
  1697. )
  1698. except ValueError as e:
  1699. raise HTTPException(404, str(e))
  1700. @router.patch("/{archive_id}", response_model=ArchiveResponse)
  1701. async def update_archive(
  1702. archive_id: int,
  1703. update_data: ArchiveUpdate,
  1704. db: AsyncSession = Depends(get_db),
  1705. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1706. require_ownership_permission(
  1707. Permission.ARCHIVES_UPDATE_ALL,
  1708. Permission.ARCHIVES_UPDATE_OWN,
  1709. )
  1710. ),
  1711. printer_scope: PrinterScope = RequestPrinterScope,
  1712. ):
  1713. """Update archive metadata (tags, notes, cost, filament grams, is_favorite, project_id)."""
  1714. from sqlalchemy.orm import selectinload
  1715. user, can_modify_all = auth_result
  1716. result = await db.execute(
  1717. select(PrintArchive)
  1718. .options(selectinload(PrintArchive.project), selectinload(PrintArchive.created_by))
  1719. .where(PrintArchive.id == archive_id)
  1720. )
  1721. archive = result.scalar_one_or_none()
  1722. if not archive or not printer_scope.allows(archive.printer_id):
  1723. raise HTTPException(404, "Archive not found")
  1724. # Ownership check
  1725. if not can_modify_all:
  1726. if archive.created_by_id != user.id:
  1727. raise HTTPException(403, "You can only update your own archives")
  1728. # Read before the writes below: the mirror needs to know whether the run's
  1729. # figure was inherited from this archive or measured on its own (#1820).
  1730. previous_filament_grams = archive.filament_used_grams
  1731. previous_cost = archive.cost
  1732. previous_status = archive.status
  1733. update_payload = update_data.model_dump(exclude_unset=True)
  1734. # Later runs of the archive are charged from its cost and weight: a caller
  1735. # limited to their own archives sets neither. The one exception is the
  1736. # weight of an archive without its 3MF (#1820), which has none and cannot
  1737. # be printed again.
  1738. if not can_modify_all:
  1739. if "cost" in update_payload:
  1740. raise HTTPException(403, "Only users who can edit all archives can set an archive's cost")
  1741. if "filament_used_grams" in update_payload and update_payload["filament_used_grams"] != previous_filament_grams:
  1742. if previous_filament_grams is not None or archive.file_path:
  1743. raise HTTPException(403, "Only users who can edit all archives can change an archive's filament weight")
  1744. # A new printer must be one the caller may use (as on upload), and both
  1745. # targets must exist. None clears either: a printer-less archive is in
  1746. # every scope, like one uploaded without a printer.
  1747. new_printer_id = update_payload.get("printer_id")
  1748. if new_printer_id is not None and new_printer_id != archive.printer_id:
  1749. printer_scope.ensure(new_printer_id)
  1750. if await db.get(Printer, new_printer_id) is None:
  1751. raise HTTPException(400, "Printer not found")
  1752. new_project_id = update_payload.get("project_id")
  1753. if new_project_id is not None and new_project_id != archive.project_id:
  1754. if await db.get(Project, new_project_id) is None:
  1755. raise HTTPException(400, "Project not found")
  1756. # #1898: how the verdict arrived is recorded with it, never on its own.
  1757. verdict_source = update_payload.pop("user_verdict_source", None)
  1758. for field, value in update_payload.items():
  1759. setattr(archive, field, value)
  1760. # New grams are a new price, unless the caller set the cost as well.
  1761. repriced = False
  1762. if (
  1763. update_payload.get("filament_used_grams")
  1764. and "cost" not in update_payload
  1765. and update_payload["filament_used_grams"] != previous_filament_grams
  1766. and (can_modify_all or archive.cost is None)
  1767. ):
  1768. archive.cost = await _one_print_cost(db, archive, previous_grams=previous_filament_grams)
  1769. repriced = True
  1770. # #1898: a landed verdict retires the one-tap capability token from the
  1771. # push notification — the links stop changing anything once someone
  1772. # decided, and report the recorded verdict instead. Clearing the verdict
  1773. # drops the provenance but leaves the token spent: it was used.
  1774. if "user_verdict" in update_payload:
  1775. if update_payload["user_verdict"] is None:
  1776. archive.user_verdict_source = None
  1777. archive.user_verdict_at = None
  1778. else:
  1779. stamp_verdict(archive, verdict_source or "api")
  1780. retire_confirm_token(archive)
  1781. # #1444: Mirror per-run classification fields to the most recent
  1782. # PrintLogEntry for this archive. PrintLogEntry.failure_reason is captured
  1783. # once at print-completion time from archive.failure_reason — which is
  1784. # NULL until the user classifies the failure via the Edit Archive modal.
  1785. # Without this mirror the Failure Analysis widget (which groups by
  1786. # print_log_entries.failure_reason) keeps showing "Unknown" forever.
  1787. # Same desync hits status: flipping it in the modal wouldn't update the
  1788. # entry either. Only the latest entry is touched because that's the run
  1789. # the modal is implicitly showing (archive.failure_reason / status are
  1790. # overwritten on each reprint to reflect the latest run's outcome).
  1791. # filament_used_grams rides along for the same reason (#1820): the filament
  1792. # totals on the Projects page and in the Prometheus metrics sum the LOG
  1793. # ENTRY's grams, not the archive's, so correcting only the archive would fix
  1794. # the card and leave every aggregate reading the old figure -- or, for a
  1795. # print that archived without its 3MF, no figure at all.
  1796. # While the archive is being reprinted its newest entry is the previous
  1797. # run's, whose outcome this edit is not about.
  1798. mirror_fields = {"failure_reason", "status", "filament_used_grams", "user_verdict"}
  1799. to_mirror = {k: v for k, v in update_payload.items() if k in mirror_fields}
  1800. if to_mirror and previous_status != "printing":
  1801. from backend.app.models.print_log import PrintLogEntry
  1802. latest_entry = await db.scalar(
  1803. select(PrintLogEntry)
  1804. .where(PrintLogEntry.archive_id == archive_id)
  1805. .order_by(PrintLogEntry.id.desc())
  1806. .limit(1)
  1807. )
  1808. if latest_entry is not None:
  1809. # ...but never over a figure the run measured for itself. A run's
  1810. # grams come from the tracked spool delta when there is one, and
  1811. # only fall back to copying the archive's estimate when there is
  1812. # not (see _compute_run_filament_grams). Overwriting a measurement
  1813. # with a typed estimate would lose the better number; the case this
  1814. # edit exists for -- a print archived with no 3MF -- leaves the run
  1815. # with nothing at all, so it is covered by the None arm.
  1816. if "filament_used_grams" in to_mirror and not (
  1817. latest_entry.filament_used_grams is None or latest_entry.filament_used_grams == previous_filament_grams
  1818. ):
  1819. del to_mirror["filament_used_grams"]
  1820. # The run's cost was inherited with its grams, so it follows them.
  1821. if "filament_used_grams" in to_mirror and repriced:
  1822. to_mirror["cost"] = archive.cost
  1823. for field, value in to_mirror.items():
  1824. setattr(latest_entry, field, value)
  1825. # A cost set by hand reaches the run that had the archive's (statistics,
  1826. # project totals and batch sums read the run's cost).
  1827. if "cost" in update_payload and previous_status != "printing":
  1828. from backend.app.services.print_log import mirror_archive_cost
  1829. await mirror_archive_cost(db, archive, previous_cost)
  1830. await db.commit()
  1831. # Re-fetch with relationships loaded after commit
  1832. result = await db.execute(
  1833. select(PrintArchive)
  1834. .options(selectinload(PrintArchive.project), selectinload(PrintArchive.created_by))
  1835. .where(PrintArchive.id == archive_id)
  1836. )
  1837. archive = result.scalar_one_or_none()
  1838. # Load run aggregate so the time/accuracy badge stays consistent with
  1839. # the list / detail endpoints when the frontend re-renders the card
  1840. # after a PATCH (#1608).
  1841. run_aggregates = await _load_run_aggregates(db, [archive.id]) if archive else {}
  1842. return archive_to_response(archive, run_aggregate=run_aggregates.get(archive.id) if archive else None)
  1843. @router.post("/{archive_id}/favorite", response_model=ArchiveResponse)
  1844. async def toggle_favorite(
  1845. archive_id: int,
  1846. db: AsyncSession = Depends(get_db),
  1847. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  1848. require_ownership_permission(
  1849. Permission.ARCHIVES_UPDATE_ALL,
  1850. Permission.ARCHIVES_UPDATE_OWN,
  1851. )
  1852. ),
  1853. printer_scope: PrinterScope = RequestPrinterScope,
  1854. ):
  1855. """Toggle favorite status for an archive."""
  1856. user, can_modify_all = auth_result
  1857. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  1858. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  1859. archive.is_favorite = not archive.is_favorite
  1860. await db.commit()
  1861. await db.refresh(archive)
  1862. return archive
  1863. async def _spoolman_owns_cost(db: AsyncSession) -> bool:
  1864. """True when per-spool pricing lives in Spoolman rather than in our tables.
  1865. Both cost recalculations below rebuild a print's cost from
  1866. ``SpoolUsageHistory``, and fall back to the built-in Filament catalogue or
  1867. the global default rate when there are no rows for it. In Spoolman mode
  1868. there are never any rows -- the built-in usage tracker is handed
  1869. ``spoolman_owns_usage`` at print start and writes none -- so that fallback
  1870. is not a recalculation, it is a downgrade: it would overwrite the
  1871. Spoolman-priced figure ``spoolman_tracking`` recorded at completion with a
  1872. default-rate one, and the per-slot spool resolution it came from is
  1873. transient and cannot be rebuilt here (#2591).
  1874. """
  1875. from backend.app.api.routes.settings import get_setting
  1876. setting = await get_setting(db, "spoolman_enabled")
  1877. return bool(setting) and setting.lower() == "true"
  1878. def _priced_usage_totals():
  1879. """Priced spool usage per archive. A row recorded without a price is left
  1880. out of both sums, so its grams are priced at the default rate instead of
  1881. counting as free filament."""
  1882. priced = SpoolUsageHistory.cost.is_not(None)
  1883. return (
  1884. func.sum(SpoolUsageHistory.cost),
  1885. func.sum(case((priced, SpoolUsageHistory.weight_used), else_=0.0)),
  1886. )
  1887. async def _one_print_cost(db: AsyncSession, archive: PrintArchive, previous_grams: float | None = None) -> float | None:
  1888. """The archive's cost for one print of its grams (see ``price_one_print``).
  1889. A Spoolman-priced cost has no usage rows to rebuild it from (#2591); it is
  1890. kept, scaled to new grams when they were edited.
  1891. """
  1892. from backend.app.models.print_log import PrintLogEntry
  1893. grams = archive.filament_used_grams
  1894. default_cost = await _default_cost_per_kg(db)
  1895. usage_cost, tracked_grams = (
  1896. await db.execute(select(*_priced_usage_totals()).where(SpoolUsageHistory.archive_id == archive.id))
  1897. ).one()
  1898. if usage_cost is not None and usage_cost > 0:
  1899. printed_grams, runs = (
  1900. await db.execute(
  1901. select(func.sum(PrintLogEntry.filament_used_grams), func.count(PrintLogEntry.id)).where(
  1902. PrintLogEntry.archive_id == archive.id
  1903. )
  1904. )
  1905. ).one()
  1906. return price_one_print(
  1907. grams,
  1908. float(usage_cost),
  1909. float(tracked_grams or 0),
  1910. float(printed_grams or 0),
  1911. int(runs or 0),
  1912. default_cost,
  1913. default_cost,
  1914. )
  1915. if not grams:
  1916. return archive.cost
  1917. if archive.cost is not None and await _spoolman_owns_cost(db):
  1918. if previous_grams:
  1919. return round(archive.cost * grams / previous_grams, 2)
  1920. return archive.cost
  1921. fallback = default_cost
  1922. if archive.filament_type:
  1923. primary_type = archive.filament_type.split(",")[0].strip()
  1924. catalog = await db.scalar(select(Filament.cost_per_kg).where(Filament.type == primary_type).limit(1))
  1925. if catalog is not None:
  1926. fallback = catalog
  1927. return price_one_print(grams, None, 0.0, 0.0, 0, fallback, default_cost)
  1928. @router.post("/recalculate-costs")
  1929. async def recalculate_all_costs(
  1930. db: AsyncSession = Depends(get_db),
  1931. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  1932. printer_scope: PrinterScope = RequestPrinterScope,
  1933. ):
  1934. """Recalculate costs for all archives based on filament usage and prices."""
  1935. from backend.app.services.print_log import mirror_archive_cost
  1936. # Only the archives of the caller's printers, as the archive list shows them.
  1937. query = select(PrintArchive)
  1938. if (clause := printer_scope.where(PrintArchive.printer_id)) is not None:
  1939. query = query.where(clause)
  1940. result = await db.execute(query)
  1941. archives = list(result.scalars().all())
  1942. # Load all filaments for lookup
  1943. filament_result = await db.execute(select(Filament))
  1944. filaments = {f.type: f.cost_per_kg for f in filament_result.scalars().all()}
  1945. default_cost_per_kg = await _default_cost_per_kg(db)
  1946. # Pre-fetch all usage costs and tracked weight by archive_id.
  1947. # Tracked weight is used to top-up the cost at the default rate for any
  1948. # filament grams not covered by an inventory spool (#1344).
  1949. usage_costs_result = await db.execute(
  1950. select(SpoolUsageHistory.archive_id, *_priced_usage_totals()).group_by(SpoolUsageHistory.archive_id)
  1951. )
  1952. usage_costs = usage_costs_result.fetchall()
  1953. cost_map = {
  1954. row[0]: (row[1], float(row[2] or 0))
  1955. for row in usage_costs
  1956. if row[0] is not None and row[1] is not None and row[1] > 0
  1957. }
  1958. from backend.app.models.print_log import PrintLogEntry
  1959. run_totals = {
  1960. row[0]: (float(row[1] or 0), int(row[2] or 0))
  1961. for row in (
  1962. await db.execute(
  1963. select(
  1964. PrintLogEntry.archive_id,
  1965. func.sum(PrintLogEntry.filament_used_grams),
  1966. func.count(PrintLogEntry.id),
  1967. ).group_by(PrintLogEntry.archive_id)
  1968. )
  1969. ).all()
  1970. }
  1971. spoolman_owns = await _spoolman_owns_cost(db)
  1972. updated = 0
  1973. preserved = 0
  1974. for archive in archives:
  1975. usage = cost_map.get(archive.id)
  1976. if usage is not None:
  1977. usage_cost, tracked_grams = usage
  1978. printed_grams, runs = run_totals.get(archive.id, (0.0, 0))
  1979. new_cost = price_one_print(
  1980. archive.filament_used_grams,
  1981. usage_cost,
  1982. tracked_grams,
  1983. printed_grams,
  1984. runs,
  1985. default_cost_per_kg,
  1986. default_cost_per_kg,
  1987. )
  1988. else:
  1989. # No usage linked to this archive. Usage without an archive link
  1990. # (recorded before usage was linked, with archiving off, or of a
  1991. # deleted archive) is not matched by print name: that charged one
  1992. # print's spool cost to every archive of the same name.
  1993. if spoolman_owns and archive.cost is not None:
  1994. # Priced from the linked Spoolman spools at completion; there is
  1995. # nothing better to recompute it from here (#2591).
  1996. new_cost = None
  1997. preserved += 1
  1998. elif archive.filament_used_grams and archive.filament_type:
  1999. primary_type = archive.filament_type.split(",")[0].strip()
  2000. cost_per_kg = filaments.get(primary_type, default_cost_per_kg)
  2001. new_cost = price_one_print(
  2002. archive.filament_used_grams, None, 0.0, 0.0, 0, cost_per_kg, default_cost_per_kg
  2003. )
  2004. else:
  2005. new_cost = None
  2006. if new_cost is not None and archive.cost != new_cost:
  2007. previous_cost = archive.cost
  2008. archive.cost = new_cost
  2009. # Its run had the old figure: statistics read the run's cost.
  2010. await mirror_archive_cost(db, archive, previous_cost)
  2011. updated += 1
  2012. await db.commit()
  2013. message = f"Recalculated costs for {updated} archives"
  2014. if preserved:
  2015. message += f"; kept {preserved} priced from Spoolman"
  2016. return {"message": message, "updated": updated, "preserved": preserved}
  2017. @router.get("/{archive_id}/duplicates")
  2018. async def get_archive_duplicates(
  2019. archive_id: int,
  2020. db: AsyncSession = Depends(get_db),
  2021. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2022. require_ownership_permission(
  2023. Permission.ARCHIVES_READ_ALL,
  2024. Permission.ARCHIVES_READ_OWN,
  2025. )
  2026. ),
  2027. printer_scope: PrinterScope = RequestPrinterScope,
  2028. ):
  2029. """Get duplicates for a specific archive."""
  2030. user, can_read_all = auth_result
  2031. service = ArchiveService(db)
  2032. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2033. makerworld_id = archive.extra_data.get("makerworld_model_id") if archive.extra_data else None
  2034. duplicates = await service.find_duplicates(
  2035. archive_id=archive.id,
  2036. content_hash=archive.content_hash,
  2037. print_name=archive.print_name,
  2038. makerworld_model_id=makerworld_id,
  2039. visible=_visible_archive_clauses(user, can_read_all, printer_scope),
  2040. )
  2041. return {"duplicates": duplicates, "count": len(duplicates)}
  2042. @router.post("/backfill-hashes")
  2043. async def backfill_content_hashes(
  2044. db: AsyncSession = Depends(get_db),
  2045. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  2046. printer_scope: PrinterScope = RequestPrinterScope,
  2047. ):
  2048. """Compute and store content hashes for all archives missing them."""
  2049. result = await db.execute(
  2050. select(PrintArchive).where(
  2051. PrintArchive.content_hash.is_(None), *_visible_archive_clauses(None, True, printer_scope)
  2052. )
  2053. )
  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. archive.content_hash = ArchiveService.compute_file_hash(file_path)
  2064. updated += 1
  2065. except Exception as e:
  2066. logger.exception("Failed to compute hash for archive %s: %s", archive.id, e)
  2067. errors.append({"id": archive.id, "error": "Failed to compute hash"})
  2068. await db.commit()
  2069. return {"updated": updated, "errors": errors}
  2070. @router.delete("/{archive_id}")
  2071. async def delete_archive(
  2072. archive_id: int,
  2073. purge_stats: bool = Query(
  2074. False,
  2075. description=(
  2076. "When false (default) the archive is soft-deleted — files removed "
  2077. "from disk, row hidden from listings, but its filament / energy / "
  2078. "time / cost contribution stays in Quick Stats. Set true to also "
  2079. "drop the row from statistics (#1343)."
  2080. ),
  2081. ),
  2082. db: AsyncSession = Depends(get_db),
  2083. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2084. require_ownership_permission(
  2085. Permission.ARCHIVES_DELETE_ALL,
  2086. Permission.ARCHIVES_DELETE_OWN,
  2087. )
  2088. ),
  2089. printer_scope: PrinterScope = RequestPrinterScope,
  2090. ):
  2091. """Delete an archive (soft by default; ``?purge_stats=true`` to hard-delete).
  2092. Both delete paths now cascade to related ``print_queue`` rows (#1734) —
  2093. hard delete via the ``ON DELETE CASCADE`` FK, soft delete via the
  2094. ``_delete_related_queue_items`` helper. A 409 guard blocks the delete
  2095. when any related queue item is currently mid-print so the dispatcher
  2096. doesn't lose its metadata trail under the running print.
  2097. """
  2098. user, can_modify_all = auth_result
  2099. # Get archive first to check ownership
  2100. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  2101. archive = result.scalar_one_or_none()
  2102. if not archive or not printer_scope.allows(archive.printer_id):
  2103. raise HTTPException(404, "Archive not found")
  2104. # Ownership check
  2105. if not can_modify_all:
  2106. if archive.created_by_id != user.id:
  2107. raise HTTPException(403, "You can only delete your own archives")
  2108. # #1734: block delete when any related queue item is currently printing.
  2109. # Both soft and hard delete are gated — an in-flight print needs its
  2110. # backing archive to stay around for the metadata trail (filament,
  2111. # plate, ams_mapping). The user can stop the print first, then retry.
  2112. from backend.app.services.archive import _count_related_queue_items
  2113. _related_total, related_printing = await _count_related_queue_items(db, archive_id)
  2114. if related_printing > 0:
  2115. raise HTTPException(
  2116. 409,
  2117. f"Cannot delete archive — {related_printing} related queue item(s) are "
  2118. f"currently printing. Stop the print first, then retry.",
  2119. )
  2120. service = ArchiveService(db)
  2121. if purge_stats:
  2122. # Hard-delete the linked PrintLogEntry rows first so their filament /
  2123. # cost / count contributions disappear from /archives/stats. The FK is
  2124. # ON DELETE SET NULL, so without this delete the runs would survive
  2125. # the archive row and keep showing up in totals (#1343 / #1378).
  2126. from sqlalchemy import delete as sa_delete
  2127. from backend.app.models.print_log import PrintLogEntry
  2128. await db.execute(sa_delete(PrintLogEntry).where(PrintLogEntry.archive_id == archive_id))
  2129. await db.commit()
  2130. if not await service.delete_archive(archive_id):
  2131. raise HTTPException(404, "Archive not found")
  2132. return {"status": "deleted", "purged_from_stats": True}
  2133. if not await service.soft_delete_archive(archive_id):
  2134. raise HTTPException(404, "Archive not found")
  2135. return {"status": "deleted", "purged_from_stats": False}
  2136. @router.get("/{archive_id}/download")
  2137. async def download_archive(
  2138. archive_id: int,
  2139. inline: bool = False,
  2140. db: AsyncSession = Depends(get_db),
  2141. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2142. require_ownership_permission(
  2143. Permission.ARCHIVES_READ_ALL,
  2144. Permission.ARCHIVES_READ_OWN,
  2145. )
  2146. ),
  2147. printer_scope: PrinterScope = RequestPrinterScope,
  2148. ):
  2149. """Download the 3MF file."""
  2150. user, can_read_all = auth_result
  2151. service = ArchiveService(db)
  2152. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2153. file_path = settings.base_dir / archive.file_path
  2154. if not file_path.is_file():
  2155. raise HTTPException(404, "File not found")
  2156. # Use inline disposition to let browser/OS handle file association
  2157. content_disposition = "inline" if inline else "attachment"
  2158. return FileResponse(
  2159. path=file_path,
  2160. filename=archive.filename,
  2161. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  2162. content_disposition_type=content_disposition,
  2163. )
  2164. @router.get("/{archive_id}/file/{filename}")
  2165. async def download_archive_with_filename(
  2166. archive_id: int,
  2167. filename: str,
  2168. db: AsyncSession = Depends(get_db),
  2169. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2170. require_ownership_permission(
  2171. Permission.ARCHIVES_READ_ALL,
  2172. Permission.ARCHIVES_READ_OWN,
  2173. )
  2174. ),
  2175. printer_scope: PrinterScope = RequestPrinterScope,
  2176. ):
  2177. """Download the 3MF file with filename in URL."""
  2178. user, can_read_all = auth_result
  2179. service = ArchiveService(db)
  2180. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2181. file_path = settings.base_dir / archive.file_path
  2182. if not file_path.is_file():
  2183. raise HTTPException(404, "File not found")
  2184. return FileResponse(
  2185. path=file_path,
  2186. filename=archive.filename,
  2187. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  2188. )
  2189. @router.post("/{archive_id}/slicer-token")
  2190. async def create_archive_slicer_token(
  2191. archive_id: int,
  2192. db: AsyncSession = Depends(get_db),
  2193. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2194. require_ownership_permission(
  2195. Permission.ARCHIVES_READ_ALL,
  2196. Permission.ARCHIVES_READ_OWN,
  2197. )
  2198. ),
  2199. printer_scope: PrinterScope = RequestPrinterScope,
  2200. ):
  2201. """Create a short-lived download token for opening files in slicer applications.
  2202. Slicer protocol handlers (bambustudioopen://, orcaslicer://) cannot send
  2203. auth headers, so they use this token in the URL path instead.
  2204. """
  2205. from backend.app.core.auth import create_slicer_download_token
  2206. user, can_read_all = auth_result
  2207. service = ArchiveService(db)
  2208. _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2209. token = await create_slicer_download_token("archive", archive_id)
  2210. return {"token": token}
  2211. @router.get("/{archive_id}/dl/{token}/{filename}")
  2212. async def download_archive_for_slicer(
  2213. archive_id: int,
  2214. token: str,
  2215. filename: str,
  2216. db: AsyncSession = Depends(get_db),
  2217. ):
  2218. """Download 3MF file using a slicer download token.
  2219. Token-authenticated (no auth headers needed). The token is short-lived and
  2220. archive-bound, created by POST /{archive_id}/slicer-token, and redeemable
  2221. for the rest of its TTL rather than exactly once -- the slicer is a separate
  2222. process that may fetch the URL more than once (#3029).
  2223. Filename is at the end of the URL so slicers can detect the file format.
  2224. """
  2225. from backend.app.core.auth import verify_slicer_download_token
  2226. if not await verify_slicer_download_token(token, "archive", archive_id, single_use=False):
  2227. raise HTTPException(403, "Invalid or expired download token")
  2228. service = ArchiveService(db)
  2229. archive = await service.get_archive(archive_id)
  2230. if not archive:
  2231. raise HTTPException(404, "Archive not found")
  2232. file_path = settings.base_dir / archive.file_path
  2233. if not file_path.is_file():
  2234. raise HTTPException(404, "File not found")
  2235. return FileResponse(
  2236. path=file_path,
  2237. filename=archive.filename,
  2238. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  2239. )
  2240. @router.get("/{archive_id}/thumbnail")
  2241. async def get_thumbnail(
  2242. archive_id: int,
  2243. db: AsyncSession = Depends(get_db),
  2244. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2245. require_media_token_ownership(
  2246. Permission.ARCHIVES_READ_ALL,
  2247. Permission.ARCHIVES_READ_OWN,
  2248. )
  2249. ),
  2250. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  2251. ):
  2252. """Get the thumbnail image.
  2253. Requires a media token query param (?token=xxx) when auth is enabled, and
  2254. returns 404 for an archive the caller may not read (#3025).
  2255. """
  2256. user, can_read_all = auth_result
  2257. service = ArchiveService(db)
  2258. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2259. if not archive.thumbnail_path:
  2260. raise HTTPException(404, "Thumbnail not found")
  2261. thumb_path = settings.base_dir / archive.thumbnail_path
  2262. if not thumb_path.exists():
  2263. raise HTTPException(404, "Thumbnail file not found")
  2264. # Use file modification time as ETag to bust cache
  2265. mtime = int(thumb_path.stat().st_mtime)
  2266. return FileResponse(
  2267. path=thumb_path,
  2268. media_type="image/png",
  2269. headers={
  2270. "Cache-Control": "no-cache, must-revalidate",
  2271. "ETag": f'"{mtime}"',
  2272. },
  2273. )
  2274. @router.get("/{archive_id}/printer-media")
  2275. async def get_archive_printer_media(
  2276. archive_id: int,
  2277. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2278. require_ownership_permission(
  2279. Permission.ARCHIVES_READ_ALL,
  2280. Permission.ARCHIVES_READ_OWN,
  2281. )
  2282. ),
  2283. can_list_printer_files: bool = Depends(probe_permissions_if_auth_enabled(Permission.PRINTERS_FILES)),
  2284. printer_scope: PrinterScope = RequestPrinterScope,
  2285. ):
  2286. """Find downloadable timelapse and `/ipcam` files for one print.
  2287. Local attached timelapses are returned without touching the printer.
  2288. Printer directories are listed only when the caller also has
  2289. ``printers:files``; otherwise the local result is returned with a warning.
  2290. Files are downloaded only after the user explicitly selects them in the UI.
  2291. """
  2292. user, can_read_all = auth_result
  2293. async with database.async_session() as db:
  2294. archive = _ensure_archive_visible(
  2295. await ArchiveService(db).get_archive(archive_id), user, can_read_all, printer_scope
  2296. )
  2297. printer = None
  2298. claimed_timelapse_stems: set[str] = set()
  2299. if archive.printer_id is not None:
  2300. printer = (await db.execute(select(Printer).where(Printer.id == archive.printer_id))).scalar_one_or_none()
  2301. if printer is not None and archive.timelapse_path is None:
  2302. claimed_timelapse_stems = await _claimed_timelapse_stems(db, archive.printer_id, archive_id)
  2303. local_timelapse = None
  2304. if archive.timelapse_path:
  2305. local_path = settings.base_dir / archive.timelapse_path
  2306. if await asyncio.to_thread(local_path.is_file):
  2307. local_timelapse = {
  2308. "name": local_path.name,
  2309. "size": (await asyncio.to_thread(local_path.stat)).st_size,
  2310. }
  2311. response = {
  2312. "archive_id": archive.id,
  2313. "printer_id": archive.printer_id,
  2314. "local_timelapse": local_timelapse,
  2315. "remote_files": [],
  2316. "warnings": [],
  2317. }
  2318. if archive.printer_id is None or archive.started_at is None:
  2319. return response
  2320. if not can_list_printer_files:
  2321. response["warnings"].append("printer_files_forbidden")
  2322. return response
  2323. if printer is None or not printer_scope.allows(printer.id):
  2324. response["warnings"].append("printer_missing")
  2325. return response
  2326. if ftps_handshake_blocked(printer.ip_address):
  2327. if local_timelapse is None:
  2328. response["warnings"].append("timelapse_unavailable")
  2329. response["warnings"].append("ipcam_unavailable")
  2330. return response
  2331. remote_files: list[dict] = []
  2332. # If no copy was attached to the archive, offer the matching printer-side
  2333. # timelapse without mutating the archive or deleting anything from the SD.
  2334. if local_timelapse is None:
  2335. videos: list[dict] = []
  2336. any_timelapse_directory_available = False
  2337. for timelapse_dir in ("/timelapse", "/timelapse/video", "/record", "/recording"):
  2338. if ftps_handshake_blocked(printer.ip_address):
  2339. break
  2340. listing = await list_files_result_async(
  2341. printer.ip_address,
  2342. printer.access_code,
  2343. timelapse_dir,
  2344. timeout=_PRINTER_MEDIA_LIST_TIMEOUT_SECONDS,
  2345. printer_model=printer.model,
  2346. )
  2347. any_timelapse_directory_available |= listing.available
  2348. candidates = [
  2349. file
  2350. for file in listing.files
  2351. if not file.get("is_directory") and str(file.get("name") or "").lower().endswith(VIDEO_SUFFIXES)
  2352. ]
  2353. if candidates:
  2354. videos = candidates
  2355. break
  2356. if not any_timelapse_directory_available:
  2357. response["warnings"].append("timelapse_unavailable")
  2358. if videos:
  2359. baseline = set(archive.timelapse_baseline or [])
  2360. eligible = [
  2361. file
  2362. for file in videos
  2363. if str(file.get("name") or "") not in baseline
  2364. and Path(str(file.get("name") or "")).stem not in claimed_timelapse_stems
  2365. ]
  2366. if archive.timelapse_baseline is not None:
  2367. candidate = eligible[0] if len(eligible) == 1 else None
  2368. else:
  2369. candidate, _ = _match_timelapse_by_timestamp(eligible, archive.started_at)
  2370. if candidate is not None:
  2371. remote_files.append(
  2372. {
  2373. "name": candidate.get("name"),
  2374. "path": candidate.get("path"),
  2375. "size": candidate.get("size") or 0,
  2376. "mtime": candidate.get("mtime"),
  2377. "kind": "timelapse",
  2378. }
  2379. )
  2380. if ftps_handshake_blocked(printer.ip_address):
  2381. response["warnings"].append("ipcam_unavailable")
  2382. response["remote_files"] = remote_files
  2383. return response
  2384. ipcam_listing = await list_files_result_async(
  2385. printer.ip_address,
  2386. printer.access_code,
  2387. "/ipcam",
  2388. timeout=_PRINTER_MEDIA_LIST_TIMEOUT_SECONDS,
  2389. printer_model=printer.model,
  2390. )
  2391. if ipcam_listing.available:
  2392. for file in match_ipcam_chunks(ipcam_listing.files, archive.started_at, archive.completed_at):
  2393. remote_files.append(
  2394. {
  2395. "name": file.get("name"),
  2396. "path": file.get("path") or f"/ipcam/{file.get('name')}",
  2397. "size": file.get("size") or 0,
  2398. "mtime": file.get("mtime"),
  2399. "kind": "ipcam",
  2400. }
  2401. )
  2402. else:
  2403. response["warnings"].append("ipcam_unavailable")
  2404. response["remote_files"] = remote_files
  2405. return response
  2406. @router.post("/{archive_id}/media-download-token")
  2407. async def create_archive_media_download_token(
  2408. archive_id: int,
  2409. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2410. require_ownership_permission(Permission.ARCHIVES_READ_ALL, Permission.ARCHIVES_READ_OWN)
  2411. ),
  2412. printer_scope: PrinterScope = RequestPrinterScope,
  2413. ):
  2414. """Mint a single-use token bound to an archive's attached timelapse."""
  2415. from backend.app.core.auth import create_slicer_download_token
  2416. user, can_read_all = auth_result
  2417. async with database.async_session() as db:
  2418. archive = _ensure_archive_visible(
  2419. await ArchiveService(db).get_archive(archive_id), user, can_read_all, printer_scope
  2420. )
  2421. if not archive.timelapse_path:
  2422. raise HTTPException(404, "Timelapse not found")
  2423. timelapse_path = settings.base_dir / archive.timelapse_path
  2424. if not await asyncio.to_thread(timelapse_path.is_file):
  2425. raise HTTPException(404, "Timelapse file not found")
  2426. return {
  2427. "token": await create_slicer_download_token("archive-timelapse", archive_id),
  2428. "filename": timelapse_path.name,
  2429. }
  2430. @router.get("/{archive_id}/media/dl/{token}/{filename}")
  2431. async def download_archive_media_with_token(
  2432. archive_id: int,
  2433. token: str,
  2434. filename: str,
  2435. ):
  2436. """Consume a resource-bound token and stream an attached timelapse."""
  2437. from backend.app.core.auth import verify_slicer_download_token
  2438. if not await verify_slicer_download_token(token, "archive-timelapse", archive_id):
  2439. return download_error_response(403, "This download link has already been used or has expired.")
  2440. async with database.async_session() as db:
  2441. archive = await ArchiveService(db).get_archive(archive_id)
  2442. if not archive or not archive.timelapse_path:
  2443. return download_error_response(404, "This print has no attached timelapse.")
  2444. timelapse_path = settings.base_dir / archive.timelapse_path
  2445. if not await asyncio.to_thread(timelapse_path.is_file):
  2446. return download_error_response(404, "The attached timelapse is no longer on disk.")
  2447. safe_filename = safe_download_filename(filename, fallback=timelapse_path.name)
  2448. return FileResponse(
  2449. path=timelapse_path,
  2450. filename=safe_filename,
  2451. headers={"Content-Disposition": build_content_disposition(safe_filename)},
  2452. )
  2453. @router.get("/{archive_id}/timelapse")
  2454. async def get_timelapse(
  2455. archive_id: int,
  2456. db: AsyncSession = Depends(get_db),
  2457. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2458. require_media_token_ownership(
  2459. Permission.ARCHIVES_READ_ALL,
  2460. Permission.ARCHIVES_READ_OWN,
  2461. )
  2462. ),
  2463. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  2464. ):
  2465. """Get the timelapse video.
  2466. Requires a media token query param (?token=xxx) when auth is enabled, and
  2467. returns 404 for an archive the caller may not read (#3025).
  2468. """
  2469. user, can_read_all = auth_result
  2470. service = ArchiveService(db)
  2471. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2472. if not archive.timelapse_path:
  2473. raise HTTPException(404, "Timelapse not found")
  2474. timelapse_path = settings.base_dir / archive.timelapse_path
  2475. if not timelapse_path.exists():
  2476. raise HTTPException(404, "Timelapse file not found")
  2477. # Use file modification time as ETag to bust cache after processing
  2478. mtime = int(timelapse_path.stat().st_mtime)
  2479. # Detect media type from file extension (AVI from P1S before background conversion)
  2480. suffix = timelapse_path.suffix.lower()
  2481. media_type = {".mp4": "video/mp4", ".avi": "video/x-msvideo", ".mkv": "video/x-matroska"}.get(suffix, "video/mp4")
  2482. ext = suffix if suffix in (".mp4", ".avi", ".mkv") else ".mp4"
  2483. return FileResponse(
  2484. path=timelapse_path,
  2485. media_type=media_type,
  2486. filename=f"{archive.print_name or 'timelapse'}{ext}",
  2487. headers={
  2488. "Cache-Control": "no-cache, must-revalidate",
  2489. "ETag": f'"{mtime}"',
  2490. },
  2491. )
  2492. @router.delete("/{archive_id}/timelapse")
  2493. async def delete_timelapse(
  2494. archive_id: int,
  2495. db: AsyncSession = Depends(get_db),
  2496. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2497. require_ownership_permission(
  2498. Permission.ARCHIVES_DELETE_ALL,
  2499. Permission.ARCHIVES_DELETE_OWN,
  2500. )
  2501. ),
  2502. printer_scope: PrinterScope = RequestPrinterScope,
  2503. ):
  2504. """Remove the timelapse video from an archive."""
  2505. user, can_modify_all = auth_result
  2506. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  2507. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  2508. if not archive.timelapse_path:
  2509. raise HTTPException(404, "No timelapse attached to this archive")
  2510. # Delete the file
  2511. timelapse_path = settings.base_dir / archive.timelapse_path
  2512. if timelapse_path.exists():
  2513. timelapse_path.unlink()
  2514. # Clear the path in database
  2515. archive.timelapse_path = None
  2516. archive.timelapse_plate_id = None
  2517. await db.commit()
  2518. from backend.app.services.archive import _announce_timelapse
  2519. await _announce_timelapse(archive_id, attached=False)
  2520. return {"status": "deleted"}
  2521. @router.post("/{archive_id}/timelapse/scan")
  2522. async def scan_timelapse(
  2523. archive_id: int,
  2524. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  2525. printer_scope: PrinterScope = RequestPrinterScope,
  2526. ):
  2527. """Scan printer for timelapse matching this archive and attach it."""
  2528. from backend.app.core.database import async_session
  2529. from backend.app.models.printer import Printer
  2530. from backend.app.services.bambu_ftp import (
  2531. DownloadDeadlineExceeded,
  2532. delete_archived_timelapse,
  2533. download_file_bytes_async,
  2534. ftps_handshake_blocked,
  2535. get_ftp_retry_settings,
  2536. list_files_async,
  2537. remote_file_settled,
  2538. with_ftp_retry,
  2539. )
  2540. # Read the archive + printer in a short session and release the pooled DB
  2541. # connection BEFORE the FTP scan/download below — a timelapse pull walks
  2542. # several directories and fetches a 100MB+ video, so holding Depends(get_db)
  2543. # across it pinned one connection idle-in-transaction for minutes (#2572).
  2544. # Scalar columns stay readable on the detached rows (expire_on_commit=False);
  2545. # the attach at the end runs in its own fresh short session.
  2546. async with async_session() as db:
  2547. archive = await ArchiveService(db).get_archive(archive_id)
  2548. if not archive:
  2549. raise HTTPException(404, "Archive not found")
  2550. if archive.timelapse_path:
  2551. return {"status": "exists", "message": "Timelapse already attached"}
  2552. if not archive.printer_id:
  2553. raise HTTPException(400, "Archive has no associated printer")
  2554. printer_scope.ensure(archive.printer_id)
  2555. result = await db.execute(select(Printer).where(Printer.id == archive.printer_id))
  2556. printer = result.scalar_one_or_none()
  2557. if not printer:
  2558. raise HTTPException(404, "Printer not found")
  2559. # Get base name from archive filename (without .3mf extension)
  2560. base_name = Path(archive.filename).stem
  2561. # Scan timelapse directory on printer
  2562. # Different printer models use different paths
  2563. files = []
  2564. for timelapse_path in ["/timelapse", "/timelapse/video", "/record", "/recording"]:
  2565. if ftps_handshake_blocked(printer.ip_address):
  2566. break
  2567. try:
  2568. files = await list_files_async(
  2569. printer.ip_address, printer.access_code, timelapse_path, printer_model=printer.model
  2570. )
  2571. if files:
  2572. break
  2573. except Exception:
  2574. continue
  2575. if not files:
  2576. # "Couldn't reach the printer" and "the printer has no timelapse
  2577. # directory" are different problems with different fixes, and both used
  2578. # to come back as one 500 (#2780). Nothing here will work while the
  2579. # printer's file service is not answering over TLS, so say that rather
  2580. # than reporting an empty directory.
  2581. if ftps_handshake_blocked(printer.ip_address):
  2582. raise HTTPException(
  2583. 503,
  2584. f"Printer {printer.ip_address} is not answering its file service over TLS. "
  2585. "Bambuddy will try again shortly.",
  2586. )
  2587. raise HTTPException(404, "No timelapse directory found on the printer")
  2588. # Look for matching timelapse
  2589. matching_file = None
  2590. video_files = [
  2591. f for f in files if not f.get("is_directory") and f.get("name", "").lower().endswith((".mp4", ".avi"))
  2592. ]
  2593. # Strategy 0: snapshot diff against the baseline captured at print start
  2594. # (#2704). This is the same comparison the automatic scan makes, and the
  2595. # only one here that doesn't depend on the printer's clock — a printer in
  2596. # LAN-only mode can't reach Bambu's NTP server, so the timestamps in both
  2597. # the filename and the FTP mtime can be days out. One reporter's P1S was
  2598. # six and a half days off, which defeats every strategy below.
  2599. #
  2600. # When a baseline exists it is authoritative and the clock-based strategies
  2601. # are skipped entirely: they can only turn an honest "pick one yourself"
  2602. # into a confident wrong answer. Those strategies stay for archives created
  2603. # before the baseline was persisted.
  2604. used_baseline = archive.timelapse_baseline is not None
  2605. # A video another archive of this printer holds is that print's, whichever
  2606. # strategy below would pick it (a short name like "1" is in every video's
  2607. # timestamp). Picking by hand still offers it: that is how a wrong match
  2608. # is corrected.
  2609. offered = video_files
  2610. async with async_session() as db:
  2611. claimed = await _claimed_timelapse_stems(db, archive.printer_id, archive_id)
  2612. video_files = [f for f in video_files if Path(f.get("name", "")).stem not in claimed]
  2613. if used_baseline:
  2614. baseline = set(archive.timelapse_baseline)
  2615. candidates = [f for f in video_files if f.get("name", "") not in baseline]
  2616. if len(candidates) == 1:
  2617. matching_file = candidates[0]
  2618. logger.info("Matched timelapse by print-start baseline: %s", matching_file.get("name"))
  2619. elif candidates:
  2620. # Ambiguous — offer only the plausible files instead of guessing.
  2621. offered = candidates
  2622. logger.info("Baseline left %s unclaimed candidates for archive %s", len(candidates), archive_id)
  2623. else:
  2624. logger.info("Baseline shows no unclaimed new video on the printer for archive %s", archive_id)
  2625. # Strategy 1: Match by print name in filename
  2626. if not used_baseline:
  2627. for f in video_files:
  2628. fname = f.get("name", "")
  2629. if base_name.lower() in fname.lower():
  2630. matching_file = f
  2631. break
  2632. # Strategy 2: Match by timestamp proximity against print START time.
  2633. # Bambu timelapse filename embeds the print start time in printer-local clock.
  2634. # See _match_timelapse_by_timestamp for the offset-search rationale and why we
  2635. # intentionally don't try to match filename against end time here.
  2636. if not used_baseline and not matching_file and archive.started_at:
  2637. candidate, diff = _match_timelapse_by_timestamp(video_files, archive.started_at)
  2638. if candidate is not None:
  2639. matching_file = candidate
  2640. logger.info("Matched timelapse by timestamp: %s (diff: %s)", candidate.get("name"), diff)
  2641. # Strategy 3: Use file modification time from FTP listing
  2642. # This handles cases where printer's filename timestamp is wrong but file mtime is correct
  2643. if not used_baseline and not matching_file and (archive.started_at or archive.completed_at or archive.created_at):
  2644. from datetime import datetime, timedelta
  2645. _archive_start = archive.started_at
  2646. archive_end = archive.completed_at or archive.created_at
  2647. best_match = None
  2648. best_diff = timedelta(hours=24)
  2649. for f in video_files:
  2650. mtime = f.get("mtime")
  2651. if mtime:
  2652. # Timelapse file should be modified during or shortly after the print
  2653. # The mtime should be close to completion time (video finishes when print ends)
  2654. if archive_end:
  2655. diff = abs(mtime - archive_end)
  2656. if diff < best_diff:
  2657. best_diff = diff
  2658. best_match = f
  2659. logger.debug(
  2660. f"Timelapse mtime match candidate: {f.get('name')}, mtime: {mtime}, diff from end: {diff}"
  2661. )
  2662. if best_match and best_diff < timedelta(hours=2):
  2663. matching_file = best_match
  2664. logger.info("Matched timelapse by file mtime: %s (diff: %s)", best_match.get("name"), best_diff)
  2665. # Strategy 4: If only one timelapse exists and archive was recently completed, use it
  2666. # This handles cases where printer clock is wrong or timezone issues exist
  2667. if not used_baseline and not matching_file and len(video_files) == 1:
  2668. from datetime import datetime, timedelta, timezone
  2669. archive_completed = archive.completed_at or archive.created_at
  2670. if archive_completed:
  2671. if archive_completed.tzinfo is None:
  2672. archive_completed = archive_completed.replace(tzinfo=timezone.utc)
  2673. time_since_completion = datetime.now(timezone.utc) - archive_completed
  2674. # If archive was completed within the last hour, assume the single timelapse is for it
  2675. if time_since_completion < timedelta(hours=1):
  2676. matching_file = video_files[0]
  2677. logger.info("Using single timelapse file as fallback: %s", video_files[0].get("name"))
  2678. # Note: We intentionally don't use a "most recent file" fallback because
  2679. # we can't verify if timelapse was actually enabled for this print.
  2680. # Instead, return the list of available files for manual selection.
  2681. if not matching_file:
  2682. # Return available files for manual selection
  2683. available_files = [
  2684. {
  2685. "name": f.get("name"),
  2686. "path": f.get("path"),
  2687. "size": f.get("size"),
  2688. "mtime": f.get("mtime").isoformat() if f.get("mtime") else None,
  2689. }
  2690. for f in offered
  2691. ]
  2692. # Sort by mtime descending (most recent first)
  2693. available_files.sort(key=lambda x: x.get("mtime") or "", reverse=True)
  2694. return {
  2695. "status": "not_found",
  2696. "message": "No matching timelapse found - please select manually",
  2697. "available_files": available_files,
  2698. }
  2699. # Download the timelapse - use the full path from the file listing
  2700. remote_path = matching_file.get("path") or f"/timelapse/{matching_file['name']}"
  2701. # Get FTP retry settings
  2702. ftp_retry_enabled, ftp_retry_count, ftp_retry_delay, ftp_timeout = await get_ftp_retry_settings()
  2703. try:
  2704. if ftp_retry_enabled:
  2705. timelapse_data = await with_ftp_retry(
  2706. download_file_bytes_async,
  2707. printer.ip_address,
  2708. printer.access_code,
  2709. remote_path,
  2710. socket_timeout=ftp_timeout,
  2711. printer_model=printer.model,
  2712. expected_size=matching_file.get("size"),
  2713. max_retries=ftp_retry_count,
  2714. retry_delay=ftp_retry_delay,
  2715. operation_name=f"Download timelapse {matching_file['name']}",
  2716. cooloff_ip=printer.ip_address,
  2717. )
  2718. else:
  2719. timelapse_data = await download_file_bytes_async(
  2720. printer.ip_address,
  2721. printer.access_code,
  2722. remote_path,
  2723. socket_timeout=ftp_timeout,
  2724. printer_model=printer.model,
  2725. expected_size=matching_file.get("size"),
  2726. )
  2727. except DownloadDeadlineExceeded as exc:
  2728. # Not retried: the deadline already fitted the file's size.
  2729. raise HTTPException(504, "The timelapse download took longer than its size allows") from exc
  2730. if not timelapse_data:
  2731. raise HTTPException(502, "Could not download the timelapse from the printer")
  2732. # Confirm the printer has finished writing before we commit to this file and
  2733. # delete the original: matching the listing's size proves we got what it
  2734. # said, not that the file was complete (#2704).
  2735. if not await remote_file_settled(
  2736. printer.ip_address,
  2737. printer.access_code,
  2738. remote_path,
  2739. len(timelapse_data),
  2740. printer_model=printer.model,
  2741. ):
  2742. raise HTTPException(409, "The printer is still writing this video — try again in a moment")
  2743. # Attach in a fresh short session (the read session was released before FTP).
  2744. async with async_session() as db:
  2745. success = await ArchiveService(db).attach_timelapse(archive_id, timelapse_data, matching_file["name"])
  2746. if not success:
  2747. raise HTTPException(500, "Failed to attach timelapse")
  2748. # Safe now, and only now: the transfer matched the size the listing reported
  2749. # and the bytes are committed to the archive (#2704).
  2750. await delete_archived_timelapse(
  2751. printer.ip_address,
  2752. printer.access_code,
  2753. remote_path,
  2754. verified=matching_file.get("size") is not None,
  2755. printer_model=printer.model,
  2756. printer_name=printer.name,
  2757. )
  2758. return {
  2759. "status": "attached",
  2760. "message": f"Timelapse '{matching_file['name']}' attached successfully",
  2761. "filename": matching_file["name"],
  2762. }
  2763. @router.post("/{archive_id}/timelapse/select")
  2764. async def select_timelapse(
  2765. archive_id: int,
  2766. filename: str = Query(..., description="Timelapse filename to attach"),
  2767. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  2768. printer_scope: PrinterScope = RequestPrinterScope,
  2769. ):
  2770. """Manually select a timelapse from the printer to attach."""
  2771. from backend.app.core.database import async_session
  2772. from backend.app.models.printer import Printer
  2773. from backend.app.services.bambu_ftp import (
  2774. DownloadDeadlineExceeded,
  2775. delete_archived_timelapse,
  2776. download_file_bytes_async,
  2777. get_ftp_retry_settings,
  2778. list_files_async,
  2779. remote_file_settled,
  2780. with_ftp_retry,
  2781. )
  2782. # Read the archive + printer in a short session and release the pooled DB
  2783. # connection BEFORE the FTP scan/download below (#2572); scalars stay
  2784. # readable after close (expire_on_commit=False), the attach reopens one.
  2785. async with async_session() as db:
  2786. archive = await ArchiveService(db).get_archive(archive_id)
  2787. if not archive:
  2788. raise HTTPException(404, "Archive not found")
  2789. if not archive.printer_id:
  2790. raise HTTPException(400, "Archive has no associated printer")
  2791. printer_scope.ensure(archive.printer_id)
  2792. result = await db.execute(select(Printer).where(Printer.id == archive.printer_id))
  2793. printer = result.scalar_one_or_none()
  2794. if not printer:
  2795. raise HTTPException(404, "Printer not found")
  2796. # Find the file on the printer
  2797. files = []
  2798. remote_path = None
  2799. expected_size = None
  2800. for timelapse_dir in ["/timelapse", "/timelapse/video", "/record", "/recording"]:
  2801. try:
  2802. files = await list_files_async(
  2803. printer.ip_address, printer.access_code, timelapse_dir, printer_model=printer.model
  2804. )
  2805. for f in files:
  2806. if f.get("name") == filename:
  2807. remote_path = f.get("path") or f"{timelapse_dir}/{filename}"
  2808. expected_size = f.get("size")
  2809. break
  2810. if remote_path:
  2811. break
  2812. except Exception:
  2813. continue
  2814. if not remote_path:
  2815. raise HTTPException(404, f"Timelapse '{filename}' not found on printer")
  2816. # Download and attach
  2817. ftp_retry_enabled, ftp_retry_count, ftp_retry_delay, ftp_timeout = await get_ftp_retry_settings()
  2818. try:
  2819. if ftp_retry_enabled:
  2820. timelapse_data = await with_ftp_retry(
  2821. download_file_bytes_async,
  2822. printer.ip_address,
  2823. printer.access_code,
  2824. remote_path,
  2825. socket_timeout=ftp_timeout,
  2826. printer_model=printer.model,
  2827. expected_size=expected_size,
  2828. max_retries=ftp_retry_count,
  2829. retry_delay=ftp_retry_delay,
  2830. operation_name=f"Download timelapse {filename}",
  2831. cooloff_ip=printer.ip_address,
  2832. )
  2833. else:
  2834. timelapse_data = await download_file_bytes_async(
  2835. printer.ip_address,
  2836. printer.access_code,
  2837. remote_path,
  2838. socket_timeout=ftp_timeout,
  2839. printer_model=printer.model,
  2840. expected_size=expected_size,
  2841. )
  2842. except DownloadDeadlineExceeded as exc:
  2843. # Not retried: the deadline already fitted the file's size.
  2844. raise HTTPException(504, "The timelapse download took longer than its size allows") from exc
  2845. if not timelapse_data:
  2846. raise HTTPException(502, "Could not download the timelapse from the printer")
  2847. # Confirm the printer has finished writing before we commit to this file and
  2848. # delete the original: matching the listing's size proves we got what it
  2849. # said, not that the file was complete (#2704).
  2850. if not await remote_file_settled(
  2851. printer.ip_address,
  2852. printer.access_code,
  2853. remote_path,
  2854. len(timelapse_data),
  2855. printer_model=printer.model,
  2856. ):
  2857. raise HTTPException(409, "The printer is still writing this video — try again in a moment")
  2858. # Attach in a fresh short session (the read session was released before FTP).
  2859. async with async_session() as db:
  2860. success = await ArchiveService(db).attach_timelapse(archive_id, timelapse_data, filename)
  2861. if not success:
  2862. raise HTTPException(500, "Failed to attach timelapse")
  2863. # Safe now, and only now: the transfer matched the size the listing reported
  2864. # and the bytes are committed to the archive (#2704).
  2865. await delete_archived_timelapse(
  2866. printer.ip_address,
  2867. printer.access_code,
  2868. remote_path,
  2869. verified=expected_size is not None,
  2870. printer_model=printer.model,
  2871. printer_name=printer.name,
  2872. )
  2873. return {
  2874. "status": "attached",
  2875. "message": f"Timelapse '{filename}' attached successfully",
  2876. "filename": filename,
  2877. }
  2878. @router.post("/{archive_id}/timelapse/upload")
  2879. async def upload_timelapse(
  2880. archive_id: int,
  2881. file: UploadFile = File(...),
  2882. db: AsyncSession = Depends(get_db),
  2883. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  2884. printer_scope: PrinterScope = RequestPrinterScope,
  2885. ):
  2886. """Manually upload a timelapse video to an archive."""
  2887. service = ArchiveService(db)
  2888. archive = await service.get_archive(archive_id)
  2889. if not archive or not printer_scope.allows(archive.printer_id):
  2890. raise HTTPException(404, "Archive not found")
  2891. if not file.filename or not file.filename.endswith((".mp4", ".avi", ".mkv")):
  2892. raise HTTPException(400, "File must be a video file (.mp4, .avi, .mkv)")
  2893. content = await file.read()
  2894. safe_filename = _safe_filename(file.filename)
  2895. success = await service.attach_timelapse(archive_id, content, safe_filename)
  2896. if not success:
  2897. raise HTTPException(500, "Failed to attach timelapse")
  2898. return {"status": "attached", "filename": safe_filename}
  2899. @router.get("/{archive_id}/timelapse/info")
  2900. async def get_timelapse_info(
  2901. archive_id: int,
  2902. db: AsyncSession = Depends(get_db),
  2903. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2904. require_ownership_permission(
  2905. Permission.ARCHIVES_READ_ALL,
  2906. Permission.ARCHIVES_READ_OWN,
  2907. )
  2908. ),
  2909. printer_scope: PrinterScope = RequestPrinterScope,
  2910. ):
  2911. """Get timelapse video metadata for editor."""
  2912. from backend.app.schemas.timelapse import TimelapseInfoResponse
  2913. from backend.app.services.timelapse_processor import TimelapseProcessor
  2914. user, can_read_all = auth_result
  2915. service = ArchiveService(db)
  2916. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2917. if not archive.timelapse_path:
  2918. raise HTTPException(404, "Timelapse not found")
  2919. timelapse_path = settings.base_dir / archive.timelapse_path
  2920. if not timelapse_path.exists():
  2921. raise HTTPException(404, "Timelapse file not found")
  2922. try:
  2923. processor = TimelapseProcessor(timelapse_path)
  2924. info = await processor.get_info()
  2925. return TimelapseInfoResponse(**info)
  2926. except Exception as e:
  2927. logger.error("Failed to get timelapse info: %s", e)
  2928. raise HTTPException(500, f"Failed to get video info: {str(e)}")
  2929. @router.get("/{archive_id}/timelapse/thumbnails")
  2930. async def get_timelapse_thumbnails(
  2931. archive_id: int,
  2932. count: int = Query(10, ge=1, le=30),
  2933. width: int = Query(160, ge=80, le=320),
  2934. db: AsyncSession = Depends(get_db),
  2935. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  2936. require_ownership_permission(
  2937. Permission.ARCHIVES_READ_ALL,
  2938. Permission.ARCHIVES_READ_OWN,
  2939. )
  2940. ),
  2941. printer_scope: PrinterScope = RequestPrinterScope,
  2942. ):
  2943. """Generate timeline thumbnail frames for visual scrubbing."""
  2944. import base64
  2945. from backend.app.schemas.timelapse import ThumbnailResponse
  2946. from backend.app.services.timelapse_processor import TimelapseProcessor
  2947. user, can_read_all = auth_result
  2948. service = ArchiveService(db)
  2949. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  2950. if not archive.timelapse_path:
  2951. raise HTTPException(404, "Timelapse not found")
  2952. timelapse_path = settings.base_dir / archive.timelapse_path
  2953. if not timelapse_path.exists():
  2954. raise HTTPException(404, "Timelapse file not found")
  2955. try:
  2956. processor = TimelapseProcessor(timelapse_path)
  2957. thumbnails = await processor.generate_thumbnails(count, width)
  2958. return ThumbnailResponse(
  2959. thumbnails=[base64.b64encode(data).decode() for _, data in thumbnails],
  2960. timestamps=[ts for ts, _ in thumbnails],
  2961. )
  2962. except Exception as e:
  2963. logger.error("Failed to generate thumbnails: %s", e)
  2964. raise HTTPException(500, f"Failed to generate thumbnails: {str(e)}")
  2965. @router.post("/{archive_id}/timelapse/process", status_code=202)
  2966. async def process_timelapse(
  2967. archive_id: int,
  2968. trim_start: float = Form(0),
  2969. trim_end: float = Form(None),
  2970. speed: float = Form(1.0),
  2971. audio_volume: float = Form(1.0),
  2972. save_mode: str = Form("new"),
  2973. output_filename: str = Form(None),
  2974. audio: UploadFile = File(None),
  2975. db: AsyncSession = Depends(get_db),
  2976. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  2977. printer_scope: PrinterScope = RequestPrinterScope,
  2978. ):
  2979. """Start re-encoding the timelapse with trim, speed and optional audio.
  2980. Runs as a background job: poll ``GET .../timelapse/job`` for progress and
  2981. the result, ``DELETE .../timelapse/job`` cancels it.
  2982. """
  2983. import shutil
  2984. import tempfile
  2985. import uuid
  2986. from backend.app.schemas.timelapse import TimelapseJobResponse
  2987. from backend.app.services import timelapse_jobs
  2988. from backend.app.services.timelapse_processor import TimelapseProcessor
  2989. # Validate speed
  2990. if not 0.25 <= speed <= 4.0:
  2991. raise HTTPException(400, "Speed must be between 0.25 and 4.0")
  2992. if not 0.0 <= audio_volume <= 1.0:
  2993. raise HTTPException(400, "audio_volume must be between 0 and 1")
  2994. if save_mode not in ("replace", "new"):
  2995. raise HTTPException(400, "save_mode must be 'replace' or 'new'")
  2996. service = ArchiveService(db)
  2997. archive = await service.get_archive(archive_id)
  2998. if not archive or not archive.timelapse_path or not printer_scope.allows(archive.printer_id):
  2999. raise HTTPException(404, "Timelapse not found")
  3000. timelapse_path = settings.base_dir / archive.timelapse_path
  3001. if not timelapse_path.exists():
  3002. raise HTTPException(404, "Timelapse file not found")
  3003. current = timelapse_jobs.get_job(archive_id)
  3004. if current is not None and current.status == timelapse_jobs.RUNNING:
  3005. raise HTTPException(409, "This timelapse is already being processed")
  3006. try:
  3007. processor = TimelapseProcessor(timelapse_path)
  3008. except RuntimeError as e:
  3009. raise HTTPException(500, f"Processing failed: {e}") from None
  3010. archive_dir = timelapse_path.parent
  3011. stored_timelapse_path = archive.timelapse_path
  3012. # What is being edited, so a replace does not overwrite a timelapse removed
  3013. # or swapped for another one while the job ran.
  3014. source = timelapse_path.stat()
  3015. source_signature = (source.st_size, source.st_mtime_ns, source.st_ino)
  3016. # Determine output path. Unique temp names: two archives' jobs run at once.
  3017. if save_mode == "replace":
  3018. # Process to temp file first, then replace
  3019. output_path = Path(tempfile.gettempdir()) / f"processed_{archive_id}_{uuid.uuid4().hex}.mp4"
  3020. else:
  3021. # Save as new file alongside original
  3022. filename = output_filename or f"{archive.print_name or 'timelapse'}_edited.mp4"
  3023. # Sanitize filename - remove path separators and traversal sequences
  3024. filename = "".join(c for c in filename if c.isalnum() or c in "._- ")
  3025. # Prevent path traversal
  3026. if ".." in filename or not filename or filename.startswith("."):
  3027. filename = f"timelapse_{archive_id}_edited"
  3028. if not filename.endswith(".mp4"):
  3029. filename += ".mp4"
  3030. output_path = archive_dir / filename # SEC-PATH-OK: filename alnum-filtered + .. rejected above
  3031. # Handle audio file
  3032. audio_temp_path = None
  3033. if audio and audio.filename:
  3034. # Validate audio file extension
  3035. if not audio.filename.lower().endswith((".mp3", ".wav", ".m4a", ".aac", ".ogg")):
  3036. raise HTTPException(400, "Audio must be .mp3, .wav, .m4a, .aac, or .ogg")
  3037. audio_content = await audio.read()
  3038. # Extract and validate suffix to prevent path injection
  3039. suffix = Path(audio.filename).suffix.lower()
  3040. if suffix not in (".mp3", ".wav", ".m4a", ".aac", ".ogg"):
  3041. raise HTTPException(400, "Invalid audio file extension")
  3042. audio_temp_path = Path(tempfile.gettempdir()) / f"audio_{archive_id}_{uuid.uuid4().hex}{suffix}"
  3043. audio_temp_path.write_bytes(audio_content)
  3044. async def run(job: timelapse_jobs.TimelapseJob) -> tuple[str, str]:
  3045. try:
  3046. success = await processor.process(
  3047. output_path=output_path,
  3048. trim_start=trim_start,
  3049. trim_end=trim_end,
  3050. speed=speed,
  3051. audio_path=audio_temp_path,
  3052. audio_volume=audio_volume,
  3053. on_progress=job.set_progress,
  3054. )
  3055. if not success:
  3056. raise RuntimeError("Video processing failed")
  3057. if save_mode == "replace":
  3058. await _ensure_timelapse_unchanged(archive_id, stored_timelapse_path, timelapse_path, source_signature)
  3059. shutil.move(str(output_path), str(timelapse_path))
  3060. return stored_timelapse_path, "Timelapse replaced successfully"
  3061. return str(output_path.relative_to(settings.base_dir)), f"Saved as {output_path.name}"
  3062. finally:
  3063. if save_mode == "replace":
  3064. output_path.unlink(missing_ok=True)
  3065. if audio_temp_path:
  3066. audio_temp_path.unlink(missing_ok=True)
  3067. try:
  3068. job = timelapse_jobs.start_job(archive_id, run)
  3069. except timelapse_jobs.JobAlreadyRunning:
  3070. # Started by another request while this one read its audio upload.
  3071. if audio_temp_path:
  3072. audio_temp_path.unlink(missing_ok=True)
  3073. raise HTTPException(409, "This timelapse is already being processed") from None
  3074. return TimelapseJobResponse.from_job(job)
  3075. async def _ensure_timelapse_unchanged(
  3076. archive_id: int, stored_path: str, timelapse_path: Path, signature: tuple[int, int, int]
  3077. ) -> None:
  3078. async with database.async_session() as db:
  3079. current = (
  3080. await db.execute(select(PrintArchive.timelapse_path).where(PrintArchive.id == archive_id))
  3081. ).scalar_one_or_none()
  3082. try:
  3083. now = timelapse_path.stat()
  3084. except FileNotFoundError:
  3085. now = None
  3086. if current != stored_path or now is None or (now.st_size, now.st_mtime_ns, now.st_ino) != signature:
  3087. raise RuntimeError("The timelapse was removed or replaced while it was being edited; the edit was not saved")
  3088. async def _timelapse_job_archive(archive_id: int, db: AsyncSession, printer_scope: PrinterScope) -> None:
  3089. archive = await ArchiveService(db).get_archive(archive_id)
  3090. if not archive or not printer_scope.allows(archive.printer_id):
  3091. raise HTTPException(404, "Timelapse not found")
  3092. @router.get("/{archive_id}/timelapse/job")
  3093. async def get_timelapse_job(
  3094. archive_id: int,
  3095. db: AsyncSession = Depends(get_db),
  3096. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  3097. printer_scope: PrinterScope = RequestPrinterScope,
  3098. ):
  3099. """The archive's running or last timelapse job: status, progress, result."""
  3100. from backend.app.schemas.timelapse import TimelapseJobResponse
  3101. from backend.app.services import timelapse_jobs
  3102. await _timelapse_job_archive(archive_id, db, printer_scope)
  3103. job = timelapse_jobs.get_job(archive_id)
  3104. if job is None:
  3105. raise HTTPException(404, "No timelapse job")
  3106. return TimelapseJobResponse.from_job(job)
  3107. @router.delete("/{archive_id}/timelapse/job")
  3108. async def cancel_timelapse_job(
  3109. archive_id: int,
  3110. db: AsyncSession = Depends(get_db),
  3111. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  3112. printer_scope: PrinterScope = RequestPrinterScope,
  3113. ):
  3114. """Cancel the running timelapse job; stops ffmpeg and drops its output."""
  3115. from backend.app.schemas.timelapse import TimelapseJobResponse
  3116. from backend.app.services import timelapse_jobs
  3117. await _timelapse_job_archive(archive_id, db, printer_scope)
  3118. if not await timelapse_jobs.cancel_job(archive_id):
  3119. raise HTTPException(404, "No timelapse job is running")
  3120. job = timelapse_jobs.get_job(archive_id)
  3121. assert job is not None
  3122. return TimelapseJobResponse.from_job(job)
  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 | ApiKeyActor | 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 | ApiKeyActor | 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 | ApiKeyActor | 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 | ApiKeyActor | 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 | ApiKeyActor | 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 | ApiKeyActor | 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 | ApiKeyActor | 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. # Each outcome carries its file's position in the request: two files of
  3888. # the same name cannot be told apart by name.
  3889. results = []
  3890. errors = []
  3891. for index, file in enumerate(files):
  3892. if not file.filename or not file.filename.endswith(".3mf"):
  3893. errors.append({"index": index, "filename": file.filename or "unknown", "error": "Not a .3mf file"})
  3894. continue
  3895. safe_filename = _safe_filename(file.filename)
  3896. temp_path = (
  3897. settings.archive_dir / "temp" / safe_filename
  3898. ) # SEC-PATH-OK: safe_filename = _safe_filename(...) basename-stripped above
  3899. temp_path.parent.mkdir(parents=True, exist_ok=True)
  3900. try:
  3901. content = await file.read()
  3902. # #1401: bulk-upload variant of the library validation. Collect
  3903. # the rejection per-file rather than aborting the whole batch
  3904. # so one bad file in a 10-file drag-drop doesn't lose the
  3905. # other nine.
  3906. try:
  3907. validate_print_file_upload(file.filename, content)
  3908. except HTTPException as exc:
  3909. errors.append({"index": index, "filename": file.filename, "error": exc.detail})
  3910. continue
  3911. temp_path.write_bytes(content)
  3912. service = ArchiveService(db)
  3913. archive = await service.archive_print(
  3914. printer_id=printer_id,
  3915. source_file=temp_path,
  3916. created_by_id=current_user.id if current_user else None,
  3917. prefer_filename_for_name=prefer_filename_for_name,
  3918. )
  3919. if archive:
  3920. results.append(
  3921. {
  3922. "index": index,
  3923. "filename": file.filename,
  3924. "id": archive.id,
  3925. "status": "success",
  3926. }
  3927. )
  3928. else:
  3929. errors.append({"index": index, "filename": file.filename, "error": "Failed to process"})
  3930. except Exception as e:
  3931. logger.exception("Failed to upload archive %s: %s", file.filename, e)
  3932. errors.append({"index": index, "filename": file.filename, "error": "Failed to process file"})
  3933. finally:
  3934. if temp_path.exists():
  3935. temp_path.unlink()
  3936. return {
  3937. "uploaded": len(results),
  3938. "failed": len(errors),
  3939. "results": results,
  3940. "errors": errors,
  3941. }
  3942. @router.get("/{archive_id}/plates")
  3943. async def get_archive_plates(
  3944. archive_id: int,
  3945. db: AsyncSession = Depends(get_db),
  3946. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  3947. require_ownership_permission(
  3948. Permission.ARCHIVES_READ_ALL,
  3949. Permission.ARCHIVES_READ_OWN,
  3950. )
  3951. ),
  3952. printer_scope: PrinterScope = RequestPrinterScope,
  3953. ):
  3954. """Get available plates from a multi-plate 3MF archive.
  3955. Returns a list of plates with their index, name, thumbnail availability,
  3956. and filament requirements. For single-plate exports, returns a single plate.
  3957. """
  3958. import re
  3959. import defusedxml.ElementTree as ET
  3960. user, can_read_all = auth_result
  3961. service = ArchiveService(db)
  3962. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  3963. file_path = settings.base_dir / archive.file_path
  3964. if not file_path.is_file():
  3965. raise HTTPException(404, "Archive file not found")
  3966. plates = []
  3967. # Initialize so the `has_gcode = bool(gcode_files)` after the try/except
  3968. # never raises NameError when the archive isn't a valid zip (e.g. plain
  3969. # .gcode file from a sliced-archive flow that didn't request 3MF output).
  3970. gcode_files: list[str] = []
  3971. # Printer / process preset names the 3MF was prepared with — used by the
  3972. # SliceModal to default its dropdowns (#1325).
  3973. embedded_presets: dict[str, str | None] = {"printer": None, "process": None}
  3974. # Process settings the designer changed away from the stock preset (#2622),
  3975. # offered in the SliceModal for a cross-printer re-slice. Same payload the
  3976. # library plates endpoint returns — SliceModal reads one shape for both.
  3977. design_overrides: list[dict] = []
  3978. try:
  3979. with zipfile.ZipFile(file_path, "r") as zf:
  3980. namelist = zf.namelist()
  3981. embedded_presets = extract_embedded_presets_from_3mf(zf)
  3982. if _PROJECT_SETTINGS_PATH in namelist:
  3983. try:
  3984. design_overrides = [
  3985. o._asdict()
  3986. for o in overrides_from_config(json.loads(zf.read(_PROJECT_SETTINGS_PATH).decode("utf-8")))
  3987. ]
  3988. except (ValueError, OSError, KeyError):
  3989. design_overrides = []
  3990. # Find all plate gcode files to determine available plates
  3991. gcode_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".gcode")]
  3992. # If no gcode is present (source-only or unsliced), fall back to plate JSON/PNG
  3993. plate_indices: list[int] = []
  3994. if gcode_files:
  3995. # Extract plate indices from gcode filenames
  3996. for gf in gcode_files:
  3997. # "Metadata/plate_5.gcode" -> 5
  3998. try:
  3999. # Remove "Metadata/plate_" and ".gcode"
  4000. plate_str = gf[15:-6]
  4001. plate_indices.append(int(plate_str))
  4002. except ValueError:
  4003. pass # Skip gcode file with non-numeric plate index
  4004. else:
  4005. plate_json_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".json")]
  4006. plate_png_files = [
  4007. n
  4008. for n in namelist
  4009. if n.startswith("Metadata/plate_")
  4010. and n.endswith(".png")
  4011. and "_small" not in n
  4012. and "no_light" not in n
  4013. ]
  4014. plate_name_candidates = plate_json_files + plate_png_files
  4015. plate_re = re.compile(r"^Metadata/plate_(\d+)\.(json|png)$")
  4016. seen_indices: set[int] = set()
  4017. for name in plate_name_candidates:
  4018. match = plate_re.match(name)
  4019. if match:
  4020. try:
  4021. index = int(match.group(1))
  4022. except ValueError:
  4023. continue
  4024. if index in seen_indices:
  4025. continue
  4026. seen_indices.add(index)
  4027. plate_indices.append(index)
  4028. if not plate_indices:
  4029. # No plate metadata found
  4030. return {
  4031. "archive_id": archive_id,
  4032. "filename": archive.filename,
  4033. "plates": [],
  4034. "is_multi_plate": False,
  4035. }
  4036. plate_indices.sort()
  4037. # Parse model_settings.config for plate names + object assignments
  4038. # Plate names are stored with plater_id and plater_name keys
  4039. plate_names = {} # plater_id -> name
  4040. plate_object_ids: dict[int, list[str]] = {}
  4041. object_names_by_id: dict[str, str] = {}
  4042. if "Metadata/model_settings.config" in namelist:
  4043. try:
  4044. model_content = zf.read("Metadata/model_settings.config").decode()
  4045. model_root = ET.fromstring(model_content)
  4046. # Build object ID -> name map
  4047. for obj_elem in model_root.findall(".//object"):
  4048. obj_id = obj_elem.get("id")
  4049. if not obj_id:
  4050. continue
  4051. name_meta = obj_elem.find("metadata[@key='name']")
  4052. obj_name = name_meta.get("value") if name_meta is not None else None
  4053. if obj_name:
  4054. object_names_by_id[obj_id] = obj_name
  4055. for plate_elem in model_root.findall(".//plate"):
  4056. plater_id = None
  4057. plater_name = None
  4058. for meta in plate_elem.findall("metadata"):
  4059. key = meta.get("key")
  4060. value = meta.get("value")
  4061. if key == "plater_id" and value:
  4062. try:
  4063. plater_id = int(value)
  4064. except ValueError:
  4065. pass # Skip plate with non-numeric plater_id
  4066. elif key == "plater_name" and value:
  4067. plater_name = value.strip()
  4068. if plater_id is not None and plater_name:
  4069. plate_names[plater_id] = plater_name
  4070. if plater_id is not None:
  4071. for instance_elem in plate_elem.findall("model_instance"):
  4072. for inst_meta in instance_elem.findall("metadata"):
  4073. if inst_meta.get("key") == "object_id":
  4074. obj_id = inst_meta.get("value")
  4075. if not obj_id:
  4076. continue
  4077. plate_object_ids.setdefault(plater_id, [])
  4078. if obj_id not in plate_object_ids[plater_id]:
  4079. plate_object_ids[plater_id].append(obj_id)
  4080. except Exception:
  4081. pass # model_settings.config parsing is optional
  4082. # Parse slice_info.config for plate metadata
  4083. plate_metadata = {} # plate_index -> {filaments, prediction, weight, name, objects}
  4084. if "Metadata/slice_info.config" in namelist:
  4085. content = zf.read("Metadata/slice_info.config").decode()
  4086. root = ET.fromstring(content)
  4087. for plate_elem in root.findall(".//plate"):
  4088. plate_info = {
  4089. "filaments": [],
  4090. "prediction": None,
  4091. "weight": None,
  4092. "name": None,
  4093. "objects": [],
  4094. "bed_type": None,
  4095. }
  4096. # Get plate index from metadata
  4097. plate_index = None
  4098. for meta in plate_elem.findall("metadata"):
  4099. key = meta.get("key")
  4100. value = meta.get("value")
  4101. if key == "index" and value:
  4102. try:
  4103. plate_index = int(value)
  4104. except ValueError:
  4105. pass # Skip plate with non-numeric index
  4106. elif key == "prediction" and value:
  4107. try:
  4108. plate_info["prediction"] = int(value)
  4109. except ValueError:
  4110. pass # Skip non-numeric print time prediction
  4111. elif key == "weight" and value:
  4112. try:
  4113. plate_info["weight"] = float(value)
  4114. except ValueError:
  4115. pass # Skip non-numeric filament weight
  4116. elif key == "curr_bed_type" and value:
  4117. # Per-plate bed type so the PrintModal can show the
  4118. # right plate alongside each option (#1281).
  4119. plate_info["bed_type"] = value.strip()
  4120. # Get filaments used in this plate
  4121. for filament_elem in plate_elem.findall("filament"):
  4122. filament_id = filament_elem.get("id")
  4123. filament_type = filament_elem.get("type", "")
  4124. filament_color = filament_elem.get("color", "")
  4125. used_g = filament_elem.get("used_g", "0")
  4126. used_m = filament_elem.get("used_m", "0")
  4127. try:
  4128. used_grams = float(used_g)
  4129. except (ValueError, TypeError):
  4130. used_grams = 0
  4131. if used_grams > 0 and filament_id:
  4132. plate_info["filaments"].append(
  4133. {
  4134. "slot_id": int(filament_id),
  4135. "type": filament_type,
  4136. "color": filament_color,
  4137. # The preset the slicer assigned (GFA01, ...): names the product line
  4138. "tray_info_idx": filament_elem.get("tray_info_idx", "") or "",
  4139. "used_grams": round(used_grams, 1),
  4140. "used_meters": float(used_m) if used_m else 0,
  4141. }
  4142. )
  4143. # Sort filaments by slot ID
  4144. plate_info["filaments"].sort(key=lambda x: x["slot_id"])
  4145. # Collect all object names on this plate
  4146. for obj_elem in plate_elem.findall("object"):
  4147. obj_name = obj_elem.get("name")
  4148. if obj_name and obj_name not in plate_info["objects"]:
  4149. plate_info["objects"].append(obj_name)
  4150. # Set plate name: prefer custom name from model_settings.config,
  4151. # fall back to first object name if no custom name was set
  4152. if plate_index is not None:
  4153. custom_name = plate_names.get(plate_index)
  4154. if custom_name:
  4155. plate_info["name"] = custom_name
  4156. else:
  4157. # Fall back to first object name as hint
  4158. if plate_info["objects"]:
  4159. plate_info["name"] = plate_info["objects"][0]
  4160. plate_metadata[plate_index] = plate_info
  4161. # Parse plate_*.json for object lists when slice_info is missing
  4162. plate_json_objects: dict[int, list[str]] = {}
  4163. for name in namelist:
  4164. match = re.match(r"^Metadata/plate_(\d+)\.json$", name)
  4165. if not match:
  4166. continue
  4167. try:
  4168. plate_index = int(match.group(1))
  4169. except ValueError:
  4170. continue
  4171. try:
  4172. payload = json.loads(zf.read(name).decode())
  4173. bbox_objects = payload.get("bbox_objects", [])
  4174. names = []
  4175. for obj in bbox_objects:
  4176. obj_name = obj.get("name") if isinstance(obj, dict) else None
  4177. if obj_name and obj_name not in names:
  4178. names.append(obj_name)
  4179. if names:
  4180. plate_json_objects[plate_index] = names
  4181. except Exception:
  4182. continue
  4183. # Build plate list
  4184. for idx in plate_indices:
  4185. meta = plate_metadata.get(idx, {})
  4186. has_thumbnail = f"Metadata/plate_{idx}.png" in namelist
  4187. objects = meta.get("objects", [])
  4188. if not objects:
  4189. objects = plate_json_objects.get(idx, [])
  4190. if not objects and plate_object_ids.get(idx):
  4191. objects = [
  4192. object_names_by_id.get(obj_id, f"Object {obj_id}") for obj_id in plate_object_ids.get(idx, [])
  4193. ]
  4194. plate_name = meta.get("name")
  4195. if not plate_name:
  4196. plate_name = plate_names.get(idx)
  4197. if not plate_name and objects:
  4198. plate_name = objects[0]
  4199. plates.append(
  4200. {
  4201. "index": idx,
  4202. "name": plate_name,
  4203. "objects": objects,
  4204. "object_count": len(objects),
  4205. "has_thumbnail": has_thumbnail,
  4206. "thumbnail_url": f"/api/v1/archives/{archive_id}/plate-thumbnail/{idx}"
  4207. if has_thumbnail
  4208. else None,
  4209. "print_time_seconds": meta.get("prediction"),
  4210. "filament_used_grams": meta.get("weight"),
  4211. "filaments": meta.get("filaments", []),
  4212. "bed_type": meta.get("bed_type"),
  4213. }
  4214. )
  4215. except Exception as e:
  4216. logger.warning("Failed to parse plates from archive %s: %s", archive_id, e)
  4217. # Has gcode iff the plate list was built from .gcode filenames (as opposed
  4218. # to the JSON/PNG fallback for source-only 3MF projects). Callers that need
  4219. # to preview gcode — the viewer, skip-objects — can gate on this instead of
  4220. # 404-ing on every plate request.
  4221. has_gcode = bool(gcode_files)
  4222. return {
  4223. "archive_id": archive_id,
  4224. "filename": archive.filename,
  4225. "plates": plates,
  4226. "is_multi_plate": len(plates) > 1,
  4227. "has_gcode": has_gcode,
  4228. "embedded_printer": embedded_presets["printer"],
  4229. "embedded_process": embedded_presets["process"],
  4230. "design_overrides": design_overrides,
  4231. }
  4232. @router.get("/{archive_id}/plate-thumbnail/{plate_index}")
  4233. async def get_plate_thumbnail(
  4234. archive_id: int,
  4235. plate_index: int,
  4236. db: AsyncSession = Depends(get_db),
  4237. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4238. require_media_token_ownership(
  4239. Permission.ARCHIVES_READ_ALL,
  4240. Permission.ARCHIVES_READ_OWN,
  4241. )
  4242. ),
  4243. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  4244. ):
  4245. """Get the thumbnail image for a specific plate.
  4246. Requires a media token query param (?token=xxx) when auth is enabled, and
  4247. returns 404 for an archive the caller may not read (#3025).
  4248. """
  4249. user, can_read_all = auth_result
  4250. service = ArchiveService(db)
  4251. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  4252. file_path = settings.base_dir / archive.file_path
  4253. if not file_path.is_file():
  4254. raise HTTPException(404, "Archive file not found")
  4255. try:
  4256. with zipfile.ZipFile(file_path, "r") as zf:
  4257. thumb_path = f"Metadata/plate_{plate_index}.png"
  4258. if thumb_path in zf.namelist():
  4259. data = zf.read(thumb_path)
  4260. return Response(content=data, media_type="image/png")
  4261. except Exception:
  4262. pass # Fall through to 404 if archive is unreadable or thumbnail missing
  4263. raise HTTPException(404, f"Thumbnail for plate {plate_index} not found")
  4264. async def _try_preview_slice_filaments(
  4265. db: AsyncSession,
  4266. *,
  4267. kind: str,
  4268. source_id: int,
  4269. plate_id: int,
  4270. file_path: Path,
  4271. request_id: str | None = None,
  4272. ) -> list[dict] | None:
  4273. """Run a preview slice via the user's configured sidecar so the filament
  4274. list endpoint can return real per-plate filaments for unsliced project
  4275. files. Returns ``None`` on any failure — the caller falls back to the
  4276. painted-face heuristic. ``request_id`` flows through to the sidecar
  4277. for live progress on the SliceModal's inline spinner + toast.
  4278. """
  4279. from backend.app.api.routes.settings import get_setting
  4280. from backend.app.services.slice_preview import get_preview_filaments
  4281. from backend.app.services.slicer_api import get_stall_timeout_seconds
  4282. preferred = (await get_setting(db, "preferred_slicer")) or "bambu_studio"
  4283. if preferred == "orcaslicer":
  4284. configured = await get_setting(db, "orcaslicer_api_url")
  4285. api_url = (configured or settings.slicer_api_url).strip()
  4286. elif preferred == "bambu_studio":
  4287. configured = await get_setting(db, "bambu_studio_api_url")
  4288. api_url = (configured or settings.bambu_studio_api_url).strip()
  4289. else:
  4290. return None
  4291. if not api_url:
  4292. return None
  4293. try:
  4294. file_bytes = file_path.read_bytes()
  4295. except OSError:
  4296. return None
  4297. return await get_preview_filaments(
  4298. kind=kind,
  4299. source_id=source_id,
  4300. plate_id=plate_id,
  4301. file_bytes=file_bytes,
  4302. file_name=file_path.name,
  4303. api_url=api_url,
  4304. request_id=request_id,
  4305. timeout_seconds=await get_stall_timeout_seconds(db),
  4306. )
  4307. @router.get("/{archive_id}/filament-requirements")
  4308. async def get_filament_requirements(
  4309. archive_id: int,
  4310. plate_id: int | None = None,
  4311. request_id: str | None = None,
  4312. full_slots: bool = False,
  4313. db: AsyncSession = Depends(get_db),
  4314. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4315. require_ownership_permission(
  4316. Permission.ARCHIVES_READ_ALL,
  4317. Permission.ARCHIVES_READ_OWN,
  4318. )
  4319. ),
  4320. printer_scope: PrinterScope = RequestPrinterScope,
  4321. ):
  4322. """Get filament requirements from the archived 3MF file.
  4323. Returns the filaments used in this print with their slot IDs, types, colors,
  4324. and usage amounts. This can be compared with current AMS state before reprinting.
  4325. Args:
  4326. archive_id: The archive ID
  4327. plate_id: Optional plate index to filter filaments for (for multi-plate files)
  4328. """
  4329. import defusedxml.ElementTree as ET
  4330. user, can_read_all = auth_result
  4331. service = ArchiveService(db)
  4332. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  4333. file_path = settings.base_dir / archive.file_path
  4334. if not file_path.is_file():
  4335. raise HTTPException(404, "Archive file not found")
  4336. filaments = []
  4337. try:
  4338. with zipfile.ZipFile(file_path, "r") as zf:
  4339. # Parse slice_info.config for filament requirements
  4340. if "Metadata/slice_info.config" in zf.namelist():
  4341. content = zf.read("Metadata/slice_info.config").decode()
  4342. root = ET.fromstring(content)
  4343. # If plate_id is specified, find filaments for that specific plate
  4344. if plate_id is not None:
  4345. # Find the plate element with matching index
  4346. for plate_elem in root.findall(".//plate"):
  4347. plate_index = None
  4348. for meta in plate_elem.findall("metadata"):
  4349. if meta.get("key") == "index":
  4350. try:
  4351. plate_index = int(meta.get("value", "0"))
  4352. except ValueError:
  4353. pass # Skip plate with non-numeric index metadata
  4354. break
  4355. if plate_index == plate_id:
  4356. # Extract filaments from this plate element
  4357. for filament_elem in plate_elem.findall("filament"):
  4358. filament_id = filament_elem.get("id")
  4359. filament_type = filament_elem.get("type", "")
  4360. filament_color = filament_elem.get("color", "")
  4361. used_g = filament_elem.get("used_g", "0")
  4362. used_m = filament_elem.get("used_m", "0")
  4363. tray_info_idx = filament_elem.get("tray_info_idx", "")
  4364. try:
  4365. used_grams = float(used_g)
  4366. except (ValueError, TypeError):
  4367. used_grams = 0
  4368. if used_grams > 0 and filament_id:
  4369. filaments.append(
  4370. {
  4371. "slot_id": int(filament_id),
  4372. "type": filament_type,
  4373. "color": filament_color,
  4374. "used_grams": round(used_grams, 1),
  4375. "used_meters": float(used_m) if used_m else 0,
  4376. "tray_info_idx": tray_info_idx,
  4377. "used_in_plate": True,
  4378. }
  4379. )
  4380. break
  4381. else:
  4382. # No plate_id specified - extract all filaments with used_g > 0
  4383. # This is the legacy behavior for single-plate files
  4384. for filament_elem in root.findall(".//filament"):
  4385. filament_id = filament_elem.get("id")
  4386. filament_type = filament_elem.get("type", "")
  4387. filament_color = filament_elem.get("color", "")
  4388. used_g = filament_elem.get("used_g", "0")
  4389. used_m = filament_elem.get("used_m", "0")
  4390. tray_info_idx = filament_elem.get("tray_info_idx", "")
  4391. # Only include filaments that are actually used
  4392. try:
  4393. used_grams = float(used_g)
  4394. except (ValueError, TypeError):
  4395. used_grams = 0
  4396. if used_grams > 0 and filament_id:
  4397. filaments.append(
  4398. {
  4399. "slot_id": int(filament_id),
  4400. "type": filament_type,
  4401. "color": filament_color,
  4402. "used_grams": round(used_grams, 1),
  4403. "used_meters": float(used_m) if used_m else 0,
  4404. "tray_info_idx": tray_info_idx,
  4405. "used_in_plate": True,
  4406. }
  4407. )
  4408. # Re-slicing a source that already carries slice_info (#2712).
  4409. # See library.py for the full rationale: the slice modal's list is
  4410. # positional, so a source using only slot 4 must still present
  4411. # four slots or the pick lands on slot 1. The print path keeps the
  4412. # used-only list it depends on.
  4413. if full_slots and filaments:
  4414. filaments = expand_to_project_slots(zf, filaments)
  4415. # Unsliced project files: see library.py for full rationale.
  4416. # Return the FULL project_settings.config slot list with a
  4417. # used_in_plate flag derived from the preview slice; the
  4418. # CLI needs every slot pre-filled to avoid silent default
  4419. # substitution.
  4420. if not filaments:
  4421. project_filaments = extract_project_filaments_from_3mf(zf)
  4422. used_slot_ids: set[int] = set()
  4423. if project_filaments and plate_id is not None:
  4424. preview = await _try_preview_slice_filaments(
  4425. db,
  4426. kind="archive",
  4427. source_id=archive_id,
  4428. plate_id=plate_id,
  4429. file_path=file_path,
  4430. request_id=request_id,
  4431. )
  4432. if preview is not None:
  4433. used_slot_ids = {f["slot_id"] for f in preview}
  4434. fallback_all_used = not used_slot_ids
  4435. for f in project_filaments:
  4436. f["used_in_plate"] = fallback_all_used or f["slot_id"] in used_slot_ids
  4437. filaments = project_filaments
  4438. # Sort by slot ID
  4439. filaments.sort(key=lambda x: x["slot_id"])
  4440. # Enrich with nozzle mapping for dual-nozzle printers
  4441. nozzle_mapping = extract_nozzle_mapping_from_3mf(zf, plate_id=plate_id)
  4442. if nozzle_mapping:
  4443. for filament in filaments:
  4444. filament["nozzle_id"] = nozzle_mapping.get(filament["slot_id"])
  4445. # Nozzle-rack machines (#1784): the print dialog offers a rack
  4446. # position per filament group, which needs the group table as well
  4447. # as the carriage above.
  4448. annotate_rack_groups(filaments, file_path, plate_id)
  4449. except Exception as e:
  4450. logger.warning("Failed to parse filament requirements from archive %s: %s", archive_id, e)
  4451. return {
  4452. "archive_id": archive_id,
  4453. "filename": archive.filename,
  4454. "plate_id": plate_id,
  4455. "filaments": filaments,
  4456. }
  4457. @router.post("/{archive_id}/slice", status_code=202)
  4458. async def slice_archive(
  4459. archive_id: int,
  4460. request: SliceRequest,
  4461. db: AsyncSession = Depends(get_db),
  4462. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.LIBRARY_UPLOAD),
  4463. printer_scope: PrinterScope = RequestPrinterScope,
  4464. actor: User | ApiKeyActor | None = RequestActor,
  4465. ):
  4466. """Enqueue a slice job for an archive's source. Returns 202 + job_id;
  4467. the slice runs in the background, the caller polls `GET /slice-jobs/{id}`.
  4468. Source preference: ``source_3mf_path`` (the un-sliced project file the
  4469. user originally sent to slice) → ``file_path`` (the sliced 3MF/gcode that
  4470. actually printed).
  4471. """
  4472. from backend.app.api.routes.library import guard_nozzle_class_reslice, slice_and_persist_as_archive
  4473. from backend.app.core.database import async_session
  4474. from backend.app.services.slice_dispatch import (
  4475. http_exception_to_job_error,
  4476. slice_dispatch,
  4477. )
  4478. archive = await db.get(PrintArchive, archive_id)
  4479. # Per-row ownership gate — mirror the archive read routes. LIBRARY_UPLOAD
  4480. # alone let a READ_OWN caller slice another user's archive by raw id even
  4481. # though GET on that id returned 404. An API key is checked as its owner
  4482. # (RequestActor); only auth off keeps can_read_all=True.
  4483. can_read_all = actor is None or actor.has_permission(Permission.ARCHIVES_READ_ALL.value)
  4484. archive = _ensure_archive_visible(archive, actor, can_read_all, printer_scope)
  4485. # The re-sliced copy belongs to the caller and can be queued as their own,
  4486. # so slicing an archive needs the same right as printing it again.
  4487. from backend.app.api.routes.print_queue import _assert_can_queue_archive
  4488. _assert_can_queue_archive(archive, actor, printer_scope)
  4489. src_relative = archive.source_3mf_path or archive.file_path
  4490. if not src_relative:
  4491. raise HTTPException(
  4492. status_code=400,
  4493. detail="Archive has no source file to slice",
  4494. )
  4495. src_path = (
  4496. Path(settings.base_dir) / src_relative
  4497. ) # 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
  4498. if not src_path.exists():
  4499. raise HTTPException(status_code=404, detail="Archive source file missing on disk")
  4500. raw_filename = archive.filename or src_path.name
  4501. src_lower = raw_filename.lower()
  4502. if not (
  4503. src_lower.endswith(".stl")
  4504. or src_lower.endswith(".3mf")
  4505. or src_lower.endswith(".step")
  4506. or src_lower.endswith(".stp")
  4507. ):
  4508. raise HTTPException(
  4509. status_code=400,
  4510. detail="Archive's source file must be STL, 3MF, or STEP to slice",
  4511. )
  4512. # Match the library route: derive the sliced output's filename from
  4513. # `print_name` when set, so the new archive row's display name lines
  4514. # up with the source's display.
  4515. src_ext = Path(raw_filename).suffix.lower() or ".3mf"
  4516. src_filename = (
  4517. f"{archive.print_name.strip()}{src_ext}" if archive.print_name and archive.print_name.strip() else raw_filename
  4518. )
  4519. model_bytes = src_path.read_bytes()
  4520. archive_id_local = archive.id
  4521. user_id = current_user.id if current_user else None
  4522. # Block a cross-nozzle-class re-slice (single-nozzle <-> H2D) up front —
  4523. # BambuStudio's multi-extruder validator would otherwise reject it with a
  4524. # cryptic error. No-op for same-class or un-sliced sources.
  4525. await guard_nozzle_class_reslice(db, current_user, request, archive.sliced_for_model)
  4526. async def _run(job_id: int):
  4527. async with async_session() as task_db:
  4528. # Re-fetch the source archive on the background-task session.
  4529. src_archive = await task_db.get(PrintArchive, archive_id_local)
  4530. if src_archive is None:
  4531. raise http_exception_to_job_error(
  4532. HTTPException(status_code=404, detail="Archive disappeared during slice")
  4533. )
  4534. try:
  4535. response = await slice_and_persist_as_archive(
  4536. task_db,
  4537. model_bytes=model_bytes,
  4538. model_filename=src_filename,
  4539. request=request,
  4540. source_archive=src_archive,
  4541. current_user_id=user_id,
  4542. job_id=job_id,
  4543. )
  4544. except HTTPException as exc:
  4545. raise http_exception_to_job_error(exc) from exc
  4546. return response.model_dump()
  4547. job = await slice_dispatch.enqueue(
  4548. kind="archive",
  4549. source_id=archive.id,
  4550. source_name=archive.print_name or archive.filename or f"archive {archive.id}",
  4551. owner_id=user_id,
  4552. run=_run,
  4553. )
  4554. return {
  4555. "job_id": job.id,
  4556. "status": job.status,
  4557. "status_url": f"/api/v1/slice-jobs/{job.id}",
  4558. }
  4559. @router.post("/{archive_id}/reprint")
  4560. async def reprint_archive(
  4561. archive_id: int,
  4562. printer_id: int,
  4563. # SECURITY.md SEC-AUTH-1: every route either has an explicit auth dep or
  4564. # is in the route-auth-coverage allowlist. Gating the deprecation stub on
  4565. # QUEUE_CREATE matches the replacement route (POST /queue/) and means
  4566. # anonymous callers bounce at auth instead of seeing the deprecation
  4567. # message — leaking "this route exists" to unauthenticated callers is
  4568. # exactly the shape the backstop guards against.
  4569. _: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_CREATE),
  4570. ):
  4571. """Legacy direct reprint endpoint. Use POST /queue/ instead."""
  4572. logger.warning(
  4573. "Gone API used: POST /archives/%s/reprint?printer_id=%s; use POST /queue/ instead",
  4574. archive_id,
  4575. printer_id,
  4576. )
  4577. raise HTTPException(
  4578. status_code=410,
  4579. detail="Direct archive reprint has been removed. Create a print queue item with POST /queue/.",
  4580. )
  4581. # =============================================================================
  4582. # Project Page API
  4583. # =============================================================================
  4584. @router.get("/{archive_id}/project-page")
  4585. async def get_project_page(
  4586. archive_id: int,
  4587. db: AsyncSession = Depends(get_db),
  4588. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4589. require_ownership_permission(
  4590. Permission.ARCHIVES_READ_ALL,
  4591. Permission.ARCHIVES_READ_OWN,
  4592. )
  4593. ),
  4594. printer_scope: PrinterScope = RequestPrinterScope,
  4595. ):
  4596. """Get the project page data from the 3MF file."""
  4597. from backend.app.schemas.archive import ProjectPageResponse
  4598. from backend.app.services.archive import ProjectPageParser
  4599. user, can_read_all = auth_result
  4600. service = ArchiveService(db)
  4601. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  4602. file_path = settings.base_dir / archive.file_path
  4603. if not file_path.is_file():
  4604. raise HTTPException(404, "Archive file not found")
  4605. parser = ProjectPageParser(file_path)
  4606. data = parser.parse(archive_id)
  4607. return ProjectPageResponse(**data)
  4608. @router.patch("/{archive_id}/project-page")
  4609. async def update_project_page(
  4610. archive_id: int,
  4611. update_data: dict,
  4612. db: AsyncSession = Depends(get_db),
  4613. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4614. require_ownership_permission(
  4615. Permission.ARCHIVES_UPDATE_ALL,
  4616. Permission.ARCHIVES_UPDATE_OWN,
  4617. )
  4618. ),
  4619. printer_scope: PrinterScope = RequestPrinterScope,
  4620. ):
  4621. """Update project page metadata in the 3MF file."""
  4622. from backend.app.services.archive import ProjectPageParser
  4623. user, can_modify_all = auth_result
  4624. service = ArchiveService(db)
  4625. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_modify_all, printer_scope)
  4626. file_path = settings.base_dir / archive.file_path
  4627. if not file_path.is_file():
  4628. raise HTTPException(404, "Archive file not found")
  4629. parser = ProjectPageParser(file_path)
  4630. success = parser.update_metadata(update_data)
  4631. if not success:
  4632. raise HTTPException(500, "Failed to update project page")
  4633. # Return updated data
  4634. data = parser.parse(archive_id)
  4635. return data
  4636. @router.get("/{archive_id}/project-image/{image_path:path}")
  4637. async def get_project_image(
  4638. archive_id: int,
  4639. image_path: str,
  4640. db: AsyncSession = Depends(get_db),
  4641. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4642. require_media_token_ownership(
  4643. Permission.ARCHIVES_READ_ALL,
  4644. Permission.ARCHIVES_READ_OWN,
  4645. )
  4646. ),
  4647. printer_scope: PrinterScope = MediaOrRequestPrinterScope,
  4648. ):
  4649. """Get an image from the 3MF project page.
  4650. Requires a media token query param (?token=xxx) when auth is enabled, and
  4651. returns 404 for an archive the caller may not read (#3025).
  4652. """
  4653. user, can_read_all = auth_result
  4654. from backend.app.services.archive import ProjectPageParser
  4655. service = ArchiveService(db)
  4656. archive = _ensure_archive_visible(await service.get_archive(archive_id), user, can_read_all, printer_scope)
  4657. file_path = settings.base_dir / archive.file_path
  4658. if not file_path.is_file():
  4659. raise HTTPException(404, "Archive file not found")
  4660. parser = ProjectPageParser(file_path)
  4661. result = parser.get_image(image_path)
  4662. if not result:
  4663. raise HTTPException(404, "Image not found in 3MF file")
  4664. image_data, content_type = result
  4665. return Response(
  4666. content=image_data,
  4667. media_type=content_type,
  4668. headers={"Cache-Control": "max-age=3600"},
  4669. )
  4670. # =============================================================================
  4671. # Source 3MF API (Original Project Files)
  4672. # =============================================================================
  4673. def _resolve_source_3mf_path(archive: PrintArchive, source_filename: str) -> Path:
  4674. """Resolve where to write a source 3MF for ``archive``.
  4675. Normal archives nest the source under ``<archive_file_dir>/source/``.
  4676. "Fallback" archives (created in main.py when MQTT reports a print start
  4677. but Bambuddy never saw the source 3MF — cloud / Handy / pre-existing
  4678. SD-card prints) carry ``file_path=""``. Joining that with ``base_dir``
  4679. via the ``/`` operator silently yields ``base_dir`` itself, whose parent
  4680. is ``base_dir.parent`` — which sent the upload to ``/app/source/`` and
  4681. raised a 500 on the final ``relative_to`` (#1531). Fallback archives
  4682. now land under ``<base_dir>/archive/no_source/<archive_id>/`` instead,
  4683. which stays inside the data volume and remains addressable by every
  4684. read site that does ``base_dir / archive.source_3mf_path``.
  4685. The resolved directory is asserted to be inside ``base_dir`` even when
  4686. ``archive.file_path`` is populated, so a row corrupted by an old import
  4687. or manual SQL edit fails with a clear 500 instead of writing outside
  4688. the data volume.
  4689. """
  4690. if archive.file_path:
  4691. archive_file = settings.base_dir / archive.file_path
  4692. source_dir = archive_file.parent / "source"
  4693. else:
  4694. source_dir = settings.base_dir / "archive" / "no_source" / str(archive.id)
  4695. # Containment check via resolve() — catches absolute file_path, `..`
  4696. # traversal, and any other shape that escapes the data volume — but we
  4697. # return the *literal* source_dir below. Resolving the returned path
  4698. # would canonicalise away a symlinked DATA_DIR (legitimate on TrueNAS /
  4699. # QNAP / Synology storage pools, and any `-v /symlink:/app/data`
  4700. # mount), which would then make the caller's
  4701. # ``source_path.relative_to(settings.base_dir)`` raise because the
  4702. # left side is canonical and the right is the symlink path.
  4703. try:
  4704. source_dir.resolve().relative_to(settings.base_dir.resolve())
  4705. except ValueError as exc:
  4706. raise HTTPException(
  4707. 500,
  4708. f"Archive {archive.id} resolves to a path outside the data directory; cannot attach source.",
  4709. ) from exc
  4710. source_dir.mkdir(parents=True, exist_ok=True)
  4711. return (
  4712. source_dir / source_filename
  4713. ) # SEC-PATH-OK: callers pass _safe_filename(...) basename-stripped; source_dir resolve+relative_to checked above
  4714. @router.post("/{archive_id}/source")
  4715. async def upload_source_3mf(
  4716. archive_id: int,
  4717. file: UploadFile = File(...),
  4718. db: AsyncSession = Depends(get_db),
  4719. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4720. require_ownership_permission(
  4721. Permission.ARCHIVES_UPDATE_ALL,
  4722. Permission.ARCHIVES_UPDATE_OWN,
  4723. )
  4724. ),
  4725. printer_scope: PrinterScope = RequestPrinterScope,
  4726. ):
  4727. """Upload the original source 3MF project file for an archive."""
  4728. user, can_modify_all = auth_result
  4729. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  4730. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  4731. if not file.filename or not file.filename.endswith(".3mf"):
  4732. raise HTTPException(400, "File must be a .3mf file")
  4733. # Save the source 3MF file - preserve original filename, strip directory components
  4734. source_filename = _safe_filename(file.filename)
  4735. source_path = _resolve_source_3mf_path(archive, source_filename)
  4736. # Delete old source file if exists
  4737. if archive.source_3mf_path:
  4738. old_source_path = settings.base_dir / archive.source_3mf_path
  4739. if old_source_path.exists():
  4740. old_source_path.unlink()
  4741. content = await file.read()
  4742. # #1401: validate zip header on source 3MF uploads too — source files
  4743. # are uploaded for reprint and slicing, so an invalid one breaks the
  4744. # same downstream paths as a bad sliced file.
  4745. from backend.app.api.routes.library import validate_print_file_upload
  4746. validate_print_file_upload(file.filename, content)
  4747. source_path.write_bytes(content)
  4748. # Update archive with source path (relative to base_dir)
  4749. archive.source_3mf_path = str(source_path.relative_to(settings.base_dir))
  4750. await db.commit()
  4751. await db.refresh(archive)
  4752. return {
  4753. "status": "uploaded",
  4754. "source_3mf_path": archive.source_3mf_path,
  4755. "filename": source_filename,
  4756. }
  4757. @router.get("/{archive_id}/source")
  4758. async def download_source_3mf(
  4759. archive_id: int,
  4760. db: AsyncSession = Depends(get_db),
  4761. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4762. require_ownership_permission(
  4763. Permission.ARCHIVES_READ_ALL,
  4764. Permission.ARCHIVES_READ_OWN,
  4765. )
  4766. ),
  4767. printer_scope: PrinterScope = RequestPrinterScope,
  4768. ):
  4769. """Download the source 3MF project file."""
  4770. user, can_read_all = auth_result
  4771. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  4772. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_read_all, printer_scope)
  4773. if not archive.source_3mf_path:
  4774. raise HTTPException(404, "No source 3MF attached to this archive")
  4775. source_path = settings.base_dir / archive.source_3mf_path
  4776. if not source_path.exists():
  4777. raise HTTPException(404, "Source 3MF file not found on disk")
  4778. # Use the actual filename from the path
  4779. filename = source_path.name
  4780. return FileResponse(
  4781. path=source_path,
  4782. filename=filename,
  4783. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  4784. )
  4785. @router.get("/{archive_id}/source/{filename}")
  4786. async def download_source_3mf_for_slicer(
  4787. archive_id: int,
  4788. filename: str,
  4789. db: AsyncSession = Depends(get_db),
  4790. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4791. require_ownership_permission(
  4792. Permission.ARCHIVES_READ_ALL,
  4793. Permission.ARCHIVES_READ_OWN,
  4794. )
  4795. ),
  4796. printer_scope: PrinterScope = RequestPrinterScope,
  4797. ):
  4798. """Download source 3MF with filename in URL."""
  4799. user, can_read_all = auth_result
  4800. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  4801. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_read_all, printer_scope)
  4802. if not archive.source_3mf_path:
  4803. raise HTTPException(404, "No source 3MF attached to this archive")
  4804. source_path = settings.base_dir / archive.source_3mf_path
  4805. if not source_path.exists():
  4806. raise HTTPException(404, "Source 3MF file not found on disk")
  4807. return FileResponse(
  4808. path=source_path,
  4809. filename=filename if filename.endswith(".3mf") else f"{filename}.3mf",
  4810. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  4811. )
  4812. @router.post("/{archive_id}/source-slicer-token")
  4813. async def create_source_slicer_token(
  4814. archive_id: int,
  4815. db: AsyncSession = Depends(get_db),
  4816. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4817. require_ownership_permission(
  4818. Permission.ARCHIVES_READ_ALL,
  4819. Permission.ARCHIVES_READ_OWN,
  4820. )
  4821. ),
  4822. printer_scope: PrinterScope = RequestPrinterScope,
  4823. ):
  4824. """Create a short-lived download token for opening source 3MF in slicer."""
  4825. from backend.app.core.auth import create_slicer_download_token
  4826. user, can_read_all = auth_result
  4827. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  4828. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_read_all, printer_scope)
  4829. if not archive.source_3mf_path:
  4830. raise HTTPException(404, "No source 3MF attached to this archive")
  4831. token = await create_slicer_download_token("source", archive_id)
  4832. return {"token": token}
  4833. @router.get("/{archive_id}/source-dl/{token}/{filename}")
  4834. async def download_source_3mf_for_slicer_with_token(
  4835. archive_id: int,
  4836. token: str,
  4837. filename: str,
  4838. db: AsyncSession = Depends(get_db),
  4839. ):
  4840. """Download source 3MF using a slicer download token.
  4841. Token-authenticated (no auth headers needed). The token is short-lived and
  4842. archive-bound, created by POST /{archive_id}/source-slicer-token, and
  4843. redeemable for the rest of its TTL rather than exactly once (#3029).
  4844. """
  4845. from backend.app.core.auth import verify_slicer_download_token
  4846. if not await verify_slicer_download_token(token, "source", archive_id, single_use=False):
  4847. raise HTTPException(403, "Invalid or expired download token")
  4848. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  4849. archive = result.scalar_one_or_none()
  4850. if not archive:
  4851. raise HTTPException(404, "Archive not found")
  4852. if not archive.source_3mf_path:
  4853. raise HTTPException(404, "No source 3MF attached to this archive")
  4854. source_path = settings.base_dir / archive.source_3mf_path
  4855. if not source_path.exists():
  4856. raise HTTPException(404, "Source 3MF file not found on disk")
  4857. return FileResponse(
  4858. path=source_path,
  4859. filename=filename if filename.endswith(".3mf") else f"{filename}.3mf",
  4860. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  4861. )
  4862. @router.post("/upload-source")
  4863. async def upload_source_3mf_by_name(
  4864. file: UploadFile = File(...),
  4865. print_name: str = Query(None, description="Match archive by print name"),
  4866. db: AsyncSession = Depends(get_db),
  4867. _: User | None = RequirePermissionIfAuthEnabled(Permission.ARCHIVES_UPDATE_ALL),
  4868. printer_scope: PrinterScope = RequestPrinterScope,
  4869. ):
  4870. """Upload source 3MF and match to archive by print name.
  4871. This endpoint is designed for slicer post-processing scripts.
  4872. It finds the most recent archive matching the print name and attaches the source.
  4873. """
  4874. if not file.filename or not file.filename.endswith(".3mf"):
  4875. raise HTTPException(400, "File must be a .3mf file")
  4876. safe_filename = _safe_filename(file.filename)
  4877. # Derive print name from filename if not provided
  4878. if not print_name:
  4879. # Remove .3mf extension and common suffixes
  4880. print_name = safe_filename.rsplit(".3mf", 1)[0]
  4881. # Remove _source suffix if present
  4882. if print_name.endswith("_source"):
  4883. print_name = print_name[:-7]
  4884. # Find matching archive - try exact match first, then fuzzy
  4885. # Only an archive on the caller's printers can be matched, as it is listed.
  4886. visible = _visible_archive_clauses(None, True, printer_scope)
  4887. result = await db.execute(
  4888. select(PrintArchive)
  4889. .where(PrintArchive.print_name == print_name, *visible)
  4890. .order_by(PrintArchive.created_at.desc())
  4891. .limit(1)
  4892. )
  4893. archive = result.scalar_one_or_none()
  4894. if not archive:
  4895. # Try matching filename without .gcode.3mf
  4896. result = await db.execute(
  4897. select(PrintArchive)
  4898. .where(PrintArchive.filename.like(f"{print_name}%"), *visible)
  4899. .order_by(PrintArchive.created_at.desc())
  4900. .limit(1)
  4901. )
  4902. archive = result.scalar_one_or_none()
  4903. if not archive:
  4904. # Try case-insensitive partial match on print_name
  4905. result = await db.execute(
  4906. select(PrintArchive)
  4907. .where(PrintArchive.print_name.ilike(f"%{print_name}%"), *visible)
  4908. .order_by(PrintArchive.created_at.desc())
  4909. .limit(1)
  4910. )
  4911. archive = result.scalar_one_or_none()
  4912. if not archive:
  4913. raise HTTPException(404, f"No archive found matching '{print_name}'")
  4914. # Save the source 3MF file - preserve original filename, strip directory components
  4915. source_filename = safe_filename
  4916. source_path = _resolve_source_3mf_path(archive, source_filename)
  4917. # Delete old source file if exists
  4918. if archive.source_3mf_path:
  4919. old_source_path = settings.base_dir / archive.source_3mf_path
  4920. if old_source_path.exists():
  4921. old_source_path.unlink()
  4922. content = await file.read()
  4923. # #1401: same zip-header check as the other upload routes — the
  4924. # match-by-name endpoint is used by slicer post-processing scripts,
  4925. # so a misconfigured script is exactly how a bad 3MF would slip in.
  4926. from backend.app.api.routes.library import validate_print_file_upload
  4927. validate_print_file_upload(file.filename, content)
  4928. source_path.write_bytes(content)
  4929. # Update archive with source path
  4930. archive.source_3mf_path = str(source_path.relative_to(settings.base_dir))
  4931. await db.commit()
  4932. await db.refresh(archive)
  4933. return {
  4934. "status": "uploaded",
  4935. "archive_id": archive.id,
  4936. "archive_name": archive.print_name or archive.filename,
  4937. "source_3mf_path": archive.source_3mf_path,
  4938. "filename": source_filename,
  4939. }
  4940. @router.delete("/{archive_id}/source")
  4941. async def delete_source_3mf(
  4942. archive_id: int,
  4943. db: AsyncSession = Depends(get_db),
  4944. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4945. require_ownership_permission(
  4946. Permission.ARCHIVES_DELETE_ALL,
  4947. Permission.ARCHIVES_DELETE_OWN,
  4948. )
  4949. ),
  4950. printer_scope: PrinterScope = RequestPrinterScope,
  4951. ):
  4952. """Delete the source 3MF project file from an archive."""
  4953. user, can_modify_all = auth_result
  4954. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  4955. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  4956. if not archive.source_3mf_path:
  4957. raise HTTPException(404, "No source 3MF attached to this archive")
  4958. # Delete the file
  4959. source_path = settings.base_dir / archive.source_3mf_path
  4960. if source_path.exists():
  4961. source_path.unlink()
  4962. # Clear the path in database
  4963. archive.source_3mf_path = None
  4964. await db.commit()
  4965. return {"status": "deleted"}
  4966. # =============================================================================
  4967. # F3D API (Fusion 360 Design Files)
  4968. # =============================================================================
  4969. @router.post("/{archive_id}/f3d")
  4970. async def upload_f3d(
  4971. archive_id: int,
  4972. file: UploadFile = File(...),
  4973. db: AsyncSession = Depends(get_db),
  4974. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  4975. require_ownership_permission(
  4976. Permission.ARCHIVES_UPDATE_ALL,
  4977. Permission.ARCHIVES_UPDATE_OWN,
  4978. )
  4979. ),
  4980. printer_scope: PrinterScope = RequestPrinterScope,
  4981. ):
  4982. """Upload a Fusion 360 design file for an archive."""
  4983. user, can_modify_all = auth_result
  4984. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  4985. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  4986. if not file.filename or not file.filename.endswith(".f3d"):
  4987. raise HTTPException(400, "File must be a .f3d file")
  4988. # Get archive directory and create f3d subdirectory
  4989. file_path = settings.base_dir / archive.file_path
  4990. archive_dir = file_path.parent
  4991. f3d_dir = archive_dir / "f3d"
  4992. f3d_dir.mkdir(exist_ok=True)
  4993. # Delete old F3D file if exists
  4994. if archive.f3d_path:
  4995. old_f3d_path = settings.base_dir / archive.f3d_path
  4996. if old_f3d_path.exists():
  4997. old_f3d_path.unlink()
  4998. # Save the F3D file - preserve original filename, strip directory components
  4999. f3d_filename = _safe_filename(file.filename)
  5000. f3d_path = f3d_dir / f3d_filename # SEC-PATH-OK: f3d_filename = _safe_filename(...) basename-stripped above
  5001. content = await file.read()
  5002. f3d_path.write_bytes(content)
  5003. # Update archive with F3D path (relative to base_dir)
  5004. archive.f3d_path = str(f3d_path.relative_to(settings.base_dir))
  5005. await db.commit()
  5006. await db.refresh(archive)
  5007. return {
  5008. "status": "uploaded",
  5009. "f3d_path": archive.f3d_path,
  5010. "filename": f3d_filename,
  5011. }
  5012. @router.get("/{archive_id}/f3d")
  5013. async def download_f3d(
  5014. archive_id: int,
  5015. db: AsyncSession = Depends(get_db),
  5016. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  5017. require_ownership_permission(
  5018. Permission.ARCHIVES_READ_ALL,
  5019. Permission.ARCHIVES_READ_OWN,
  5020. )
  5021. ),
  5022. printer_scope: PrinterScope = RequestPrinterScope,
  5023. ):
  5024. """Download the Fusion 360 design file."""
  5025. user, can_read_all = auth_result
  5026. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  5027. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_read_all, printer_scope)
  5028. if not archive.f3d_path:
  5029. raise HTTPException(404, "No F3D file attached to this archive")
  5030. f3d_path = settings.base_dir / archive.f3d_path
  5031. if not f3d_path.exists():
  5032. raise HTTPException(404, "F3D file not found on disk")
  5033. # Use the actual filename from the path
  5034. filename = f3d_path.name
  5035. return FileResponse(
  5036. path=f3d_path,
  5037. filename=filename,
  5038. media_type="application/octet-stream",
  5039. )
  5040. @router.delete("/{archive_id}/f3d")
  5041. async def delete_f3d(
  5042. archive_id: int,
  5043. db: AsyncSession = Depends(get_db),
  5044. auth_result: tuple[User | ApiKeyActor | None, bool] = Depends(
  5045. require_ownership_permission(
  5046. Permission.ARCHIVES_DELETE_ALL,
  5047. Permission.ARCHIVES_DELETE_OWN,
  5048. )
  5049. ),
  5050. printer_scope: PrinterScope = RequestPrinterScope,
  5051. ):
  5052. """Delete the Fusion 360 design file from an archive."""
  5053. user, can_modify_all = auth_result
  5054. result = await db.execute(select(PrintArchive).where(PrintArchive.id == archive_id))
  5055. archive = _ensure_archive_visible(result.scalar_one_or_none(), user, can_modify_all, printer_scope)
  5056. if not archive.f3d_path:
  5057. raise HTTPException(404, "No F3D file attached to this archive")
  5058. # Delete the file
  5059. f3d_path = settings.base_dir / archive.f3d_path
  5060. if f3d_path.exists():
  5061. f3d_path.unlink()
  5062. # Clear the path in database
  5063. archive.f3d_path = None
  5064. await db.commit()
  5065. return {"status": "deleted"}