settings.py 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. import io
  2. import logging
  3. import os
  4. import zipfile
  5. from datetime import datetime
  6. from pathlib import Path
  7. from fastapi import APIRouter, Depends, File, UploadFile
  8. from fastapi.responses import FileResponse, JSONResponse
  9. from sqlalchemy import delete, select
  10. from sqlalchemy.ext.asyncio import AsyncSession
  11. from backend.app.core.auth import RequirePermissionIfAuthEnabled
  12. from backend.app.core.config import settings as app_settings
  13. from backend.app.core.database import get_db
  14. from backend.app.core.permissions import Permission
  15. from backend.app.models.settings import Settings
  16. from backend.app.models.user import User
  17. from backend.app.schemas.settings import AppSettings, AppSettingsUpdate
  18. logger = logging.getLogger(__name__)
  19. router = APIRouter(prefix="/settings", tags=["settings"])
  20. # Default settings
  21. DEFAULT_SETTINGS = AppSettings()
  22. async def get_setting(db: AsyncSession, key: str) -> str | None:
  23. """Get a single setting value by key."""
  24. result = await db.execute(select(Settings).where(Settings.key == key))
  25. setting = result.scalar_one_or_none()
  26. return setting.value if setting else None
  27. async def get_external_login_url(db: AsyncSession) -> str:
  28. """Get the external URL for the login page.
  29. Uses external_url from settings if available, otherwise falls back to APP_URL env var.
  30. Args:
  31. db: Database session
  32. Returns:
  33. Full URL to the login page
  34. """
  35. import os
  36. external_url = await get_setting(db, "external_url")
  37. if external_url:
  38. external_url = external_url.rstrip("/")
  39. else:
  40. external_url = os.environ.get("APP_URL", "http://localhost:5173")
  41. return external_url + "/login"
  42. async def set_setting(db: AsyncSession, key: str, value: str) -> None:
  43. """Set a single setting value."""
  44. from backend.app.core.db_dialect import upsert_setting
  45. await upsert_setting(db, Settings, key, value)
  46. @router.get("", response_model=AppSettings)
  47. @router.get("/", response_model=AppSettings)
  48. async def get_settings(
  49. db: AsyncSession = Depends(get_db),
  50. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  51. ):
  52. """Get all application settings."""
  53. settings_dict = DEFAULT_SETTINGS.model_dump()
  54. # Load saved settings from database
  55. result = await db.execute(select(Settings))
  56. db_settings = result.scalars().all()
  57. for setting in db_settings:
  58. if setting.key in settings_dict:
  59. # Parse the value based on the expected type
  60. if setting.key in [
  61. "auto_archive",
  62. "save_thumbnails",
  63. "capture_finish_photo",
  64. "spoolman_enabled",
  65. "spoolman_disable_weight_sync",
  66. "spoolman_report_partial_usage",
  67. "disable_filament_warnings",
  68. "prefer_lowest_filament",
  69. "check_updates",
  70. "check_printer_firmware",
  71. "include_beta_updates",
  72. "virtual_printer_enabled",
  73. "ftp_retry_enabled",
  74. "mqtt_enabled",
  75. "mqtt_use_tls",
  76. "ha_enabled",
  77. "per_printer_mapping_expanded",
  78. "prometheus_enabled",
  79. "user_notifications_enabled",
  80. "queue_drying_enabled",
  81. "queue_drying_block",
  82. "ambient_drying_enabled",
  83. "require_plate_clear",
  84. "queue_shortest_first",
  85. "default_bed_levelling",
  86. "default_flow_cali",
  87. "default_vibration_cali",
  88. "default_layer_inspect",
  89. "default_timelapse",
  90. "ldap_enabled",
  91. "ldap_auto_provision",
  92. ]:
  93. settings_dict[setting.key] = setting.value.lower() == "true"
  94. elif setting.key in [
  95. "default_filament_cost",
  96. "energy_cost_per_kwh",
  97. "ams_temp_good",
  98. "ams_temp_fair",
  99. "library_disk_warning_gb",
  100. "low_stock_threshold",
  101. ]:
  102. settings_dict[setting.key] = float(setting.value)
  103. elif setting.key in [
  104. "ams_humidity_good",
  105. "ams_humidity_fair",
  106. "ams_history_retention_days",
  107. "ftp_retry_count",
  108. "ftp_retry_delay",
  109. "ftp_timeout",
  110. "mqtt_port",
  111. "stagger_group_size",
  112. "stagger_interval_minutes",
  113. ]:
  114. settings_dict[setting.key] = int(setting.value)
  115. elif setting.key == "default_printer_id":
  116. # Handle nullable integer
  117. settings_dict[setting.key] = int(setting.value) if setting.value and setting.value != "None" else None
  118. else:
  119. settings_dict[setting.key] = setting.value
  120. # Get Home Assistant settings (with environment variable overrides)
  121. ha_settings = await get_homeassistant_settings(db)
  122. settings_dict.update(ha_settings)
  123. # Never return LDAP bind password in API responses
  124. settings_dict["ldap_bind_password"] = ""
  125. return AppSettings(**settings_dict)
  126. @router.put("/", response_model=AppSettings)
  127. async def update_settings(
  128. settings_update: AppSettingsUpdate,
  129. db: AsyncSession = Depends(get_db),
  130. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  131. ):
  132. """Update application settings."""
  133. update_data = settings_update.model_dump(exclude_unset=True)
  134. # Check if any MQTT settings are being updated
  135. mqtt_keys = {
  136. "mqtt_enabled",
  137. "mqtt_broker",
  138. "mqtt_port",
  139. "mqtt_username",
  140. "mqtt_password",
  141. "mqtt_topic_prefix",
  142. "mqtt_use_tls",
  143. }
  144. mqtt_updated = bool(mqtt_keys & set(update_data.keys()))
  145. for key, value in update_data.items():
  146. # Convert value to string for storage
  147. if isinstance(value, bool):
  148. str_value = "true" if value else "false"
  149. elif value is None:
  150. str_value = "None"
  151. else:
  152. str_value = str(value)
  153. await set_setting(db, key, str_value)
  154. await db.commit()
  155. # Expire all objects to ensure fresh reads after commit
  156. db.expire_all()
  157. # Reconfigure MQTT relay if any MQTT settings changed
  158. if mqtt_updated:
  159. try:
  160. from backend.app.services.mqtt_relay import mqtt_relay
  161. mqtt_settings = {
  162. "mqtt_enabled": (await get_setting(db, "mqtt_enabled") or "false") == "true",
  163. "mqtt_broker": await get_setting(db, "mqtt_broker") or "",
  164. "mqtt_port": int(await get_setting(db, "mqtt_port") or "1883"),
  165. "mqtt_username": await get_setting(db, "mqtt_username") or "",
  166. "mqtt_password": await get_setting(db, "mqtt_password") or "",
  167. "mqtt_topic_prefix": await get_setting(db, "mqtt_topic_prefix") or "bambuddy",
  168. "mqtt_use_tls": (await get_setting(db, "mqtt_use_tls") or "false") == "true",
  169. }
  170. await mqtt_relay.configure(mqtt_settings)
  171. except Exception:
  172. pass # Don't fail the settings update if MQTT reconfiguration fails
  173. # Return updated settings
  174. return await get_settings(db)
  175. @router.patch("/", response_model=AppSettings)
  176. @router.patch("", response_model=AppSettings)
  177. async def patch_settings(
  178. settings_update: AppSettingsUpdate,
  179. db: AsyncSession = Depends(get_db),
  180. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  181. ):
  182. """Partially update application settings (same as PUT, for REST compatibility)."""
  183. return await update_settings(settings_update, db, _)
  184. @router.post("/reset", response_model=AppSettings)
  185. async def reset_settings(
  186. db: AsyncSession = Depends(get_db),
  187. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  188. ):
  189. """Reset all settings to defaults."""
  190. # Delete all settings
  191. result = await db.execute(select(Settings))
  192. for setting in result.scalars().all():
  193. await db.delete(setting)
  194. await db.commit()
  195. return DEFAULT_SETTINGS
  196. @router.get("/default-sidebar-order")
  197. async def get_default_sidebar_order(
  198. db: AsyncSession = Depends(get_db),
  199. ):
  200. """Get the admin-set default sidebar order.
  201. Intentionally unauthenticated: non-admin users need to read this value to apply
  202. the default sidebar order, but may lack SETTINGS_READ permission.
  203. The value is non-sensitive (sidebar item IDs only).
  204. """
  205. value = await get_setting(db, "default_sidebar_order")
  206. return {"default_sidebar_order": value or ""}
  207. @router.get("/check-ffmpeg")
  208. async def check_ffmpeg():
  209. """Check if ffmpeg is installed and available."""
  210. from backend.app.services.camera import get_ffmpeg_path
  211. ffmpeg_path = get_ffmpeg_path()
  212. return {
  213. "installed": ffmpeg_path is not None,
  214. "path": ffmpeg_path,
  215. }
  216. @router.get("/spoolman")
  217. async def get_spoolman_settings(
  218. db: AsyncSession = Depends(get_db),
  219. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  220. ):
  221. """Get Spoolman integration settings."""
  222. spoolman_enabled = await get_setting(db, "spoolman_enabled") or "false"
  223. spoolman_url = await get_setting(db, "spoolman_url") or ""
  224. spoolman_sync_mode = await get_setting(db, "spoolman_sync_mode") or "auto"
  225. spoolman_disable_weight_sync = await get_setting(db, "spoolman_disable_weight_sync") or "false"
  226. spoolman_report_partial_usage = await get_setting(db, "spoolman_report_partial_usage") or "true"
  227. return {
  228. "spoolman_enabled": spoolman_enabled,
  229. "spoolman_url": spoolman_url,
  230. "spoolman_sync_mode": spoolman_sync_mode,
  231. "spoolman_disable_weight_sync": spoolman_disable_weight_sync,
  232. "spoolman_report_partial_usage": spoolman_report_partial_usage,
  233. }
  234. @router.put("/spoolman")
  235. async def update_spoolman_settings(
  236. settings: dict,
  237. db: AsyncSession = Depends(get_db),
  238. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  239. ):
  240. """Update Spoolman integration settings."""
  241. if "spoolman_enabled" in settings:
  242. old_val = await get_setting(db, "spoolman_enabled") or "false"
  243. new_val = settings["spoolman_enabled"]
  244. await set_setting(db, "spoolman_enabled", new_val)
  245. # Switching to Spoolman: clear built-in inventory slot assignments
  246. if old_val.lower() != "true" and new_val.lower() == "true":
  247. from backend.app.models.spool_assignment import SpoolAssignment
  248. result = await db.execute(delete(SpoolAssignment))
  249. logger.info("Cleared %d spool assignments on switch to Spoolman mode", result.rowcount)
  250. if "spoolman_url" in settings:
  251. await set_setting(db, "spoolman_url", settings["spoolman_url"])
  252. if "spoolman_sync_mode" in settings:
  253. await set_setting(db, "spoolman_sync_mode", settings["spoolman_sync_mode"])
  254. if "spoolman_disable_weight_sync" in settings:
  255. await set_setting(db, "spoolman_disable_weight_sync", settings["spoolman_disable_weight_sync"])
  256. if "spoolman_report_partial_usage" in settings:
  257. await set_setting(db, "spoolman_report_partial_usage", settings["spoolman_report_partial_usage"])
  258. await db.commit()
  259. db.expire_all()
  260. # Return updated settings
  261. return await get_spoolman_settings(db)
  262. async def get_homeassistant_settings(db: AsyncSession) -> dict:
  263. """
  264. Get Home Assistant integration settings.
  265. Environment variables (HA_URL, HA_TOKEN) take precedence over database settings.
  266. """
  267. import os
  268. # Check environment variables first
  269. ha_url_env = os.environ.get("HA_URL")
  270. ha_token_env = os.environ.get("HA_TOKEN")
  271. # Fall back to database values
  272. ha_url = ha_url_env or await get_setting(db, "ha_url") or ""
  273. ha_token = ha_token_env or await get_setting(db, "ha_token") or ""
  274. ha_enabled_db = await get_setting(db, "ha_enabled") or "false"
  275. # Track which settings come from environment
  276. ha_url_from_env = bool(ha_url_env)
  277. ha_token_from_env = bool(ha_token_env)
  278. ha_env_managed = ha_url_from_env and ha_token_from_env
  279. # Auto-enable when both env vars are set, otherwise use database value
  280. if ha_url_env and ha_token_env:
  281. ha_enabled = True
  282. else:
  283. ha_enabled = ha_enabled_db.lower() == "true"
  284. return {
  285. "ha_enabled": ha_enabled,
  286. "ha_url": ha_url,
  287. "ha_token": ha_token,
  288. "ha_url_from_env": ha_url_from_env,
  289. "ha_token_from_env": ha_token_from_env,
  290. "ha_env_managed": ha_env_managed,
  291. }
  292. async def create_backup_zip(output_path: Path | None = None) -> tuple[Path, str]:
  293. """Create a complete backup ZIP (database + all data directories).
  294. If output_path is given, the ZIP is written there.
  295. Otherwise a temporary file is created (caller must clean up).
  296. Returns (zip_path, filename).
  297. """
  298. import shutil
  299. import tempfile
  300. from backend.app.core.db_dialect import is_sqlite
  301. base_dir = app_settings.base_dir
  302. filename = f"bambuddy-backup-{datetime.now().strftime('%Y%m%d-%H%M%S')}.zip"
  303. with tempfile.TemporaryDirectory() as temp_dir:
  304. temp_path = Path(temp_dir)
  305. if is_sqlite():
  306. from sqlalchemy import text
  307. from backend.app.core.database import engine
  308. db_path = Path(app_settings.database_url.replace("sqlite+aiosqlite:///", ""))
  309. # Checkpoint WAL to ensure all data is in main db file
  310. async with engine.begin() as conn:
  311. await conn.execute(text("PRAGMA wal_checkpoint(TRUNCATE)"))
  312. # Copy database file
  313. shutil.copy2(db_path, temp_path / "bambuddy.db")
  314. else:
  315. # PostgreSQL: export to a portable SQLite file via SQLAlchemy.
  316. # This makes backups restorable on both SQLite and Postgres installs.
  317. import json
  318. import sqlite3
  319. from backend.app.core.database import Base, engine
  320. backup_db_path = temp_path / "bambuddy.db"
  321. dst = sqlite3.connect(str(backup_db_path))
  322. metadata = Base.metadata
  323. # Create tables in SQLite backup (simplified — just column names and types)
  324. for table in metadata.sorted_tables:
  325. cols = []
  326. pk_cols = [col.name for col in table.columns if col.primary_key]
  327. for col in table.columns:
  328. col_type = "TEXT" # Default
  329. type_str = str(col.type).upper()
  330. if "INT" in type_str:
  331. col_type = "INTEGER"
  332. elif "FLOAT" in type_str or "REAL" in type_str or "NUMERIC" in type_str:
  333. col_type = "REAL"
  334. elif "BOOL" in type_str:
  335. col_type = "BOOLEAN"
  336. # Only inline PRIMARY KEY for single-column PKs
  337. pk = " PRIMARY KEY" if col.primary_key and len(pk_cols) == 1 else ""
  338. cols.append(f"{col.name} {col_type}{pk}")
  339. # Add composite primary key constraint if needed
  340. if len(pk_cols) > 1:
  341. cols.append(f"PRIMARY KEY ({', '.join(pk_cols)})")
  342. dst.execute(f"CREATE TABLE IF NOT EXISTS {table.name} ({', '.join(cols)})") # noqa: S608
  343. # Export data from Postgres to SQLite
  344. async with engine.connect() as conn:
  345. for table in metadata.sorted_tables:
  346. result = await conn.execute(table.select())
  347. rows = result.fetchall()
  348. if not rows:
  349. continue
  350. columns = list(result.keys())
  351. placeholders = ", ".join(["?"] * len(columns))
  352. col_list = ", ".join(columns)
  353. insert_sql = f"INSERT INTO {table.name} ({col_list}) VALUES ({placeholders})" # noqa: S608 # nosec B608 — table/column names from ORM metadata, not user input
  354. def _serialize_row(row):
  355. return tuple(json.dumps(v) if isinstance(v, (list, dict)) else v for v in row)
  356. dst.executemany(insert_sql, [_serialize_row(row) for row in rows])
  357. dst.commit()
  358. dst.close()
  359. logger.info("PostgreSQL backup exported to portable SQLite format")
  360. # Copy data directories (if they exist)
  361. dirs_to_backup = [
  362. ("archive", base_dir / "archive"),
  363. ("virtual_printer", base_dir / "virtual_printer"),
  364. ("plate_calibration", app_settings.plate_calibration_dir),
  365. ("icons", base_dir / "icons"),
  366. ("projects", base_dir / "projects"),
  367. ]
  368. for name, src_dir in dirs_to_backup:
  369. if src_dir.exists() and any(src_dir.iterdir()):
  370. try:
  371. shutil.copytree(src_dir, temp_path / name)
  372. except shutil.Error as e:
  373. logger.warning("Some files in %s could not be copied: %s", name, e)
  374. except PermissionError as e:
  375. logger.warning("Permission denied copying %s: %s", name, e)
  376. # Create ZIP
  377. if output_path is not None:
  378. zip_file = output_path / filename
  379. else:
  380. fd, tmp = tempfile.mkstemp(suffix=".zip")
  381. os.close(fd)
  382. zip_file = Path(tmp)
  383. with zipfile.ZipFile(zip_file, "w", zipfile.ZIP_DEFLATED) as zf:
  384. for file_path in temp_path.rglob("*"):
  385. if file_path.is_file():
  386. arcname = file_path.relative_to(temp_path)
  387. zf.write(file_path, arcname)
  388. return zip_file, filename
  389. @router.get("/backup")
  390. async def create_backup(
  391. db: AsyncSession = Depends(get_db),
  392. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_BACKUP),
  393. ):
  394. """Create a complete backup (database + all files) as a ZIP download."""
  395. from starlette.background import BackgroundTask
  396. try:
  397. zip_file, filename = await create_backup_zip()
  398. return FileResponse(
  399. path=zip_file,
  400. filename=filename,
  401. media_type="application/zip",
  402. background=BackgroundTask(lambda: zip_file.unlink(missing_ok=True)),
  403. )
  404. except Exception as e:
  405. logger.error("Backup failed: %s", e, exc_info=True)
  406. return JSONResponse(
  407. status_code=500,
  408. content={"success": False, "message": "Backup failed. Check server logs for details."},
  409. )
  410. async def _import_sqlite_to_postgres(sqlite_path: Path, postgres_url: str):
  411. """Import data from a SQLite database file into the current PostgreSQL database.
  412. Used for cross-database restore (SQLite backup → PostgreSQL).
  413. Reads all tables from the SQLite file and bulk-inserts into Postgres.
  414. """
  415. import sqlite3
  416. from sqlalchemy import text
  417. from backend.app.core.database import Base, _create_engine
  418. # Create a temporary engine for the import (current engine was disposed)
  419. pg_engine = _create_engine()
  420. try:
  421. # Open SQLite file directly (sync — it's a local file read)
  422. src = sqlite3.connect(str(sqlite_path))
  423. src.row_factory = sqlite3.Row
  424. # Get list of tables from SQLite (skip internal/FTS tables)
  425. cursor = src.execute(
  426. "SELECT name FROM sqlite_master WHERE type='table' "
  427. "AND name NOT LIKE 'sqlite_%' AND name NOT LIKE 'archive_fts%'"
  428. )
  429. src_tables = {row["name"] for row in cursor.fetchall()}
  430. # Get Postgres tables from our ORM models
  431. metadata = Base.metadata
  432. pg_tables = set(metadata.tables.keys())
  433. # Only import tables that exist in both source and destination
  434. tables_to_import = src_tables & pg_tables
  435. sorted_tables = [t.name for t in metadata.sorted_tables if t.name in tables_to_import]
  436. # Phase 1: Drop all tables and recreate WITHOUT foreign keys.
  437. # This avoids all FK ordering/orphan issues during import.
  438. saved_fks = {}
  439. for table in metadata.sorted_tables:
  440. fks = list(table.foreign_key_constraints)
  441. if fks:
  442. saved_fks[table.name] = fks
  443. for fk in fks:
  444. table.constraints.discard(fk)
  445. async with pg_engine.begin() as conn:
  446. # Drop every existing table in the public schema with CASCADE
  447. # rather than `metadata.drop_all`. Two reasons:
  448. # 1. The user's live DB may carry orphan tables from removed
  449. # features (e.g. the legacy `spoolman_slot_assignments`,
  450. # `spoolman_k_profile`) that hold FK constraints back to
  451. # ORM tables. `drop_all` doesn't know they exist and emits
  452. # `DROP TABLE printers` without CASCADE — Postgres refuses
  453. # and the whole restore aborts (#XXXX).
  454. # 2. Even within the metadata, `drop_all` is FK-ordered and
  455. # breaks if a future schema rename leaves old constraints
  456. # around. CASCADE is the right tool for a destructive
  457. # restore: the user is intentionally wiping state.
  458. await conn.execute(
  459. text(
  460. "DO $$ DECLARE r RECORD; BEGIN "
  461. "FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') LOOP "
  462. "EXECUTE 'DROP TABLE IF EXISTS public.' || quote_ident(r.tablename) || ' CASCADE'; "
  463. "END LOOP; END $$;"
  464. )
  465. )
  466. await conn.run_sync(metadata.create_all)
  467. # Restore FK definitions in metadata (needed for re-adding later)
  468. for table_name, fks in saved_fks.items():
  469. table_obj = metadata.tables[table_name]
  470. for fk in fks:
  471. table_obj.constraints.add(fk)
  472. # Phase 2: Import data (no FKs to worry about)
  473. async with pg_engine.begin() as conn:
  474. # Import each table in dependency order (parents before children)
  475. for table_name in sorted_tables:
  476. rows = src.execute(f"SELECT * FROM {table_name}").fetchall() # noqa: S608 # nosec B608
  477. if not rows:
  478. continue
  479. # Filter to columns that exist in the Postgres table
  480. src_columns = rows[0].keys()
  481. pg_table = metadata.tables.get(table_name)
  482. pg_columns = {c.name for c in pg_table.columns} if pg_table is not None else set()
  483. columns = [c for c in src_columns if c in pg_columns]
  484. if not columns:
  485. continue
  486. col_list = ", ".join(columns)
  487. param_list = ", ".join(f":{c}" for c in columns)
  488. # ON CONFLICT DO NOTHING handles duplicate rows from SQLite (which doesn't enforce unique constraints)
  489. insert_sql = text(f"INSERT INTO {table_name} ({col_list}) VALUES ({param_list}) ON CONFLICT DO NOTHING") # noqa: S608 # nosec B608
  490. # Identify columns that need type conversion (SQLite stores booleans
  491. # as int and datetimes as str — asyncpg requires native Python types)
  492. from datetime import datetime as dt
  493. bool_columns = set()
  494. datetime_columns = set()
  495. not_null_defaults = {} # col_name -> default value for NOT NULL columns
  496. if pg_table is not None:
  497. for col in pg_table.columns:
  498. if col.name not in columns:
  499. continue
  500. col_type = str(col.type)
  501. if col_type == "BOOLEAN":
  502. bool_columns.add(col.name)
  503. elif col_type in ("DATETIME", "TIMESTAMP WITHOUT TIME ZONE", "TIMESTAMP WITH TIME ZONE"):
  504. datetime_columns.add(col.name)
  505. # Track NOT NULL columns with defaults — older backups may have NULL
  506. # for columns added after the backup was created
  507. if not col.nullable:
  508. if col.default is not None:
  509. default = col.default.arg
  510. if callable(default):
  511. default = default(None)
  512. not_null_defaults[col.name] = default
  513. elif col.server_default is not None:
  514. # server_default=func.now() → use current timestamp
  515. if col.name in datetime_columns:
  516. not_null_defaults[col.name] = "__now__"
  517. else:
  518. # Try to extract literal server default
  519. sd = str(col.server_default.arg) if hasattr(col.server_default, "arg") else None
  520. if sd is not None:
  521. not_null_defaults[col.name] = sd
  522. now = dt.now()
  523. def _convert_row(
  524. row, cols=columns, bools=bool_columns, dts=datetime_columns, nn_defaults=not_null_defaults, _now=now
  525. ):
  526. result = {}
  527. for c in cols:
  528. val = row[c]
  529. if val is None and c in nn_defaults:
  530. val = _now if nn_defaults[c] == "__now__" else nn_defaults[c]
  531. if val is not None:
  532. if c in bools:
  533. val = bool(val)
  534. elif c in dts and isinstance(val, str):
  535. try:
  536. val = dt.fromisoformat(val)
  537. except ValueError:
  538. pass
  539. result[c] = val
  540. return result
  541. batch = [_convert_row(row) for row in rows]
  542. await conn.execute(insert_sql, batch)
  543. logger.info("Imported %d rows into %s", len(batch), table_name)
  544. # Reset sequences to max(id) + 1 for each table with an id column
  545. for table_name in sorted_tables:
  546. try:
  547. async with conn.begin_nested():
  548. result = await conn.execute(text(f"SELECT MAX(id) FROM {table_name}")) # noqa: S608 # nosec B608
  549. max_id = result.scalar()
  550. if max_id is not None:
  551. seq_name = f"{table_name}_id_seq"
  552. await conn.execute(text(f"SELECT setval('{seq_name}', {max_id})")) # noqa: S608
  553. except Exception:
  554. pass # Table may not have an id column or sequence
  555. src.close()
  556. logger.info("Cross-database import complete: %d tables imported", len(tables_to_import))
  557. # Recreate FK constraints from ORM metadata (not from saved definitions).
  558. # Use individual transactions so orphaned SQLite data doesn't block valid FKs.
  559. from sqlalchemy.schema import AddConstraint
  560. failed_fks = []
  561. for table in metadata.sorted_tables:
  562. for fk in table.foreign_key_constraints:
  563. try:
  564. async with pg_engine.begin() as fk_conn:
  565. await fk_conn.execute(AddConstraint(fk))
  566. except Exception:
  567. failed_fks.append(f"{table.name}.{fk.name}")
  568. if failed_fks:
  569. logger.warning(
  570. "Could not restore %d FK constraints (orphaned data in SQLite): %s",
  571. len(failed_fks),
  572. ", ".join(failed_fks),
  573. )
  574. finally:
  575. await pg_engine.dispose()
  576. @router.post("/restore")
  577. async def restore_backup(
  578. file: UploadFile = File(...),
  579. db: AsyncSession = Depends(get_db),
  580. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_RESTORE),
  581. ):
  582. """Restore from a complete backup ZIP.
  583. Replaces the database and all data directories from the backup ZIP.
  584. Requires a restart after restore.
  585. """
  586. import shutil
  587. import tempfile
  588. from fastapi import HTTPException
  589. from backend.app.core.database import close_all_connections, init_db, reinitialize_database
  590. from backend.app.core.db_dialect import is_sqlite
  591. from backend.app.services.virtual_printer import virtual_printer_manager
  592. base_dir = app_settings.base_dir
  593. with tempfile.TemporaryDirectory() as temp_dir:
  594. temp_path = Path(temp_dir)
  595. # 1. Read and extract ZIP
  596. content = await file.read()
  597. # Check if it's a valid ZIP
  598. if not file.filename or not file.filename.endswith(".zip"):
  599. raise HTTPException(400, "Invalid backup file: must be a .zip file")
  600. try:
  601. with zipfile.ZipFile(io.BytesIO(content), "r") as zf:
  602. zf.extractall(temp_path)
  603. except zipfile.BadZipFile:
  604. raise HTTPException(400, "Invalid backup file: not a valid ZIP")
  605. # 2. Validate backup
  606. backup_db = temp_path / "bambuddy.db"
  607. if not backup_db.exists():
  608. raise HTTPException(400, "Invalid backup: missing bambuddy.db")
  609. try:
  610. import asyncio
  611. # 3. Stop virtual printer if running (releases file locks)
  612. try:
  613. if virtual_printer_manager.is_enabled:
  614. logger.info("Stopping virtual printer for restore...")
  615. await virtual_printer_manager.configure(enabled=False)
  616. await asyncio.sleep(1)
  617. except Exception as e:
  618. logger.warning("Failed to stop virtual printer: %s", e)
  619. # 4. Close current database connections
  620. logger.info("Closing database connections...")
  621. await close_all_connections()
  622. # 5. Replace database
  623. logger.info("Restoring database from backup...")
  624. if is_sqlite():
  625. db_path = Path(app_settings.database_url.replace("sqlite+aiosqlite:///", ""))
  626. # Use SQLite's online backup API instead of shutil.copy2.
  627. # The pragma at database.py:19 runs the live DB in WAL mode,
  628. # which means a naive file copy is unsafe: anything written
  629. # to the live DB before this call that hasn't been
  630. # checkpointed yet (seed_default_groups + init_db on first
  631. # start, plus whatever background heartbeats wrote during
  632. # the request window) sits in bambuddy.db-wal with valid
  633. # checksums. The route handler's own `db: Depends(get_db)`
  634. # session also keeps a connection checked out across
  635. # engine.dispose(), holding fds to the WAL inode. With
  636. # `shutil.copy2` SQLite finds the stale WAL on the next
  637. # open and silently re-applies those page-level writes on
  638. # top of the restored DB, partially clobbering it with
  639. # fresh-install state — the user sees a "successful"
  640. # restore where most rows and settings have reverted to
  641. # defaults (#1211 / #668). The page-by-page backup API
  642. # opens both DBs as real SQLite connections, takes the
  643. # right locks, and routes new pages through the live DB's
  644. # own WAL — so concurrent open sessions see their own
  645. # snapshot until they close (transaction isolation) but
  646. # can't corrupt the restored state.
  647. import sqlite3
  648. src_conn = sqlite3.connect(str(backup_db))
  649. try:
  650. dst_conn = sqlite3.connect(str(db_path))
  651. try:
  652. src_conn.backup(dst_conn)
  653. finally:
  654. dst_conn.close()
  655. finally:
  656. src_conn.close()
  657. else:
  658. # Import SQLite backup into PostgreSQL
  659. logger.info("Importing SQLite backup into PostgreSQL...")
  660. await _import_sqlite_to_postgres(backup_db, app_settings.database_url)
  661. # 6. Replace data directories
  662. # For Docker compatibility: clear contents then copy (don't delete mount points)
  663. dirs_to_restore = [
  664. ("archive", base_dir / "archive"),
  665. ("virtual_printer", base_dir / "virtual_printer"),
  666. ("plate_calibration", app_settings.plate_calibration_dir),
  667. ("icons", base_dir / "icons"),
  668. ("projects", base_dir / "projects"),
  669. ]
  670. skipped_dirs = []
  671. for name, dest_dir in dirs_to_restore:
  672. src_dir = temp_path / name
  673. if src_dir.exists():
  674. logger.info("Restoring %s directory...", name)
  675. try:
  676. # Clear destination contents (not the dir itself - may be Docker mount)
  677. if dest_dir.exists():
  678. for item in dest_dir.iterdir():
  679. try:
  680. if item.is_dir():
  681. shutil.rmtree(item)
  682. else:
  683. item.unlink()
  684. except OSError as e:
  685. logger.warning("Could not delete %s: %s", item, e)
  686. else:
  687. dest_dir.mkdir(parents=True, exist_ok=True)
  688. # Copy contents from backup
  689. for item in src_dir.iterdir():
  690. dest_item = dest_dir / item.name
  691. if item.is_dir():
  692. shutil.copytree(item, dest_item)
  693. else:
  694. shutil.copy2(item, dest_item)
  695. except OSError as e:
  696. logger.warning("Could not restore %s directory: %s", name, e)
  697. skipped_dirs.append(name)
  698. # 7. Reinitialize the database engine and apply schema migrations so that
  699. # tables added after the backup was created (e.g. ams_labels) exist
  700. # immediately, without requiring a manual restart.
  701. await reinitialize_database()
  702. await init_db()
  703. logger.info("Restore complete - restart required")
  704. message = "Backup restored successfully. Please restart Bambuddy for changes to take effect."
  705. if skipped_dirs:
  706. message += f" Note: Some directories could not be restored ({', '.join(skipped_dirs)})."
  707. return {
  708. "success": True,
  709. "message": message,
  710. }
  711. except Exception as e:
  712. logger.error("Restore failed: %s", e, exc_info=True)
  713. return JSONResponse(
  714. status_code=500,
  715. content={"success": False, "message": "Restore failed. Check server logs for details."},
  716. )
  717. @router.get("/network-interfaces")
  718. async def get_network_interfaces(
  719. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  720. ):
  721. """Get available network interfaces with all IPs (primary + aliases)."""
  722. from backend.app.services.network_utils import get_all_interface_ips
  723. interfaces = get_all_interface_ips()
  724. return {"interfaces": interfaces}
  725. @router.get("/virtual-printer/models")
  726. async def get_virtual_printer_models(
  727. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  728. ):
  729. """Get available virtual printer models."""
  730. from backend.app.services.virtual_printer import (
  731. DEFAULT_VIRTUAL_PRINTER_MODEL,
  732. VIRTUAL_PRINTER_MODELS,
  733. )
  734. return {
  735. "models": VIRTUAL_PRINTER_MODELS,
  736. "default": DEFAULT_VIRTUAL_PRINTER_MODEL,
  737. }
  738. @router.get("/virtual-printer")
  739. async def get_virtual_printer_settings(
  740. db: AsyncSession = Depends(get_db),
  741. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  742. ):
  743. """Get virtual printer settings and status."""
  744. from backend.app.services.virtual_printer import (
  745. DEFAULT_VIRTUAL_PRINTER_MODEL,
  746. virtual_printer_manager,
  747. )
  748. enabled = await get_setting(db, "virtual_printer_enabled")
  749. access_code = await get_setting(db, "virtual_printer_access_code")
  750. mode = await get_setting(db, "virtual_printer_mode")
  751. model = await get_setting(db, "virtual_printer_model")
  752. target_printer_id = await get_setting(db, "virtual_printer_target_printer_id")
  753. remote_interface_ip = await get_setting(db, "virtual_printer_remote_interface_ip")
  754. tailscale_disabled_raw = await get_setting(db, "virtual_printer_tailscale_disabled")
  755. archive_name_source = await get_setting(db, "virtual_printer_archive_name_source")
  756. return {
  757. "enabled": enabled == "true" if enabled else False,
  758. "access_code_set": bool(access_code),
  759. "mode": mode or "immediate",
  760. "model": model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  761. "target_printer_id": int(target_printer_id) if target_printer_id else None,
  762. "remote_interface_ip": remote_interface_ip or "",
  763. "tailscale_disabled": tailscale_disabled_raw == "true" if tailscale_disabled_raw else True,
  764. "archive_name_source": archive_name_source if archive_name_source in ("metadata", "filename") else "metadata",
  765. "status": virtual_printer_manager.get_status(),
  766. }
  767. @router.put("/virtual-printer")
  768. async def update_virtual_printer_settings(
  769. enabled: bool = None,
  770. access_code: str = None,
  771. mode: str = None,
  772. model: str = None,
  773. target_printer_id: int = None,
  774. remote_interface_ip: str = None,
  775. tailscale_disabled: bool = None,
  776. archive_name_source: str = None,
  777. db: AsyncSession = Depends(get_db),
  778. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  779. ):
  780. """Update virtual printer settings and restart services if needed.
  781. For proxy mode with SSDP proxy (dual-homed setup):
  782. - remote_interface_ip: IP of interface on slicer's network (LAN B)
  783. - Local interface is auto-detected based on target printer IP
  784. """
  785. from sqlalchemy import select
  786. from backend.app.models.printer import Printer
  787. from backend.app.services.virtual_printer import (
  788. DEFAULT_VIRTUAL_PRINTER_MODEL,
  789. VIRTUAL_PRINTER_MODELS,
  790. virtual_printer_manager,
  791. )
  792. # Get current values
  793. current_enabled = await get_setting(db, "virtual_printer_enabled") == "true"
  794. current_access_code = await get_setting(db, "virtual_printer_access_code") or ""
  795. current_mode = await get_setting(db, "virtual_printer_mode") or "immediate"
  796. current_model = await get_setting(db, "virtual_printer_model") or DEFAULT_VIRTUAL_PRINTER_MODEL
  797. current_target_id_str = await get_setting(db, "virtual_printer_target_printer_id")
  798. current_target_id = int(current_target_id_str) if current_target_id_str else None
  799. current_remote_iface = await get_setting(db, "virtual_printer_remote_interface_ip") or ""
  800. current_ts_disabled_raw = await get_setting(db, "virtual_printer_tailscale_disabled")
  801. # Default True (opt-in) when the setting has never been saved — matches the model default.
  802. current_ts_disabled = current_ts_disabled_raw == "true" if current_ts_disabled_raw else True
  803. # Apply updates
  804. new_enabled = enabled if enabled is not None else current_enabled
  805. new_access_code = access_code if access_code is not None else current_access_code
  806. new_mode = mode if mode is not None else current_mode
  807. new_model = model if model is not None else current_model
  808. new_target_id = target_printer_id if target_printer_id is not None else current_target_id
  809. new_remote_iface = remote_interface_ip if remote_interface_ip is not None else current_remote_iface
  810. new_ts_disabled = tailscale_disabled if tailscale_disabled is not None else current_ts_disabled
  811. # Validate mode
  812. # "review" is the new name for "queue" (pending review before archiving)
  813. # "print_queue" archives and adds to print queue (unassigned)
  814. # "proxy" is transparent TCP proxy to a real printer
  815. if new_mode not in ("immediate", "queue", "review", "print_queue", "proxy"):
  816. return JSONResponse(
  817. status_code=400,
  818. content={"detail": "Mode must be 'immediate', 'review', 'print_queue', or 'proxy'"},
  819. )
  820. # Validate archive_name_source
  821. if archive_name_source is not None and archive_name_source not in ("metadata", "filename"):
  822. return JSONResponse(
  823. status_code=400,
  824. content={"detail": "archive_name_source must be 'metadata' or 'filename'"},
  825. )
  826. # Normalize legacy "queue" to "review" for storage
  827. if new_mode == "queue":
  828. new_mode = "review"
  829. # Validate model
  830. if model is not None and model not in VIRTUAL_PRINTER_MODELS:
  831. return JSONResponse(
  832. status_code=400,
  833. content={"detail": f"Invalid model. Must be one of: {', '.join(VIRTUAL_PRINTER_MODELS.keys())}"},
  834. )
  835. # Mode-specific validation and printer lookup
  836. target_printer_ip = ""
  837. target_printer_serial = ""
  838. if new_mode == "proxy":
  839. # Proxy mode requires target printer when enabling
  840. if new_enabled and not new_target_id:
  841. # If just switching to proxy mode (not explicitly enabling), auto-disable
  842. if enabled is None:
  843. new_enabled = False
  844. else:
  845. return JSONResponse(
  846. status_code=400,
  847. content={"detail": "Target printer is required for proxy mode"},
  848. )
  849. # Look up printer IP and serial if we have a target
  850. if new_target_id:
  851. result = await db.execute(select(Printer).where(Printer.id == new_target_id))
  852. printer = result.scalar_one_or_none()
  853. if not printer:
  854. return JSONResponse(
  855. status_code=400,
  856. content={"detail": f"Printer with ID {new_target_id} not found"},
  857. )
  858. target_printer_ip = printer.ip_address
  859. target_printer_serial = printer.serial_number
  860. # Access code not required for proxy mode
  861. else:
  862. # Non-proxy modes require access code when enabling
  863. if new_enabled and not new_access_code:
  864. # If just switching modes (not explicitly enabling), auto-disable
  865. if enabled is None:
  866. new_enabled = False
  867. else:
  868. return JSONResponse(
  869. status_code=400,
  870. content={"detail": "Access code is required when enabling virtual printer"},
  871. )
  872. # Validate access code length (Bambu Studio requires exactly 8 characters)
  873. if access_code is not None and access_code and len(access_code) != 8:
  874. return JSONResponse(
  875. status_code=400,
  876. content={"detail": "Access code must be exactly 8 characters"},
  877. )
  878. # Save settings
  879. await set_setting(db, "virtual_printer_enabled", "true" if new_enabled else "false")
  880. if access_code is not None:
  881. await set_setting(db, "virtual_printer_access_code", access_code)
  882. await set_setting(db, "virtual_printer_mode", new_mode)
  883. if model is not None:
  884. await set_setting(db, "virtual_printer_model", model)
  885. if target_printer_id is not None:
  886. await set_setting(db, "virtual_printer_target_printer_id", str(target_printer_id))
  887. if remote_interface_ip is not None:
  888. await set_setting(db, "virtual_printer_remote_interface_ip", remote_interface_ip)
  889. if tailscale_disabled is not None:
  890. await set_setting(db, "virtual_printer_tailscale_disabled", "true" if tailscale_disabled else "false")
  891. if archive_name_source is not None:
  892. await set_setting(db, "virtual_printer_archive_name_source", archive_name_source)
  893. # Propagate tailscale_disabled to the first VirtualPrinter row so sync_from_db() picks it up
  894. if tailscale_disabled is not None:
  895. from backend.app.models.virtual_printer import VirtualPrinter as VPModel
  896. vp_result = await db.execute(select(VPModel).order_by(VPModel.position).limit(1))
  897. first_vp = vp_result.scalar_one_or_none()
  898. if first_vp is not None:
  899. first_vp.tailscale_disabled = new_ts_disabled
  900. await db.commit()
  901. db.expire_all()
  902. # Reconfigure virtual printer
  903. try:
  904. await virtual_printer_manager.configure(
  905. enabled=new_enabled,
  906. access_code=new_access_code,
  907. mode=new_mode,
  908. model=new_model,
  909. target_printer_ip=target_printer_ip,
  910. target_printer_serial=target_printer_serial,
  911. remote_interface_ip=new_remote_iface,
  912. )
  913. except ValueError as e:
  914. logger.warning("Virtual printer configuration validation error: %s", e)
  915. return JSONResponse(
  916. status_code=400,
  917. content={"detail": "Invalid virtual printer configuration. Check the provided values."},
  918. )
  919. except Exception as e:
  920. logger.error("Failed to configure virtual printer: %s", e, exc_info=True)
  921. return JSONResponse(
  922. status_code=500,
  923. content={"detail": "Failed to configure virtual printer. Check server logs for details."},
  924. )
  925. return await get_virtual_printer_settings(db)
  926. # =============================================================================
  927. # MQTT Relay Settings
  928. # =============================================================================
  929. @router.get("/mqtt/status")
  930. async def get_mqtt_status(
  931. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  932. ):
  933. """Get MQTT relay connection status."""
  934. from backend.app.services.mqtt_relay import mqtt_relay
  935. return mqtt_relay.get_status()