// services/copyparty.js import { spawn } from 'node:child_process'; let processHandle = null; export function startCopyparty(config) { const cp = config.copyparty; if (!cp?.enabled || processHandle) return; const mountPoint = config.system.mountPoint; // volflag :c,hist=/tmp keeps sqlite write hits off the flash drive and sd card const volSpec = `${mountPoint}:${cp.volumeName}:${cp.permissions}:c,hist=/tmp/copyparty`; const args = [ cp.binaryPath, '-p', String(cp.port), '-a', `${cp.user}:${cp.password}`, '-v', volSpec, ...(cp.extraArgs || []) ]; console.log(`[COPYPARTY] Starting server on port ${cp.port}...`); processHandle = spawn('python3', args, { stdio: 'ignore', // swap to 'inherit' if debugging startup flags detached: false }); processHandle.on('error', (err) => { console.error('[COPYPARTY] Failed to spawn:', err.message); processHandle = null; }); processHandle.on('exit', (code) => { console.log(`[COPYPARTY] Process exited (code ${code})`); processHandle = null; }); } export function stopCopyparty() { if (!processHandle) return; console.log('[COPYPARTY] Stopping server...'); processHandle.kill('SIGTERM'); processHandle = null; }