BlueGit

timfa filemanager

Files

filemanager / server / config.js

config.js

Add scheduled restart backend
timfa Tim Falken · pushed by timfa · 5 months ago
9e526bef History
config.js 3.3 KiB Download Raw
1'use strict';
2
3const fs = require('fs');
4const path = require('path');
5
6const CONFIG_PATH = path.join(__dirname, '../config.json');
7
8const DEFAULT_SCHEDULED_RESTART = {
9 enabled: false,
10 cadence: 'daily',
11 time: '04:00',
12};
13
14function normalizeScheduledRestart (value)
15{
16 const raw = value && typeof value === 'object' ? value : {};
17 const cadence = String(raw.cadence || '').trim().toLowerCase();
18 const allowedCadences = new Set(['daily', 'every3days', 'weekly', 'biweekly', 'monthly']);
19 const time = String(raw.time || '').trim();
20
21 return {
22 enabled: !!raw.enabled,
23 cadence: allowedCadences.has(cadence) ? cadence : DEFAULT_SCHEDULED_RESTART.cadence,
24 time: /^([01]\d|2[0-3]):([0-5]\d)$/.test(time) ? time : DEFAULT_SCHEDULED_RESTART.time,
25 };
26}
27
28function loadConfig ()
29{
30 try
31 {
32 const raw = fs.readFileSync(CONFIG_PATH, 'utf8');
33 const parsed = JSON.parse(raw);
34 if (!parsed || typeof parsed !== 'object')
35 {
36 return { port: 3000, paths: [], rootAliases: {} };
37 }
38 if (!parsed.rootAliases || typeof parsed.rootAliases !== 'object')
39 {
40 parsed.rootAliases = {};
41 }
42 parsed.scheduledRestart = normalizeScheduledRestart(parsed.scheduledRestart);
43 return parsed;
44 } catch
45 {
46 const defaults = { port: 3000, paths: [], rootAliases: {}, scheduledRestart: { ...DEFAULT_SCHEDULED_RESTART } };
47 fs.writeFileSync(CONFIG_PATH, JSON.stringify(defaults, null, 2), 'utf8');
48 return defaults;
49 }
50}
51
52function saveConfig (config)
53{
54 const dir = path.dirname(CONFIG_PATH);
55 const tmp = path.join(dir, '.config.json.tmp');
56 const payload = JSON.stringify(config, null, 2);
57
58 // Atomic replace to avoid partially-written config files.
59 fs.writeFileSync(tmp, payload, 'utf8');
60 fs.renameSync(tmp, CONFIG_PATH);
61}
62
63function normalizeForCompare (p)
64{
65 const resolved = path.resolve(p);
66 // Windows paths are case-insensitive for normal filesystems.
67 return process.platform === 'win32' ? resolved.toLowerCase() : resolved;
68}
69
70/**
71 * Returns the de-duplicated, overlap-filtered list of configured paths.
72 * A child path is dropped when a parent path is also configured.
73 *
74 * Works on both Windows and Linux paths.
75 */
76function getEffectivePaths (rawPaths)
77{
78 if (!Array.isArray(rawPaths)) return [];
79
80 const sep = path.sep;
81 const normalized = rawPaths.map(p => path.resolve(p));
82 const unique = [...new Set(normalized)];
83
84 return unique.filter(p =>
85 {
86 const cmpP = normalizeForCompare(p);
87 // Keep p only if no OTHER path in the list is a strict parent of p
88 return !unique.some(other =>
89 {
90 if (other === p) return false;
91 const cmpOther = normalizeForCompare(other);
92 return cmpP.startsWith(cmpOther + sep);
93 });
94 });
95}
96
97/**
98 * Checks whether `filePath` is safely inside one of the `allowedRoots`.
99 * Returns the matching root or null.
100 */
101function getAllowedRoot (filePath, allowedRoots)
102{
103 const resolved = path.resolve(filePath);
104 const cmpResolved = normalizeForCompare(resolved);
105 const sep = path.sep;
106
107 for (const root of allowedRoots)
108 {
109 const resolvedRoot = path.resolve(root);
110 const cmpRoot = normalizeForCompare(resolvedRoot);
111 if (cmpResolved === cmpRoot || cmpResolved.startsWith(cmpRoot + sep))
112 {
113 return resolvedRoot;
114 }
115 }
116 return null;
117}
118
119module.exports = { loadConfig, saveConfig, getEffectivePaths, getAllowedRoot };