refactor: clean up comments and unused code in various files

This commit is contained in:
bobbert committed 2026-04-14 19:05:22 +01:00
1 parent 9b20804f5a
commit bb6bd0f4b3
28 files changed
+14 -392

No files matched your search

+2 -41
View File
@@ -14,17 +14,12 @@ const execFileAsync = util.promisify(execFile);
const SSH_OPTIONS = ['-o', 'StrictHostKeyChecking=no', '-o', 'UserKnownHostsFile=/dev/null'];
// Read the default NAS photo directory from the config file
const NAS_CONFIG_PATH = path.join(__dirname, 'nas_config.txt');
let DEFAULT_PHOTO_DIR = '/home/user/photos';
try {
const raw = fs.readFileSync(NAS_CONFIG_PATH, 'utf8').trim();
if (raw) DEFAULT_PHOTO_DIR = raw.split('\n')[0].trim();
} catch (_) {
// Config file missing — keep the hard-coded default
}
// SSH/SFTP shell helpers
} catch (_) {}
function runCommand(cmd, args, options = {}) {
return execFileAsync(cmd, args, {
@@ -66,8 +61,6 @@ async function runSftpBatch(conn, batchCommands) {
}
}
// Express setup
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
@@ -82,8 +75,6 @@ app.use((req, res, next) => {
next();
});
// JWT verification middleware
function verifyToken(req, res, next) {
const authHeader = req.headers.authorization;
const token = authHeader?.split(' ')[1];
@@ -100,10 +91,6 @@ function verifyToken(req, res, next) {
}
}
// ─── REST Endpoints ──────────────────────────────────────────────
// Health check
app.get('/api/health', (req, res) => {
res.json({
status: 'ok',
@@ -113,14 +100,10 @@ app.get('/api/health', (req, res) => {
});
});
// Return the current NAS photo directory read from nas_config.txt
app.get('/api/config/photo-dir', (req, res) => {
res.json({ photoDir: DEFAULT_PHOTO_DIR });
});
// Establish an SFTP connection (stores credentials, returns a JWT)
app.post('/api/sftp/connect', (req, res) => {
const { host, port = 22, username, password } = req.body;
if (!host || !username || !password) {
@@ -141,8 +124,6 @@ app.post('/api/sftp/connect', (req, res) => {
});
});
// List directory contents
app.post('/api/sftp/list', verifyToken, async (req, res) => {
const { path: dirPath = DEFAULT_PHOTO_DIR } = req.body;
const conn = req.connection;
@@ -170,8 +151,6 @@ app.post('/api/sftp/list', verifyToken, async (req, res) => {
}
});
// Download a file from the NAS
app.get('/api/sftp/download', verifyToken, async (req, res) => {
const { path: filePath } = req.query;
if (!filePath) return res.status(400).json({ error: 'Missing path parameter' });
@@ -183,7 +162,7 @@ app.get('/api/sftp/download', verifyToken, async (req, res) => {
try {
await runCommand('scp', buildScpArgs(conn, `${conn.username}@${conn.host}:${filePath}`, tempFilePath));
res.download(tempFilePath, filename, () => {
try { fs.unlinkSync(tempFilePath); } catch (_) { /* already cleaned */ }
try { fs.unlinkSync(tempFilePath); } catch (_) {}
});
} catch (err) {
console.error('Download error:', err.message);
@@ -191,8 +170,6 @@ app.get('/api/sftp/download', verifyToken, async (req, res) => {
}
});
// Upload a file to the NAS
app.post('/api/sftp/upload', verifyToken, async (req, res) => {
const { remotePath } = req.body;
if (!remotePath || !req.files?.file) {
@@ -218,8 +195,6 @@ app.post('/api/sftp/upload', verifyToken, async (req, res) => {
}
});
// Create a directory on the NAS
app.post('/api/sftp/mkdir', verifyToken, async (req, res) => {
const { path: dirPath } = req.body;
if (!dirPath) return res.status(400).json({ error: 'Missing path parameter' });
@@ -234,8 +209,6 @@ app.post('/api/sftp/mkdir', verifyToken, async (req, res) => {
}
});
// Delete a file on the NAS
app.post('/api/sftp/delete', verifyToken, async (req, res) => {
const { path: filePath } = req.body;
if (!filePath) return res.status(400).json({ error: 'Missing path parameter' });
@@ -250,18 +223,12 @@ app.post('/api/sftp/delete', verifyToken, async (req, res) => {
}
});
// Disconnect from the NAS
app.post('/api/sftp/disconnect', verifyToken, (req, res) => {
connections.delete(req.connectionId);
console.log(`Disconnected: ${req.connectionId}`);
res.json({ success: true, message: 'Disconnected from SFTP server' });
});
// ─── Google Photos → NAS Proxy Endpoints ─────────────────────────
// Fetch media items from Google Photos on behalf of the mobile client
app.post('/api/google-photos/fetch', verifyToken, async (req, res) => {
const { googleAccessToken, pageSize = 50, pageToken } = req.body;
if (!googleAccessToken) {
@@ -302,8 +269,6 @@ app.post('/api/google-photos/fetch', verifyToken, async (req, res) => {
}
});
// Download a Google Photos item and save it directly to the NAS
app.post('/api/google-photos/download-to-nas', verifyToken, async (req, res) => {
const { googleAccessToken, baseUrl, filename, remotePath } = req.body;
const conn = req.connection;
@@ -337,14 +302,10 @@ app.post('/api/google-photos/download-to-nas', verifyToken, async (req, res) =>
}
});
// Sync status (placeholder for future batch sync)
app.post('/api/google-photos/sync-status', verifyToken, (req, res) => {
res.json({ success: true, status: 'idle', message: 'No active sync in progress', lastSync: null });
});
// ─── Lifecycle ───────────────────────────────────────────────────
process.on('SIGINT', () => {
console.log('\nShutting down...');
connections.forEach((_, id) => connections.delete(id));