library.py 159 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965
  1. """API routes for File Manager (Library) functionality."""
  2. import base64
  3. import binascii
  4. import contextlib
  5. import hashlib
  6. import json
  7. import logging
  8. import os
  9. import re
  10. import shutil
  11. import uuid
  12. import zipfile
  13. from datetime import datetime, timezone
  14. from pathlib import Path
  15. from fastapi import APIRouter, Depends, File, HTTPException, Query, Response, UploadFile
  16. from fastapi.responses import FileResponse as FastAPIFileResponse
  17. from sqlalchemy import func, select
  18. from sqlalchemy.ext.asyncio import AsyncSession
  19. from sqlalchemy.orm import selectinload
  20. from backend.app.api.routes.cloud import resolve_api_key_cloud_owner
  21. from backend.app.core.auth import (
  22. RequireCameraStreamTokenIfAuthEnabled,
  23. require_ownership_permission,
  24. require_permission_if_auth_enabled,
  25. )
  26. from backend.app.core.config import settings as app_settings
  27. from backend.app.core.database import get_db
  28. from backend.app.core.permissions import Permission
  29. from backend.app.models.archive import PrintArchive
  30. from backend.app.models.library import LibraryFile, LibraryFolder
  31. from backend.app.models.print_queue import PrintQueueItem
  32. from backend.app.models.project import Project
  33. from backend.app.models.user import User
  34. from backend.app.schemas.library import (
  35. AddToQueueError,
  36. AddToQueueRequest,
  37. AddToQueueResponse,
  38. AddToQueueResult,
  39. BatchThumbnailRequest,
  40. BatchThumbnailResponse,
  41. BatchThumbnailResult,
  42. BulkDeleteRequest,
  43. BulkDeleteResponse,
  44. ExternalFolderCreate,
  45. FileDuplicate,
  46. FileListResponse,
  47. FileMoveRequest,
  48. FilePrintRequest,
  49. FileResponse as FileResponseSchema,
  50. FileUpdate,
  51. FileUploadResponse,
  52. FolderCreate,
  53. FolderResponse,
  54. FolderTreeItem,
  55. FolderUpdate,
  56. ZipExtractError,
  57. ZipExtractResponse,
  58. ZipExtractResult,
  59. )
  60. from backend.app.schemas.slicer import SliceRequest, SliceResponse
  61. from backend.app.services.archive import ThreeMFParser
  62. from backend.app.services.stl_thumbnail import generate_stl_thumbnail
  63. from backend.app.utils.threemf_tools import (
  64. extract_nozzle_mapping_from_3mf,
  65. extract_project_filaments_from_3mf,
  66. extract_source_printer_model_from_3mf,
  67. )
  68. logger = logging.getLogger(__name__)
  69. router = APIRouter(prefix="/library", tags=["library"])
  70. def get_library_dir() -> Path:
  71. """Get the library storage directory."""
  72. base_dir = Path(app_settings.archive_dir)
  73. library_dir = base_dir / "library"
  74. library_dir.mkdir(parents=True, exist_ok=True)
  75. return library_dir
  76. def get_library_files_dir() -> Path:
  77. """Get the directory for library files."""
  78. files_dir = get_library_dir() / "files"
  79. files_dir.mkdir(parents=True, exist_ok=True)
  80. return files_dir
  81. def get_library_thumbnails_dir() -> Path:
  82. """Get the directory for library thumbnails."""
  83. thumbnails_dir = get_library_dir() / "thumbnails"
  84. thumbnails_dir.mkdir(parents=True, exist_ok=True)
  85. return thumbnails_dir
  86. def to_relative_path(absolute_path: Path | str) -> str:
  87. """Convert an absolute path to a path relative to base_dir for storage."""
  88. if not absolute_path:
  89. return ""
  90. abs_path = Path(absolute_path)
  91. base_dir = Path(app_settings.base_dir)
  92. try:
  93. return str(abs_path.relative_to(base_dir))
  94. except ValueError:
  95. # Path is not under base_dir, return as-is (shouldn't happen normally)
  96. return str(abs_path)
  97. def to_absolute_path(relative_path: str | None) -> Path | None:
  98. """Convert a relative path (from database) to an absolute path for file operations."""
  99. if not relative_path:
  100. return None
  101. path = Path(relative_path)
  102. # Handle already-absolute paths verbatim (backwards compatibility during migration).
  103. # Legacy DB rows may store absolute paths that predate the base_dir layout; the
  104. # traversal guard below only applies to relative paths coming from user input.
  105. if path.is_absolute():
  106. return path.resolve()
  107. base = Path(app_settings.base_dir).resolve()
  108. resolved = (base / relative_path).resolve()
  109. # Guard against path traversal — resolved path must stay inside base_dir.
  110. # Use is_relative_to() to avoid the /data/app vs /data/app_evil prefix confusion
  111. # that a plain startswith(str(base)) check would miss.
  112. if not resolved.is_relative_to(base):
  113. raise ValueError(f"Path escapes base directory: {relative_path!r}")
  114. return resolved
  115. def calculate_file_hash(file_path: Path) -> str:
  116. """Calculate SHA256 hash of a file."""
  117. sha256_hash = hashlib.sha256()
  118. with open(file_path, "rb") as f:
  119. for byte_block in iter(lambda: f.read(4096), b""):
  120. sha256_hash.update(byte_block)
  121. return sha256_hash.hexdigest()
  122. def _resolve_upload_destination(target_folder: LibraryFolder | None, filename: str) -> tuple[Path, bool]:
  123. """Resolve the on-disk destination for an uploaded file.
  124. Non-external target: returns ``(<library_files_dir>/<uuid><ext>, False)``.
  125. Writable external target: writes to ``<external_path>/<filename>``
  126. (preserves the real filename so the file is recognisable on the mount);
  127. returns ``(dest, True)``. Raises ``HTTPException`` for read-only external
  128. folders (403), missing/inaccessible/non-writable external paths (400), and
  129. filename collisions on the external mount (409). See #1112 — previously
  130. uploads to writable external folders were silently misrouted to the
  131. internal library dir.
  132. """
  133. if target_folder is not None and target_folder.is_external:
  134. if target_folder.external_readonly:
  135. raise HTTPException(status_code=403, detail="Cannot upload to a read-only external folder")
  136. if not target_folder.external_path:
  137. raise HTTPException(status_code=400, detail="External folder has no configured path")
  138. ext_dir = Path(target_folder.external_path)
  139. if not ext_dir.exists() or not ext_dir.is_dir():
  140. raise HTTPException(
  141. status_code=400,
  142. detail=f"External path is not accessible: {target_folder.external_path}",
  143. )
  144. if not os.access(ext_dir, os.W_OK):
  145. raise HTTPException(
  146. status_code=400,
  147. detail=f"External path is not writable: {target_folder.external_path}",
  148. )
  149. # Guard against path-traversal via a pathological filename — join then
  150. # verify the resolved destination is still inside the external dir.
  151. dest = (ext_dir / filename).resolve()
  152. try:
  153. dest.relative_to(ext_dir.resolve())
  154. except ValueError:
  155. raise HTTPException(status_code=400, detail="Invalid filename")
  156. if dest.exists():
  157. raise HTTPException(
  158. status_code=409,
  159. detail=f"A file named {filename!r} already exists in the external folder",
  160. )
  161. return dest, True
  162. ext = os.path.splitext(filename)[1].lower()
  163. return get_library_files_dir() / f"{uuid.uuid4().hex}{ext}", False
  164. def _stored_file_path(abs_path: Path, is_external: bool) -> str:
  165. """Produce the value to persist in ``LibraryFile.file_path``.
  166. External files store the absolute mount path directly (same as scan does),
  167. so ``to_absolute_path`` round-trips through its ``is_absolute()`` fast
  168. path. Managed files store a path relative to ``base_dir`` for portability.
  169. """
  170. return str(abs_path) if is_external else to_relative_path(abs_path)
  171. class _MoveSkip(Exception):
  172. """Signalled by ``_move_file_bytes`` to skip a file with a user-visible reason.
  173. Carries an optional `code` for machine-friendly grouping (the
  174. front-end can localise it) and a fallback English `reason` for logs.
  175. """
  176. def __init__(self, code: str, reason: str):
  177. super().__init__(reason)
  178. self.code = code
  179. self.reason = reason
  180. def _resolve_source_disk_path(file: LibraryFile) -> Path | None:
  181. """Return the absolute on-disk path for an existing LibraryFile, or None
  182. if it can't be located (legacy DB row, deleted file, etc.)."""
  183. if file.is_external:
  184. return Path(file.file_path) if file.file_path else None
  185. return to_absolute_path(file.file_path)
  186. def _move_file_bytes(file: LibraryFile, target_folder: LibraryFolder | None) -> str:
  187. """Physically relocate `file`'s bytes to match `target_folder`.
  188. Used by the move endpoint when source/target straddle the
  189. managed↔external boundary (#1112 follow-up — the prior implementation
  190. updated the DB row's ``folder_id`` but never moved the bytes, so a
  191. file moved to an external SMB folder showed up in Bambuddy's UI but
  192. not on the NAS).
  193. Returns the new ``file_path`` value to persist (relative for managed
  194. targets, absolute for external targets — matches the upload + scan
  195. paths). Raises ``_MoveSkip`` for any condition that would make the
  196. move unsafe (target unwritable, filename collision, source missing).
  197. The copy-then-unlink ordering means a partial copy followed by a
  198. failed unlink leaves both the source and the dest on disk — better
  199. than the symmetric "rename or move" which would lose the source if
  200. the target write didn't complete on a flaky mount. The DB row stays
  201. pointed at the source until the caller commits the new ``file_path``.
  202. """
  203. src = _resolve_source_disk_path(file)
  204. if not src or not src.exists():
  205. raise _MoveSkip("source_missing", "source file missing on disk")
  206. target_is_external = target_folder is not None and target_folder.is_external
  207. if target_is_external:
  208. if target_folder.external_readonly:
  209. # Already blocked at top level, but defence-in-depth.
  210. raise _MoveSkip("target_readonly", "target external folder is read-only")
  211. if not target_folder.external_path:
  212. raise _MoveSkip("target_misconfigured", "target external folder has no path")
  213. ext_dir = Path(target_folder.external_path)
  214. if not ext_dir.exists() or not ext_dir.is_dir():
  215. raise _MoveSkip("target_inaccessible", f"target path not accessible: {ext_dir}")
  216. if not os.access(ext_dir, os.W_OK):
  217. raise _MoveSkip("target_unwritable", f"target path not writable: {ext_dir}")
  218. dest = (ext_dir / file.filename).resolve()
  219. try:
  220. dest.relative_to(ext_dir.resolve())
  221. except ValueError:
  222. raise _MoveSkip("invalid_filename", f"unsafe filename: {file.filename!r}") from None
  223. if dest.exists():
  224. raise _MoveSkip("name_collision", f"a file named {file.filename!r} already exists in target")
  225. try:
  226. shutil.copy2(src, dest)
  227. except OSError as e:
  228. # Clean up partial dest so a retry can succeed.
  229. with contextlib.suppress(OSError):
  230. dest.unlink(missing_ok=True)
  231. raise _MoveSkip("copy_failed", f"copy failed: {e}") from e
  232. else:
  233. # → managed (root or non-external folder): generate a fresh UUID
  234. # filename in the internal store so we don't collide with another
  235. # file that happens to share `filename`.
  236. ext = src.suffix.lower()
  237. dest = get_library_files_dir() / f"{uuid.uuid4().hex}{ext}"
  238. try:
  239. shutil.copy2(src, dest)
  240. except OSError as e:
  241. with contextlib.suppress(OSError):
  242. dest.unlink(missing_ok=True)
  243. raise _MoveSkip("copy_failed", f"copy failed: {e}") from e
  244. # Copy succeeded — unlink the original. A failure here leaves an
  245. # orphan on disk but the DB row is consistent against the new dest.
  246. try:
  247. src.unlink(missing_ok=True)
  248. except OSError as e:
  249. logger.warning(
  250. "Move: copied %s → %s but couldn't remove source: %s",
  251. src,
  252. dest,
  253. e,
  254. )
  255. return _stored_file_path(dest, is_external=target_is_external)
  256. def _clean_3mf_metadata(obj):
  257. """Strip bytes and thumbnail-carrier keys so the payload is JSON-storable.
  258. Shared by ``upload_file`` and :func:`save_3mf_bytes_to_library` — the
  259. ``ThreeMFParser`` output embeds the thumbnail bytes under
  260. ``_thumbnail_data``/``_thumbnail_ext`` and may also include raw bytes in
  261. other fields, none of which can be JSON-encoded.
  262. """
  263. if isinstance(obj, dict):
  264. return {
  265. k: _clean_3mf_metadata(v)
  266. for k, v in obj.items()
  267. if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext")
  268. }
  269. if isinstance(obj, list):
  270. return [_clean_3mf_metadata(i) for i in obj if not isinstance(i, bytes)]
  271. if isinstance(obj, bytes):
  272. return None
  273. return obj
  274. async def save_3mf_bytes_to_library(
  275. db: AsyncSession,
  276. *,
  277. file_bytes: bytes,
  278. filename: str,
  279. folder_id: int | None = None,
  280. source_type: str | None = None,
  281. source_url: str | None = None,
  282. owner_id: int | None = None,
  283. ) -> tuple[LibraryFile, bool]:
  284. """Save a 3MF blob into the library and return ``(library_file, was_existing)``.
  285. Used by routes that receive a 3MF in-process rather than as a multipart
  286. upload (currently: MakerWorld import; reusable for any future source that
  287. fetches bytes server-side). Deduplicates by ``source_url`` when provided —
  288. if a LibraryFile with the same source_url already exists, the existing
  289. row is returned and the bytes are NOT re-saved (MakerWorld signed URLs
  290. change each download, so hash-based dedupe alone would miss re-imports).
  291. Parses 3MF metadata + thumbnail the same way the multipart upload route
  292. does, via :class:`ThreeMFParser`. Paths are stored as relative so the
  293. library is portable across installs.
  294. """
  295. # Source-URL-based dedupe: return the existing row untouched.
  296. if source_url:
  297. existing = await db.execute(LibraryFile.active().where(LibraryFile.source_url == source_url).limit(1))
  298. existing_row = existing.scalar_one_or_none()
  299. if existing_row is not None:
  300. return existing_row, True
  301. # Persist bytes to disk under a UUID-scoped filename; keep the original
  302. # extension so downstream logic (ThreeMFParser, thumbnail viewer) works.
  303. ext = os.path.splitext(filename)[1].lower() or ".3mf"
  304. unique_filename = f"{uuid.uuid4().hex}{ext}"
  305. file_path = get_library_files_dir() / unique_filename
  306. with open(file_path, "wb") as fh:
  307. fh.write(file_bytes)
  308. file_hash = calculate_file_hash(file_path)
  309. # Extract metadata + thumbnail from the 3MF.
  310. metadata: dict | None = None
  311. thumbnail_path: str | None = None
  312. if ext == ".3mf":
  313. try:
  314. parser = ThreeMFParser(str(file_path))
  315. raw_metadata = parser.parse()
  316. thumb_data = raw_metadata.get("_thumbnail_data")
  317. thumb_ext = raw_metadata.get("_thumbnail_ext", ".png")
  318. if thumb_data:
  319. thumbs_dir = get_library_thumbnails_dir()
  320. thumb_filename = f"{uuid.uuid4().hex}{thumb_ext}"
  321. thumb_path = thumbs_dir / thumb_filename
  322. with open(thumb_path, "wb") as fh:
  323. fh.write(thumb_data)
  324. thumbnail_path = str(thumb_path)
  325. metadata = _clean_3mf_metadata(raw_metadata) or None
  326. except Exception as exc:
  327. # Matches the multipart upload route's behaviour — a bad 3MF should
  328. # still land in the library so the user can see / delete it rather
  329. # than failing the whole request.
  330. logger.warning("Failed to parse 3MF %s: %s", filename, exc)
  331. library_file = LibraryFile(
  332. folder_id=folder_id,
  333. filename=filename,
  334. file_path=to_relative_path(file_path),
  335. file_type=ext[1:] if ext else "unknown",
  336. file_size=len(file_bytes),
  337. file_hash=file_hash,
  338. thumbnail_path=to_relative_path(thumbnail_path) if thumbnail_path else None,
  339. file_metadata=metadata,
  340. source_type=source_type,
  341. source_url=source_url,
  342. created_by_id=owner_id,
  343. )
  344. db.add(library_file)
  345. await db.commit()
  346. await db.refresh(library_file)
  347. return library_file, False
  348. def extract_gcode_thumbnail(file_path: Path) -> bytes | None:
  349. """Extract embedded thumbnail from gcode file.
  350. Supports PrusaSlicer/BambuStudio format:
  351. ; thumbnail begin WxH SIZE
  352. ; base64data...
  353. ; thumbnail end
  354. """
  355. try:
  356. thumbnail_data = None
  357. in_thumbnail = False
  358. thumbnail_lines = []
  359. best_size = 0
  360. with open(file_path, errors="ignore") as f:
  361. # Only read first 50KB for performance (thumbnails are at the start)
  362. content = f.read(50000)
  363. for line in content.split("\n"):
  364. line = line.strip()
  365. # Check for thumbnail start
  366. if line.startswith("; thumbnail begin"):
  367. in_thumbnail = True
  368. thumbnail_lines = []
  369. # Parse dimensions: "; thumbnail begin 300x300 12345"
  370. match = re.search(r"(\d+)x(\d+)", line)
  371. if match:
  372. width = int(match.group(1))
  373. # Prefer larger thumbnails (up to 300px)
  374. if width > best_size and width <= 300:
  375. best_size = width
  376. continue
  377. # Check for thumbnail end
  378. if line.startswith("; thumbnail end"):
  379. if in_thumbnail and thumbnail_lines:
  380. try:
  381. # Decode the base64 data
  382. b64_data = "".join(thumbnail_lines)
  383. decoded = base64.b64decode(b64_data)
  384. # Only keep if this is the best size or first valid thumbnail
  385. if thumbnail_data is None or best_size > 0:
  386. thumbnail_data = decoded
  387. except (binascii.Error, ValueError):
  388. pass # Skip thumbnail with invalid base64 data
  389. in_thumbnail = False
  390. thumbnail_lines = []
  391. continue
  392. # Collect thumbnail data
  393. if in_thumbnail and line.startswith(";"):
  394. # Remove the leading "; " or ";"
  395. data_line = line[1:].strip()
  396. if data_line:
  397. thumbnail_lines.append(data_line)
  398. return thumbnail_data
  399. except Exception as e:
  400. logger.warning("Failed to extract gcode thumbnail: %s", e)
  401. return None
  402. def create_image_thumbnail(file_path: Path, thumbnails_dir: Path, max_size: int = 256) -> str | None:
  403. """Create a thumbnail from an image file.
  404. For small images, copies directly. For larger images, resizes.
  405. Returns the thumbnail path or None on failure.
  406. """
  407. try:
  408. from PIL import Image
  409. thumb_filename = f"{uuid.uuid4().hex}.png"
  410. thumb_path = thumbnails_dir / thumb_filename
  411. with Image.open(file_path) as img:
  412. # Convert to RGB if necessary (for PNG with transparency, etc.)
  413. if img.mode in ("RGBA", "LA", "P"):
  414. # Create white background for transparency
  415. background = Image.new("RGB", img.size, (255, 255, 255))
  416. if img.mode == "P":
  417. img = img.convert("RGBA")
  418. background.paste(img, mask=img.split()[-1] if img.mode == "RGBA" else None)
  419. img = background
  420. elif img.mode != "RGB":
  421. img = img.convert("RGB")
  422. # Resize if larger than max_size
  423. if img.width > max_size or img.height > max_size:
  424. img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
  425. img.save(thumb_path, "PNG", optimize=True)
  426. return str(thumb_path)
  427. except ImportError:
  428. # PIL not installed, just copy the file if it's small enough
  429. logger.warning("PIL not installed, copying image as thumbnail")
  430. try:
  431. file_size = file_path.stat().st_size
  432. if file_size < 500000: # Less than 500KB
  433. thumb_filename = f"{uuid.uuid4().hex}{file_path.suffix}"
  434. thumb_path = thumbnails_dir / thumb_filename
  435. shutil.copy2(file_path, thumb_path)
  436. return str(thumb_path)
  437. except OSError:
  438. pass # File inaccessible; fall through to return None
  439. return None
  440. except Exception as e:
  441. logger.warning("Failed to create image thumbnail: %s", e)
  442. return None
  443. # Supported image extensions for thumbnails
  444. IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".tiff", ".tif"}
  445. # ============ Folder Endpoints ============
  446. @router.get("/folders", response_model=list[FolderTreeItem])
  447. @router.get("/folders/", response_model=list[FolderTreeItem])
  448. async def list_folders(
  449. response: Response,
  450. db: AsyncSession = Depends(get_db),
  451. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  452. ):
  453. """Get all folders as a tree structure."""
  454. # Prevent browser caching of folder list
  455. response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
  456. # Get all folders with project and archive joins
  457. result = await db.execute(
  458. select(LibraryFolder, Project.name, PrintArchive.print_name)
  459. .outerjoin(Project, LibraryFolder.project_id == Project.id)
  460. .outerjoin(PrintArchive, LibraryFolder.archive_id == PrintArchive.id)
  461. .order_by(LibraryFolder.name)
  462. )
  463. rows = result.all()
  464. # Get file counts per folder
  465. file_counts_result = await db.execute(
  466. select(LibraryFile.folder_id, func.count(LibraryFile.id))
  467. .where(LibraryFile.folder_id.isnot(None), LibraryFile.deleted_at.is_(None))
  468. .group_by(LibraryFile.folder_id)
  469. )
  470. file_counts = dict(file_counts_result.all())
  471. # Build tree structure
  472. folder_map = {}
  473. root_folders = []
  474. for folder, project_name, archive_name in rows:
  475. folder_item = FolderTreeItem(
  476. id=folder.id,
  477. name=folder.name,
  478. parent_id=folder.parent_id,
  479. project_id=folder.project_id,
  480. archive_id=folder.archive_id,
  481. project_name=project_name,
  482. archive_name=archive_name,
  483. is_external=folder.is_external,
  484. external_path=folder.external_path,
  485. external_readonly=folder.external_readonly,
  486. file_count=file_counts.get(folder.id, 0),
  487. children=[],
  488. )
  489. folder_map[folder.id] = folder_item
  490. # Link children to parents
  491. for folder, _, _ in rows:
  492. folder_item = folder_map[folder.id]
  493. if folder.parent_id is None:
  494. root_folders.append(folder_item)
  495. elif folder.parent_id in folder_map:
  496. folder_map[folder.parent_id].children.append(folder_item)
  497. return root_folders
  498. @router.get("/folders/by-project/{project_id}", response_model=list[FolderResponse])
  499. async def get_folders_by_project(
  500. project_id: int,
  501. db: AsyncSession = Depends(get_db),
  502. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  503. ):
  504. """Get all folders linked to a specific project."""
  505. result = await db.execute(
  506. select(LibraryFolder, Project.name)
  507. .outerjoin(Project, LibraryFolder.project_id == Project.id)
  508. .where(LibraryFolder.project_id == project_id)
  509. .order_by(LibraryFolder.name)
  510. )
  511. rows = result.all()
  512. folders = []
  513. for folder, project_name in rows:
  514. # Get file count
  515. file_count_result = await db.execute(
  516. select(func.count(LibraryFile.id)).where(
  517. LibraryFile.folder_id == folder.id,
  518. LibraryFile.deleted_at.is_(None),
  519. )
  520. )
  521. file_count = file_count_result.scalar() or 0
  522. folders.append(
  523. FolderResponse(
  524. id=folder.id,
  525. name=folder.name,
  526. parent_id=folder.parent_id,
  527. project_id=folder.project_id,
  528. archive_id=folder.archive_id,
  529. project_name=project_name,
  530. archive_name=None,
  531. is_external=folder.is_external,
  532. external_path=folder.external_path,
  533. external_readonly=folder.external_readonly,
  534. external_show_hidden=folder.external_show_hidden,
  535. file_count=file_count,
  536. created_at=folder.created_at,
  537. updated_at=folder.updated_at,
  538. )
  539. )
  540. return folders
  541. @router.get("/folders/by-archive/{archive_id}", response_model=list[FolderResponse])
  542. async def get_folders_by_archive(
  543. archive_id: int,
  544. db: AsyncSession = Depends(get_db),
  545. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  546. ):
  547. """Get all folders linked to a specific archive."""
  548. result = await db.execute(
  549. select(LibraryFolder, PrintArchive.print_name)
  550. .outerjoin(PrintArchive, LibraryFolder.archive_id == PrintArchive.id)
  551. .where(LibraryFolder.archive_id == archive_id)
  552. .order_by(LibraryFolder.name)
  553. )
  554. rows = result.all()
  555. folders = []
  556. for folder, archive_name in rows:
  557. # Get file count
  558. file_count_result = await db.execute(
  559. select(func.count(LibraryFile.id)).where(
  560. LibraryFile.folder_id == folder.id,
  561. LibraryFile.deleted_at.is_(None),
  562. )
  563. )
  564. file_count = file_count_result.scalar() or 0
  565. folders.append(
  566. FolderResponse(
  567. id=folder.id,
  568. name=folder.name,
  569. parent_id=folder.parent_id,
  570. project_id=folder.project_id,
  571. archive_id=folder.archive_id,
  572. project_name=None,
  573. archive_name=archive_name,
  574. is_external=folder.is_external,
  575. external_path=folder.external_path,
  576. external_readonly=folder.external_readonly,
  577. external_show_hidden=folder.external_show_hidden,
  578. file_count=file_count,
  579. created_at=folder.created_at,
  580. updated_at=folder.updated_at,
  581. )
  582. )
  583. return folders
  584. @router.post("/folders", response_model=FolderResponse)
  585. @router.post("/folders/", response_model=FolderResponse)
  586. async def create_folder(
  587. data: FolderCreate,
  588. db: AsyncSession = Depends(get_db),
  589. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  590. ):
  591. """Create a new folder."""
  592. # Verify parent exists if specified
  593. if data.parent_id is not None:
  594. parent_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.parent_id))
  595. if not parent_result.scalar_one_or_none():
  596. raise HTTPException(status_code=404, detail="Parent folder not found")
  597. # Verify project exists if specified
  598. project_name = None
  599. if data.project_id is not None:
  600. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  601. project = project_result.scalar_one_or_none()
  602. if not project:
  603. raise HTTPException(status_code=404, detail="Project not found")
  604. project_name = project.name
  605. # Verify archive exists if specified
  606. archive_name = None
  607. if data.archive_id is not None:
  608. archive_result = await db.execute(select(PrintArchive).where(PrintArchive.id == data.archive_id))
  609. archive = archive_result.scalar_one_or_none()
  610. if not archive:
  611. raise HTTPException(status_code=404, detail="Archive not found")
  612. archive_name = archive.print_name
  613. folder = LibraryFolder(
  614. name=data.name,
  615. parent_id=data.parent_id,
  616. project_id=data.project_id,
  617. archive_id=data.archive_id,
  618. )
  619. db.add(folder)
  620. await db.commit()
  621. await db.refresh(folder)
  622. return FolderResponse(
  623. id=folder.id,
  624. name=folder.name,
  625. parent_id=folder.parent_id,
  626. project_id=folder.project_id,
  627. archive_id=folder.archive_id,
  628. project_name=project_name,
  629. archive_name=archive_name,
  630. is_external=folder.is_external,
  631. external_path=folder.external_path,
  632. external_readonly=folder.external_readonly,
  633. external_show_hidden=folder.external_show_hidden,
  634. file_count=0,
  635. created_at=folder.created_at,
  636. updated_at=folder.updated_at,
  637. )
  638. @router.get("/folders/{folder_id}", response_model=FolderResponse)
  639. async def get_folder(
  640. folder_id: int,
  641. db: AsyncSession = Depends(get_db),
  642. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  643. ):
  644. """Get a folder by ID."""
  645. result = await db.execute(
  646. select(LibraryFolder, Project.name, PrintArchive.print_name)
  647. .outerjoin(Project, LibraryFolder.project_id == Project.id)
  648. .outerjoin(PrintArchive, LibraryFolder.archive_id == PrintArchive.id)
  649. .where(LibraryFolder.id == folder_id)
  650. )
  651. row = result.one_or_none()
  652. if not row:
  653. raise HTTPException(status_code=404, detail="Folder not found")
  654. folder, project_name, archive_name = row
  655. # Get file count
  656. file_count_result = await db.execute(
  657. select(func.count(LibraryFile.id)).where(
  658. LibraryFile.folder_id == folder_id,
  659. LibraryFile.deleted_at.is_(None),
  660. )
  661. )
  662. file_count = file_count_result.scalar() or 0
  663. return FolderResponse(
  664. id=folder.id,
  665. name=folder.name,
  666. parent_id=folder.parent_id,
  667. project_id=folder.project_id,
  668. archive_id=folder.archive_id,
  669. project_name=project_name,
  670. archive_name=archive_name,
  671. is_external=folder.is_external,
  672. external_path=folder.external_path,
  673. external_readonly=folder.external_readonly,
  674. external_show_hidden=folder.external_show_hidden,
  675. file_count=file_count,
  676. created_at=folder.created_at,
  677. updated_at=folder.updated_at,
  678. )
  679. @router.put("/folders/{folder_id}", response_model=FolderResponse)
  680. async def update_folder(
  681. folder_id: int,
  682. data: FolderUpdate,
  683. db: AsyncSession = Depends(get_db),
  684. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPDATE_ALL)),
  685. ):
  686. """Update a folder.
  687. Note: Folders require library:update_all permission since they don't have
  688. ownership tracking.
  689. """
  690. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  691. folder = result.scalar_one_or_none()
  692. if not folder:
  693. raise HTTPException(status_code=404, detail="Folder not found")
  694. if data.name is not None:
  695. folder.name = data.name
  696. if data.parent_id is not None:
  697. # Prevent circular reference
  698. if data.parent_id == folder_id:
  699. raise HTTPException(status_code=400, detail="Folder cannot be its own parent")
  700. # Check for circular reference in ancestors
  701. if data.parent_id != 0: # 0 means move to root
  702. current_id = data.parent_id
  703. while current_id is not None:
  704. if current_id == folder_id:
  705. raise HTTPException(status_code=400, detail="Cannot move folder into its own subtree")
  706. parent_result = await db.execute(select(LibraryFolder.parent_id).where(LibraryFolder.id == current_id))
  707. current_id = parent_result.scalar()
  708. folder.parent_id = data.parent_id
  709. else:
  710. folder.parent_id = None
  711. # Update project_id (0 to unlink)
  712. if data.project_id is not None:
  713. if data.project_id == 0:
  714. folder.project_id = None
  715. else:
  716. # Verify project exists
  717. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  718. if not project_result.scalar_one_or_none():
  719. raise HTTPException(status_code=404, detail="Project not found")
  720. folder.project_id = data.project_id
  721. # Update archive_id (0 to unlink)
  722. if data.archive_id is not None:
  723. if data.archive_id == 0:
  724. folder.archive_id = None
  725. else:
  726. # Verify archive exists
  727. archive_result = await db.execute(select(PrintArchive).where(PrintArchive.id == data.archive_id))
  728. if not archive_result.scalar_one_or_none():
  729. raise HTTPException(status_code=404, detail="Archive not found")
  730. folder.archive_id = data.archive_id
  731. await db.commit()
  732. await db.refresh(folder)
  733. # Get file count and names
  734. file_count_result = await db.execute(
  735. select(func.count(LibraryFile.id)).where(
  736. LibraryFile.folder_id == folder_id,
  737. LibraryFile.deleted_at.is_(None),
  738. )
  739. )
  740. file_count = file_count_result.scalar() or 0
  741. # Get project and archive names
  742. project_name = None
  743. archive_name = None
  744. if folder.project_id:
  745. project_result = await db.execute(select(Project.name).where(Project.id == folder.project_id))
  746. project_name = project_result.scalar()
  747. if folder.archive_id:
  748. archive_result = await db.execute(select(PrintArchive.print_name).where(PrintArchive.id == folder.archive_id))
  749. archive_name = archive_result.scalar()
  750. return FolderResponse(
  751. id=folder.id,
  752. name=folder.name,
  753. parent_id=folder.parent_id,
  754. project_id=folder.project_id,
  755. archive_id=folder.archive_id,
  756. project_name=project_name,
  757. archive_name=archive_name,
  758. is_external=folder.is_external,
  759. external_path=folder.external_path,
  760. external_readonly=folder.external_readonly,
  761. external_show_hidden=folder.external_show_hidden,
  762. file_count=file_count,
  763. created_at=folder.created_at,
  764. updated_at=folder.updated_at,
  765. )
  766. @router.delete("/folders/{folder_id}")
  767. async def delete_folder(
  768. folder_id: int,
  769. db: AsyncSession = Depends(get_db),
  770. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_DELETE_ALL)),
  771. ):
  772. """Delete a folder and all its contents (cascade).
  773. Note: Folders require library:delete_all permission since they don't have
  774. ownership tracking.
  775. """
  776. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  777. folder = result.scalar_one_or_none()
  778. if not folder:
  779. raise HTTPException(status_code=404, detail="Folder not found")
  780. # External folders: only remove DB records, never delete files from external path
  781. is_ext = folder.is_external
  782. # Get all files in this folder and subfolders to delete from disk
  783. async def get_all_file_ids(fid: int) -> list[int]:
  784. """Recursively get all file IDs in a folder tree."""
  785. file_ids = []
  786. # Get files in this folder
  787. files_result = await db.execute(
  788. select(LibraryFile.id, LibraryFile.file_path, LibraryFile.thumbnail_path, LibraryFile.is_external).where(
  789. LibraryFile.folder_id == fid
  790. )
  791. )
  792. for fid_val, file_path, thumb_path, file_is_ext in files_result.all():
  793. file_ids.append(fid_val)
  794. # Only delete non-external files from disk
  795. if not is_ext and not file_is_ext:
  796. try:
  797. if file_path and os.path.exists(file_path):
  798. os.remove(file_path)
  799. if thumb_path and os.path.exists(thumb_path):
  800. os.remove(thumb_path)
  801. except OSError as e:
  802. logger.warning("Failed to delete file: %s", e)
  803. # Get child folders and recurse
  804. children_result = await db.execute(select(LibraryFolder.id).where(LibraryFolder.parent_id == fid))
  805. for (child_id,) in children_result.all():
  806. file_ids.extend(await get_all_file_ids(child_id))
  807. return file_ids
  808. await get_all_file_ids(folder_id)
  809. # Delete folder (cascade will handle files and subfolders)
  810. await db.delete(folder)
  811. await db.commit()
  812. return {"status": "success", "message": "Folder deleted"}
  813. # ============ External Folder Endpoints ============
  814. # Blocked system directories that cannot be mounted
  815. _BLOCKED_PREFIXES = (
  816. "/proc",
  817. "/sys",
  818. "/dev",
  819. "/run",
  820. "/boot",
  821. "/sbin",
  822. "/bin",
  823. "/usr/sbin",
  824. "/usr/bin",
  825. "/lib",
  826. "/etc",
  827. )
  828. # Supported file extensions for external folder scanning
  829. _SCANNABLE_EXTENSIONS = {
  830. ".3mf",
  831. ".gcode",
  832. ".gcode.3mf",
  833. ".stl",
  834. ".obj",
  835. ".step",
  836. ".stp",
  837. ".png",
  838. ".jpg",
  839. ".jpeg",
  840. ".gif",
  841. ".webp",
  842. ".svg",
  843. }
  844. def _validate_external_path(path_str: str) -> Path:
  845. """Validate an external path is safe to mount."""
  846. path = Path(path_str).resolve()
  847. if not path.is_absolute():
  848. raise HTTPException(status_code=400, detail="Path must be absolute")
  849. for prefix in _BLOCKED_PREFIXES:
  850. if str(path).startswith(prefix):
  851. raise HTTPException(status_code=400, detail=f"Cannot mount system directory: {prefix}")
  852. if not path.exists():
  853. raise HTTPException(status_code=400, detail=f"Path does not exist: {path}")
  854. if not path.is_dir():
  855. raise HTTPException(status_code=400, detail=f"Path is not a directory: {path}")
  856. # Check readability
  857. if not os.access(path, os.R_OK):
  858. raise HTTPException(status_code=400, detail=f"Path is not readable: {path}")
  859. return path
  860. @router.post("/folders/external", response_model=FolderResponse)
  861. async def create_external_folder(
  862. data: ExternalFolderCreate,
  863. db: AsyncSession = Depends(get_db),
  864. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  865. ):
  866. """Create an external folder that points to a host directory."""
  867. resolved = _validate_external_path(data.external_path)
  868. # Check no other external folder already points to this path
  869. existing = await db.execute(
  870. select(LibraryFolder).where(
  871. LibraryFolder.is_external.is_(True),
  872. LibraryFolder.external_path == str(resolved),
  873. )
  874. )
  875. if existing.scalar_one_or_none():
  876. raise HTTPException(status_code=409, detail="An external folder already exists for this path")
  877. # Verify parent exists if specified
  878. if data.parent_id is not None:
  879. parent_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.parent_id))
  880. if not parent_result.scalar_one_or_none():
  881. raise HTTPException(status_code=404, detail="Parent folder not found")
  882. folder = LibraryFolder(
  883. name=data.name,
  884. parent_id=data.parent_id,
  885. is_external=True,
  886. external_path=str(resolved),
  887. external_readonly=data.readonly,
  888. external_show_hidden=data.show_hidden,
  889. )
  890. db.add(folder)
  891. await db.commit()
  892. await db.refresh(folder)
  893. return FolderResponse(
  894. id=folder.id,
  895. name=folder.name,
  896. parent_id=folder.parent_id,
  897. project_id=None,
  898. archive_id=None,
  899. is_external=True,
  900. external_path=folder.external_path,
  901. external_readonly=folder.external_readonly,
  902. external_show_hidden=folder.external_show_hidden,
  903. file_count=0,
  904. created_at=folder.created_at,
  905. updated_at=folder.updated_at,
  906. )
  907. @router.post("/folders/{folder_id}/scan")
  908. async def scan_external_folder(
  909. folder_id: int,
  910. db: AsyncSession = Depends(get_db),
  911. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  912. ):
  913. """Scan an external folder and sync files to the database.
  914. Discovers new files, removes DB entries for deleted files.
  915. Does not copy files — stores the external path directly.
  916. """
  917. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  918. folder = result.scalar_one_or_none()
  919. if not folder:
  920. raise HTTPException(status_code=404, detail="Folder not found")
  921. if not folder.is_external or not folder.external_path:
  922. raise HTTPException(status_code=400, detail="Not an external folder")
  923. ext_path = Path(folder.external_path)
  924. if not ext_path.exists() or not ext_path.is_dir():
  925. raise HTTPException(status_code=400, detail=f"External path is not accessible: {folder.external_path}")
  926. # Collect all existing child external subfolder IDs (single query)
  927. all_folder_ids = [folder_id]
  928. child_result = await db.execute(
  929. select(LibraryFolder).where(
  930. LibraryFolder.is_external.is_(True),
  931. LibraryFolder.parent_id.isnot(None),
  932. )
  933. )
  934. all_child_folders = child_result.scalars().all()
  935. # Walk the parent chain to find all descendants of folder_id
  936. parent_to_children: dict[int, list] = {}
  937. for cf in all_child_folders:
  938. parent_to_children.setdefault(cf.parent_id, []).append(cf)
  939. queue = [folder_id]
  940. while queue:
  941. pid = queue.pop()
  942. for child in parent_to_children.get(pid, []):
  943. all_folder_ids.append(child.id)
  944. queue.append(child.id)
  945. # Get existing DB files across root and all subfolders
  946. existing_result = await db.execute(
  947. LibraryFile.active().where(
  948. LibraryFile.folder_id.in_(all_folder_ids),
  949. LibraryFile.is_external.is_(True),
  950. )
  951. )
  952. existing_files = {f.file_path: f for f in existing_result.scalars().all()}
  953. # Build folder cache: relative path -> folder_id (for resolving subfolders)
  954. # Pre-populate with existing child folders keyed by their external_path
  955. folder_cache: dict[str, int] = {"": folder_id}
  956. for fid in all_folder_ids:
  957. if fid == folder_id:
  958. continue
  959. # Find the child folder object
  960. for cf in all_child_folders:
  961. if cf.id == fid and cf.external_path:
  962. try:
  963. rel = str(Path(cf.external_path).relative_to(ext_path))
  964. if rel != ".":
  965. folder_cache[rel] = cf.id
  966. except ValueError:
  967. pass
  968. # Scan the directory
  969. added = 0
  970. removed = 0
  971. found_paths: set[str] = set()
  972. seen_rel_dirs: set[str] = set()
  973. for dirpath, dirnames, filenames in os.walk(ext_path):
  974. # Filter hidden directories unless configured
  975. if not folder.external_show_hidden:
  976. dirnames[:] = [d for d in dirnames if not d.startswith(".")]
  977. rel_dir = str(Path(dirpath).relative_to(ext_path))
  978. if rel_dir == ".":
  979. rel_dir = ""
  980. seen_rel_dirs.add(rel_dir)
  981. # Resolve or create subfolder chain for this directory
  982. if rel_dir and rel_dir not in folder_cache:
  983. parts = Path(rel_dir).parts
  984. current_path = ""
  985. current_parent = folder_id
  986. for part in parts:
  987. current_path = f"{current_path}/{part}".lstrip("/")
  988. if current_path in folder_cache:
  989. current_parent = folder_cache[current_path]
  990. else:
  991. existing_sub = await db.execute(
  992. select(LibraryFolder).where(
  993. LibraryFolder.name == part,
  994. LibraryFolder.parent_id == current_parent,
  995. LibraryFolder.is_external.is_(True),
  996. )
  997. )
  998. existing_folder = existing_sub.scalar_one_or_none()
  999. if existing_folder:
  1000. current_parent = existing_folder.id
  1001. else:
  1002. new_folder = LibraryFolder(
  1003. name=part,
  1004. parent_id=current_parent,
  1005. is_external=True,
  1006. external_path=str(ext_path / current_path),
  1007. external_readonly=folder.external_readonly,
  1008. external_show_hidden=folder.external_show_hidden,
  1009. )
  1010. db.add(new_folder)
  1011. await db.flush()
  1012. current_parent = new_folder.id
  1013. folder_cache[current_path] = current_parent
  1014. target_folder_id = folder_cache.get(rel_dir, folder_id)
  1015. for filename in filenames:
  1016. # Skip hidden files unless configured
  1017. if not folder.external_show_hidden and filename.startswith("."):
  1018. continue
  1019. filepath = Path(dirpath) / filename
  1020. ext = filepath.suffix.lower()
  1021. # Check for compound extensions like .gcode.3mf
  1022. if ext not in _SCANNABLE_EXTENSIONS:
  1023. # Check compound
  1024. compound = "".join(filepath.suffixes[-2:]).lower() if len(filepath.suffixes) >= 2 else ""
  1025. if compound not in _SCANNABLE_EXTENSIONS:
  1026. continue
  1027. # Resolve symlinks and ensure still under external_path
  1028. try:
  1029. real_path = filepath.resolve()
  1030. real_path.relative_to(ext_path.resolve())
  1031. except (ValueError, OSError):
  1032. continue # Symlink escapes the external dir
  1033. file_path_str = str(filepath)
  1034. found_paths.add(file_path_str)
  1035. if file_path_str in existing_files:
  1036. continue # Already tracked
  1037. # Get file info
  1038. try:
  1039. stat = filepath.stat()
  1040. except OSError:
  1041. continue
  1042. file_type = ext[1:] if ext else "unknown"
  1043. # For compound extensions, use the meaningful part
  1044. if file_type in ("3mf",) and len(filepath.suffixes) >= 2:
  1045. inner = filepath.suffixes[-2].lower()
  1046. if inner == ".gcode":
  1047. file_type = "gcode.3mf"
  1048. # Extract thumbnail for 3mf files
  1049. thumbnail_path = None
  1050. file_metadata = None
  1051. if file_type == "3mf":
  1052. try:
  1053. parser = ThreeMFParser(str(filepath))
  1054. raw_metadata = parser.parse()
  1055. if raw_metadata:
  1056. # Extract thumbnail before cleaning metadata
  1057. thumb_data = raw_metadata.get("_thumbnail_data")
  1058. thumbnail_ext = raw_metadata.get("_thumbnail_ext", ".png")
  1059. if thumb_data:
  1060. thumb_dir = get_library_thumbnails_dir()
  1061. thumb_filename = f"{uuid.uuid4().hex}{thumbnail_ext}"
  1062. thumb_full = thumb_dir / thumb_filename
  1063. thumb_full.write_bytes(thumb_data)
  1064. thumbnail_path = to_relative_path(thumb_full)
  1065. # Clean metadata - remove non-JSON-serializable data (bytes, etc.)
  1066. def clean_metadata(obj):
  1067. if isinstance(obj, dict):
  1068. return {
  1069. k: clean_metadata(v)
  1070. for k, v in obj.items()
  1071. if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext")
  1072. }
  1073. elif isinstance(obj, list):
  1074. return [clean_metadata(i) for i in obj if not isinstance(i, bytes)]
  1075. elif isinstance(obj, bytes):
  1076. return None
  1077. return obj
  1078. file_metadata = clean_metadata(raw_metadata)
  1079. except Exception as e:
  1080. logger.debug("Failed to extract metadata from external 3mf %s: %s", filepath, e)
  1081. # Generate thumbnail for STL files
  1082. if file_type == "stl" and thumbnail_path is None:
  1083. try:
  1084. thumb_dir = get_library_thumbnails_dir()
  1085. thumb_result = generate_stl_thumbnail(str(filepath), str(thumb_dir))
  1086. if thumb_result:
  1087. thumbnail_path = to_relative_path(Path(thumb_result))
  1088. except Exception as e:
  1089. logger.debug("Failed to generate STL thumbnail for external %s: %s", filepath, e)
  1090. # Extract gcode thumbnail
  1091. if file_type == "gcode" and thumbnail_path is None:
  1092. thumb_data = extract_gcode_thumbnail(filepath)
  1093. if thumb_data:
  1094. thumb_dir = get_library_thumbnails_dir()
  1095. thumb_filename = f"{uuid.uuid4().hex}.png"
  1096. thumb_full = thumb_dir / thumb_filename
  1097. thumb_full.write_bytes(thumb_data)
  1098. thumbnail_path = to_relative_path(thumb_full)
  1099. # Create thumbnail for image files
  1100. if ext.lower() in IMAGE_EXTENSIONS and thumbnail_path is None:
  1101. thumbnail_path_str = create_image_thumbnail(filepath, get_library_thumbnails_dir())
  1102. if thumbnail_path_str:
  1103. thumbnail_path = to_relative_path(Path(thumbnail_path_str))
  1104. db_file = LibraryFile(
  1105. folder_id=target_folder_id,
  1106. is_external=True,
  1107. filename=filename,
  1108. file_path=file_path_str,
  1109. file_type=file_type,
  1110. file_size=stat.st_size,
  1111. file_hash=None, # Skip hashing external files for performance
  1112. thumbnail_path=thumbnail_path,
  1113. file_metadata=file_metadata,
  1114. )
  1115. db.add(db_file)
  1116. added += 1
  1117. # Remove DB entries for files that no longer exist on disk
  1118. for path_str, db_file in existing_files.items():
  1119. if path_str not in found_paths:
  1120. # Clean up thumbnail if we generated one
  1121. if db_file.thumbnail_path:
  1122. try:
  1123. abs_thumb = to_absolute_path(db_file.thumbnail_path)
  1124. if abs_thumb and abs_thumb.exists():
  1125. abs_thumb.unlink()
  1126. except OSError:
  1127. pass
  1128. await db.delete(db_file)
  1129. removed += 1
  1130. # Remove empty subfolders whose directories no longer exist on disk
  1131. # Process deepest-first by sorting on path depth (descending)
  1132. subfolder_entries = [(rel, fid) for rel, fid in folder_cache.items() if rel and fid != folder_id]
  1133. subfolder_entries.sort(key=lambda x: x[0].count("/"), reverse=True)
  1134. for rel_path, sub_fid in subfolder_entries:
  1135. if rel_path in seen_rel_dirs:
  1136. continue # Directory still exists on disk
  1137. # Check if subfolder has any remaining files
  1138. file_count_result = await db.execute(
  1139. select(func.count(LibraryFile.id)).where(
  1140. LibraryFile.folder_id == sub_fid,
  1141. LibraryFile.deleted_at.is_(None),
  1142. )
  1143. )
  1144. if (file_count_result.scalar() or 0) == 0:
  1145. # Check if it has any remaining child folders
  1146. child_count_result = await db.execute(
  1147. select(func.count(LibraryFolder.id)).where(LibraryFolder.parent_id == sub_fid)
  1148. )
  1149. if (child_count_result.scalar() or 0) == 0:
  1150. sub_folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == sub_fid))
  1151. sub_folder_obj = sub_folder_result.scalar_one_or_none()
  1152. if sub_folder_obj:
  1153. await db.delete(sub_folder_obj)
  1154. await db.commit()
  1155. return {"status": "success", "added": added, "removed": removed}
  1156. # ============ File Endpoints ============
  1157. @router.get("/files", response_model=list[FileListResponse])
  1158. @router.get("/files/", response_model=list[FileListResponse])
  1159. async def list_files(
  1160. response: Response,
  1161. folder_id: int | None = None,
  1162. project_id: int | None = None,
  1163. include_root: bool = True,
  1164. db: AsyncSession = Depends(get_db),
  1165. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  1166. ):
  1167. """List files, optionally filtered by folder or project.
  1168. Args:
  1169. folder_id: Filter by folder ID. If None and include_root=True, returns root files.
  1170. project_id: Return all files across folders linked to this project (bulk fetch, avoids N+1).
  1171. include_root: If True and folder_id is None, returns files at root level.
  1172. If False and folder_id is None, returns all files.
  1173. """
  1174. query = LibraryFile.active().options(selectinload(LibraryFile.created_by))
  1175. if folder_id is not None:
  1176. query = query.where(LibraryFile.folder_id == folder_id)
  1177. elif project_id is not None:
  1178. # Single join instead of one query per folder (avoids N+1 pattern)
  1179. query = query.join(LibraryFolder, LibraryFile.folder_id == LibraryFolder.id)
  1180. query = query.where(LibraryFolder.project_id == project_id)
  1181. elif include_root:
  1182. query = query.where(LibraryFile.folder_id.is_(None))
  1183. query = query.order_by(LibraryFile.filename)
  1184. result = await db.execute(query)
  1185. files = result.scalars().all()
  1186. # Get duplicate counts
  1187. hash_counts = {}
  1188. if files:
  1189. hashes = [f.file_hash for f in files if f.file_hash]
  1190. if hashes:
  1191. dup_result = await db.execute(
  1192. select(LibraryFile.file_hash, func.count(LibraryFile.id))
  1193. .where(LibraryFile.file_hash.in_(hashes), LibraryFile.deleted_at.is_(None))
  1194. .group_by(LibraryFile.file_hash)
  1195. )
  1196. hash_counts = {h: c - 1 for h, c in dup_result.all()} # -1 to exclude self
  1197. # Prevent browser caching of file list
  1198. response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
  1199. file_list = []
  1200. for f in files:
  1201. # Extract key metadata for display
  1202. print_name = None
  1203. print_time = None
  1204. filament_grams = None
  1205. sliced_for_model = None
  1206. if f.file_metadata:
  1207. print_name = f.file_metadata.get("print_name")
  1208. print_time = f.file_metadata.get("print_time_seconds")
  1209. filament_grams = f.file_metadata.get("filament_used_grams")
  1210. sliced_for_model = f.file_metadata.get("sliced_for_model")
  1211. file_list.append(
  1212. FileListResponse(
  1213. id=f.id,
  1214. folder_id=f.folder_id,
  1215. is_external=f.is_external,
  1216. filename=f.filename,
  1217. file_type=f.file_type,
  1218. file_size=f.file_size,
  1219. thumbnail_path=f.thumbnail_path,
  1220. print_count=f.print_count,
  1221. duplicate_count=hash_counts.get(f.file_hash, 0) if f.file_hash else 0,
  1222. created_by_id=f.created_by_id,
  1223. created_by_username=f.created_by.username if f.created_by else None,
  1224. created_at=f.created_at,
  1225. print_name=print_name,
  1226. print_time_seconds=print_time,
  1227. filament_used_grams=filament_grams,
  1228. sliced_for_model=sliced_for_model,
  1229. )
  1230. )
  1231. return file_list
  1232. @router.post("/files", response_model=FileUploadResponse)
  1233. @router.post("/files/", response_model=FileUploadResponse)
  1234. async def upload_file(
  1235. file: UploadFile = File(...),
  1236. folder_id: int | None = None,
  1237. generate_stl_thumbnails: bool = Query(default=True),
  1238. db: AsyncSession = Depends(get_db),
  1239. current_user: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  1240. ):
  1241. """Upload a file to the library."""
  1242. try:
  1243. if not file.filename:
  1244. raise HTTPException(status_code=400, detail="Filename is required")
  1245. filename = file.filename
  1246. ext = os.path.splitext(filename)[1].lower()
  1247. # Handle files without extension
  1248. file_type = ext[1:] if ext else "unknown"
  1249. # Verify folder exists if specified
  1250. target_folder = None
  1251. if folder_id is not None:
  1252. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  1253. target_folder = folder_result.scalar_one_or_none()
  1254. if not target_folder:
  1255. raise HTTPException(status_code=404, detail="Folder not found")
  1256. # Writable external folders write through to the mount so the file is
  1257. # visible outside Bambuddy (#1112); everything else lands under the
  1258. # internal library dir with a UUID-scoped filename.
  1259. file_path, is_external_upload = _resolve_upload_destination(target_folder, filename)
  1260. # Save file
  1261. content = await file.read()
  1262. with open(file_path, "wb") as f:
  1263. f.write(content)
  1264. # Calculate hash
  1265. file_hash = calculate_file_hash(file_path)
  1266. # Check for duplicates
  1267. dup_result = await db.execute(
  1268. select(LibraryFile.id).where(LibraryFile.file_hash == file_hash, LibraryFile.deleted_at.is_(None)).limit(1)
  1269. )
  1270. duplicate_of = dup_result.scalar()
  1271. # Extract metadata and thumbnail
  1272. metadata = {}
  1273. thumbnail_path = None
  1274. thumbnails_dir = get_library_thumbnails_dir()
  1275. if ext == ".3mf":
  1276. try:
  1277. parser = ThreeMFParser(str(file_path))
  1278. raw_metadata = parser.parse()
  1279. # Extract thumbnail before cleaning metadata
  1280. thumbnail_data = raw_metadata.get("_thumbnail_data")
  1281. thumbnail_ext = raw_metadata.get("_thumbnail_ext", ".png")
  1282. # Save thumbnail if extracted
  1283. if thumbnail_data:
  1284. thumb_filename = f"{uuid.uuid4().hex}{thumbnail_ext}"
  1285. thumb_path = thumbnails_dir / thumb_filename
  1286. with open(thumb_path, "wb") as f:
  1287. f.write(thumbnail_data)
  1288. thumbnail_path = str(thumb_path)
  1289. # Clean metadata - remove non-JSON-serializable data (bytes, etc.)
  1290. def clean_metadata(obj):
  1291. if isinstance(obj, dict):
  1292. return {
  1293. k: clean_metadata(v)
  1294. for k, v in obj.items()
  1295. if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext")
  1296. }
  1297. elif isinstance(obj, list):
  1298. return [clean_metadata(i) for i in obj if not isinstance(i, bytes)]
  1299. elif isinstance(obj, bytes):
  1300. return None
  1301. return obj
  1302. metadata = clean_metadata(raw_metadata)
  1303. except Exception as e:
  1304. logger.warning("Failed to parse 3MF: %s", e)
  1305. elif ext == ".gcode":
  1306. # Extract embedded thumbnail from gcode
  1307. try:
  1308. thumbnail_data = extract_gcode_thumbnail(file_path)
  1309. if thumbnail_data:
  1310. thumb_filename = f"{uuid.uuid4().hex}.png"
  1311. thumb_path = thumbnails_dir / thumb_filename
  1312. with open(thumb_path, "wb") as f:
  1313. f.write(thumbnail_data)
  1314. thumbnail_path = str(thumb_path)
  1315. except Exception as e:
  1316. logger.warning("Failed to extract gcode thumbnail: %s", e)
  1317. elif ext.lower() in IMAGE_EXTENSIONS:
  1318. # For image files, create a thumbnail from the image itself
  1319. thumbnail_path = create_image_thumbnail(file_path, thumbnails_dir)
  1320. elif ext == ".stl":
  1321. # Generate STL thumbnail if enabled
  1322. if generate_stl_thumbnails:
  1323. thumbnail_path = generate_stl_thumbnail(file_path, thumbnails_dir)
  1324. # Create database entry (managed files store relative paths for portability;
  1325. # external files store the absolute mount path — same shape as scan produces)
  1326. library_file = LibraryFile(
  1327. folder_id=folder_id,
  1328. is_external=is_external_upload,
  1329. filename=filename,
  1330. file_path=_stored_file_path(file_path, is_external_upload),
  1331. file_type=file_type,
  1332. file_size=len(content),
  1333. file_hash=file_hash,
  1334. thumbnail_path=to_relative_path(thumbnail_path) if thumbnail_path else None,
  1335. file_metadata=metadata if metadata else None,
  1336. created_by_id=current_user.id if current_user else None,
  1337. )
  1338. db.add(library_file)
  1339. await db.commit()
  1340. await db.refresh(library_file)
  1341. return FileUploadResponse(
  1342. id=library_file.id,
  1343. filename=library_file.filename,
  1344. file_type=library_file.file_type,
  1345. file_size=library_file.file_size,
  1346. thumbnail_path=library_file.thumbnail_path,
  1347. duplicate_of=duplicate_of,
  1348. metadata=library_file.file_metadata,
  1349. )
  1350. except HTTPException:
  1351. raise
  1352. except Exception as e:
  1353. logger.error("Upload failed for %s: %s", file.filename, e, exc_info=True)
  1354. raise HTTPException(status_code=500, detail=f"Upload failed: {str(e)}")
  1355. @router.post("/files/extract-zip", response_model=ZipExtractResponse)
  1356. async def extract_zip_file(
  1357. file: UploadFile = File(...),
  1358. folder_id: int | None = Query(default=None),
  1359. preserve_structure: bool = Query(default=True),
  1360. create_folder_from_zip: bool = Query(default=False),
  1361. generate_stl_thumbnails: bool = Query(default=True),
  1362. db: AsyncSession = Depends(get_db),
  1363. current_user: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  1364. ):
  1365. """Upload and extract a ZIP file to the library.
  1366. Args:
  1367. file: The ZIP file to extract
  1368. folder_id: Target folder ID (None = root)
  1369. preserve_structure: If True, recreate folder structure from ZIP; if False, extract all files flat
  1370. create_folder_from_zip: If True, create a folder named after the ZIP file and extract into it
  1371. generate_stl_thumbnails: If True, generate thumbnails for STL files
  1372. """
  1373. import tempfile
  1374. if not file.filename or not file.filename.lower().endswith(".zip"):
  1375. raise HTTPException(status_code=400, detail="Only ZIP files are supported")
  1376. # Verify target folder exists if specified
  1377. if folder_id is not None:
  1378. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  1379. target_folder = folder_result.scalar_one_or_none()
  1380. if not target_folder:
  1381. raise HTTPException(status_code=404, detail="Target folder not found")
  1382. if target_folder.is_external and target_folder.external_readonly:
  1383. raise HTTPException(status_code=403, detail="Cannot extract ZIP to a read-only external folder")
  1384. if target_folder.is_external:
  1385. # Writable external folders aren't supported by extract-zip because the
  1386. # nested-subfolder creation path would need to mkdir on the mount and
  1387. # create matching is_external=True LibraryFolder rows — a separate
  1388. # design. Direct the user at Scan, which already handles that shape
  1389. # (#1112).
  1390. raise HTTPException(
  1391. status_code=400,
  1392. detail=(
  1393. "Cannot extract ZIP directly into an external folder. "
  1394. "Extract the ZIP on the external mount and run 'Scan External Folder' instead."
  1395. ),
  1396. )
  1397. # Save ZIP to temp file
  1398. try:
  1399. with tempfile.NamedTemporaryFile(delete=False, suffix=".zip") as tmp:
  1400. content = await file.read()
  1401. tmp.write(content)
  1402. tmp_path = tmp.name
  1403. except Exception as e:
  1404. raise HTTPException(status_code=500, detail=f"Failed to save ZIP file: {str(e)}")
  1405. extracted_files: list[ZipExtractResult] = []
  1406. errors: list[ZipExtractError] = []
  1407. folders_created = 0
  1408. folder_cache: dict[str, int] = {} # path -> folder_id
  1409. # If create_folder_from_zip is True, create a folder named after the ZIP file
  1410. zip_folder_id = folder_id
  1411. logger.info(
  1412. f"ZIP extraction: create_folder_from_zip={create_folder_from_zip}, folder_id={folder_id}, filename={file.filename}"
  1413. )
  1414. if create_folder_from_zip and file.filename:
  1415. # Remove .zip extension to get folder name
  1416. zip_folder_name = file.filename[:-4] if file.filename.lower().endswith(".zip") else file.filename
  1417. # Check if folder already exists
  1418. existing = await db.execute(
  1419. select(LibraryFolder).where(
  1420. LibraryFolder.name == zip_folder_name,
  1421. LibraryFolder.parent_id == folder_id if folder_id else LibraryFolder.parent_id.is_(None),
  1422. )
  1423. )
  1424. existing_folder = existing.scalar_one_or_none()
  1425. if existing_folder:
  1426. zip_folder_id = existing_folder.id
  1427. logger.info("Reusing existing folder '%s' with id=%s", zip_folder_name, zip_folder_id)
  1428. else:
  1429. # Create folder
  1430. new_folder = LibraryFolder(name=zip_folder_name, parent_id=folder_id)
  1431. db.add(new_folder)
  1432. await db.flush()
  1433. await db.commit() # Commit folder creation immediately
  1434. zip_folder_id = new_folder.id
  1435. folders_created += 1
  1436. logger.info("Created new folder '%s' with id=%s", zip_folder_name, zip_folder_id)
  1437. try:
  1438. with zipfile.ZipFile(tmp_path, "r") as zf:
  1439. # Filter out directories and hidden/system files
  1440. file_list = [
  1441. name
  1442. for name in zf.namelist()
  1443. if not name.endswith("/")
  1444. and not name.startswith("__MACOSX")
  1445. and not os.path.basename(name).startswith(".")
  1446. ]
  1447. for zip_path in file_list:
  1448. try:
  1449. # Determine target folder (use zip_folder_id as base if create_folder_from_zip was used)
  1450. target_folder_id = zip_folder_id
  1451. if preserve_structure:
  1452. # Get directory path from ZIP
  1453. dir_path = os.path.dirname(zip_path)
  1454. if dir_path:
  1455. # Create folder structure
  1456. parts = dir_path.split("/")
  1457. current_parent = zip_folder_id
  1458. current_path = ""
  1459. for part in parts:
  1460. if not part:
  1461. continue
  1462. current_path = f"{current_path}/{part}" if current_path else part
  1463. if current_path in folder_cache:
  1464. current_parent = folder_cache[current_path]
  1465. else:
  1466. # Check if folder exists
  1467. existing = await db.execute(
  1468. select(LibraryFolder).where(
  1469. LibraryFolder.name == part,
  1470. LibraryFolder.parent_id == current_parent
  1471. if current_parent
  1472. else LibraryFolder.parent_id.is_(None),
  1473. )
  1474. )
  1475. existing_folder = existing.scalar_one_or_none()
  1476. if existing_folder:
  1477. current_parent = existing_folder.id
  1478. else:
  1479. # Create folder
  1480. new_folder = LibraryFolder(name=part, parent_id=current_parent)
  1481. db.add(new_folder)
  1482. await db.flush()
  1483. current_parent = new_folder.id
  1484. folders_created += 1
  1485. folder_cache[current_path] = current_parent
  1486. target_folder_id = current_parent
  1487. # Extract file
  1488. filename = os.path.basename(zip_path)
  1489. ext = os.path.splitext(filename)[1].lower()
  1490. file_type = ext[1:] if ext else "unknown"
  1491. # Generate unique filename for storage
  1492. unique_filename = f"{uuid.uuid4().hex}{ext}"
  1493. file_path = get_library_files_dir() / unique_filename
  1494. # Extract and save file
  1495. file_content = zf.read(zip_path)
  1496. with open(file_path, "wb") as f:
  1497. f.write(file_content)
  1498. # Calculate hash
  1499. file_hash = calculate_file_hash(file_path)
  1500. # Extract metadata and thumbnail for 3MF files
  1501. metadata = {}
  1502. thumbnail_path = None
  1503. thumbnails_dir = get_library_thumbnails_dir()
  1504. if ext == ".3mf":
  1505. try:
  1506. parser = ThreeMFParser(str(file_path))
  1507. raw_metadata = parser.parse()
  1508. thumbnail_data = raw_metadata.get("_thumbnail_data")
  1509. thumbnail_ext = raw_metadata.get("_thumbnail_ext", ".png")
  1510. if thumbnail_data:
  1511. thumb_filename = f"{uuid.uuid4().hex}{thumbnail_ext}"
  1512. thumb_path = thumbnails_dir / thumb_filename
  1513. with open(thumb_path, "wb") as f:
  1514. f.write(thumbnail_data)
  1515. thumbnail_path = str(thumb_path)
  1516. def clean_metadata(obj):
  1517. if isinstance(obj, dict):
  1518. return {
  1519. k: clean_metadata(v)
  1520. for k, v in obj.items()
  1521. if not isinstance(v, bytes) and k not in ("_thumbnail_data", "_thumbnail_ext")
  1522. }
  1523. elif isinstance(obj, list):
  1524. return [clean_metadata(i) for i in obj if not isinstance(i, bytes)]
  1525. elif isinstance(obj, bytes):
  1526. return None
  1527. return obj
  1528. metadata = clean_metadata(raw_metadata)
  1529. except Exception as e:
  1530. logger.warning("Failed to parse 3MF from ZIP: %s", e)
  1531. elif ext == ".gcode":
  1532. try:
  1533. thumbnail_data = extract_gcode_thumbnail(file_path)
  1534. if thumbnail_data:
  1535. thumb_filename = f"{uuid.uuid4().hex}.png"
  1536. thumb_path = thumbnails_dir / thumb_filename
  1537. with open(thumb_path, "wb") as f:
  1538. f.write(thumbnail_data)
  1539. thumbnail_path = str(thumb_path)
  1540. except Exception as e:
  1541. logger.warning("Failed to extract gcode thumbnail from ZIP: %s", e)
  1542. elif ext.lower() in IMAGE_EXTENSIONS:
  1543. thumbnail_path = create_image_thumbnail(file_path, thumbnails_dir)
  1544. elif ext == ".stl":
  1545. # Generate STL thumbnail if enabled
  1546. if generate_stl_thumbnails:
  1547. thumbnail_path = generate_stl_thumbnail(file_path, thumbnails_dir)
  1548. # Create database entry (store relative paths for portability)
  1549. library_file = LibraryFile(
  1550. folder_id=target_folder_id,
  1551. filename=filename,
  1552. file_path=to_relative_path(file_path),
  1553. file_type=file_type,
  1554. file_size=len(file_content),
  1555. file_hash=file_hash,
  1556. thumbnail_path=to_relative_path(thumbnail_path) if thumbnail_path else None,
  1557. file_metadata=metadata if metadata else None,
  1558. created_by_id=current_user.id if current_user else None,
  1559. )
  1560. db.add(library_file)
  1561. await db.flush()
  1562. await db.refresh(library_file)
  1563. extracted_files.append(
  1564. ZipExtractResult(
  1565. filename=filename,
  1566. file_id=library_file.id,
  1567. folder_id=target_folder_id,
  1568. )
  1569. )
  1570. # Commit after each file to release database lock
  1571. # This prevents long-running transactions from blocking other requests
  1572. await db.commit()
  1573. except Exception as e:
  1574. logger.error("Failed to extract %s: %s", zip_path, e)
  1575. errors.append(ZipExtractError(filename=os.path.basename(zip_path), error=str(e)))
  1576. # Rollback the failed file but continue with others
  1577. await db.rollback()
  1578. return ZipExtractResponse(
  1579. extracted=len(extracted_files),
  1580. folders_created=folders_created,
  1581. files=extracted_files,
  1582. errors=errors,
  1583. )
  1584. except zipfile.BadZipFile:
  1585. raise HTTPException(status_code=400, detail="Invalid or corrupted ZIP file")
  1586. except Exception as e:
  1587. logger.error("ZIP extraction failed: %s", e, exc_info=True)
  1588. raise HTTPException(status_code=500, detail=f"ZIP extraction failed: {str(e)}")
  1589. finally:
  1590. # Clean up temp file
  1591. try:
  1592. os.unlink(tmp_path)
  1593. except OSError:
  1594. pass # Best-effort temp file cleanup; ignore if already removed
  1595. # ============ STL Thumbnail Batch Generation ============
  1596. @router.post("/generate-stl-thumbnails", response_model=BatchThumbnailResponse)
  1597. async def batch_generate_stl_thumbnails(
  1598. request: BatchThumbnailRequest,
  1599. db: AsyncSession = Depends(get_db),
  1600. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPDATE_ALL)),
  1601. ):
  1602. """Generate thumbnails for STL files in batch.
  1603. Note: Requires library:update_all permission since this is a batch operation
  1604. that may affect files owned by different users.
  1605. Can generate thumbnails for:
  1606. - Specific file IDs (file_ids)
  1607. - All STL files in a folder (folder_id)
  1608. - All STL files missing thumbnails (all_missing=True)
  1609. """
  1610. thumbnails_dir = get_library_thumbnails_dir()
  1611. results: list[BatchThumbnailResult] = []
  1612. # Build query based on request
  1613. query = LibraryFile.active().where(LibraryFile.file_type == "stl")
  1614. if request.file_ids:
  1615. # Specific files
  1616. query = query.where(LibraryFile.id.in_(request.file_ids))
  1617. elif request.folder_id is not None:
  1618. # All STL files in a specific folder
  1619. query = query.where(LibraryFile.folder_id == request.folder_id)
  1620. if not request.all_missing:
  1621. # If not specifically asking for missing thumbnails, get all
  1622. pass
  1623. else:
  1624. query = query.where(LibraryFile.thumbnail_path.is_(None))
  1625. elif request.all_missing:
  1626. # All STL files without thumbnails
  1627. query = query.where(LibraryFile.thumbnail_path.is_(None))
  1628. else:
  1629. # No criteria specified - return empty
  1630. return BatchThumbnailResponse(
  1631. processed=0,
  1632. succeeded=0,
  1633. failed=0,
  1634. results=[],
  1635. )
  1636. result = await db.execute(query)
  1637. stl_files = result.scalars().all()
  1638. succeeded = 0
  1639. failed = 0
  1640. for stl_file in stl_files:
  1641. file_path = to_absolute_path(stl_file.file_path)
  1642. if not file_path or not file_path.exists():
  1643. results.append(
  1644. BatchThumbnailResult(
  1645. file_id=stl_file.id,
  1646. filename=stl_file.filename,
  1647. success=False,
  1648. error="File not found on disk",
  1649. )
  1650. )
  1651. failed += 1
  1652. continue
  1653. try:
  1654. thumbnail_path = generate_stl_thumbnail(file_path, thumbnails_dir)
  1655. if thumbnail_path:
  1656. # Update database with relative path
  1657. stl_file.thumbnail_path = to_relative_path(thumbnail_path)
  1658. await db.flush()
  1659. results.append(
  1660. BatchThumbnailResult(
  1661. file_id=stl_file.id,
  1662. filename=stl_file.filename,
  1663. success=True,
  1664. )
  1665. )
  1666. succeeded += 1
  1667. else:
  1668. results.append(
  1669. BatchThumbnailResult(
  1670. file_id=stl_file.id,
  1671. filename=stl_file.filename,
  1672. success=False,
  1673. error="Thumbnail generation failed",
  1674. )
  1675. )
  1676. failed += 1
  1677. except Exception as e:
  1678. logger.error("Failed to generate thumbnail for %s: %s", stl_file.filename, e)
  1679. results.append(
  1680. BatchThumbnailResult(
  1681. file_id=stl_file.id,
  1682. filename=stl_file.filename,
  1683. success=False,
  1684. error=str(e),
  1685. )
  1686. )
  1687. failed += 1
  1688. await db.commit()
  1689. return BatchThumbnailResponse(
  1690. processed=len(stl_files),
  1691. succeeded=succeeded,
  1692. failed=failed,
  1693. results=results,
  1694. )
  1695. # ============ Queue Operations ============
  1696. # NOTE: These routes must be defined BEFORE /files/{file_id} to avoid path parameter conflicts
  1697. def is_sliced_file(filename: str) -> bool:
  1698. """Check if a file is a sliced (printable) file.
  1699. Sliced files are:
  1700. - .gcode files
  1701. - .3mf files that contain '.gcode.' in the name (e.g., filename.gcode.3mf)
  1702. """
  1703. lower = filename.lower()
  1704. return lower.endswith(".gcode") or ".gcode." in lower
  1705. @router.post("/files/add-to-queue", response_model=AddToQueueResponse)
  1706. async def add_files_to_queue(
  1707. request: AddToQueueRequest,
  1708. db: AsyncSession = Depends(get_db),
  1709. _: User | None = Depends(require_permission_if_auth_enabled(Permission.QUEUE_CREATE)),
  1710. ):
  1711. """Add library files to the print queue.
  1712. Only sliced files (.gcode or .gcode.3mf) can be added to the queue.
  1713. The archive will be created automatically when the print starts.
  1714. """
  1715. added: list[AddToQueueResult] = []
  1716. errors: list[AddToQueueError] = []
  1717. # Get all requested files
  1718. result = await db.execute(LibraryFile.active().where(LibraryFile.id.in_(request.file_ids)))
  1719. files = {f.id: f for f in result.scalars().all()}
  1720. # Get max position for queue ordering
  1721. pos_result = await db.execute(select(func.coalesce(func.max(PrintQueueItem.position), 0)))
  1722. max_position = pos_result.scalar() or 0
  1723. for file_id in request.file_ids:
  1724. lib_file = files.get(file_id)
  1725. if not lib_file:
  1726. errors.append(AddToQueueError(file_id=file_id, filename="(not found)", error="File not found"))
  1727. continue
  1728. # Validate file is sliced
  1729. if not is_sliced_file(lib_file.filename):
  1730. errors.append(
  1731. AddToQueueError(
  1732. file_id=file_id,
  1733. filename=lib_file.filename,
  1734. error="Not a sliced file. Only .gcode or .gcode.3mf files can be printed.",
  1735. )
  1736. )
  1737. continue
  1738. try:
  1739. # Verify file exists on disk
  1740. file_path = Path(app_settings.base_dir) / lib_file.file_path
  1741. if not file_path.exists():
  1742. errors.append(
  1743. AddToQueueError(file_id=file_id, filename=lib_file.filename, error="File not found on disk")
  1744. )
  1745. continue
  1746. # Create queue item referencing library file (archive created at print start)
  1747. max_position += 1
  1748. queue_item = PrintQueueItem(
  1749. printer_id=None, # Unassigned
  1750. library_file_id=file_id,
  1751. position=max_position,
  1752. status="pending",
  1753. )
  1754. db.add(queue_item)
  1755. await db.flush() # Get queue_item.id
  1756. added.append(
  1757. AddToQueueResult(
  1758. file_id=file_id,
  1759. filename=lib_file.filename,
  1760. queue_item_id=queue_item.id,
  1761. )
  1762. )
  1763. except Exception as e:
  1764. logger.exception("Error adding file %s to queue", file_id)
  1765. errors.append(AddToQueueError(file_id=file_id, filename=lib_file.filename, error=str(e)))
  1766. await db.commit()
  1767. return AddToQueueResponse(added=added, errors=errors)
  1768. @router.get("/files/{file_id}/plates")
  1769. async def get_library_file_plates(
  1770. file_id: int,
  1771. db: AsyncSession = Depends(get_db),
  1772. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  1773. ):
  1774. """Get available plates from a multi-plate 3MF library file.
  1775. Returns a list of plates with their index, name, thumbnail availability,
  1776. and filament requirements. For single-plate exports, returns a single plate.
  1777. """
  1778. import json
  1779. import defusedxml.ElementTree as ET
  1780. # Get the library file
  1781. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  1782. lib_file = result.scalar_one_or_none()
  1783. if not lib_file:
  1784. raise HTTPException(status_code=404, detail="File not found")
  1785. file_path = Path(app_settings.base_dir) / lib_file.file_path
  1786. if not file_path.exists():
  1787. raise HTTPException(status_code=404, detail="File not found on disk")
  1788. # Only 3MF files have plates
  1789. if not lib_file.filename.lower().endswith(".3mf"):
  1790. return {"file_id": file_id, "filename": lib_file.filename, "plates": [], "is_multi_plate": False}
  1791. plates = []
  1792. try:
  1793. with zipfile.ZipFile(file_path, "r") as zf:
  1794. namelist = zf.namelist()
  1795. # Find all plate gcode files to determine available plates
  1796. gcode_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".gcode")]
  1797. # If no gcode is present (source-only or unsliced), fall back to plate JSON/PNG
  1798. plate_indices: list[int] = []
  1799. if gcode_files:
  1800. # Extract plate indices from gcode filenames
  1801. for gf in gcode_files:
  1802. try:
  1803. plate_str = gf[15:-6] # Remove "Metadata/plate_" and ".gcode"
  1804. plate_indices.append(int(plate_str))
  1805. except ValueError:
  1806. pass # Skip gcode file with non-numeric plate index
  1807. else:
  1808. plate_json_files = [n for n in namelist if n.startswith("Metadata/plate_") and n.endswith(".json")]
  1809. plate_png_files = [
  1810. n
  1811. for n in namelist
  1812. if n.startswith("Metadata/plate_")
  1813. and n.endswith(".png")
  1814. and "_small" not in n
  1815. and "no_light" not in n
  1816. ]
  1817. plate_name_candidates = plate_json_files + plate_png_files
  1818. plate_re = re.compile(r"^Metadata/plate_(\d+)\.(json|png)$")
  1819. seen_indices: set[int] = set()
  1820. for name in plate_name_candidates:
  1821. match = plate_re.match(name)
  1822. if match:
  1823. try:
  1824. index = int(match.group(1))
  1825. except ValueError:
  1826. continue
  1827. if index in seen_indices:
  1828. continue
  1829. seen_indices.add(index)
  1830. plate_indices.append(index)
  1831. if not plate_indices:
  1832. # No plate metadata found
  1833. return {"file_id": file_id, "filename": lib_file.filename, "plates": [], "is_multi_plate": False}
  1834. plate_indices.sort()
  1835. # Parse model_settings.config for plate names + object assignments
  1836. plate_names = {}
  1837. plate_object_ids: dict[int, list[str]] = {}
  1838. object_names_by_id: dict[str, str] = {}
  1839. if "Metadata/model_settings.config" in namelist:
  1840. try:
  1841. model_content = zf.read("Metadata/model_settings.config").decode()
  1842. model_root = ET.fromstring(model_content)
  1843. for obj_elem in model_root.findall(".//object"):
  1844. obj_id = obj_elem.get("id")
  1845. if not obj_id:
  1846. continue
  1847. name_meta = obj_elem.find("metadata[@key='name']")
  1848. obj_name = name_meta.get("value") if name_meta is not None else None
  1849. if obj_name:
  1850. object_names_by_id[obj_id] = obj_name
  1851. for plate_elem in model_root.findall(".//plate"):
  1852. plater_id = None
  1853. plater_name = None
  1854. for meta in plate_elem.findall("metadata"):
  1855. key = meta.get("key")
  1856. value = meta.get("value")
  1857. if key == "plater_id" and value:
  1858. try:
  1859. plater_id = int(value)
  1860. except ValueError:
  1861. pass # Ignore plate with non-numeric plater_id
  1862. elif key == "plater_name" and value:
  1863. plater_name = value.strip()
  1864. if plater_id is not None and plater_name:
  1865. plate_names[plater_id] = plater_name
  1866. if plater_id is not None:
  1867. for instance_elem in plate_elem.findall("model_instance"):
  1868. for inst_meta in instance_elem.findall("metadata"):
  1869. if inst_meta.get("key") == "object_id":
  1870. obj_id = inst_meta.get("value")
  1871. if not obj_id:
  1872. continue
  1873. plate_object_ids.setdefault(plater_id, [])
  1874. if obj_id not in plate_object_ids[plater_id]:
  1875. plate_object_ids[plater_id].append(obj_id)
  1876. except Exception:
  1877. pass # model_settings.config is optional; skip if missing or malformed
  1878. # Parse slice_info.config for plate metadata
  1879. plate_metadata = {}
  1880. if "Metadata/slice_info.config" in namelist:
  1881. content = zf.read("Metadata/slice_info.config").decode()
  1882. root = ET.fromstring(content)
  1883. for plate_elem in root.findall(".//plate"):
  1884. plate_info = {"filaments": [], "prediction": None, "weight": None, "name": None, "objects": []}
  1885. plate_index = None
  1886. for meta in plate_elem.findall("metadata"):
  1887. key = meta.get("key")
  1888. value = meta.get("value")
  1889. if key == "index" and value:
  1890. try:
  1891. plate_index = int(value)
  1892. except ValueError:
  1893. pass # Ignore plate with non-numeric index
  1894. elif key == "prediction" and value:
  1895. try:
  1896. plate_info["prediction"] = int(value)
  1897. except ValueError:
  1898. pass # Leave prediction as None if not a valid integer
  1899. elif key == "weight" and value:
  1900. try:
  1901. plate_info["weight"] = float(value)
  1902. except ValueError:
  1903. pass # Leave weight as None if not a valid number
  1904. # Get filaments used in this plate
  1905. for filament_elem in plate_elem.findall("filament"):
  1906. filament_id = filament_elem.get("id")
  1907. filament_type = filament_elem.get("type", "")
  1908. filament_color = filament_elem.get("color", "")
  1909. used_g = filament_elem.get("used_g", "0")
  1910. used_m = filament_elem.get("used_m", "0")
  1911. try:
  1912. used_grams = float(used_g)
  1913. except (ValueError, TypeError):
  1914. used_grams = 0
  1915. if used_grams > 0 and filament_id:
  1916. plate_info["filaments"].append(
  1917. {
  1918. "slot_id": int(filament_id),
  1919. "type": filament_type,
  1920. "color": filament_color,
  1921. "used_grams": round(used_grams, 1),
  1922. "used_meters": float(used_m) if used_m else 0,
  1923. }
  1924. )
  1925. plate_info["filaments"].sort(key=lambda x: x["slot_id"])
  1926. # Collect object names
  1927. for obj_elem in plate_elem.findall("object"):
  1928. obj_name = obj_elem.get("name")
  1929. if obj_name and obj_name not in plate_info["objects"]:
  1930. plate_info["objects"].append(obj_name)
  1931. # Set plate name
  1932. if plate_index is not None:
  1933. custom_name = plate_names.get(plate_index)
  1934. if custom_name:
  1935. plate_info["name"] = custom_name
  1936. elif plate_info["objects"]:
  1937. plate_info["name"] = plate_info["objects"][0]
  1938. plate_metadata[plate_index] = plate_info
  1939. # Parse plate_*.json for object lists when slice_info is missing
  1940. plate_json_objects: dict[int, list[str]] = {}
  1941. for name in namelist:
  1942. match = re.match(r"^Metadata/plate_(\d+)\.json$", name)
  1943. if not match:
  1944. continue
  1945. try:
  1946. plate_index = int(match.group(1))
  1947. except ValueError:
  1948. continue
  1949. try:
  1950. payload = json.loads(zf.read(name).decode())
  1951. bbox_objects = payload.get("bbox_objects", [])
  1952. names: list[str] = []
  1953. for obj in bbox_objects:
  1954. obj_name = obj.get("name") if isinstance(obj, dict) else None
  1955. if obj_name and obj_name not in names:
  1956. names.append(obj_name)
  1957. if names:
  1958. plate_json_objects[plate_index] = names
  1959. except Exception:
  1960. continue
  1961. # Build plate list
  1962. for idx in plate_indices:
  1963. meta = plate_metadata.get(idx, {})
  1964. has_thumbnail = f"Metadata/plate_{idx}.png" in namelist
  1965. objects = meta.get("objects", [])
  1966. if not objects:
  1967. objects = plate_json_objects.get(idx, [])
  1968. if not objects and plate_object_ids.get(idx):
  1969. objects = [
  1970. object_names_by_id.get(obj_id, f"Object {obj_id}") for obj_id in plate_object_ids.get(idx, [])
  1971. ]
  1972. plate_name = meta.get("name")
  1973. if not plate_name:
  1974. plate_name = plate_names.get(idx)
  1975. if not plate_name and objects:
  1976. plate_name = objects[0]
  1977. plates.append(
  1978. {
  1979. "index": idx,
  1980. "name": plate_name,
  1981. "objects": objects,
  1982. "object_count": len(objects),
  1983. "has_thumbnail": has_thumbnail,
  1984. "thumbnail_url": f"/api/v1/library/files/{file_id}/plate-thumbnail/{idx}"
  1985. if has_thumbnail
  1986. else None,
  1987. "print_time_seconds": meta.get("prediction"),
  1988. "filament_used_grams": meta.get("weight"),
  1989. "filaments": meta.get("filaments", []),
  1990. }
  1991. )
  1992. except Exception as e:
  1993. logger.warning("Failed to parse plates from library file %s: %s", file_id, e)
  1994. # SliceModal pre-check signal: the source 3MF's bound printer model. The
  1995. # CLI cannot re-slice for a different printer; surface this so the modal
  1996. # can warn the user before they pick a mismatched profile.
  1997. source_printer_model: str | None = None
  1998. try:
  1999. with zipfile.ZipFile(file_path, "r") as zf:
  2000. source_printer_model = extract_source_printer_model_from_3mf(zf)
  2001. except (zipfile.BadZipFile, OSError):
  2002. pass
  2003. return {
  2004. "file_id": file_id,
  2005. "filename": lib_file.filename,
  2006. "plates": plates,
  2007. "is_multi_plate": len(plates) > 1,
  2008. "source_printer_model": source_printer_model,
  2009. }
  2010. @router.get("/files/{file_id}/plate-thumbnail/{plate_index}")
  2011. async def get_library_file_plate_thumbnail(
  2012. file_id: int,
  2013. plate_index: int,
  2014. db: AsyncSession = Depends(get_db),
  2015. _: None = RequireCameraStreamTokenIfAuthEnabled,
  2016. ):
  2017. """Get the thumbnail image for a specific plate from a library file."""
  2018. from starlette.responses import Response
  2019. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2020. lib_file = result.scalar_one_or_none()
  2021. if not lib_file:
  2022. raise HTTPException(status_code=404, detail="File not found")
  2023. file_path = Path(app_settings.base_dir) / lib_file.file_path
  2024. if not file_path.exists():
  2025. raise HTTPException(status_code=404, detail="File not found on disk")
  2026. try:
  2027. with zipfile.ZipFile(file_path, "r") as zf:
  2028. thumb_path = f"Metadata/plate_{plate_index}.png"
  2029. if thumb_path in zf.namelist():
  2030. data = zf.read(thumb_path)
  2031. return Response(content=data, media_type="image/png")
  2032. except Exception:
  2033. pass # Archive unreadable or thumbnail missing; fall through to 404
  2034. raise HTTPException(status_code=404, detail=f"Thumbnail for plate {plate_index} not found")
  2035. async def _try_preview_slice_filaments(
  2036. db: AsyncSession,
  2037. *,
  2038. kind: str,
  2039. source_id: int,
  2040. plate_id: int,
  2041. file_path: Path,
  2042. request_id: str | None = None,
  2043. bundle_id: str | None = None,
  2044. printer_name: str | None = None,
  2045. process_name: str | None = None,
  2046. filament_names: list[str] | None = None,
  2047. ) -> list[dict] | None:
  2048. """Run a preview slice via the user's configured sidecar. Same shape as
  2049. the matching helper in archives.py — see that module for rationale.
  2050. ``request_id``: when supplied, forwarded to the sidecar so the
  2051. SliceModal's inline spinner + toast can poll the matching progress
  2052. endpoint and show "Generating G-code (45%)" for the preview as well.
  2053. ``bundle_id`` / ``printer_name`` / ``process_name`` / ``filament_names``:
  2054. when all are supplied, the preview uses ``slice_with_bundle`` against
  2055. the named bundle's preset triplet so the preview's gram numbers reflect
  2056. the same profiles the real print will use. Partial context falls back
  2057. to the embedded-settings path so a half-completed Bundle-tier selection
  2058. in the modal doesn't error out.
  2059. """
  2060. from backend.app.api.routes.settings import get_setting
  2061. from backend.app.services.slice_preview import get_preview_filaments
  2062. preferred = (await get_setting(db, "preferred_slicer")) or "bambu_studio"
  2063. if preferred == "orcaslicer":
  2064. configured = await get_setting(db, "orcaslicer_api_url")
  2065. api_url = (configured or app_settings.slicer_api_url).strip()
  2066. elif preferred == "bambu_studio":
  2067. configured = await get_setting(db, "bambu_studio_api_url")
  2068. api_url = (configured or app_settings.bambu_studio_api_url).strip()
  2069. else:
  2070. return None
  2071. if not api_url:
  2072. return None
  2073. try:
  2074. file_bytes = file_path.read_bytes()
  2075. except OSError:
  2076. return None
  2077. return await get_preview_filaments(
  2078. kind=kind,
  2079. source_id=source_id,
  2080. plate_id=plate_id,
  2081. file_bytes=file_bytes,
  2082. file_name=file_path.name,
  2083. api_url=api_url,
  2084. request_id=request_id,
  2085. bundle_id=bundle_id,
  2086. printer_name=printer_name,
  2087. process_name=process_name,
  2088. filament_names=filament_names,
  2089. )
  2090. @router.get("/files/{file_id}/filament-requirements")
  2091. async def get_library_file_filament_requirements(
  2092. file_id: int,
  2093. plate_id: int | None = None,
  2094. request_id: str | None = None,
  2095. bundle_id: str | None = None,
  2096. printer_name: str | None = None,
  2097. process_name: str | None = None,
  2098. filament_names: str | None = None,
  2099. db: AsyncSession = Depends(get_db),
  2100. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  2101. ):
  2102. """Get filament requirements from a library file.
  2103. Parses the 3MF file to extract filament slot IDs, types, colors, and usage.
  2104. This enables AMS slot assignment when printing from the file manager.
  2105. Args:
  2106. file_id: The library file ID
  2107. plate_id: Optional plate index to get filaments for a specific plate
  2108. bundle_id / printer_name / process_name / filament_names: Optional
  2109. bundle context. When all four are supplied, the preview slice
  2110. (run for unsliced project files) uses ``slice_with_bundle``
  2111. against the named preset triplet instead of the embedded-
  2112. settings fallback. ``filament_names`` is comma- or semicolon-
  2113. separated to mirror the slice route's multi-color form.
  2114. """
  2115. import defusedxml.ElementTree as ET
  2116. # Get the library file
  2117. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2118. lib_file = result.scalar_one_or_none()
  2119. if not lib_file:
  2120. raise HTTPException(status_code=404, detail="File not found")
  2121. # Get the full file path
  2122. file_path = Path(app_settings.base_dir) / lib_file.file_path
  2123. if not file_path.exists():
  2124. raise HTTPException(status_code=404, detail="File not found on disk")
  2125. # Only 3MF files have parseable filament info
  2126. if not lib_file.filename.lower().endswith(".3mf"):
  2127. return {"file_id": file_id, "filename": lib_file.filename, "plate_id": plate_id, "filaments": []}
  2128. filaments = []
  2129. try:
  2130. with zipfile.ZipFile(file_path, "r") as zf:
  2131. # Parse slice_info.config for filament requirements
  2132. if "Metadata/slice_info.config" in zf.namelist():
  2133. content = zf.read("Metadata/slice_info.config").decode()
  2134. root = ET.fromstring(content)
  2135. if plate_id is not None:
  2136. # Find filaments for specific plate
  2137. for plate_elem in root.findall(".//plate"):
  2138. # Check if this is the requested plate
  2139. plate_index = None
  2140. for meta in plate_elem.findall("metadata"):
  2141. if meta.get("key") == "index":
  2142. try:
  2143. plate_index = int(meta.get("value", ""))
  2144. except ValueError:
  2145. pass # Skip plate with non-numeric index value
  2146. break
  2147. if plate_index == plate_id:
  2148. # Extract filaments from this plate
  2149. for filament_elem in plate_elem.findall("filament"):
  2150. filament_id = filament_elem.get("id")
  2151. filament_type = filament_elem.get("type", "")
  2152. filament_color = filament_elem.get("color", "")
  2153. used_g = filament_elem.get("used_g", "0")
  2154. used_m = filament_elem.get("used_m", "0")
  2155. tray_info_idx = filament_elem.get("tray_info_idx", "")
  2156. try:
  2157. used_grams = float(used_g)
  2158. except (ValueError, TypeError):
  2159. used_grams = 0
  2160. if used_grams > 0 and filament_id:
  2161. filaments.append(
  2162. {
  2163. "slot_id": int(filament_id),
  2164. "type": filament_type,
  2165. "color": filament_color,
  2166. "used_grams": round(used_grams, 1),
  2167. "used_meters": float(used_m) if used_m else 0,
  2168. "tray_info_idx": tray_info_idx,
  2169. # Sliced output already pre-filtered by used_g>0,
  2170. # so every entry that survives is in fact used by
  2171. # this plate. Print-dispatch consumers ignore the
  2172. # flag; SliceModal uses it to enable/disable rows.
  2173. "used_in_plate": True,
  2174. }
  2175. )
  2176. break
  2177. else:
  2178. # Extract all filaments with used_g > 0 (for single-plate or overview)
  2179. for filament_elem in root.findall(".//filament"):
  2180. filament_id = filament_elem.get("id")
  2181. filament_type = filament_elem.get("type", "")
  2182. filament_color = filament_elem.get("color", "")
  2183. used_g = filament_elem.get("used_g", "0")
  2184. used_m = filament_elem.get("used_m", "0")
  2185. tray_info_idx = filament_elem.get("tray_info_idx", "")
  2186. try:
  2187. used_grams = float(used_g)
  2188. except (ValueError, TypeError):
  2189. used_grams = 0
  2190. if used_grams > 0 and filament_id:
  2191. filaments.append(
  2192. {
  2193. "slot_id": int(filament_id),
  2194. "type": filament_type,
  2195. "color": filament_color,
  2196. "used_grams": round(used_grams, 1),
  2197. "used_meters": float(used_m) if used_m else 0,
  2198. "tray_info_idx": tray_info_idx,
  2199. "used_in_plate": True,
  2200. }
  2201. )
  2202. # Unsliced project files: slice_info had no per-plate data.
  2203. # Return the FULL project_settings.config AMS slot list so
  2204. # the slicer CLI receives a profile for every project slot
  2205. # (otherwise it silently fills the gap from embedded
  2206. # defaults — surfaces as "I picked white but the print has
  2207. # grey" because the source's grey support filament leaks
  2208. # into the output). Use the preview slice to mark which
  2209. # slots the picked plate actually consumes; the SliceModal
  2210. # disables the unused rows so the user only interacts with
  2211. # the dropdowns that matter, while the backend still has
  2212. # the complete list to pass to the CLI.
  2213. if not filaments:
  2214. project_filaments = extract_project_filaments_from_3mf(zf)
  2215. used_slot_ids: set[int] = set()
  2216. if project_filaments and plate_id is not None:
  2217. # Bundle context flows through optional query params so
  2218. # callers without a Bundle-tier selection (the common
  2219. # case) hit the same path as before.
  2220. parsed_filament_names: list[str] | None = None
  2221. if filament_names:
  2222. parsed_filament_names = [
  2223. n.strip() for n in filament_names.replace(";", ",").split(",") if n.strip()
  2224. ] or None
  2225. preview = await _try_preview_slice_filaments(
  2226. db,
  2227. kind="library_file",
  2228. source_id=file_id,
  2229. plate_id=plate_id,
  2230. file_path=file_path,
  2231. request_id=request_id,
  2232. bundle_id=bundle_id,
  2233. printer_name=printer_name,
  2234. process_name=process_name,
  2235. filament_names=parsed_filament_names,
  2236. )
  2237. if preview is not None:
  2238. used_slot_ids = {f["slot_id"] for f in preview}
  2239. # Default to "every slot is used" when preview-slice
  2240. # didn't produce data: better to over-enable dropdowns
  2241. # than under-enable and have the user unable to pick a
  2242. # filament the plate actually uses.
  2243. fallback_all_used = not used_slot_ids
  2244. for f in project_filaments:
  2245. f["used_in_plate"] = fallback_all_used or f["slot_id"] in used_slot_ids
  2246. filaments = project_filaments
  2247. # Sort by slot ID
  2248. filaments.sort(key=lambda x: x["slot_id"])
  2249. # Enrich with nozzle mapping for dual-nozzle printers
  2250. nozzle_mapping = extract_nozzle_mapping_from_3mf(zf)
  2251. if nozzle_mapping:
  2252. for filament in filaments:
  2253. filament["nozzle_id"] = nozzle_mapping.get(filament["slot_id"])
  2254. except Exception as e:
  2255. logger.warning("Failed to parse filament requirements from library file %s: %s", file_id, e)
  2256. return {
  2257. "file_id": file_id,
  2258. "filename": lib_file.filename,
  2259. "plate_id": plate_id,
  2260. "filaments": filaments,
  2261. }
  2262. _STRIPPABLE_3MF_CONFIGS = frozenset(
  2263. {
  2264. # Settings dump used by --load-settings validation; the CLI tries to
  2265. # match its sentinel values (`prime_tower_brim_width: -1`, empty
  2266. # arrays) against the supplied profile and rejects out-of-range.
  2267. "Metadata/project_settings.config",
  2268. # Per-object settings overrides referencing the source plate's
  2269. # filament IDs / printer IDs. When the user picks a different
  2270. # printer / filament triplet, the IDs no longer resolve and the
  2271. # CLI exits non-zero on input validation.
  2272. "Metadata/model_settings.config",
  2273. # Slicer-version + plate-config + filament-mapping snapshot from
  2274. # the original slice. Includes the original printer model and
  2275. # filament references; mismatches against `--load-settings`
  2276. # consistently surfaced as `Slicer CLI failed (500)` for every
  2277. # 3MF in production. Removing it lets the CLI build a fresh slice
  2278. # plan from the supplied profile triplet.
  2279. "Metadata/slice_info.config",
  2280. # Multi-part / split-mesh metadata referencing object IDs from the
  2281. # original slice. Strip for the same reason — preserves the geometry
  2282. # in `3D/3dmodel.model` while dropping the orphan references.
  2283. "Metadata/cut_information.xml",
  2284. }
  2285. )
  2286. def _strip_3mf_embedded_settings(zip_bytes: bytes) -> bytes:
  2287. """Remove embedded slicer-config metadata from a 3MF.
  2288. Bambuddy supplies the slicer profile triplet via the sidecar's
  2289. ``--load-settings`` path; the 3MF's embedded settings would otherwise be
  2290. validated by the CLI first and can fail with sentinel-value range
  2291. checks (`prime_tower_brim_width: -1 not in range`, etc.) regardless of
  2292. what we pass via ``--load-settings``. Stripping the embedded configs
  2293. forces the CLI to use the supplied profiles only. Geometry
  2294. (``3D/3dmodel.model``), thumbnails, color, and multi-part data inside
  2295. the 3MF are preserved.
  2296. The set of strippable filenames is centralised in
  2297. ``_STRIPPABLE_3MF_CONFIGS`` — see that constant for the per-file
  2298. rationale. Project-settings alone wasn't enough: real-world Bambu
  2299. Studio 3MFs cross-reference printer / filament IDs from the other
  2300. metadata configs, and any single leftover triggered the validation
  2301. failure that made every profile-driven slice fall back to embedded
  2302. settings.
  2303. """
  2304. from io import BytesIO
  2305. src = BytesIO(zip_bytes)
  2306. dst = BytesIO()
  2307. with zipfile.ZipFile(src, "r") as zin, zipfile.ZipFile(dst, "w", zipfile.ZIP_DEFLATED) as zout:
  2308. for item in zin.infolist():
  2309. if item.filename in _STRIPPABLE_3MF_CONFIGS:
  2310. continue
  2311. zout.writestr(item, zin.read(item.filename))
  2312. return dst.getvalue()
  2313. # Keys in ``Metadata/project_settings.config`` that BambuStudio writes ``"-1"``
  2314. # to when the user wants the value inherited from the parent process preset.
  2315. # The CLI's ``StaticPrintConfig`` validator runs against the embedded settings
  2316. # *before* ``--load-settings`` overrides apply, so a sentinel ``"-1"`` trips
  2317. # the field's lower-bound range check and the CLI exits non-zero before our
  2318. # profile triplet is ever consulted (#1201 — MakerWorld P2S models).
  2319. #
  2320. # Allowlisted (rather than "strip every '-1' value") because some fields
  2321. # legitimately accept negative numbers (z_offset, translation values, etc.)
  2322. # and a blanket strip would silently corrupt those.
  2323. #
  2324. # Add new entries here as more reports surface — the slicer's error message
  2325. # names the offending field directly (`<field>: -1 not in range [...]`).
  2326. _PROJECT_SETTINGS_SENTINEL_KEYS = frozenset(
  2327. {
  2328. # Reported in #1201 (MakerWorld P2S 3MFs).
  2329. "raft_first_layer_expansion",
  2330. "tree_support_wall_count",
  2331. # Cited in the strip-experiment comment block above as a known sentinel
  2332. # case from earlier reports.
  2333. "prime_tower_brim_width",
  2334. }
  2335. )
  2336. def _sanitize_project_settings_sentinels(zip_bytes: bytes) -> bytes:
  2337. """Strip ``"-1"`` inherit-from-parent sentinels from the 3MF's
  2338. ``Metadata/project_settings.config`` so the slicer CLI's range validator
  2339. accepts the file (#1201).
  2340. Removes only allowlisted keys (see ``_PROJECT_SETTINGS_SENTINEL_KEYS``)
  2341. when their value is exactly ``"-1"``. The rest of the config — and every
  2342. other entry in the zip — is preserved byte-for-byte. Unlike the earlier
  2343. full-strip experiment (see ``_strip_3mf_embedded_settings`` and the
  2344. cautionary comment in ``_run_slicer_with_fallback``) this leaves
  2345. ``StaticPrintConfig`` initialisation intact: the file is still present,
  2346. still parses, and the slicer falls back to the supplied
  2347. ``--load-settings`` value for the removed key.
  2348. Returns the original bytes unchanged when no sanitisation is needed
  2349. (input isn't a valid zip, no ``project_settings.config``, no allowlisted
  2350. sentinels present, or any other parse failure) so the caller can pass
  2351. the result on without further checks.
  2352. """
  2353. from io import BytesIO
  2354. try:
  2355. with zipfile.ZipFile(BytesIO(zip_bytes), "r") as zin:
  2356. if "Metadata/project_settings.config" not in zin.namelist():
  2357. return zip_bytes
  2358. try:
  2359. config = json.loads(zin.read("Metadata/project_settings.config").decode("utf-8"))
  2360. except (json.JSONDecodeError, UnicodeDecodeError):
  2361. return zip_bytes
  2362. if not isinstance(config, dict):
  2363. return zip_bytes
  2364. removed = [key for key in _PROJECT_SETTINGS_SENTINEL_KEYS if config.get(key) == "-1"]
  2365. if not removed:
  2366. return zip_bytes
  2367. for key in removed:
  2368. config.pop(key, None)
  2369. patched = json.dumps(config)
  2370. logger.info(
  2371. "3MF sanitiser: removed sentinel '-1' for keys %s — slicer will use --load-settings defaults",
  2372. sorted(removed),
  2373. )
  2374. dst = BytesIO()
  2375. with zipfile.ZipFile(dst, "w", zipfile.ZIP_DEFLATED) as zout:
  2376. for item in zin.infolist():
  2377. if item.filename == "Metadata/project_settings.config":
  2378. zout.writestr(item, patched)
  2379. else:
  2380. zout.writestr(item, zin.read(item.filename))
  2381. return dst.getvalue()
  2382. except (zipfile.BadZipFile, OSError):
  2383. return zip_bytes
  2384. async def _run_slicer_with_fallback(
  2385. db: AsyncSession,
  2386. *,
  2387. model_bytes: bytes,
  2388. model_filename: str,
  2389. request: SliceRequest,
  2390. current_user_id: int | None = None,
  2391. job_id: int | None = None,
  2392. ):
  2393. """Validate presets, dispatch to the right sidecar, run the slicer with
  2394. the auto-fallback for 3MF inputs whose `--load-settings` path crashes the
  2395. CLI. Returns ``(SliceResult, used_embedded_settings: bool)``. Raises
  2396. ``HTTPException`` for any caller-facing error.
  2397. `current_user_id` is needed to resolve **cloud** presets — the cloud token
  2398. is per-user when auth is enabled. For the legacy / local-only path it can
  2399. be left ``None``.
  2400. `job_id`: when set, a request_id is generated and a parallel poller
  2401. pushes the sidecar's --pipe-fed progress events onto
  2402. ``slice_dispatch.set_progress(job_id, ...)`` so the UI's persistent
  2403. toast can show "Generating G-code (75%)" instead of just elapsed
  2404. time. Pass None for synchronous routes that aren't tracked by the
  2405. dispatcher.
  2406. """
  2407. from backend.app.api.routes.settings import get_setting
  2408. from backend.app.services.preset_resolver import resolve_preset_ref
  2409. from backend.app.services.slicer_api import (
  2410. SlicerApiServerError,
  2411. SlicerApiService,
  2412. SlicerApiUnavailableError,
  2413. SlicerInputError,
  2414. )
  2415. # Bundle dispatch path: when SliceRequest.bundle is set, the schema
  2416. # validator short-circuited the presets-required check, so the
  2417. # PresetRef fields may all be None. Skip resolve_preset_ref entirely
  2418. # — the sidecar will materialise the per-category JSONs from the
  2419. # bundle's extracted directory at slice time.
  2420. use_bundle = request.bundle is not None
  2421. user: User | None = None
  2422. presets: dict[str, str] = {}
  2423. filament_jsons: list[str] = []
  2424. if not use_bundle:
  2425. # Resolve each slot via the source-aware resolver. The schema
  2426. # validator has already normalised legacy `*_preset_id: int`
  2427. # fields into `PresetRef(source='local', id=str(int))`, so all
  2428. # three are guaranteed non-None here.
  2429. if current_user_id is not None:
  2430. user = await db.get(User, current_user_id)
  2431. refs = {
  2432. "printer": request.printer_preset,
  2433. "process": request.process_preset,
  2434. }
  2435. for slot, ref in refs.items():
  2436. assert ref is not None, "schema validator guarantees PresetRef is set"
  2437. presets[slot] = await resolve_preset_ref(db, user, ref, slot)
  2438. # Multi-color: resolve each filament slot in plate order. The schema
  2439. # validator backfilled `filament_presets` from the legacy `filament_preset`
  2440. # field for single-color callers, so this list is always non-empty.
  2441. for ref in request.filament_presets:
  2442. assert ref is not None, "schema validator guarantees filament list is non-None"
  2443. filament_jsons.append(await resolve_preset_ref(db, user, ref, "filament"))
  2444. # Slicer routing — pick the sidecar URL by preferred_slicer.
  2445. # The per-install URL setting (Settings UI → Slicer card) wins; an
  2446. # empty value falls back to the SLICER_API_URL / BAMBU_STUDIO_API_URL
  2447. # env defaults defined in core/config.py.
  2448. preferred = (await get_setting(db, "preferred_slicer")) or "bambu_studio"
  2449. if preferred == "orcaslicer":
  2450. configured = await get_setting(db, "orcaslicer_api_url")
  2451. api_url = (configured or app_settings.slicer_api_url).strip()
  2452. elif preferred == "bambu_studio":
  2453. configured = await get_setting(db, "bambu_studio_api_url")
  2454. api_url = (configured or app_settings.bambu_studio_api_url).strip()
  2455. else:
  2456. raise HTTPException(
  2457. status_code=400,
  2458. detail=f"Unknown preferred_slicer setting: '{preferred}'. Expected 'orcaslicer' or 'bambu_studio'.",
  2459. )
  2460. # Note: an earlier version of this code stripped Metadata/project_settings.
  2461. # config + model_settings.config + slice_info.config + cut_information.xml
  2462. # before forwarding the 3MF, the theory being that --load-settings would
  2463. # then take precedence cleanly. That theory was wrong: model_settings.
  2464. # config carries the plate definitions the CLI needs to map `--slice N`
  2465. # to a real plate, and slice_info / project_settings supply baseline
  2466. # config the CLI's StaticPrintConfig pass needs at all. Stripping ANY
  2467. # of them caused the CLI to silently exit immediately after
  2468. # "Initializing StaticPrintConfigs" — exit code 0, no result.json, no
  2469. # stderr — which Node's child_process treated as failure and Bambuddy
  2470. # then masked by falling back to slice_without_profiles using the
  2471. # un-stripped bytes (and the source's embedded printer). Net effect:
  2472. # every 3MF slice with profiles silently produced wrong-printer output.
  2473. # Forwarding the original bytes lets --load-settings override the
  2474. # specific fields the user changed (printer/process/filament) while
  2475. # the embedded plate / model definitions remain intact.
  2476. is_3mf = model_filename.lower().endswith(".3mf")
  2477. primary_bytes = model_bytes
  2478. if is_3mf:
  2479. # Strip "-1" inherit-from-parent sentinels from
  2480. # Metadata/project_settings.config so the CLI's StaticPrintConfig
  2481. # range validator accepts the file (#1201). Surgical — keeps the
  2482. # config present, just removes the offending keys; the supplied
  2483. # --load-settings (and the fallback's embedded values for keys we
  2484. # didn't touch) still drive the slice.
  2485. primary_bytes = _sanitize_project_settings_sentinels(primary_bytes)
  2486. used_embedded_settings = False
  2487. service = SlicerApiService(api_url)
  2488. # When this slice is dispatcher-tracked, generate a request_id so
  2489. # the sidecar publishes progress under it, and wire a callback that
  2490. # forwards each frame onto SliceDispatchService.set_progress for the
  2491. # status-poll endpoint to surface to the UI.
  2492. progress_request_id: str | None = None
  2493. progress_callback = None
  2494. if job_id is not None:
  2495. from uuid import uuid4
  2496. from backend.app.services.slice_dispatch import slice_dispatch as _dispatch
  2497. progress_request_id = str(uuid4())
  2498. def _on_progress(snapshot: dict) -> None:
  2499. _dispatch.set_progress(job_id, snapshot)
  2500. progress_callback = _on_progress
  2501. try:
  2502. try:
  2503. if use_bundle:
  2504. # Bundle dispatch: sidecar materialises the JSON triplet
  2505. # from the stored .bbscfg by name. ``request.bundle`` is
  2506. # guaranteed non-None here by the use_bundle branch above.
  2507. assert request.bundle is not None
  2508. result = await service.slice_with_bundle(
  2509. model_bytes=primary_bytes,
  2510. model_filename=model_filename,
  2511. bundle_id=request.bundle.bundle_id,
  2512. printer_name=request.bundle.printer_name,
  2513. process_name=request.bundle.process_name,
  2514. filament_names=request.bundle.filament_names,
  2515. plate=request.plate,
  2516. export_3mf=request.export_3mf,
  2517. request_id=progress_request_id,
  2518. on_progress=progress_callback,
  2519. )
  2520. else:
  2521. result = await service.slice_with_profiles(
  2522. model_bytes=primary_bytes,
  2523. model_filename=model_filename,
  2524. printer_profile_json=presets["printer"],
  2525. process_profile_json=presets["process"],
  2526. filament_profile_jsons=filament_jsons,
  2527. plate=request.plate,
  2528. export_3mf=request.export_3mf,
  2529. request_id=progress_request_id,
  2530. on_progress=progress_callback,
  2531. )
  2532. except SlicerApiServerError as exc:
  2533. if not is_3mf:
  2534. raise
  2535. logger.warning(
  2536. "Slicer CLI rejected --load-settings for %s (%s); retrying with embedded settings",
  2537. model_filename,
  2538. exc,
  2539. )
  2540. # Forward the same request_id + callback so the toast's live
  2541. # progress keeps updating across the fallback retry instead
  2542. # of going blank for the rest of the slice. Use the sanitised
  2543. # bytes — the embedded-settings path also reads the same
  2544. # project_settings.config and the same range validator runs
  2545. # there too, so without sanitisation the fallback would die
  2546. # on the same sentinel error (#1201). Same fallback applies
  2547. # to the bundle path: if the resolved triplet crashes the CLI,
  2548. # embedded settings give the user *something* rather than a
  2549. # hard failure (the SliceModal flags the difference via
  2550. # used_embedded_settings).
  2551. result = await service.slice_without_profiles(
  2552. model_bytes=primary_bytes,
  2553. model_filename=model_filename,
  2554. plate=request.plate,
  2555. export_3mf=request.export_3mf,
  2556. request_id=progress_request_id,
  2557. on_progress=progress_callback,
  2558. )
  2559. used_embedded_settings = True
  2560. except SlicerInputError as exc:
  2561. raise HTTPException(status_code=400, detail=str(exc)) from exc
  2562. except SlicerApiServerError as exc:
  2563. raise HTTPException(status_code=502, detail=str(exc)) from exc
  2564. except SlicerApiUnavailableError as exc:
  2565. raise HTTPException(status_code=502, detail=str(exc)) from exc
  2566. finally:
  2567. await service.close()
  2568. return result, used_embedded_settings
  2569. async def slice_and_persist(
  2570. db: AsyncSession,
  2571. *,
  2572. model_bytes: bytes,
  2573. model_filename: str,
  2574. folder_id: int | None,
  2575. extra_metadata: dict | None,
  2576. request: SliceRequest,
  2577. current_user_id: int | None,
  2578. job_id: int | None = None,
  2579. ) -> SliceResponse:
  2580. """Slice a model and save the result as a new ``LibraryFile`` in
  2581. ``folder_id`` (same folder as the source by convention).
  2582. Always exports as ``.gcode.3mf`` so the existing library thumbnail
  2583. pipeline works on the new file. Plain ``.gcode`` would have no
  2584. embedded thumbnail to extract.
  2585. """
  2586. from backend.app.services.archive import ThreeMFParser
  2587. library_request = request.model_copy(update={"export_3mf": True})
  2588. result, used_embedded_settings = await _run_slicer_with_fallback(
  2589. db,
  2590. model_bytes=model_bytes,
  2591. model_filename=model_filename,
  2592. request=library_request,
  2593. current_user_id=current_user_id,
  2594. job_id=job_id,
  2595. )
  2596. base_name = model_filename.rsplit(".", 1)[0]
  2597. out_filename = f"{base_name}.gcode.3mf"
  2598. unique_name = f"{uuid.uuid4().hex}.gcode.3mf"
  2599. out_path = get_library_files_dir() / unique_name
  2600. out_path.write_bytes(result.content)
  2601. # Extract thumbnail from the produced 3MF so the library card shows a
  2602. # preview. Failures here aren't fatal — the file is still useful
  2603. # without a thumbnail.
  2604. thumbnail_relative: str | None = None
  2605. parsed_metadata: dict = {}
  2606. try:
  2607. parser = ThreeMFParser(str(out_path))
  2608. parsed = parser.parse()
  2609. thumb_data = parsed.get("_thumbnail_data")
  2610. thumb_ext = parsed.get("_thumbnail_ext", ".png")
  2611. if thumb_data:
  2612. thumb_filename = f"{uuid.uuid4().hex}{thumb_ext}"
  2613. thumb_path = get_library_thumbnails_dir() / thumb_filename
  2614. thumb_path.write_bytes(thumb_data)
  2615. thumbnail_relative = to_relative_path(thumb_path)
  2616. cleaned = _clean_3mf_metadata(parsed)
  2617. if isinstance(cleaned, dict):
  2618. parsed_metadata = cleaned
  2619. except Exception as exc:
  2620. logger.warning("Failed to parse sliced 3MF metadata for %s: %s", out_filename, exc)
  2621. # The parsed 3MF metadata carries a `print_name` lifted from the source
  2622. # file's embedded settings (BambuStudio always sets this; OrcaSlicer
  2623. # often leaves it blank). The FileManager listing prefers print_name
  2624. # over filename for display, which makes a sliced row indistinguishable
  2625. # from its source. Drop print_name so the listing falls back to the
  2626. # actual filename — which already ends in ".gcode.3mf" and self-describes
  2627. # as the sliced output.
  2628. metadata: dict = {k: v for k, v in parsed_metadata.items() if k != "print_name"}
  2629. metadata.update(
  2630. {
  2631. "print_time_seconds": result.print_time_seconds,
  2632. "filament_used_g": result.filament_used_g,
  2633. "filament_used_mm": result.filament_used_mm,
  2634. }
  2635. )
  2636. if used_embedded_settings:
  2637. metadata["used_embedded_settings"] = True
  2638. if extra_metadata:
  2639. metadata.update(extra_metadata)
  2640. new_file = LibraryFile(
  2641. folder_id=folder_id,
  2642. filename=out_filename,
  2643. file_path=to_relative_path(out_path),
  2644. # Sliced output is a `.gcode.3mf` zip with embedded G-code, but the
  2645. # user-facing meaning is "ready-to-print G-code" — using "gcode"
  2646. # gives it the same badge as plain .gcode files and distinguishes
  2647. # it from un-sliced `.3mf` source models.
  2648. file_type="gcode",
  2649. file_size=len(result.content),
  2650. file_hash=hashlib.sha256(result.content).hexdigest(),
  2651. thumbnail_path=thumbnail_relative,
  2652. file_metadata=metadata,
  2653. source_type="sliced",
  2654. created_by_id=current_user_id,
  2655. )
  2656. db.add(new_file)
  2657. await db.commit()
  2658. # No refresh: expire_on_commit=False keeps id/filename accessible, and
  2659. # refreshing here flakes under pytest-xdist when teardown of a sibling
  2660. # test races the SELECT.
  2661. return SliceResponse(
  2662. library_file_id=new_file.id,
  2663. name=new_file.filename,
  2664. print_time_seconds=result.print_time_seconds,
  2665. filament_used_g=result.filament_used_g,
  2666. filament_used_mm=result.filament_used_mm,
  2667. used_embedded_settings=used_embedded_settings,
  2668. )
  2669. async def slice_and_persist_as_archive(
  2670. db: AsyncSession,
  2671. *,
  2672. model_bytes: bytes,
  2673. model_filename: str,
  2674. request: SliceRequest,
  2675. source_archive, # PrintArchive — hint kept loose to avoid cyclic import
  2676. current_user_id: int | None,
  2677. job_id: int | None = None,
  2678. ):
  2679. """Slice a model and save the result as a new ``PrintArchive`` row,
  2680. inheriting printer / project / makerworld metadata from the source
  2681. archive. Always exports as a `.gcode.3mf` so the existing thumbnail
  2682. and plates infrastructure (which expects a zip-shaped 3MF) works on
  2683. the new archive. Returns ``SliceArchiveResponse``.
  2684. """
  2685. from backend.app.models.archive import PrintArchive
  2686. from backend.app.schemas.slicer import SliceArchiveResponse
  2687. from backend.app.services.archive import ThreeMFParser
  2688. # Archive sinks always want a 3MF. The library route still respects the
  2689. # caller's `export_3mf` flag; here we override.
  2690. archive_request = request.model_copy(update={"export_3mf": True})
  2691. result, used_embedded_settings = await _run_slicer_with_fallback(
  2692. db,
  2693. model_bytes=model_bytes,
  2694. model_filename=model_filename,
  2695. request=archive_request,
  2696. job_id=job_id,
  2697. current_user_id=current_user_id,
  2698. )
  2699. base_name = model_filename.rsplit(".", 1)[0]
  2700. out_filename = f"{base_name}.gcode.3mf"
  2701. timestamp = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S")
  2702. printer_folder = str(source_archive.printer_id) if source_archive.printer_id is not None else "unassigned"
  2703. archive_subdir = f"{timestamp}_{base_name}_sliced"
  2704. archive_dir = app_settings.archive_dir / printer_folder / archive_subdir
  2705. archive_dir.mkdir(parents=True, exist_ok=True)
  2706. out_path = archive_dir / out_filename
  2707. out_path.write_bytes(result.content)
  2708. # Extract a thumbnail from the produced 3MF so the new archive card has
  2709. # a preview. The 3MF parser pulls Metadata/plate_*.png; failures here
  2710. # shouldn't fail the whole slice — the archive row is still useful
  2711. # without a thumbnail.
  2712. thumbnail_path: str | None = None
  2713. parsed_metadata: dict = {}
  2714. try:
  2715. parser = ThreeMFParser(str(out_path))
  2716. parsed = parser.parse()
  2717. thumb_data = parsed.get("_thumbnail_data")
  2718. thumb_ext = parsed.get("_thumbnail_ext", ".png")
  2719. if thumb_data:
  2720. thumb_dest = archive_dir / f"thumbnail{thumb_ext}"
  2721. thumb_dest.write_bytes(thumb_data)
  2722. thumbnail_path = str(thumb_dest.relative_to(app_settings.base_dir))
  2723. parsed_metadata = {k: v for k, v in parsed.items() if not k.startswith("_")}
  2724. except Exception as exc:
  2725. logger.warning("Failed to parse sliced 3MF metadata for %s: %s", out_filename, exc)
  2726. metadata = dict(source_archive.extra_data) if source_archive.extra_data else {}
  2727. metadata.update(parsed_metadata)
  2728. metadata.update(
  2729. {
  2730. "sliced_from_archive_id": source_archive.id,
  2731. "print_time_seconds": result.print_time_seconds,
  2732. "filament_used_g": result.filament_used_g,
  2733. "filament_used_mm": result.filament_used_mm,
  2734. }
  2735. )
  2736. if used_embedded_settings:
  2737. metadata["used_embedded_settings"] = True
  2738. # Prefer the actually-used filament list from the sliced output's
  2739. # slice_info.config (parsed_metadata.filament_* — only entries with
  2740. # used_g > 0). Falling back to the source_archive's list would
  2741. # surface every project-wide AMS slot, including ones the picked
  2742. # plate doesn't use (16+ swatches on the card for a 2-color print).
  2743. new_filament_type = parsed_metadata.get("filament_type") or source_archive.filament_type
  2744. new_filament_color = parsed_metadata.get("filament_color") or source_archive.filament_color
  2745. new_archive = PrintArchive(
  2746. printer_id=source_archive.printer_id,
  2747. project_id=source_archive.project_id,
  2748. filename=out_filename,
  2749. file_path=str(out_path.relative_to(app_settings.base_dir)),
  2750. file_size=len(result.content),
  2751. content_hash=hashlib.sha256(result.content).hexdigest(),
  2752. thumbnail_path=thumbnail_path,
  2753. # Inherit identity from the source archive so the new entry shows
  2754. # up alongside its sibling in the archives list.
  2755. print_name=(source_archive.print_name or base_name) + " (re-sliced)",
  2756. print_time_seconds=result.print_time_seconds,
  2757. filament_used_grams=result.filament_used_g or None,
  2758. filament_type=new_filament_type,
  2759. filament_color=new_filament_color,
  2760. layer_height=source_archive.layer_height,
  2761. nozzle_diameter=source_archive.nozzle_diameter,
  2762. sliced_for_model=source_archive.sliced_for_model,
  2763. makerworld_url=source_archive.makerworld_url,
  2764. designer=source_archive.designer,
  2765. # Sliced-but-not-printed: keep status default ("completed") so it
  2766. # surfaces in the normal archives list, but do not stamp
  2767. # started/completed_at — the user hasn't actually printed it yet.
  2768. extra_data=metadata,
  2769. created_by_id=current_user_id,
  2770. )
  2771. db.add(new_archive)
  2772. await db.commit()
  2773. await db.refresh(new_archive)
  2774. return SliceArchiveResponse(
  2775. archive_id=new_archive.id,
  2776. name=new_archive.print_name or out_filename,
  2777. print_time_seconds=result.print_time_seconds,
  2778. filament_used_g=result.filament_used_g,
  2779. filament_used_mm=result.filament_used_mm,
  2780. used_embedded_settings=used_embedded_settings,
  2781. )
  2782. @router.post("/files/{file_id}/slice", status_code=202)
  2783. async def slice_library_file(
  2784. file_id: int,
  2785. request: SliceRequest,
  2786. db: AsyncSession = Depends(get_db),
  2787. current_user: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_UPLOAD)),
  2788. api_key_cloud_owner: User | None = Depends(resolve_api_key_cloud_owner),
  2789. ):
  2790. """Enqueue a slice job for a library file. Returns 202 + job_id; the
  2791. slice runs in the background, the caller polls `GET /slice-jobs/{id}`.
  2792. """
  2793. from backend.app.core.database import async_session
  2794. from backend.app.services.slice_dispatch import (
  2795. http_exception_to_job_error,
  2796. slice_dispatch,
  2797. )
  2798. src_result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2799. lib_file = src_result.scalar_one_or_none()
  2800. if not lib_file:
  2801. raise HTTPException(status_code=404, detail="File not found")
  2802. src_lower = (lib_file.filename or "").lower()
  2803. if not (
  2804. src_lower.endswith(".stl")
  2805. or src_lower.endswith(".3mf")
  2806. or src_lower.endswith(".step")
  2807. or src_lower.endswith(".stp")
  2808. ):
  2809. raise HTTPException(status_code=400, detail="Source file must be STL, 3MF, or STEP")
  2810. src_path = Path(app_settings.base_dir) / lib_file.file_path
  2811. if not src_path.exists():
  2812. raise HTTPException(status_code=404, detail="Source file missing on disk")
  2813. # Capture inputs the bg task needs — the request DB session is closed
  2814. # before the background task runs.
  2815. model_bytes = src_path.read_bytes()
  2816. folder_id = lib_file.folder_id
  2817. source_lib_file_id = lib_file.id
  2818. # API-keyed callers get None from the auth gate (auth.py keeps that
  2819. # behaviour to avoid a wider scope expansion). Fall back to the API
  2820. # key's owner so cloud-preset resolution can read the stored
  2821. # cloud_token (#1182 follow-up).
  2822. cloud_token_user = current_user or api_key_cloud_owner
  2823. user_id = cloud_token_user.id if cloud_token_user else None
  2824. # If the source has a `print_name` in its metadata (BambuStudio always
  2825. # sets this; OrcaSlicer often leaves it blank), derive the sliced
  2826. # output's filename from it instead of the raw filename. The source
  2827. # row's display already prefers print_name, so the sliced row's
  2828. # filename ("Piggo the piggy bank.gcode.3mf") will match the source's
  2829. # display name ("Piggo the piggy bank") with the gcode extension added.
  2830. src_print_name = None
  2831. if lib_file.file_metadata:
  2832. candidate = lib_file.file_metadata.get("print_name")
  2833. if isinstance(candidate, str) and candidate.strip():
  2834. src_print_name = candidate.strip()
  2835. src_ext = Path(lib_file.filename).suffix.lower() or ".3mf"
  2836. model_filename = f"{src_print_name}{src_ext}" if src_print_name else lib_file.filename
  2837. async def _run(job_id: int):
  2838. async with async_session() as task_db:
  2839. try:
  2840. response = await slice_and_persist(
  2841. task_db,
  2842. model_bytes=model_bytes,
  2843. model_filename=model_filename,
  2844. folder_id=folder_id,
  2845. extra_metadata={"sliced_from_library_file_id": source_lib_file_id},
  2846. request=request,
  2847. current_user_id=user_id,
  2848. job_id=job_id,
  2849. )
  2850. except HTTPException as exc:
  2851. raise http_exception_to_job_error(exc) from exc
  2852. return response.model_dump()
  2853. job = await slice_dispatch.enqueue(
  2854. kind="library_file",
  2855. source_id=lib_file.id,
  2856. source_name=lib_file.filename,
  2857. run=_run,
  2858. )
  2859. return {
  2860. "job_id": job.id,
  2861. "status": job.status,
  2862. "status_url": f"/api/v1/slice-jobs/{job.id}",
  2863. }
  2864. @router.post("/files/{file_id}/print")
  2865. async def print_library_file(
  2866. file_id: int,
  2867. printer_id: int,
  2868. body: FilePrintRequest | None = None,
  2869. db: AsyncSession = Depends(get_db),
  2870. current_user: User | None = Depends(require_permission_if_auth_enabled(Permission.PRINTERS_CONTROL)),
  2871. ):
  2872. """Dispatch a library file for send/start on a printer.
  2873. The actual send/start work is handled asynchronously by background
  2874. dispatch so the UI can continue immediately.
  2875. Only sliced files (.gcode or .gcode.3mf) can be printed.
  2876. """
  2877. from backend.app.models.printer import Printer
  2878. from backend.app.services.background_dispatch import DispatchEnqueueRejected, background_dispatch
  2879. from backend.app.services.printer_manager import printer_manager
  2880. # Use defaults if no body provided
  2881. if body is None:
  2882. body = FilePrintRequest()
  2883. # Get the library file
  2884. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  2885. lib_file = result.scalar_one_or_none()
  2886. if not lib_file:
  2887. raise HTTPException(status_code=404, detail="File not found")
  2888. # Validate file is sliced
  2889. if not is_sliced_file(lib_file.filename):
  2890. raise HTTPException(
  2891. status_code=400,
  2892. detail="Not a sliced file. Only .gcode or .gcode.3mf files can be printed.",
  2893. )
  2894. # Get the full file path
  2895. file_path = Path(app_settings.base_dir) / lib_file.file_path
  2896. if not file_path.exists():
  2897. raise HTTPException(status_code=404, detail="File not found on disk")
  2898. # Get printer
  2899. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  2900. printer = result.scalar_one_or_none()
  2901. if not printer:
  2902. raise HTTPException(status_code=404, detail="Printer not found")
  2903. # Check printer is connected
  2904. if not printer_manager.is_connected(printer_id):
  2905. raise HTTPException(status_code=400, detail="Printer is not connected")
  2906. # Validate project exists before dispatching so a bogus ID yields 404, not a FK-constraint 500
  2907. if body.project_id is not None:
  2908. project_result = await db.execute(select(Project).where(Project.id == body.project_id))
  2909. if not project_result.scalar_one_or_none():
  2910. raise HTTPException(status_code=404, detail="Project not found")
  2911. plate_name = body.plate_name
  2912. if not plate_name and body.plate_id is not None:
  2913. plate_name = f"Plate {body.plate_id}"
  2914. dispatch_source_name = lib_file.filename
  2915. if plate_name:
  2916. dispatch_source_name = f"{lib_file.filename} • {plate_name}"
  2917. try:
  2918. dispatch_result = await background_dispatch.dispatch_print_library_file(
  2919. file_id=file_id,
  2920. filename=dispatch_source_name,
  2921. printer_id=printer_id,
  2922. printer_name=printer.name,
  2923. options=body.model_dump(exclude_none=True, exclude={"cleanup_library_after_dispatch"}),
  2924. project_id=body.project_id,
  2925. requested_by_user_id=current_user.id if current_user else None,
  2926. requested_by_username=current_user.username if current_user else None,
  2927. cleanup_library_after_dispatch=body.cleanup_library_after_dispatch,
  2928. )
  2929. except DispatchEnqueueRejected as e:
  2930. raise HTTPException(status_code=409, detail=str(e)) from e
  2931. return {
  2932. "status": "dispatched",
  2933. "printer_id": printer_id,
  2934. "archive_id": None,
  2935. "filename": lib_file.filename,
  2936. "dispatch_job_id": dispatch_result["dispatch_job_id"],
  2937. "dispatch_position": dispatch_result["dispatch_position"],
  2938. }
  2939. # ============ File Detail Endpoints ============
  2940. @router.get("/files/{file_id}", response_model=FileResponseSchema)
  2941. async def get_file(
  2942. file_id: int,
  2943. db: AsyncSession = Depends(get_db),
  2944. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  2945. ):
  2946. """Get a file by ID with full details."""
  2947. result = await db.execute(
  2948. LibraryFile.active().options(selectinload(LibraryFile.created_by)).where(LibraryFile.id == file_id)
  2949. )
  2950. file = result.scalar_one_or_none()
  2951. if not file:
  2952. raise HTTPException(status_code=404, detail="File not found")
  2953. # Get folder name
  2954. folder_name = None
  2955. if file.folder_id:
  2956. folder_result = await db.execute(select(LibraryFolder.name).where(LibraryFolder.id == file.folder_id))
  2957. folder_name = folder_result.scalar()
  2958. # Get project name
  2959. project_name = None
  2960. if file.project_id:
  2961. project_result = await db.execute(select(Project.name).where(Project.id == file.project_id))
  2962. project_name = project_result.scalar()
  2963. # Get duplicates
  2964. duplicates = []
  2965. duplicate_count = 0
  2966. if file.file_hash:
  2967. dup_result = await db.execute(
  2968. select(LibraryFile, LibraryFolder.name)
  2969. .outerjoin(LibraryFolder, LibraryFile.folder_id == LibraryFolder.id)
  2970. .where(
  2971. LibraryFile.file_hash == file.file_hash,
  2972. LibraryFile.id != file.id,
  2973. LibraryFile.deleted_at.is_(None),
  2974. )
  2975. )
  2976. for dup_file, dup_folder_name in dup_result.all():
  2977. duplicates.append(
  2978. FileDuplicate(
  2979. id=dup_file.id,
  2980. filename=dup_file.filename,
  2981. folder_id=dup_file.folder_id,
  2982. folder_name=dup_folder_name,
  2983. created_at=dup_file.created_at,
  2984. )
  2985. )
  2986. duplicate_count = len(duplicates)
  2987. # Extract key metadata fields
  2988. print_name = None
  2989. print_time = None
  2990. filament_grams = None
  2991. sliced_for_model = None
  2992. if file.file_metadata:
  2993. print_name = file.file_metadata.get("print_name")
  2994. print_time = file.file_metadata.get("print_time_seconds")
  2995. filament_grams = file.file_metadata.get("filament_used_grams")
  2996. sliced_for_model = file.file_metadata.get("sliced_for_model")
  2997. return FileResponseSchema(
  2998. id=file.id,
  2999. folder_id=file.folder_id,
  3000. folder_name=folder_name,
  3001. project_id=file.project_id,
  3002. project_name=project_name,
  3003. filename=file.filename,
  3004. file_path=file.file_path,
  3005. file_type=file.file_type,
  3006. file_size=file.file_size,
  3007. file_hash=file.file_hash,
  3008. thumbnail_path=file.thumbnail_path,
  3009. metadata=file.file_metadata,
  3010. print_count=file.print_count,
  3011. last_printed_at=file.last_printed_at,
  3012. notes=file.notes,
  3013. duplicates=duplicates if duplicates else None,
  3014. duplicate_count=duplicate_count,
  3015. created_by_id=file.created_by_id,
  3016. created_by_username=file.created_by.username if file.created_by else None,
  3017. created_at=file.created_at,
  3018. updated_at=file.updated_at,
  3019. print_name=print_name,
  3020. print_time_seconds=print_time,
  3021. filament_used_grams=filament_grams,
  3022. sliced_for_model=sliced_for_model,
  3023. )
  3024. @router.put("/files/{file_id}", response_model=FileResponseSchema)
  3025. async def update_file(
  3026. file_id: int,
  3027. data: FileUpdate,
  3028. db: AsyncSession = Depends(get_db),
  3029. auth_result: tuple[User | None, bool] = Depends(
  3030. require_ownership_permission(
  3031. Permission.LIBRARY_UPDATE_ALL,
  3032. Permission.LIBRARY_UPDATE_OWN,
  3033. )
  3034. ),
  3035. ):
  3036. """Update a file's metadata."""
  3037. user, can_modify_all = auth_result
  3038. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3039. file = result.scalar_one_or_none()
  3040. if not file:
  3041. raise HTTPException(status_code=404, detail="File not found")
  3042. # Ownership check
  3043. if not can_modify_all:
  3044. if file.created_by_id != user.id:
  3045. raise HTTPException(status_code=403, detail="You can only update your own files")
  3046. if data.filename is not None:
  3047. # Validate filename doesn't contain path separators
  3048. if "/" in data.filename or "\\" in data.filename:
  3049. raise HTTPException(status_code=400, detail="Filename cannot contain path separators")
  3050. file.filename = data.filename
  3051. # Also update print_name in file_metadata so the display name matches
  3052. if file.file_metadata and "print_name" in file.file_metadata:
  3053. file.file_metadata = {**file.file_metadata, "print_name": data.filename}
  3054. if data.folder_id is not None:
  3055. if data.folder_id == 0:
  3056. file.folder_id = None
  3057. else:
  3058. # Verify folder exists
  3059. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.folder_id))
  3060. if not folder_result.scalar_one_or_none():
  3061. raise HTTPException(status_code=404, detail="Folder not found")
  3062. file.folder_id = data.folder_id
  3063. if data.project_id is not None:
  3064. if data.project_id == 0:
  3065. file.project_id = None
  3066. else:
  3067. # Verify project exists
  3068. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  3069. if not project_result.scalar_one_or_none():
  3070. raise HTTPException(status_code=404, detail="Project not found")
  3071. file.project_id = data.project_id
  3072. if data.notes is not None:
  3073. file.notes = data.notes if data.notes else None
  3074. await db.commit()
  3075. await db.refresh(file)
  3076. # Return full response (reuse get_file logic)
  3077. return await get_file(file_id, db)
  3078. @router.delete("/files/{file_id}")
  3079. async def delete_file(
  3080. file_id: int,
  3081. db: AsyncSession = Depends(get_db),
  3082. auth_result: tuple[User | None, bool] = Depends(
  3083. require_ownership_permission(
  3084. Permission.LIBRARY_DELETE_ALL,
  3085. Permission.LIBRARY_DELETE_OWN,
  3086. )
  3087. ),
  3088. ):
  3089. """Move a file to the trash (soft-delete).
  3090. The file's bytes and thumbnail stay on disk until the trash sweeper
  3091. hard-deletes the row after the retention window (see #1008). External
  3092. files skip the trash entirely — they can't be restored from disk and the
  3093. underlying file is outside Bambuddy's control, so we just drop the DB
  3094. record and thumbnail.
  3095. """
  3096. user, can_modify_all = auth_result
  3097. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3098. file = result.scalar_one_or_none()
  3099. if not file:
  3100. raise HTTPException(status_code=404, detail="File not found")
  3101. # Ownership check
  3102. if not can_modify_all:
  3103. if file.created_by_id != user.id:
  3104. raise HTTPException(status_code=403, detail="You can only delete your own files")
  3105. if file.is_external:
  3106. # External files bypass the trash — just drop the DB row + our thumbnail.
  3107. abs_thumb_path = to_absolute_path(file.thumbnail_path)
  3108. if abs_thumb_path and abs_thumb_path.exists():
  3109. try:
  3110. abs_thumb_path.unlink()
  3111. except OSError as e:
  3112. logger.warning("Failed to delete thumbnail from disk: %s", e)
  3113. await db.delete(file)
  3114. await db.commit()
  3115. return {"status": "success", "message": "File deleted", "trashed": False}
  3116. # Managed file: soft-delete. Sweeper removes bytes + thumbnail after retention.
  3117. file.deleted_at = datetime.now(timezone.utc)
  3118. await db.commit()
  3119. return {"status": "success", "message": "File moved to trash", "trashed": True}
  3120. # ============ File Content Endpoints ============
  3121. @router.get("/files/{file_id}/download")
  3122. async def download_file(
  3123. file_id: int,
  3124. db: AsyncSession = Depends(get_db),
  3125. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  3126. ):
  3127. """Download a file."""
  3128. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3129. file = result.scalar_one_or_none()
  3130. if not file:
  3131. raise HTTPException(status_code=404, detail="File not found")
  3132. abs_path = to_absolute_path(file.file_path)
  3133. if not abs_path or not abs_path.exists():
  3134. raise HTTPException(status_code=404, detail="File not found on disk")
  3135. return FastAPIFileResponse(
  3136. str(abs_path),
  3137. filename=file.filename,
  3138. media_type="application/octet-stream",
  3139. )
  3140. @router.post("/files/{file_id}/slicer-token")
  3141. async def create_library_slicer_token(
  3142. file_id: int,
  3143. db: AsyncSession = Depends(get_db),
  3144. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  3145. ):
  3146. """Create a short-lived download token for opening files in slicer applications.
  3147. Slicer protocol handlers (bambustudioopen://, orcaslicer://) cannot send
  3148. auth headers, so they use this token in the URL path instead.
  3149. """
  3150. from backend.app.core.auth import create_slicer_download_token
  3151. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3152. file = result.scalar_one_or_none()
  3153. if not file:
  3154. raise HTTPException(status_code=404, detail="File not found")
  3155. token = await create_slicer_download_token("library", file_id)
  3156. return {"token": token}
  3157. @router.get("/files/{file_id}/dl/{token}/{filename}")
  3158. async def download_library_file_for_slicer(
  3159. file_id: int,
  3160. token: str,
  3161. filename: str,
  3162. db: AsyncSession = Depends(get_db),
  3163. ):
  3164. """Download a library file using a slicer download token.
  3165. Token-authenticated (no auth headers needed). The token is short-lived
  3166. and single-use, created by POST /files/{file_id}/slicer-token.
  3167. Filename is at the end of the URL so slicers can detect the file format.
  3168. """
  3169. from backend.app.core.auth import verify_slicer_download_token
  3170. if not await verify_slicer_download_token(token, "library", file_id):
  3171. raise HTTPException(status_code=403, detail="Invalid or expired download token")
  3172. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3173. file = result.scalar_one_or_none()
  3174. if not file:
  3175. raise HTTPException(status_code=404, detail="File not found")
  3176. abs_path = to_absolute_path(file.file_path)
  3177. if not abs_path or not abs_path.exists():
  3178. raise HTTPException(status_code=404, detail="File not found on disk")
  3179. return FastAPIFileResponse(
  3180. str(abs_path),
  3181. filename=file.filename,
  3182. media_type="application/octet-stream",
  3183. )
  3184. @router.get("/files/{file_id}/thumbnail")
  3185. async def get_thumbnail(
  3186. file_id: int,
  3187. db: AsyncSession = Depends(get_db),
  3188. _: None = RequireCameraStreamTokenIfAuthEnabled,
  3189. ):
  3190. """Get a file's thumbnail."""
  3191. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3192. file = result.scalar_one_or_none()
  3193. if not file:
  3194. raise HTTPException(status_code=404, detail="File not found")
  3195. abs_thumb_path = to_absolute_path(file.thumbnail_path)
  3196. if not abs_thumb_path or not abs_thumb_path.exists():
  3197. raise HTTPException(status_code=404, detail="Thumbnail not found")
  3198. # Detect media type from extension
  3199. thumb_ext = abs_thumb_path.suffix.lower()
  3200. media_types = {
  3201. ".png": "image/png",
  3202. ".jpg": "image/jpeg",
  3203. ".jpeg": "image/jpeg",
  3204. ".gif": "image/gif",
  3205. ".webp": "image/webp",
  3206. }
  3207. media_type = media_types.get(thumb_ext, "image/png")
  3208. return FastAPIFileResponse(str(abs_thumb_path), media_type=media_type)
  3209. @router.get("/files/{file_id}/gcode")
  3210. async def get_gcode(
  3211. file_id: int,
  3212. db: AsyncSession = Depends(get_db),
  3213. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  3214. ):
  3215. """Get gcode for a file (for preview)."""
  3216. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3217. file = result.scalar_one_or_none()
  3218. if not file:
  3219. raise HTTPException(status_code=404, detail="File not found")
  3220. abs_path = to_absolute_path(file.file_path)
  3221. if not abs_path or not abs_path.exists():
  3222. raise HTTPException(status_code=404, detail="File not found on disk")
  3223. if file.file_type == "gcode":
  3224. return FastAPIFileResponse(str(abs_path), media_type="text/plain")
  3225. elif file.file_type == "3mf":
  3226. # Extract gcode from 3mf
  3227. try:
  3228. with zipfile.ZipFile(str(abs_path), "r") as zf:
  3229. # Find gcode file
  3230. gcode_files = [n for n in zf.namelist() if n.endswith(".gcode")]
  3231. if not gcode_files:
  3232. raise HTTPException(status_code=404, detail="No gcode found in 3MF file")
  3233. gcode_content = zf.read(gcode_files[0])
  3234. from fastapi.responses import Response
  3235. return Response(content=gcode_content, media_type="text/plain")
  3236. except zipfile.BadZipFile:
  3237. raise HTTPException(status_code=400, detail="Invalid 3MF file")
  3238. else:
  3239. raise HTTPException(status_code=400, detail="Unsupported file type")
  3240. # ============ Bulk Operations ============
  3241. @router.post("/files/move")
  3242. async def move_files(
  3243. data: FileMoveRequest,
  3244. db: AsyncSession = Depends(get_db),
  3245. auth_result: tuple[User | None, bool] = Depends(
  3246. require_ownership_permission(
  3247. Permission.LIBRARY_UPDATE_ALL,
  3248. Permission.LIBRARY_UPDATE_OWN,
  3249. )
  3250. ),
  3251. ):
  3252. """Move multiple files to a folder.
  3253. Cross-boundary moves (managed ↔ external, or external ↔ external)
  3254. physically relocate the bytes — see ``_move_file_bytes``. Same-boundary
  3255. moves stay DB-only because the file's on-disk location doesn't depend
  3256. on which managed folder owns it.
  3257. Files not owned by the user are skipped (unless user has ``*_all``
  3258. permission). Each skip carries a structured reason so the UI can
  3259. surface "5 of 10 files were skipped: 3 had filename collisions on
  3260. the NAS, 2 are no longer on disk" rather than a blank "skipped: 5".
  3261. """
  3262. user, can_modify_all = auth_result
  3263. # Verify folder exists if specified
  3264. target_folder: LibraryFolder | None = None
  3265. if data.folder_id is not None:
  3266. folder_result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == data.folder_id))
  3267. target_folder = folder_result.scalar_one_or_none()
  3268. if not target_folder:
  3269. raise HTTPException(status_code=404, detail="Folder not found")
  3270. if target_folder.is_external and target_folder.external_readonly:
  3271. raise HTTPException(status_code=403, detail="Cannot move files to a read-only external folder")
  3272. target_is_external = target_folder is not None and target_folder.is_external
  3273. moved = 0
  3274. skipped = 0
  3275. skipped_reasons: list[dict] = []
  3276. for file_id in data.file_ids:
  3277. result = await db.execute(
  3278. LibraryFile.active().options(selectinload(LibraryFile.folder)).where(LibraryFile.id == file_id)
  3279. )
  3280. file = result.scalar_one_or_none()
  3281. if not file:
  3282. continue
  3283. # Ownership check
  3284. if not can_modify_all and file.created_by_id != user.id:
  3285. skipped += 1
  3286. skipped_reasons.append({"file_id": file_id, "code": "not_owner", "reason": "not the file owner"})
  3287. continue
  3288. # No bytes need to move when both ends are managed (same-boundary).
  3289. if not file.is_external and not target_is_external:
  3290. file.folder_id = data.folder_id
  3291. moved += 1
  3292. continue
  3293. # Block moves out of a read-only external mount. The user only has
  3294. # read access to the source, and a move is semantically a delete on
  3295. # the source — which a read-only mount can't fulfil. Without this
  3296. # guard we'd succeed at copying to the target, fail to unlink the
  3297. # source, and the same file would now exist in two places (with
  3298. # the DB pointing at only one).
  3299. if file.is_external and file.folder is not None and file.folder.external_readonly:
  3300. skipped += 1
  3301. skipped_reasons.append(
  3302. {"file_id": file_id, "code": "source_readonly", "reason": "source is on a read-only external folder"}
  3303. )
  3304. continue
  3305. # Otherwise relocate the bytes, then update the DB row to match.
  3306. try:
  3307. new_file_path = _move_file_bytes(file, target_folder)
  3308. except _MoveSkip as e:
  3309. skipped += 1
  3310. skipped_reasons.append({"file_id": file_id, "code": e.code, "reason": e.reason})
  3311. continue
  3312. file.is_external = target_is_external
  3313. file.folder_id = data.folder_id
  3314. file.file_path = new_file_path
  3315. # External rows historically carry `file_hash=None` (scan skips
  3316. # hashing). When pulling an external file into managed storage,
  3317. # compute the hash so dedup detection works for future uploads
  3318. # of the same content.
  3319. if not target_is_external and file.file_hash is None:
  3320. try:
  3321. abs_path = to_absolute_path(new_file_path)
  3322. if abs_path:
  3323. file.file_hash = calculate_file_hash(abs_path)
  3324. except OSError:
  3325. pass # leave hash null; dedup just won't match this row
  3326. moved += 1
  3327. await db.commit()
  3328. return {
  3329. "status": "success",
  3330. "moved": moved,
  3331. "skipped": skipped,
  3332. "skipped_reasons": skipped_reasons,
  3333. }
  3334. @router.post("/bulk-delete", response_model=BulkDeleteResponse)
  3335. async def bulk_delete(
  3336. data: BulkDeleteRequest,
  3337. db: AsyncSession = Depends(get_db),
  3338. auth_result: tuple[User | None, bool] = Depends(
  3339. require_ownership_permission(
  3340. Permission.LIBRARY_DELETE_ALL,
  3341. Permission.LIBRARY_DELETE_OWN,
  3342. )
  3343. ),
  3344. ):
  3345. """Delete multiple files and/or folders.
  3346. Files not owned by the user are skipped (unless user has *_all permission).
  3347. """
  3348. user, can_modify_all = auth_result
  3349. deleted_files = 0
  3350. deleted_folders = 0
  3351. skipped_files = 0
  3352. # Delete files first. Managed files go to trash (sweeper hard-deletes bytes
  3353. # later); external files bypass trash since their disk state is outside our
  3354. # control and can't be restored from trash anyway.
  3355. now = datetime.now(timezone.utc)
  3356. for file_id in data.file_ids:
  3357. result = await db.execute(LibraryFile.active().where(LibraryFile.id == file_id))
  3358. file = result.scalar_one_or_none()
  3359. if not file:
  3360. continue
  3361. if not can_modify_all and file.created_by_id != user.id:
  3362. skipped_files += 1
  3363. continue
  3364. if file.is_external:
  3365. abs_thumb_path = to_absolute_path(file.thumbnail_path)
  3366. if abs_thumb_path and abs_thumb_path.exists():
  3367. try:
  3368. abs_thumb_path.unlink()
  3369. except OSError as e:
  3370. logger.warning("Failed to delete thumbnail from disk: %s", e)
  3371. await db.delete(file)
  3372. else:
  3373. file.deleted_at = now
  3374. deleted_files += 1
  3375. # Delete folders (cascade will handle contents)
  3376. # Note: Folders don't have ownership tracking currently, require *_all permission
  3377. for folder_id in data.folder_ids:
  3378. if not can_modify_all:
  3379. # Users without *_all permission cannot delete folders
  3380. continue
  3381. result = await db.execute(select(LibraryFolder).where(LibraryFolder.id == folder_id))
  3382. folder = result.scalar_one_or_none()
  3383. if folder:
  3384. # Count files that will be deleted
  3385. file_count_result = await db.execute(
  3386. select(func.count(LibraryFile.id)).where(
  3387. LibraryFile.folder_id == folder_id,
  3388. LibraryFile.deleted_at.is_(None),
  3389. )
  3390. )
  3391. deleted_files += file_count_result.scalar() or 0
  3392. await db.delete(folder)
  3393. deleted_folders += 1
  3394. await db.commit()
  3395. return BulkDeleteResponse(deleted_files=deleted_files, deleted_folders=deleted_folders)
  3396. # ============ Stats Endpoint ============
  3397. @router.get("/stats")
  3398. async def get_library_stats(
  3399. db: AsyncSession = Depends(get_db),
  3400. _: User | None = Depends(require_permission_if_auth_enabled(Permission.LIBRARY_READ)),
  3401. ):
  3402. """Get library statistics."""
  3403. # Stats exclude trashed files — users see counts/sizes for what's actually in the library.
  3404. active_only = LibraryFile.deleted_at.is_(None)
  3405. # Total files
  3406. total_files_result = await db.execute(select(func.count(LibraryFile.id)).where(active_only))
  3407. total_files = total_files_result.scalar() or 0
  3408. # Total folders
  3409. total_folders_result = await db.execute(select(func.count(LibraryFolder.id)))
  3410. total_folders = total_folders_result.scalar() or 0
  3411. # Total size
  3412. total_size_result = await db.execute(select(func.sum(LibraryFile.file_size)).where(active_only))
  3413. total_size = total_size_result.scalar() or 0
  3414. # Files by type
  3415. type_result = await db.execute(
  3416. select(LibraryFile.file_type, func.count(LibraryFile.id)).where(active_only).group_by(LibraryFile.file_type)
  3417. )
  3418. files_by_type = dict(type_result.all())
  3419. # Total prints
  3420. total_prints_result = await db.execute(select(func.sum(LibraryFile.print_count)).where(active_only))
  3421. total_prints = total_prints_result.scalar() or 0
  3422. # Disk space info
  3423. library_dir = get_library_dir()
  3424. try:
  3425. disk_stat = shutil.disk_usage(library_dir)
  3426. disk_free_bytes = disk_stat.free
  3427. disk_total_bytes = disk_stat.total
  3428. disk_used_bytes = disk_stat.used
  3429. except OSError:
  3430. disk_free_bytes = 0
  3431. disk_total_bytes = 0
  3432. disk_used_bytes = 0
  3433. return {
  3434. "total_files": total_files,
  3435. "total_folders": total_folders,
  3436. "total_size_bytes": total_size,
  3437. "files_by_type": files_by_type,
  3438. "total_prints": total_prints,
  3439. "disk_free_bytes": disk_free_bytes,
  3440. "disk_total_bytes": disk_total_bytes,
  3441. "disk_used_bytes": disk_used_bytes,
  3442. }