archives.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. from pathlib import Path
  2. import zipfile
  3. import io
  4. from fastapi import APIRouter, Depends, HTTPException, UploadFile, File, Request
  5. from fastapi.responses import FileResponse, Response
  6. from sqlalchemy.ext.asyncio import AsyncSession
  7. from sqlalchemy import select, func
  8. from backend.app.core.config import settings
  9. from backend.app.core.database import get_db
  10. from backend.app.models.archive import PrintArchive
  11. from backend.app.schemas.archive import ArchiveResponse, ArchiveUpdate, ArchiveStats
  12. from backend.app.services.archive import ArchiveService
  13. router = APIRouter(prefix="/archives", tags=["archives"])
  14. @router.get("/", response_model=list[ArchiveResponse])
  15. async def list_archives(
  16. printer_id: int | None = None,
  17. limit: int = 50,
  18. offset: int = 0,
  19. db: AsyncSession = Depends(get_db),
  20. ):
  21. """List archived prints."""
  22. service = ArchiveService(db)
  23. return await service.list_archives(
  24. printer_id=printer_id,
  25. limit=limit,
  26. offset=offset,
  27. )
  28. @router.get("/stats", response_model=ArchiveStats)
  29. async def get_archive_stats(db: AsyncSession = Depends(get_db)):
  30. """Get statistics across all archives."""
  31. # Total counts
  32. total_result = await db.execute(select(func.count(PrintArchive.id)))
  33. total_prints = total_result.scalar() or 0
  34. successful_result = await db.execute(
  35. select(func.count(PrintArchive.id)).where(PrintArchive.status == "completed")
  36. )
  37. successful_prints = successful_result.scalar() or 0
  38. failed_result = await db.execute(
  39. select(func.count(PrintArchive.id)).where(PrintArchive.status == "failed")
  40. )
  41. failed_prints = failed_result.scalar() or 0
  42. # Totals
  43. time_result = await db.execute(
  44. select(func.sum(PrintArchive.print_time_seconds))
  45. )
  46. total_time = (time_result.scalar() or 0) / 3600 # Convert to hours
  47. filament_result = await db.execute(
  48. select(func.sum(PrintArchive.filament_used_grams))
  49. )
  50. total_filament = filament_result.scalar() or 0
  51. cost_result = await db.execute(
  52. select(func.sum(PrintArchive.cost))
  53. )
  54. total_cost = cost_result.scalar() or 0
  55. # By filament type (split comma-separated values for multi-material prints)
  56. filament_type_result = await db.execute(
  57. select(PrintArchive.filament_type)
  58. .where(PrintArchive.filament_type.isnot(None))
  59. )
  60. prints_by_filament: dict[str, int] = {}
  61. for (filament_types,) in filament_type_result.all():
  62. # Split by comma and count each type
  63. for ftype in filament_types.split(","):
  64. ftype = ftype.strip()
  65. if ftype:
  66. prints_by_filament[ftype] = prints_by_filament.get(ftype, 0) + 1
  67. # By printer
  68. printer_result = await db.execute(
  69. select(PrintArchive.printer_id, func.count(PrintArchive.id))
  70. .group_by(PrintArchive.printer_id)
  71. )
  72. prints_by_printer = {str(k): v for k, v in printer_result.all()}
  73. return ArchiveStats(
  74. total_prints=total_prints,
  75. successful_prints=successful_prints,
  76. failed_prints=failed_prints,
  77. total_print_time_hours=round(total_time, 1),
  78. total_filament_grams=round(total_filament, 1),
  79. total_cost=round(total_cost, 2),
  80. prints_by_filament_type=prints_by_filament,
  81. prints_by_printer=prints_by_printer,
  82. )
  83. @router.get("/{archive_id}", response_model=ArchiveResponse)
  84. async def get_archive(archive_id: int, db: AsyncSession = Depends(get_db)):
  85. """Get a specific archive."""
  86. service = ArchiveService(db)
  87. archive = await service.get_archive(archive_id)
  88. if not archive:
  89. raise HTTPException(404, "Archive not found")
  90. return archive
  91. @router.patch("/{archive_id}", response_model=ArchiveResponse)
  92. async def update_archive(
  93. archive_id: int,
  94. update_data: ArchiveUpdate,
  95. db: AsyncSession = Depends(get_db),
  96. ):
  97. """Update archive metadata (tags, notes, cost, is_favorite)."""
  98. result = await db.execute(
  99. select(PrintArchive).where(PrintArchive.id == archive_id)
  100. )
  101. archive = result.scalar_one_or_none()
  102. if not archive:
  103. raise HTTPException(404, "Archive not found")
  104. for field, value in update_data.model_dump(exclude_unset=True).items():
  105. setattr(archive, field, value)
  106. await db.commit()
  107. await db.refresh(archive)
  108. return archive
  109. @router.post("/{archive_id}/favorite", response_model=ArchiveResponse)
  110. async def toggle_favorite(
  111. archive_id: int,
  112. db: AsyncSession = Depends(get_db),
  113. ):
  114. """Toggle favorite status for an archive."""
  115. result = await db.execute(
  116. select(PrintArchive).where(PrintArchive.id == archive_id)
  117. )
  118. archive = result.scalar_one_or_none()
  119. if not archive:
  120. raise HTTPException(404, "Archive not found")
  121. archive.is_favorite = not archive.is_favorite
  122. await db.commit()
  123. await db.refresh(archive)
  124. return archive
  125. @router.post("/{archive_id}/rescan", response_model=ArchiveResponse)
  126. async def rescan_archive(archive_id: int, db: AsyncSession = Depends(get_db)):
  127. """Rescan the 3MF file and update metadata."""
  128. from backend.app.services.archive import ThreeMFParser
  129. result = await db.execute(
  130. select(PrintArchive).where(PrintArchive.id == archive_id)
  131. )
  132. archive = result.scalar_one_or_none()
  133. if not archive:
  134. raise HTTPException(404, "Archive not found")
  135. file_path = settings.base_dir / archive.file_path
  136. if not file_path.exists():
  137. raise HTTPException(404, "Archive file not found")
  138. # Parse the 3MF file
  139. parser = ThreeMFParser(file_path)
  140. metadata = parser.parse()
  141. # Update fields from metadata
  142. if metadata.get("filament_type"):
  143. archive.filament_type = metadata["filament_type"]
  144. if metadata.get("filament_color"):
  145. archive.filament_color = metadata["filament_color"]
  146. if metadata.get("print_time_seconds"):
  147. archive.print_time_seconds = metadata["print_time_seconds"]
  148. if metadata.get("filament_used_grams"):
  149. archive.filament_used_grams = metadata["filament_used_grams"]
  150. if metadata.get("layer_height"):
  151. archive.layer_height = metadata["layer_height"]
  152. if metadata.get("nozzle_diameter"):
  153. archive.nozzle_diameter = metadata["nozzle_diameter"]
  154. if metadata.get("bed_temperature"):
  155. archive.bed_temperature = metadata["bed_temperature"]
  156. if metadata.get("nozzle_temperature"):
  157. archive.nozzle_temperature = metadata["nozzle_temperature"]
  158. if metadata.get("makerworld_url"):
  159. archive.makerworld_url = metadata["makerworld_url"]
  160. if metadata.get("designer"):
  161. archive.designer = metadata["designer"]
  162. await db.commit()
  163. await db.refresh(archive)
  164. return archive
  165. @router.post("/rescan-all")
  166. async def rescan_all_archives(db: AsyncSession = Depends(get_db)):
  167. """Rescan all archives and update their metadata."""
  168. from backend.app.services.archive import ThreeMFParser
  169. result = await db.execute(select(PrintArchive))
  170. archives = list(result.scalars().all())
  171. updated = 0
  172. errors = []
  173. for archive in archives:
  174. try:
  175. file_path = settings.base_dir / archive.file_path
  176. if not file_path.exists():
  177. errors.append({"id": archive.id, "error": "File not found"})
  178. continue
  179. parser = ThreeMFParser(file_path)
  180. metadata = parser.parse()
  181. if metadata.get("filament_type"):
  182. archive.filament_type = metadata["filament_type"]
  183. if metadata.get("filament_color"):
  184. archive.filament_color = metadata["filament_color"]
  185. if metadata.get("print_time_seconds"):
  186. archive.print_time_seconds = metadata["print_time_seconds"]
  187. if metadata.get("filament_used_grams"):
  188. archive.filament_used_grams = metadata["filament_used_grams"]
  189. if metadata.get("layer_height"):
  190. archive.layer_height = metadata["layer_height"]
  191. if metadata.get("nozzle_diameter"):
  192. archive.nozzle_diameter = metadata["nozzle_diameter"]
  193. if metadata.get("makerworld_url"):
  194. archive.makerworld_url = metadata["makerworld_url"]
  195. if metadata.get("designer"):
  196. archive.designer = metadata["designer"]
  197. updated += 1
  198. except Exception as e:
  199. errors.append({"id": archive.id, "error": str(e)})
  200. await db.commit()
  201. return {"updated": updated, "errors": errors}
  202. @router.delete("/{archive_id}")
  203. async def delete_archive(archive_id: int, db: AsyncSession = Depends(get_db)):
  204. """Delete an archive."""
  205. service = ArchiveService(db)
  206. if not await service.delete_archive(archive_id):
  207. raise HTTPException(404, "Archive not found")
  208. return {"status": "deleted"}
  209. @router.get("/{archive_id}/download")
  210. async def download_archive(
  211. archive_id: int,
  212. inline: bool = False,
  213. db: AsyncSession = Depends(get_db),
  214. ):
  215. """Download the 3MF file."""
  216. service = ArchiveService(db)
  217. archive = await service.get_archive(archive_id)
  218. if not archive:
  219. raise HTTPException(404, "Archive not found")
  220. file_path = settings.base_dir / archive.file_path
  221. if not file_path.exists():
  222. raise HTTPException(404, "File not found")
  223. # Use inline disposition to let browser/OS handle file association
  224. content_disposition = "inline" if inline else "attachment"
  225. return FileResponse(
  226. path=file_path,
  227. filename=archive.filename,
  228. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  229. content_disposition_type=content_disposition,
  230. )
  231. @router.get("/{archive_id}/file/{filename}")
  232. async def download_archive_with_filename(
  233. archive_id: int,
  234. filename: str,
  235. db: AsyncSession = Depends(get_db),
  236. ):
  237. """Download the 3MF file with filename in URL (for Bambu Studio protocol)."""
  238. service = ArchiveService(db)
  239. archive = await service.get_archive(archive_id)
  240. if not archive:
  241. raise HTTPException(404, "Archive not found")
  242. file_path = settings.base_dir / archive.file_path
  243. if not file_path.exists():
  244. raise HTTPException(404, "File not found")
  245. return FileResponse(
  246. path=file_path,
  247. filename=archive.filename,
  248. media_type="application/vnd.ms-package.3dmanufacturing-3dmodel+xml",
  249. )
  250. @router.get("/{archive_id}/thumbnail")
  251. async def get_thumbnail(archive_id: int, db: AsyncSession = Depends(get_db)):
  252. """Get the thumbnail image."""
  253. service = ArchiveService(db)
  254. archive = await service.get_archive(archive_id)
  255. if not archive or not archive.thumbnail_path:
  256. raise HTTPException(404, "Thumbnail not found")
  257. thumb_path = settings.base_dir / archive.thumbnail_path
  258. if not thumb_path.exists():
  259. raise HTTPException(404, "Thumbnail file not found")
  260. return FileResponse(path=thumb_path, media_type="image/png")
  261. @router.get("/{archive_id}/timelapse")
  262. async def get_timelapse(archive_id: int, db: AsyncSession = Depends(get_db)):
  263. """Get the timelapse video."""
  264. service = ArchiveService(db)
  265. archive = await service.get_archive(archive_id)
  266. if not archive or not archive.timelapse_path:
  267. raise HTTPException(404, "Timelapse not found")
  268. timelapse_path = settings.base_dir / archive.timelapse_path
  269. if not timelapse_path.exists():
  270. raise HTTPException(404, "Timelapse file not found")
  271. return FileResponse(
  272. path=timelapse_path,
  273. media_type="video/mp4",
  274. filename=f"{archive.print_name or 'timelapse'}.mp4",
  275. )
  276. @router.post("/{archive_id}/timelapse/scan")
  277. async def scan_timelapse(
  278. archive_id: int,
  279. db: AsyncSession = Depends(get_db),
  280. ):
  281. """Scan printer for timelapse matching this archive and attach it."""
  282. from backend.app.models.printer import Printer
  283. from backend.app.services.bambu_ftp import list_files_async, download_file_bytes_async
  284. service = ArchiveService(db)
  285. archive = await service.get_archive(archive_id)
  286. if not archive:
  287. raise HTTPException(404, "Archive not found")
  288. if archive.timelapse_path:
  289. return {"status": "exists", "message": "Timelapse already attached"}
  290. if not archive.printer_id:
  291. raise HTTPException(400, "Archive has no associated printer")
  292. # Get printer
  293. result = await db.execute(select(Printer).where(Printer.id == archive.printer_id))
  294. printer = result.scalar_one_or_none()
  295. if not printer:
  296. raise HTTPException(404, "Printer not found")
  297. # Get base name from archive filename (without .3mf extension)
  298. base_name = Path(archive.filename).stem
  299. # Scan timelapse directory on printer
  300. try:
  301. files = await list_files_async(printer.ip_address, printer.access_code, "/timelapse/video")
  302. except Exception:
  303. raise HTTPException(500, "Failed to connect to printer")
  304. # Look for matching timelapse
  305. matching_file = None
  306. mp4_files = [f for f in files if not f.get("is_directory") and f.get("name", "").endswith(".mp4")]
  307. # Strategy 1: Match by print name in filename
  308. for f in mp4_files:
  309. fname = f.get("name", "")
  310. if base_name.lower() in fname.lower():
  311. matching_file = f
  312. break
  313. # Strategy 2: Match by timestamp proximity
  314. if not matching_file and (archive.started_at or archive.completed_at or archive.created_at):
  315. import re
  316. from datetime import datetime, timedelta
  317. archive_time = archive.started_at or archive.completed_at or archive.created_at
  318. best_match = None
  319. best_diff = timedelta(hours=24) # Max 24 hour difference
  320. for f in mp4_files:
  321. fname = f.get("name", "")
  322. # Parse timestamp from filename like "video_2025-11-24_03-17-40.mp4"
  323. match = re.search(r'(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})', fname)
  324. if match:
  325. try:
  326. file_time = datetime.strptime(match.group(1), "%Y-%m-%d_%H-%M-%S")
  327. # Timelapse is usually created at print end, so compare to completed_at or created_at
  328. compare_time = archive.completed_at or archive.created_at
  329. if compare_time:
  330. diff = abs(file_time - compare_time)
  331. if diff < best_diff:
  332. best_diff = diff
  333. best_match = f
  334. except ValueError:
  335. continue
  336. if best_match and best_diff < timedelta(hours=2): # Within 2 hours
  337. matching_file = best_match
  338. if not matching_file:
  339. return {"status": "not_found", "message": "No matching timelapse found on printer"}
  340. # Download the timelapse
  341. remote_path = f"/timelapse/video/{matching_file['name']}"
  342. timelapse_data = await download_file_bytes_async(
  343. printer.ip_address, printer.access_code, remote_path
  344. )
  345. if not timelapse_data:
  346. raise HTTPException(500, "Failed to download timelapse")
  347. # Attach timelapse to archive
  348. success = await service.attach_timelapse(
  349. archive_id, timelapse_data, matching_file["name"]
  350. )
  351. if not success:
  352. raise HTTPException(500, "Failed to attach timelapse")
  353. return {
  354. "status": "attached",
  355. "message": f"Timelapse '{matching_file['name']}' attached successfully",
  356. "filename": matching_file["name"],
  357. }
  358. @router.post("/{archive_id}/timelapse/upload")
  359. async def upload_timelapse(
  360. archive_id: int,
  361. file: UploadFile = File(...),
  362. db: AsyncSession = Depends(get_db),
  363. ):
  364. """Manually upload a timelapse video to an archive."""
  365. service = ArchiveService(db)
  366. archive = await service.get_archive(archive_id)
  367. if not archive:
  368. raise HTTPException(404, "Archive not found")
  369. if not file.filename or not file.filename.endswith((".mp4", ".avi", ".mkv")):
  370. raise HTTPException(400, "File must be a video file (.mp4, .avi, .mkv)")
  371. content = await file.read()
  372. success = await service.attach_timelapse(archive_id, content, file.filename)
  373. if not success:
  374. raise HTTPException(500, "Failed to attach timelapse")
  375. return {"status": "attached", "filename": file.filename}
  376. # ============================================
  377. # Photo Endpoints
  378. # ============================================
  379. @router.post("/{archive_id}/photos")
  380. async def upload_photo(
  381. archive_id: int,
  382. file: UploadFile = File(...),
  383. db: AsyncSession = Depends(get_db),
  384. ):
  385. """Upload a photo of the printed result."""
  386. result = await db.execute(
  387. select(PrintArchive).where(PrintArchive.id == archive_id)
  388. )
  389. archive = result.scalar_one_or_none()
  390. if not archive:
  391. raise HTTPException(404, "Archive not found")
  392. if not file.filename or not file.filename.lower().endswith((".jpg", ".jpeg", ".png", ".webp")):
  393. raise HTTPException(400, "File must be an image (.jpg, .jpeg, .png, .webp)")
  394. # Get archive directory
  395. file_path = settings.base_dir / archive.file_path
  396. archive_dir = file_path.parent
  397. photos_dir = archive_dir / "photos"
  398. photos_dir.mkdir(exist_ok=True)
  399. # Generate unique filename
  400. import uuid
  401. ext = Path(file.filename).suffix.lower()
  402. photo_filename = f"{uuid.uuid4().hex[:8]}{ext}"
  403. photo_path = photos_dir / photo_filename
  404. # Save file
  405. content = await file.read()
  406. photo_path.write_bytes(content)
  407. # Update archive photos list (create new list to trigger SQLAlchemy change detection)
  408. photos = list(archive.photos or [])
  409. photos.append(photo_filename)
  410. archive.photos = photos
  411. await db.commit()
  412. await db.refresh(archive)
  413. return {"status": "uploaded", "filename": photo_filename, "photos": archive.photos}
  414. @router.get("/{archive_id}/photos/{filename}")
  415. async def get_photo(
  416. archive_id: int,
  417. filename: str,
  418. db: AsyncSession = Depends(get_db),
  419. ):
  420. """Get a specific photo."""
  421. result = await db.execute(
  422. select(PrintArchive).where(PrintArchive.id == archive_id)
  423. )
  424. archive = result.scalar_one_or_none()
  425. if not archive:
  426. raise HTTPException(404, "Archive not found")
  427. file_path = settings.base_dir / archive.file_path
  428. photo_path = file_path.parent / "photos" / filename
  429. if not photo_path.exists():
  430. raise HTTPException(404, "Photo not found")
  431. # Determine media type
  432. ext = Path(filename).suffix.lower()
  433. media_types = {
  434. ".jpg": "image/jpeg",
  435. ".jpeg": "image/jpeg",
  436. ".png": "image/png",
  437. ".webp": "image/webp",
  438. }
  439. media_type = media_types.get(ext, "image/jpeg")
  440. return FileResponse(path=photo_path, media_type=media_type)
  441. @router.delete("/{archive_id}/photos/{filename}")
  442. async def delete_photo(
  443. archive_id: int,
  444. filename: str,
  445. db: AsyncSession = Depends(get_db),
  446. ):
  447. """Delete a photo."""
  448. result = await db.execute(
  449. select(PrintArchive).where(PrintArchive.id == archive_id)
  450. )
  451. archive = result.scalar_one_or_none()
  452. if not archive:
  453. raise HTTPException(404, "Archive not found")
  454. if not archive.photos or filename not in archive.photos:
  455. raise HTTPException(404, "Photo not found")
  456. # Delete file
  457. file_path = settings.base_dir / archive.file_path
  458. photo_path = file_path.parent / "photos" / filename
  459. if photo_path.exists():
  460. photo_path.unlink()
  461. # Update archive photos list
  462. photos = [p for p in archive.photos if p != filename]
  463. archive.photos = photos if photos else None
  464. await db.commit()
  465. return {"status": "deleted", "photos": archive.photos}
  466. # ============================================
  467. # QR Code Endpoint
  468. # ============================================
  469. @router.get("/{archive_id}/qrcode")
  470. async def get_qrcode(
  471. archive_id: int,
  472. request: Request,
  473. size: int = 200,
  474. db: AsyncSession = Depends(get_db),
  475. ):
  476. """Generate a QR code that links to this archive."""
  477. import qrcode
  478. from qrcode.image.styledpil import StyledPilImage
  479. result = await db.execute(
  480. select(PrintArchive).where(PrintArchive.id == archive_id)
  481. )
  482. archive = result.scalar_one_or_none()
  483. if not archive:
  484. raise HTTPException(404, "Archive not found")
  485. # Build URL to archive detail page
  486. base_url = str(request.base_url).rstrip('/')
  487. archive_url = f"{base_url}/archives?id={archive_id}"
  488. # Generate QR code
  489. qr = qrcode.QRCode(
  490. version=1,
  491. error_correction=qrcode.constants.ERROR_CORRECT_M,
  492. box_size=10,
  493. border=2,
  494. )
  495. qr.add_data(archive_url)
  496. qr.make(fit=True)
  497. img = qr.make_image(fill_color="black", back_color="white")
  498. # Resize if needed
  499. if size != 200:
  500. img = img.resize((size, size))
  501. # Convert to bytes
  502. buffer = io.BytesIO()
  503. img.save(buffer, format="PNG")
  504. buffer.seek(0)
  505. return Response(
  506. content=buffer.getvalue(),
  507. media_type="image/png",
  508. headers={
  509. "Content-Disposition": f'inline; filename="qr_{archive.print_name or archive_id}.png"'
  510. }
  511. )
  512. @router.get("/{archive_id}/capabilities")
  513. async def get_archive_capabilities(archive_id: int, db: AsyncSession = Depends(get_db)):
  514. """Check what viewing capabilities are available for this 3MF file."""
  515. import json
  516. import re
  517. service = ArchiveService(db)
  518. archive = await service.get_archive(archive_id)
  519. if not archive:
  520. raise HTTPException(404, "Archive not found")
  521. file_path = settings.base_dir / archive.file_path
  522. if not file_path.exists():
  523. raise HTTPException(404, "File not found")
  524. has_model = False
  525. has_gcode = False
  526. build_volume = {"x": 256, "y": 256, "z": 256} # Default to X1/P1 size
  527. try:
  528. with zipfile.ZipFile(file_path, 'r') as zf:
  529. names = zf.namelist()
  530. # Check for G-code
  531. has_gcode = any(n.startswith('Metadata/') and n.endswith('.gcode') for n in names)
  532. # Check for 3D model - need to look for actual mesh data
  533. for name in names:
  534. if name.endswith('.model'):
  535. try:
  536. content = zf.read(name).decode('utf-8')
  537. # Check if this model file contains actual mesh vertices
  538. if '<vertex' in content or '<mesh' in content:
  539. has_model = True
  540. break
  541. except Exception:
  542. pass
  543. # Extract build volume from project settings
  544. if 'Metadata/project_settings.config' in names:
  545. try:
  546. config_content = zf.read('Metadata/project_settings.config').decode('utf-8')
  547. config_data = json.loads(config_content)
  548. # Parse printable_area: ['0x0', '256x0', '256x256', '0x256']
  549. printable_area = config_data.get('printable_area', [])
  550. if printable_area and len(printable_area) >= 3:
  551. # Get max X and Y from the corner coordinates
  552. max_x = 0
  553. max_y = 0
  554. for coord in printable_area:
  555. if 'x' in coord:
  556. parts = coord.split('x')
  557. if len(parts) == 2:
  558. try:
  559. x, y = int(parts[0]), int(parts[1])
  560. max_x = max(max_x, x)
  561. max_y = max(max_y, y)
  562. except ValueError:
  563. pass
  564. if max_x > 0 and max_y > 0:
  565. build_volume["x"] = max_x
  566. build_volume["y"] = max_y
  567. # Parse printable_height
  568. printable_height = config_data.get('printable_height')
  569. if printable_height:
  570. try:
  571. build_volume["z"] = int(printable_height)
  572. except (ValueError, TypeError):
  573. pass
  574. except Exception:
  575. pass
  576. except zipfile.BadZipFile:
  577. raise HTTPException(400, "Invalid 3MF file")
  578. return {
  579. "has_model": has_model,
  580. "has_gcode": has_gcode,
  581. "build_volume": build_volume,
  582. }
  583. @router.get("/{archive_id}/gcode")
  584. async def get_gcode(archive_id: int, db: AsyncSession = Depends(get_db)):
  585. """Extract and return G-code from the 3MF file."""
  586. service = ArchiveService(db)
  587. archive = await service.get_archive(archive_id)
  588. if not archive:
  589. raise HTTPException(404, "Archive not found")
  590. file_path = settings.base_dir / archive.file_path
  591. if not file_path.exists():
  592. raise HTTPException(404, "File not found")
  593. try:
  594. with zipfile.ZipFile(file_path, 'r') as zf:
  595. # Bambu 3MF files store G-code in Metadata/plate_X.gcode
  596. gcode_files = [n for n in zf.namelist() if n.startswith('Metadata/') and n.endswith('.gcode')]
  597. if not gcode_files:
  598. raise HTTPException(
  599. 404,
  600. "No G-code found. This file hasn't been sliced yet - G-code is only available after slicing in Bambu Studio."
  601. )
  602. # Get the first plate's G-code (usually plate_1.gcode)
  603. gcode_content = zf.read(gcode_files[0]).decode('utf-8')
  604. return Response(content=gcode_content, media_type="text/plain")
  605. except zipfile.BadZipFile:
  606. raise HTTPException(400, "Invalid 3MF file")
  607. except HTTPException:
  608. raise
  609. except Exception as e:
  610. raise HTTPException(500, f"Error extracting G-code: {str(e)}")
  611. @router.post("/upload")
  612. async def upload_archive(
  613. file: UploadFile = File(...),
  614. printer_id: int | None = None,
  615. db: AsyncSession = Depends(get_db),
  616. ):
  617. """Manually upload a 3MF file to archive."""
  618. if not file.filename or not file.filename.endswith(".3mf"):
  619. raise HTTPException(400, "File must be a .3mf file")
  620. # Save uploaded file temporarily
  621. temp_path = settings.archive_dir / "temp" / file.filename
  622. temp_path.parent.mkdir(parents=True, exist_ok=True)
  623. try:
  624. content = await file.read()
  625. temp_path.write_bytes(content)
  626. service = ArchiveService(db)
  627. archive = await service.archive_print(
  628. printer_id=printer_id,
  629. source_file=temp_path,
  630. )
  631. if not archive:
  632. raise HTTPException(400, "Failed to archive file")
  633. return ArchiveResponse.model_validate(archive)
  634. finally:
  635. if temp_path.exists():
  636. temp_path.unlink()
  637. @router.post("/upload-bulk")
  638. async def upload_archives_bulk(
  639. files: list[UploadFile] = File(...),
  640. printer_id: int | None = None,
  641. db: AsyncSession = Depends(get_db),
  642. ):
  643. """Bulk upload multiple 3MF files to archive."""
  644. results = []
  645. errors = []
  646. for file in files:
  647. if not file.filename or not file.filename.endswith(".3mf"):
  648. errors.append({"filename": file.filename or "unknown", "error": "Not a .3mf file"})
  649. continue
  650. temp_path = settings.archive_dir / "temp" / file.filename
  651. temp_path.parent.mkdir(parents=True, exist_ok=True)
  652. try:
  653. content = await file.read()
  654. temp_path.write_bytes(content)
  655. service = ArchiveService(db)
  656. archive = await service.archive_print(
  657. printer_id=printer_id,
  658. source_file=temp_path,
  659. )
  660. if archive:
  661. results.append({
  662. "filename": file.filename,
  663. "id": archive.id,
  664. "status": "success",
  665. })
  666. else:
  667. errors.append({"filename": file.filename, "error": "Failed to process"})
  668. except Exception as e:
  669. errors.append({"filename": file.filename, "error": str(e)})
  670. finally:
  671. if temp_path.exists():
  672. temp_path.unlink()
  673. return {
  674. "uploaded": len(results),
  675. "failed": len(errors),
  676. "results": results,
  677. "errors": errors,
  678. }
  679. @router.post("/{archive_id}/reprint")
  680. async def reprint_archive(
  681. archive_id: int,
  682. printer_id: int,
  683. db: AsyncSession = Depends(get_db),
  684. ):
  685. """Send an archived 3MF file to a printer and start printing."""
  686. from backend.app.models.printer import Printer
  687. from backend.app.services.bambu_ftp import upload_file_async
  688. from backend.app.services.printer_manager import printer_manager
  689. # Get archive
  690. service = ArchiveService(db)
  691. archive = await service.get_archive(archive_id)
  692. if not archive:
  693. raise HTTPException(404, "Archive not found")
  694. # Get printer
  695. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  696. printer = result.scalar_one_or_none()
  697. if not printer:
  698. raise HTTPException(404, "Printer not found")
  699. # Check printer is connected
  700. if not printer_manager.is_connected(printer_id):
  701. raise HTTPException(400, "Printer is not connected")
  702. # Get the 3MF file path
  703. file_path = settings.base_dir / archive.file_path
  704. if not file_path.exists():
  705. raise HTTPException(404, "Archive file not found")
  706. # Upload file to printer via FTP
  707. remote_filename = archive.filename
  708. remote_path = f"/cache/{remote_filename}"
  709. uploaded = await upload_file_async(
  710. printer.ip_address,
  711. printer.access_code,
  712. file_path,
  713. remote_path,
  714. )
  715. if not uploaded:
  716. raise HTTPException(500, "Failed to upload file to printer")
  717. # Start the print
  718. started = printer_manager.start_print(printer_id, remote_filename)
  719. if not started:
  720. raise HTTPException(500, "Failed to start print")
  721. return {
  722. "status": "printing",
  723. "printer_id": printer_id,
  724. "archive_id": archive_id,
  725. "filename": archive.filename,
  726. }