settings.py 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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. await conn.run_sync(metadata.drop_all)
  447. await conn.run_sync(metadata.create_all)
  448. # Restore FK definitions in metadata (needed for re-adding later)
  449. for table_name, fks in saved_fks.items():
  450. table_obj = metadata.tables[table_name]
  451. for fk in fks:
  452. table_obj.constraints.add(fk)
  453. # Phase 2: Import data (no FKs to worry about)
  454. async with pg_engine.begin() as conn:
  455. # Import each table in dependency order (parents before children)
  456. for table_name in sorted_tables:
  457. rows = src.execute(f"SELECT * FROM {table_name}").fetchall() # noqa: S608 # nosec B608
  458. if not rows:
  459. continue
  460. # Filter to columns that exist in the Postgres table
  461. src_columns = rows[0].keys()
  462. pg_table = metadata.tables.get(table_name)
  463. pg_columns = {c.name for c in pg_table.columns} if pg_table is not None else set()
  464. columns = [c for c in src_columns if c in pg_columns]
  465. if not columns:
  466. continue
  467. col_list = ", ".join(columns)
  468. param_list = ", ".join(f":{c}" for c in columns)
  469. # ON CONFLICT DO NOTHING handles duplicate rows from SQLite (which doesn't enforce unique constraints)
  470. insert_sql = text(f"INSERT INTO {table_name} ({col_list}) VALUES ({param_list}) ON CONFLICT DO NOTHING") # noqa: S608 # nosec B608
  471. # Identify columns that need type conversion (SQLite stores booleans
  472. # as int and datetimes as str — asyncpg requires native Python types)
  473. from datetime import datetime as dt
  474. bool_columns = set()
  475. datetime_columns = set()
  476. not_null_defaults = {} # col_name -> default value for NOT NULL columns
  477. if pg_table is not None:
  478. for col in pg_table.columns:
  479. if col.name not in columns:
  480. continue
  481. col_type = str(col.type)
  482. if col_type == "BOOLEAN":
  483. bool_columns.add(col.name)
  484. elif col_type in ("DATETIME", "TIMESTAMP WITHOUT TIME ZONE", "TIMESTAMP WITH TIME ZONE"):
  485. datetime_columns.add(col.name)
  486. # Track NOT NULL columns with defaults — older backups may have NULL
  487. # for columns added after the backup was created
  488. if not col.nullable:
  489. if col.default is not None:
  490. default = col.default.arg
  491. if callable(default):
  492. default = default(None)
  493. not_null_defaults[col.name] = default
  494. elif col.server_default is not None:
  495. # server_default=func.now() → use current timestamp
  496. if col.name in datetime_columns:
  497. not_null_defaults[col.name] = "__now__"
  498. else:
  499. # Try to extract literal server default
  500. sd = str(col.server_default.arg) if hasattr(col.server_default, "arg") else None
  501. if sd is not None:
  502. not_null_defaults[col.name] = sd
  503. now = dt.now()
  504. def _convert_row(
  505. row, cols=columns, bools=bool_columns, dts=datetime_columns, nn_defaults=not_null_defaults, _now=now
  506. ):
  507. result = {}
  508. for c in cols:
  509. val = row[c]
  510. if val is None and c in nn_defaults:
  511. val = _now if nn_defaults[c] == "__now__" else nn_defaults[c]
  512. if val is not None:
  513. if c in bools:
  514. val = bool(val)
  515. elif c in dts and isinstance(val, str):
  516. try:
  517. val = dt.fromisoformat(val)
  518. except ValueError:
  519. pass
  520. result[c] = val
  521. return result
  522. batch = [_convert_row(row) for row in rows]
  523. await conn.execute(insert_sql, batch)
  524. logger.info("Imported %d rows into %s", len(batch), table_name)
  525. # Reset sequences to max(id) + 1 for each table with an id column
  526. for table_name in sorted_tables:
  527. try:
  528. async with conn.begin_nested():
  529. result = await conn.execute(text(f"SELECT MAX(id) FROM {table_name}")) # noqa: S608 # nosec B608
  530. max_id = result.scalar()
  531. if max_id is not None:
  532. seq_name = f"{table_name}_id_seq"
  533. await conn.execute(text(f"SELECT setval('{seq_name}', {max_id})")) # noqa: S608
  534. except Exception:
  535. pass # Table may not have an id column or sequence
  536. src.close()
  537. logger.info("Cross-database import complete: %d tables imported", len(tables_to_import))
  538. # Recreate FK constraints from ORM metadata (not from saved definitions).
  539. # Use individual transactions so orphaned SQLite data doesn't block valid FKs.
  540. from sqlalchemy.schema import AddConstraint
  541. failed_fks = []
  542. for table in metadata.sorted_tables:
  543. for fk in table.foreign_key_constraints:
  544. try:
  545. async with pg_engine.begin() as fk_conn:
  546. await fk_conn.execute(AddConstraint(fk))
  547. except Exception:
  548. failed_fks.append(f"{table.name}.{fk.name}")
  549. if failed_fks:
  550. logger.warning(
  551. "Could not restore %d FK constraints (orphaned data in SQLite): %s",
  552. len(failed_fks),
  553. ", ".join(failed_fks),
  554. )
  555. finally:
  556. await pg_engine.dispose()
  557. @router.post("/restore")
  558. async def restore_backup(
  559. file: UploadFile = File(...),
  560. db: AsyncSession = Depends(get_db),
  561. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_RESTORE),
  562. ):
  563. """Restore from a complete backup ZIP.
  564. Replaces the database and all data directories from the backup ZIP.
  565. Requires a restart after restore.
  566. """
  567. import shutil
  568. import tempfile
  569. from fastapi import HTTPException
  570. from backend.app.core.database import close_all_connections, init_db, reinitialize_database
  571. from backend.app.core.db_dialect import is_sqlite
  572. from backend.app.services.virtual_printer import virtual_printer_manager
  573. base_dir = app_settings.base_dir
  574. with tempfile.TemporaryDirectory() as temp_dir:
  575. temp_path = Path(temp_dir)
  576. # 1. Read and extract ZIP
  577. content = await file.read()
  578. # Check if it's a valid ZIP
  579. if not file.filename or not file.filename.endswith(".zip"):
  580. raise HTTPException(400, "Invalid backup file: must be a .zip file")
  581. try:
  582. with zipfile.ZipFile(io.BytesIO(content), "r") as zf:
  583. zf.extractall(temp_path)
  584. except zipfile.BadZipFile:
  585. raise HTTPException(400, "Invalid backup file: not a valid ZIP")
  586. # 2. Validate backup
  587. backup_db = temp_path / "bambuddy.db"
  588. if not backup_db.exists():
  589. raise HTTPException(400, "Invalid backup: missing bambuddy.db")
  590. try:
  591. import asyncio
  592. # 3. Stop virtual printer if running (releases file locks)
  593. try:
  594. if virtual_printer_manager.is_enabled:
  595. logger.info("Stopping virtual printer for restore...")
  596. await virtual_printer_manager.configure(enabled=False)
  597. await asyncio.sleep(1)
  598. except Exception as e:
  599. logger.warning("Failed to stop virtual printer: %s", e)
  600. # 4. Close current database connections
  601. logger.info("Closing database connections...")
  602. await close_all_connections()
  603. # 5. Replace database
  604. logger.info("Restoring database from backup...")
  605. if is_sqlite():
  606. db_path = Path(app_settings.database_url.replace("sqlite+aiosqlite:///", ""))
  607. shutil.copy2(backup_db, db_path)
  608. else:
  609. # Import SQLite backup into PostgreSQL
  610. logger.info("Importing SQLite backup into PostgreSQL...")
  611. await _import_sqlite_to_postgres(backup_db, app_settings.database_url)
  612. # 6. Replace data directories
  613. # For Docker compatibility: clear contents then copy (don't delete mount points)
  614. dirs_to_restore = [
  615. ("archive", base_dir / "archive"),
  616. ("virtual_printer", base_dir / "virtual_printer"),
  617. ("plate_calibration", app_settings.plate_calibration_dir),
  618. ("icons", base_dir / "icons"),
  619. ("projects", base_dir / "projects"),
  620. ]
  621. skipped_dirs = []
  622. for name, dest_dir in dirs_to_restore:
  623. src_dir = temp_path / name
  624. if src_dir.exists():
  625. logger.info("Restoring %s directory...", name)
  626. try:
  627. # Clear destination contents (not the dir itself - may be Docker mount)
  628. if dest_dir.exists():
  629. for item in dest_dir.iterdir():
  630. try:
  631. if item.is_dir():
  632. shutil.rmtree(item)
  633. else:
  634. item.unlink()
  635. except OSError as e:
  636. logger.warning("Could not delete %s: %s", item, e)
  637. else:
  638. dest_dir.mkdir(parents=True, exist_ok=True)
  639. # Copy contents from backup
  640. for item in src_dir.iterdir():
  641. dest_item = dest_dir / item.name
  642. if item.is_dir():
  643. shutil.copytree(item, dest_item)
  644. else:
  645. shutil.copy2(item, dest_item)
  646. except OSError as e:
  647. logger.warning("Could not restore %s directory: %s", name, e)
  648. skipped_dirs.append(name)
  649. # 7. Reinitialize the database engine and apply schema migrations so that
  650. # tables added after the backup was created (e.g. ams_labels) exist
  651. # immediately, without requiring a manual restart.
  652. await reinitialize_database()
  653. await init_db()
  654. logger.info("Restore complete - restart required")
  655. message = "Backup restored successfully. Please restart Bambuddy for changes to take effect."
  656. if skipped_dirs:
  657. message += f" Note: Some directories could not be restored ({', '.join(skipped_dirs)})."
  658. return {
  659. "success": True,
  660. "message": message,
  661. }
  662. except Exception as e:
  663. logger.error("Restore failed: %s", e, exc_info=True)
  664. return JSONResponse(
  665. status_code=500,
  666. content={"success": False, "message": "Restore failed. Check server logs for details."},
  667. )
  668. @router.get("/network-interfaces")
  669. async def get_network_interfaces(
  670. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  671. ):
  672. """Get available network interfaces with all IPs (primary + aliases)."""
  673. from backend.app.services.network_utils import get_all_interface_ips
  674. interfaces = get_all_interface_ips()
  675. return {"interfaces": interfaces}
  676. @router.get("/virtual-printer/models")
  677. async def get_virtual_printer_models(
  678. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  679. ):
  680. """Get available virtual printer models."""
  681. from backend.app.services.virtual_printer import (
  682. DEFAULT_VIRTUAL_PRINTER_MODEL,
  683. VIRTUAL_PRINTER_MODELS,
  684. )
  685. return {
  686. "models": VIRTUAL_PRINTER_MODELS,
  687. "default": DEFAULT_VIRTUAL_PRINTER_MODEL,
  688. }
  689. @router.get("/virtual-printer")
  690. async def get_virtual_printer_settings(
  691. db: AsyncSession = Depends(get_db),
  692. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  693. ):
  694. """Get virtual printer settings and status."""
  695. from backend.app.services.virtual_printer import (
  696. DEFAULT_VIRTUAL_PRINTER_MODEL,
  697. virtual_printer_manager,
  698. )
  699. enabled = await get_setting(db, "virtual_printer_enabled")
  700. access_code = await get_setting(db, "virtual_printer_access_code")
  701. mode = await get_setting(db, "virtual_printer_mode")
  702. model = await get_setting(db, "virtual_printer_model")
  703. target_printer_id = await get_setting(db, "virtual_printer_target_printer_id")
  704. remote_interface_ip = await get_setting(db, "virtual_printer_remote_interface_ip")
  705. tailscale_disabled_raw = await get_setting(db, "virtual_printer_tailscale_disabled")
  706. return {
  707. "enabled": enabled == "true" if enabled else False,
  708. "access_code_set": bool(access_code),
  709. "mode": mode or "immediate",
  710. "model": model or DEFAULT_VIRTUAL_PRINTER_MODEL,
  711. "target_printer_id": int(target_printer_id) if target_printer_id else None,
  712. "remote_interface_ip": remote_interface_ip or "",
  713. "tailscale_disabled": tailscale_disabled_raw == "true" if tailscale_disabled_raw else False,
  714. "status": virtual_printer_manager.get_status(),
  715. }
  716. @router.put("/virtual-printer")
  717. async def update_virtual_printer_settings(
  718. enabled: bool = None,
  719. access_code: str = None,
  720. mode: str = None,
  721. model: str = None,
  722. target_printer_id: int = None,
  723. remote_interface_ip: str = None,
  724. tailscale_disabled: bool = None,
  725. db: AsyncSession = Depends(get_db),
  726. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
  727. ):
  728. """Update virtual printer settings and restart services if needed.
  729. For proxy mode with SSDP proxy (dual-homed setup):
  730. - remote_interface_ip: IP of interface on slicer's network (LAN B)
  731. - Local interface is auto-detected based on target printer IP
  732. """
  733. from sqlalchemy import select
  734. from backend.app.models.printer import Printer
  735. from backend.app.services.virtual_printer import (
  736. DEFAULT_VIRTUAL_PRINTER_MODEL,
  737. VIRTUAL_PRINTER_MODELS,
  738. virtual_printer_manager,
  739. )
  740. # Get current values
  741. current_enabled = await get_setting(db, "virtual_printer_enabled") == "true"
  742. current_access_code = await get_setting(db, "virtual_printer_access_code") or ""
  743. current_mode = await get_setting(db, "virtual_printer_mode") or "immediate"
  744. current_model = await get_setting(db, "virtual_printer_model") or DEFAULT_VIRTUAL_PRINTER_MODEL
  745. current_target_id_str = await get_setting(db, "virtual_printer_target_printer_id")
  746. current_target_id = int(current_target_id_str) if current_target_id_str else None
  747. current_remote_iface = await get_setting(db, "virtual_printer_remote_interface_ip") or ""
  748. current_ts_disabled_raw = await get_setting(db, "virtual_printer_tailscale_disabled")
  749. current_ts_disabled = current_ts_disabled_raw == "true" if current_ts_disabled_raw else False
  750. # Apply updates
  751. new_enabled = enabled if enabled is not None else current_enabled
  752. new_access_code = access_code if access_code is not None else current_access_code
  753. new_mode = mode if mode is not None else current_mode
  754. new_model = model if model is not None else current_model
  755. new_target_id = target_printer_id if target_printer_id is not None else current_target_id
  756. new_remote_iface = remote_interface_ip if remote_interface_ip is not None else current_remote_iface
  757. new_ts_disabled = tailscale_disabled if tailscale_disabled is not None else current_ts_disabled
  758. # Validate mode
  759. # "review" is the new name for "queue" (pending review before archiving)
  760. # "print_queue" archives and adds to print queue (unassigned)
  761. # "proxy" is transparent TCP proxy to a real printer
  762. if new_mode not in ("immediate", "queue", "review", "print_queue", "proxy"):
  763. return JSONResponse(
  764. status_code=400,
  765. content={"detail": "Mode must be 'immediate', 'review', 'print_queue', or 'proxy'"},
  766. )
  767. # Normalize legacy "queue" to "review" for storage
  768. if new_mode == "queue":
  769. new_mode = "review"
  770. # Validate model
  771. if model is not None and model not in VIRTUAL_PRINTER_MODELS:
  772. return JSONResponse(
  773. status_code=400,
  774. content={"detail": f"Invalid model. Must be one of: {', '.join(VIRTUAL_PRINTER_MODELS.keys())}"},
  775. )
  776. # Mode-specific validation and printer lookup
  777. target_printer_ip = ""
  778. target_printer_serial = ""
  779. if new_mode == "proxy":
  780. # Proxy mode requires target printer when enabling
  781. if new_enabled and not new_target_id:
  782. # If just switching to proxy mode (not explicitly enabling), auto-disable
  783. if enabled is None:
  784. new_enabled = False
  785. else:
  786. return JSONResponse(
  787. status_code=400,
  788. content={"detail": "Target printer is required for proxy mode"},
  789. )
  790. # Look up printer IP and serial if we have a target
  791. if new_target_id:
  792. result = await db.execute(select(Printer).where(Printer.id == new_target_id))
  793. printer = result.scalar_one_or_none()
  794. if not printer:
  795. return JSONResponse(
  796. status_code=400,
  797. content={"detail": f"Printer with ID {new_target_id} not found"},
  798. )
  799. target_printer_ip = printer.ip_address
  800. target_printer_serial = printer.serial_number
  801. # Access code not required for proxy mode
  802. else:
  803. # Non-proxy modes require access code when enabling
  804. if new_enabled and not new_access_code:
  805. # If just switching modes (not explicitly enabling), auto-disable
  806. if enabled is None:
  807. new_enabled = False
  808. else:
  809. return JSONResponse(
  810. status_code=400,
  811. content={"detail": "Access code is required when enabling virtual printer"},
  812. )
  813. # Validate access code length (Bambu Studio requires exactly 8 characters)
  814. if access_code is not None and access_code and len(access_code) != 8:
  815. return JSONResponse(
  816. status_code=400,
  817. content={"detail": "Access code must be exactly 8 characters"},
  818. )
  819. # Save settings
  820. await set_setting(db, "virtual_printer_enabled", "true" if new_enabled else "false")
  821. if access_code is not None:
  822. await set_setting(db, "virtual_printer_access_code", access_code)
  823. await set_setting(db, "virtual_printer_mode", new_mode)
  824. if model is not None:
  825. await set_setting(db, "virtual_printer_model", model)
  826. if target_printer_id is not None:
  827. await set_setting(db, "virtual_printer_target_printer_id", str(target_printer_id))
  828. if remote_interface_ip is not None:
  829. await set_setting(db, "virtual_printer_remote_interface_ip", remote_interface_ip)
  830. if tailscale_disabled is not None:
  831. await set_setting(db, "virtual_printer_tailscale_disabled", "true" if tailscale_disabled else "false")
  832. # Propagate tailscale_disabled to the first VirtualPrinter row so sync_from_db() picks it up
  833. if tailscale_disabled is not None:
  834. from backend.app.models.virtual_printer import VirtualPrinter as VPModel
  835. vp_result = await db.execute(select(VPModel).order_by(VPModel.position).limit(1))
  836. first_vp = vp_result.scalar_one_or_none()
  837. if first_vp is not None:
  838. first_vp.tailscale_disabled = new_ts_disabled
  839. await db.commit()
  840. db.expire_all()
  841. # Reconfigure virtual printer
  842. try:
  843. await virtual_printer_manager.configure(
  844. enabled=new_enabled,
  845. access_code=new_access_code,
  846. mode=new_mode,
  847. model=new_model,
  848. target_printer_ip=target_printer_ip,
  849. target_printer_serial=target_printer_serial,
  850. remote_interface_ip=new_remote_iface,
  851. )
  852. except ValueError as e:
  853. logger.warning("Virtual printer configuration validation error: %s", e)
  854. return JSONResponse(
  855. status_code=400,
  856. content={"detail": "Invalid virtual printer configuration. Check the provided values."},
  857. )
  858. except Exception as e:
  859. logger.error("Failed to configure virtual printer: %s", e, exc_info=True)
  860. return JSONResponse(
  861. status_code=500,
  862. content={"detail": "Failed to configure virtual printer. Check server logs for details."},
  863. )
  864. return await get_virtual_printer_settings(db)
  865. # =============================================================================
  866. # MQTT Relay Settings
  867. # =============================================================================
  868. @router.get("/mqtt/status")
  869. async def get_mqtt_status(
  870. _: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_READ),
  871. ):
  872. """Get MQTT relay connection status."""
  873. from backend.app.services.mqtt_relay import mqtt_relay
  874. return mqtt_relay.get_status()