BlueGit

timfa filemanager

Files

filemanager / server / api / v1.js

v1.js

v1.js 33.7 KiB Download Raw
1'use strict';
2
3// Public, versioned file API for sync clients (phones, desktops, services).
4// Auth: Bearer access tokens from /oauth/token or personal access tokens.
5// Every request is checked with the same permission helpers as the web UI,
6// so a token can never see or change more than its user.
7
8const express = require('express');
9const fs = require('fs');
10const path = require('path');
11const crypto = require('crypto');
12const { pipeline } = require('stream/promises');
13const { Transform } = require('stream');
14
15const store = require('./store');
16const syncIndex = require('./sync-index');
17const { getDb } = require('../db');
18const { getClientIp } = require('../auth');
19const { logError } = require('../logger');
20const filesRouter = require('../routes/files');
21const { deleteIndexUnder, rewriteIndexPaths, reindexDirectoryTree } = require('../search-index');
22const { enqueueFileHash } = require('../file-hash-queue');
23
24const helpers = filesRouter.helpers;
25const router = express.Router();
26const json = express.json({ limit: '256kb' });
27
28const USER_ROOT_ID = '~';
29const GB = 1024 ** 3;
30const MB = 1024 ** 2;
31const DEFAULTS = {
32 maxUploadBytes: 10 * GB,
33 maxChunkBytes: 64 * MB,
34 maxOpenUploadsPerUser: 20,
35 uploadTtlHours: 24,
36 maxHashBytes: 4 * GB,
37 scanIntervalSeconds: 15,
38 maxSyncEntries: 500000,
39};
40const API_UPLOAD_DIR_DEFAULT = path.join(__dirname, '../../api-uploads');
41
42// ---------------------------------------------------------------------------
43// Helpers
44// ---------------------------------------------------------------------------
45
46class ApiError extends Error
47{
48 constructor (status, code, message, extra)
49 {
50 super(message);
51 this.status = status;
52 this.code = code;
53 this.extra = extra;
54 }
55}
56
57function apiConfig (config)
58{
59 const raw = config && typeof config.api === 'object' && config.api ? config.api : {};
60 const out = { ...DEFAULTS };
61 for (const k of Object.keys(DEFAULTS))
62 {
63 const v = Number(raw[k]);
64 if (Number.isFinite(v) && v > 0) out[k] = v;
65 }
66 out.uploadDir = typeof raw.uploadDir === 'string' && raw.uploadDir.trim() ? raw.uploadDir.trim() : API_UPLOAD_DIR_DEFAULT;
67 return out;
68}
69
70function sendError (res, err)
71{
72 if (!(err instanceof ApiError))
73 {
74 logError('API v1 request failed', err);
75 err = new ApiError(500, 'internal_error', 'Internal server error.');
76 }
77 if (res.headersSent) { res.destroy(); return; }
78 res.status(err.status).json({ error: err.code, message: err.message, ...(err.extra || {}) });
79}
80
81const wrap = (fn) => (req, res) => Promise.resolve(fn(req, res)).catch((err) => sendError(res, err));
82
83function parseEtag (value)
84{
85 const v = String(value || '').trim();
86 if (!v) return null;
87 return v.replace(/^W\//, '').replace(/^"|"$/g, '');
88}
89
90function loadApiUser (username)
91{
92 const row = getDb().prepare('SELECT id, username, is_admin, permissions FROM users WHERE username = ?').get([username]);
93 if (!row) return null;
94 let permissions = {};
95 try { permissions = JSON.parse(row.permissions || '{}'); } catch { permissions = {}; }
96 return { id: row.id, username: row.username, isAdmin: row.is_admin === 1, permissions };
97}
98
99function authenticate (req, res, next)
100{
101 const header = req.get('authorization') || '';
102 const token = header.startsWith('Bearer ') ? header.slice(7).trim() : '';
103 const resolved = store.resolveBearer(token);
104 const user = resolved ? loadApiUser(resolved.grant.username) : null;
105 if (!resolved || !user)
106 {
107 res.set('WWW-Authenticate', 'Bearer error="invalid_token"');
108 return res.status(401).json({ error: 'invalid_token', message: 'Missing, invalid, expired or revoked access token.' });
109 }
110 store.touchGrant(resolved.grant.id, getClientIp(req));
111 req.user = user;
112 req.apiGrant = resolved.grant;
113 req.apiScopes = resolved.scopes;
114 next();
115}
116
117function requireScope (scope)
118{
119 return (req, res, next) =>
120 {
121 if (!req.apiScopes.includes(scope))
122 {
123 res.set('WWW-Authenticate', `Bearer error="insufficient_scope", scope="${scope}"`);
124 return res.status(403).json({ error: 'insufficient_scope', message: `This token lacks the ${scope} scope.` });
125 }
126 next();
127 };
128}
129
130/** Roots visible to the user, keyed by a stable id used in virtual paths. */
131function rootsForUser (user)
132{
133 const { config, roots } = helpers.loadConfigAndRoots();
134 const aliases = config.rootAliases && typeof config.rootAliases === 'object' ? config.rootAliases : {};
135 const list = [];
136 const userFolder = helpers.getUserFolderPath(config, user.username);
137 if (userFolder)
138 {
139 try { fs.mkdirSync(userFolder, { recursive: true }); } catch { /* non-fatal */ }
140 list.push({ id: USER_ROOT_ID, name: 'My Folder', abs: path.resolve(userFolder), writable: true, isUserFolder: true });
141 }
142 const seen = new Set(list.map((r) => r.id));
143 for (const r of roots)
144 {
145 const label = path.basename(r);
146 if (!helpers.canRead(user, label) || seen.has(label)) continue;
147 const info = helpers.resolveRootForUser(r, roots, user, config);
148 seen.add(label);
149 list.push({
150 id: label,
151 name: String(aliases[path.resolve(r)] || '').trim() || label,
152 abs: path.resolve(r),
153 writable: !!(info && helpers.canWriteInfo(user, info)),
154 isUserFolder: false,
155 });
156 }
157 return { config, roots, list };
158}
159
160function validSegment (s)
161{
162 return s && s !== '.' && s !== '..' && !s.includes('\0') && !s.includes('\\') && Buffer.byteLength(s) <= 255;
163}
164
165/**
166 * Resolve a virtual path ("/<root>/a/b") for the user. Throws ApiError.
167 * Returns { root, rel, abs, vpath, config, info }.
168 */
169function resolvePath (user, vpath, { write = false, ctx } = {})
170{
171 const raw = String(vpath == null ? '' : vpath);
172 if (!raw.startsWith('/')) throw new ApiError(400, 'invalid_path', 'Paths must start with /<root-id>/.');
173 if (raw.length > 4096) throw new ApiError(400, 'invalid_path', 'Path too long.');
174 const parts = raw.split('/').slice(1).filter((p, i, arr) => !(p === '' && i === arr.length - 1));
175 if (!parts.length || parts.some((p) => !validSegment(p))) throw new ApiError(400, 'invalid_path', 'Invalid path segment.');
176 const c = ctx || rootsForUser(user);
177 const root = c.list.find((r) => r.id === parts[0]);
178 if (!root) throw new ApiError(404, 'not_found', 'No such root.');
179 const rel = parts.slice(1).join('/');
180 const abs = rel ? path.join(root.abs, ...parts.slice(1)) : root.abs;
181
182 // Same checks as the web UI, so the API can never exceed the user's rights.
183 const info = helpers.resolveRootForUser(abs, c.roots, user, c.config);
184 if (!info || !helpers.canReadInfo(user, info)) throw new ApiError(404, 'not_found', 'Not found.');
185 if (write && !helpers.canWriteInfo(user, info)) throw new ApiError(403, 'forbidden', 'You do not have write access here.');
186
187 // Refuse paths that escape the root through symlinks.
188 let probe = abs;
189 while (probe !== root.abs && !fs.existsSync(probe)) probe = path.dirname(probe);
190 try
191 {
192 const realRoot = fs.realpathSync(root.abs);
193 const realProbe = fs.realpathSync(probe);
194 if (realProbe !== realRoot && !realProbe.startsWith(realRoot + path.sep)) throw new ApiError(404, 'not_found', 'Not found.');
195 } catch (err)
196 {
197 if (err instanceof ApiError) throw err;
198 }
199
200 return { root, rel, abs, vpath: '/' + [root.id, ...parts.slice(1)].join('/'), config: c.config, info, ctx: c };
201}
202
203function lstatOrNull (abs)
204{
205 try { return fs.lstatSync(abs); } catch { return null; }
206}
207
208function knownSha256 (abs, stat)
209{
210 const row = getDb().prepare('SELECT size, mtime_ms, sha256 FROM api_hashes WHERE abs_path = ?').get([abs]);
211 if (row && Number(row.size) === stat.size && Number(row.mtime_ms) === Math.floor(stat.mtimeMs)) return row.sha256;
212 return null;
213}
214
215function rememberSha256 (abs, stat, sha)
216{
217 getDb().prepare(`INSERT INTO api_hashes (abs_path, size, mtime_ms, sha256) VALUES (?, ?, ?, ?)
218 ON CONFLICT(abs_path) DO UPDATE SET size = excluded.size, mtime_ms = excluded.mtime_ms, sha256 = excluded.sha256`)
219 .run([abs, stat.size, Math.floor(stat.mtimeMs), sha]);
220}
221
222async function computeSha256 (abs)
223{
224 const h = crypto.createHash('sha256');
225 await pipeline(fs.createReadStream(abs), h);
226 return h.digest('hex');
227}
228
229function entryJson (rootId, rel, stat, abs)
230{
231 const e = syncIndex.entryFromStat(rel, stat);
232 const out = {
233 id: e.file_id,
234 path: '/' + (rel ? `${rootId}/${rel}` : rootId),
235 name: rel ? path.posix.basename(rel) : rootId,
236 type: e.is_dir ? 'folder' : 'file',
237 size: e.size,
238 mtime: new Date(e.mtime_ms).toISOString(),
239 mtime_ms: e.mtime_ms,
240 version: e.version,
241 };
242 if (!e.is_dir) out.sha256 = abs ? knownSha256(abs, stat) : null;
243 return out;
244}
245
246function currentEntry (p)
247{
248 const st = lstatOrNull(p.abs);
249 return st ? entryJson(p.root.id, p.rel, st, p.abs) : null;
250}
251
252/**
253 * Optimistic concurrency for writes to `p`:
254 * - If-None-Match: * -> target must not exist (412)
255 * - If-Match: <ver> -> target must exist with that version (412)
256 * - neither -> 409 when the target exists, unless overwrite=true
257 */
258function checkPreconditions (p, { ifMatch, ifNoneMatch, overwrite })
259{
260 const st = lstatOrNull(p.abs);
261 const current = st ? entryJson(p.root.id, p.rel, st, p.abs) : null;
262 if (ifNoneMatch === '*' && st) throw new ApiError(412, 'precondition_failed', 'Target already exists.', { current });
263 if (ifMatch)
264 {
265 if (!st) throw new ApiError(412, 'precondition_failed', 'Target no longer exists.', { current: null });
266 if (ifMatch !== '*' && ifMatch !== current.version) throw new ApiError(412, 'precondition_failed', 'Target was changed by someone else.', { current });
267 }
268 if (st && st.isDirectory()) throw new ApiError(409, 'conflict', 'A folder exists at this path.', { current });
269 if (st && !ifMatch && !overwrite) throw new ApiError(409, 'conflict', 'Target exists. Send If-Match with its version, or overwrite=true.', { current });
270 return st;
271}
272
273function requireParentFolder (p)
274{
275 const parent = path.dirname(p.abs);
276 const st = lstatOrNull(parent);
277 if (!st || !st.isDirectory()) throw new ApiError(409, 'parent_missing', 'Parent folder does not exist.');
278}
279
280function boolParam (v)
281{
282 return v === true || v === 'true' || v === '1' || v === 1;
283}
284
285function parseMtime (v)
286{
287 if (v == null || v === '') return null;
288 const n = Number(v);
289 if (Number.isFinite(n) && n > 0) return n;
290 const d = Date.parse(String(v));
291 if (Number.isFinite(d)) return d;
292 throw new ApiError(400, 'invalid_request', 'mtime must be epoch milliseconds or an ISO date.');
293}
294
295function recordUploadMeta (abs, username)
296{
297 const db = getDb();
298 const uploadedAt = new Date().toISOString();
299 const existing = db.prepare('SELECT file_path FROM file_meta WHERE file_path = ?').get([abs]);
300 if (existing)
301 {
302 db.prepare('UPDATE file_meta SET uploader = ?, sha256 = NULL, uploaded_at = ? WHERE file_path = ?').run([username, uploadedAt, abs]);
303 } else
304 {
305 db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at, sha256) VALUES (?, ?, ?, ?, ?, ?)')
306 .run([abs, username, '[]', '{}', uploadedAt, null]);
307 }
308 try { enqueueFileHash(abs); } catch { /* best effort */ }
309 Promise.resolve().then(() => reindexDirectoryTree(path.dirname(abs))).catch(() => {});
310}
311
312function tempPathIn (dir)
313{
314 return path.join(dir, `${syncIndex.TEMP_PREFIX}${crypto.randomBytes(8).toString('hex')}.part`);
315}
316
317function limitStream (max, onTooBig)
318{
319 let total = 0;
320 return new Transform({
321 transform (chunk, enc, cb)
322 {
323 total += chunk.length;
324 if (total > max) return cb(onTooBig());
325 cb(null, chunk);
326 },
327 });
328}
329
330/** Move a finished temp file into place atomically and apply mtime. */
331function finalizeFile (tempAbs, p, mtimeMs)
332{
333 if (mtimeMs)
334 {
335 const t = new Date(mtimeMs);
336 fs.utimesSync(tempAbs, t, t);
337 }
338 fs.renameSync(tempAbs, p.abs);
339}
340
341function checkQuota (p, addedBytes)
342{
343 if (!p.info || !p.info.isUserFolder) return;
344 const msg = helpers.checkUserFolderSizeLimit(p.config, p.reqUsername, addedBytes);
345 if (msg) throw new ApiError(507, 'quota_exceeded', msg);
346}
347
348// ---------------------------------------------------------------------------
349// Routes
350// ---------------------------------------------------------------------------
351
352router.use((req, res, next) =>
353{
354 res.set('Access-Control-Allow-Origin', '*');
355 res.set('Access-Control-Allow-Headers', 'Authorization, Content-Type, If-Match, If-None-Match, Range, If-Range, Upload-Offset, X-Mtime');
356 res.set('Access-Control-Allow-Methods', 'GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS');
357 res.set('Access-Control-Expose-Headers', 'ETag, Content-Range, Accept-Ranges, Upload-Offset, Location, Content-Length');
358 if (req.method === 'OPTIONS') return res.status(204).end();
359 next();
360});
361
362router.get('/', (req, res) =>
363{
364 res.json({ name: 'filemanager', api: 'v1', openapi: '/api/v1/openapi.json', oauth: '/.well-known/oauth-authorization-server' });
365});
366
367router.get('/openapi.json', (req, res) =>
368{
369 res.sendFile(path.join(__dirname, '../../docs/openapi.json'));
370});
371
372router.use(authenticate);
373
374router.get('/me', (req, res) =>
375{
376 const { config, list } = rootsForUser(req.user);
377 const a = apiConfig(config);
378 res.json({
379 username: req.user.username,
380 scopes: req.apiScopes,
381 grant: { id: req.apiGrant.id, kind: req.apiGrant.kind, name: req.apiGrant.name },
382 roots: list.map((r) => ({ id: r.id, name: r.name, writable: r.writable && req.apiScopes.includes('files:write') })),
383 limits: { maxUploadBytes: a.maxUploadBytes, maxChunkBytes: a.maxChunkBytes, maxListLimit: 1000, maxDeltaLimit: 1000 },
384 trash: false,
385 });
386});
387
388router.get('/roots', requireScope('files:read'), (req, res) =>
389{
390 const { list } = rootsForUser(req.user);
391 res.json({ roots: list.map((r) => ({ id: r.id, name: r.name, path: '/' + r.id, writable: r.writable && req.apiScopes.includes('files:write') })) });
392});
393
394router.get('/files/metadata', requireScope('files:read'), wrap(async (req, res) =>
395{
396 const p = resolvePath(req.user, req.query.path);
397 const st = lstatOrNull(p.abs);
398 if (!st || st.isSymbolicLink()) throw new ApiError(404, 'not_found', 'Not found.');
399 if (boolParam(req.query.hash) && st.isFile() && !knownSha256(p.abs, st))
400 {
401 const a = apiConfig(p.config);
402 if (st.size > a.maxHashBytes) throw new ApiError(413, 'too_large', `Hashing on demand is limited to ${a.maxHashBytes} bytes.`);
403 const sha = await computeSha256(p.abs);
404 const st2 = lstatOrNull(p.abs);
405 if (st2 && st2.size === st.size && st2.mtimeMs === st.mtimeMs) rememberSha256(p.abs, st2, sha);
406 }
407 const e = currentEntry(p);
408 res.set('ETag', `"${e.version}"`);
409 res.json(e);
410}));
411
412router.get('/files/list', requireScope('files:read'), wrap(async (req, res) =>
413{
414 const p = resolvePath(req.user, req.query.path);
415 const st = lstatOrNull(p.abs);
416 if (!st) throw new ApiError(404, 'not_found', 'Not found.');
417 if (!st.isDirectory()) throw new ApiError(400, 'not_a_folder', 'Path is not a folder.');
418 const limit = Math.max(1, Math.min(1000, Number(req.query.limit) || 500));
419 const offset = Math.max(0, Number(req.query.page_token) || 0);
420 const names = (await fs.promises.readdir(p.abs)).filter((n) => !syncIndex.isIgnoredName(n)).sort();
421 const page = names.slice(offset, offset + limit);
422 const entries = [];
423 for (const n of page)
424 {
425 const childAbs = path.join(p.abs, n);
426 const cst = lstatOrNull(childAbs);
427 if (!cst || cst.isSymbolicLink() || (!cst.isFile() && !cst.isDirectory())) continue;
428 entries.push(entryJson(p.root.id, p.rel ? `${p.rel}/${n}` : n, cst, childAbs));
429 }
430 const next = offset + limit < names.length ? String(offset + limit) : null;
431 res.json({ folder: currentEntry(p), entries, next_page_token: next });
432}));
433
434function downloadHandler (req, res)
435{
436 let p;
437 try { p = resolvePath(req.user, req.query.path); } catch (err) { return sendError(res, err); }
438 const st = lstatOrNull(p.abs);
439 if (!st || !st.isFile()) return sendError(res, new ApiError(st ? 400 : 404, st ? 'not_a_file' : 'not_found', st ? 'Path is not a file.' : 'Not found.'));
440 const e = entryJson(p.root.id, p.rel, st, p.abs);
441 res.set('ETag', `"${e.version}"`);
442 res.set('X-File-Id', e.id);
443 if (e.sha256) res.set('Digest', `sha-256=${Buffer.from(e.sha256, 'hex').toString('base64')}`);
444 res.set('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(e.name)}`);
445 const inm = String(req.get('if-none-match') || '').split(',').map(parseEtag);
446 if (inm.includes(e.version) || inm.includes('*')) return res.status(304).end();
447 res.sendFile(p.abs, { dotfiles: 'allow', etag: false, lastModified: true, acceptRanges: true, cacheControl: false }, (err) =>
448 {
449 if (err && !res.headersSent) sendError(res, new ApiError(err.status || 500, 'download_failed', 'Download failed.'));
450 });
451}
452
453router.get('/files/content', requireScope('files:read'), downloadHandler);
454router.head('/files/content', requireScope('files:read'), downloadHandler);
455
456router.put('/files/content', requireScope('files:write'), wrap(async (req, res) =>
457{
458 const p = resolvePath(req.user, req.query.path, { write: true });
459 if (!p.rel) throw new ApiError(400, 'invalid_path', 'Cannot write to a root.');
460 p.reqUsername = req.user.username;
461 const a = apiConfig(p.config);
462 const pre = { ifMatch: parseEtag(req.get('if-match')), ifNoneMatch: parseEtag(req.get('if-none-match')), overwrite: boolParam(req.query.overwrite) };
463 const mtime = parseMtime(req.query.mtime || req.get('x-mtime'));
464 requireParentFolder(p);
465 checkPreconditions(p, pre);
466 const declared = req.get('content-length') != null ? Number(req.get('content-length')) : null;
467 if (declared != null && declared > a.maxUploadBytes) throw new ApiError(413, 'too_large', `Maximum upload size is ${a.maxUploadBytes} bytes; use /uploads for large files.`);
468 if (declared != null) checkQuota(p, declared);
469
470 const temp = tempPathIn(path.dirname(p.abs));
471 const hash = crypto.createHash('sha256');
472 let size = 0;
473 try
474 {
475 await pipeline(
476 req,
477 limitStream(a.maxUploadBytes, () => new ApiError(413, 'too_large', `Maximum upload size is ${a.maxUploadBytes} bytes.`)),
478 new Transform({ transform (chunk, enc, cb) { size += chunk.length; hash.update(chunk); cb(null, chunk); } }),
479 fs.createWriteStream(temp, { flags: 'wx' }),
480 );
481 const sha = hash.digest('hex');
482 const expected = req.get('x-content-sha256');
483 if (expected && expected.toLowerCase() !== sha) throw new ApiError(422, 'checksum_mismatch', 'Uploaded content does not match X-Content-SHA256.');
484 if (declared == null) checkQuota(p, size);
485 checkPreconditions(p, pre); // re-check right before the atomic replace
486 const existed = !!lstatOrNull(p.abs);
487 finalizeFile(temp, p, mtime);
488 const st = fs.lstatSync(p.abs);
489 rememberSha256(p.abs, st, sha);
490 recordUploadMeta(p.abs, req.user.username);
491 syncIndex.markDirty(p.root.abs);
492 const e = entryJson(p.root.id, p.rel, st, p.abs);
493 res.set('ETag', `"${e.version}"`);
494 res.status(existed ? 200 : 201).json(e);
495 } finally
496 {
497 fs.rmSync(temp, { force: true });
498 }
499}));
500
501router.post('/folders', requireScope('files:write'), json, wrap(async (req, res) =>
502{
503 const p = resolvePath(req.user, req.body && req.body.path, { write: true });
504 if (!p.rel) throw new ApiError(409, 'conflict', 'Root already exists.');
505 const st = lstatOrNull(p.abs);
506 if (st)
507 {
508 if (st.isDirectory() && boolParam(req.body.exist_ok)) return res.status(200).json(currentEntry(p));
509 throw new ApiError(409, 'conflict', 'Something already exists at this path.', { current: currentEntry(p) });
510 }
511 if (boolParam(req.body.parents))
512 {
513 fs.mkdirSync(p.abs, { recursive: true });
514 } else
515 {
516 requireParentFolder(p);
517 fs.mkdirSync(p.abs);
518 }
519 syncIndex.markDirty(p.root.abs);
520 res.status(201).json(currentEntry(p));
521}));
522
523router.post('/files/move', requireScope('files:write'), json, wrap(async (req, res) =>
524{
525 const body = req.body || {};
526 const ctx = rootsForUser(req.user);
527 const from = resolvePath(req.user, body.from, { write: true, ctx });
528 const to = resolvePath(req.user, body.to, { write: true, ctx });
529 if (!from.rel || !to.rel) throw new ApiError(400, 'invalid_path', 'Roots cannot be moved or replaced.');
530 const st = lstatOrNull(from.abs);
531 if (!st) throw new ApiError(404, 'not_found', 'Source not found.');
532 const ifMatch = parseEtag(body.if_match || req.get('if-match'));
533 const cur = entryJson(from.root.id, from.rel, st, from.abs);
534 if (ifMatch && ifMatch !== '*' && ifMatch !== cur.version) throw new ApiError(412, 'precondition_failed', 'Source was changed by someone else.', { current: cur });
535 if (from.abs === to.abs) return res.json(cur);
536 if (to.abs.startsWith(from.abs + path.sep)) throw new ApiError(400, 'invalid_path', 'Cannot move a folder into itself.');
537 requireParentFolder(to);
538 const dst = lstatOrNull(to.abs);
539 // Case-only renames on case-insensitive filesystems resolve to the same inode.
540 const sameNode = dst && dst.ino === st.ino && dst.dev === st.dev;
541 if (dst && !sameNode)
542 {
543 if (!boolParam(body.overwrite)) throw new ApiError(409, 'conflict', 'Destination exists.', { current: currentEntry(to) });
544 if (dst.isDirectory() || st.isDirectory()) throw new ApiError(409, 'conflict', 'Folders are never overwritten.', { current: currentEntry(to) });
545 }
546 if (to.info && to.info.isUserFolder && from.root.abs !== to.root.abs)
547 {
548 to.reqUsername = req.user.username;
549 checkQuota(to, st.isDirectory() ? 0 : st.size);
550 }
551 helpers.movePathWithFallback(from.abs, to.abs);
552 helpers.rewriteMetaPaths(from.abs, to.abs);
553 try { rewriteIndexPaths(from.abs, to.abs); } catch { /* best effort */ }
554 getDb().prepare('DELETE FROM api_hashes WHERE abs_path = ?').run([from.abs]);
555 syncIndex.markDirty(from.root.abs);
556 syncIndex.markDirty(to.root.abs);
557 res.json(currentEntry(to));
558}));
559
560router.delete('/files', requireScope('files:write'), wrap(async (req, res) =>
561{
562 const p = resolvePath(req.user, req.query.path, { write: true });
563 if (!p.rel) throw new ApiError(400, 'invalid_path', 'Roots cannot be deleted.');
564 if (p.ctx.roots.map((r) => path.resolve(r)).includes(path.resolve(p.abs))) throw new ApiError(400, 'invalid_path', 'Configured roots cannot be deleted.');
565 const st = lstatOrNull(p.abs);
566 if (!st) throw new ApiError(404, 'not_found', 'Not found.');
567 const ifMatch = parseEtag(req.get('if-match'));
568 const cur = entryJson(p.root.id, p.rel, st, p.abs);
569 if (ifMatch && ifMatch !== '*' && ifMatch !== cur.version) throw new ApiError(412, 'precondition_failed', 'Target was changed by someone else.', { current: cur });
570 if (st.isDirectory())
571 {
572 const children = fs.readdirSync(p.abs);
573 if (children.length && !boolParam(req.query.recursive)) throw new ApiError(409, 'folder_not_empty', 'Folder is not empty; pass recursive=true.');
574 fs.rmSync(p.abs, { recursive: true, force: true });
575 } else
576 {
577 fs.unlinkSync(p.abs);
578 }
579 getDb().prepare('DELETE FROM file_meta WHERE file_path = ? OR file_path LIKE ?').run([p.abs, p.abs + path.sep + '%']);
580 getDb().prepare('DELETE FROM api_hashes WHERE abs_path = ? OR abs_path LIKE ?').run([p.abs, p.abs + path.sep + '%']);
581 try { deleteIndexUnder(p.abs); } catch { /* best effort */ }
582 syncIndex.markDirty(p.root.abs);
583 res.status(204).end();
584}));
585
586// ---------------------------------------------------------------------------
587// Delta / change feed
588// ---------------------------------------------------------------------------
589
590function encodeCursor (obj)
591{
592 return Buffer.from(JSON.stringify(obj)).toString('base64url');
593}
594
595function decodeCursor (raw)
596{
597 try
598 {
599 const c = JSON.parse(Buffer.from(String(raw), 'base64url').toString('utf8'));
600 if (c && typeof c === 'object') return c;
601 } catch { /* fallthrough */ }
602 throw new ApiError(400, 'invalid_cursor', 'Malformed cursor.');
603}
604
605function rootTag (root)
606{
607 return crypto.createHash('sha256').update(root.abs).digest('base64url').slice(0, 12);
608}
609
610router.get('/delta', requireScope('files:read'), wrap(async (req, res) =>
611{
612 const ctx = rootsForUser(req.user);
613 let rootId = req.query.root;
614 let cursor = null;
615 if (req.query.cursor)
616 {
617 cursor = decodeCursor(req.query.cursor);
618 rootId = rootId || cursor.r;
619 }
620 const root = ctx.list.find((r) => r.id === rootId);
621 if (!root) throw new ApiError(404, 'not_found', 'Unknown root; pass root=<id> from /roots.');
622 if (cursor && (cursor.r !== root.id || cursor.k !== rootTag(root))) throw new ApiError(400, 'invalid_cursor', 'Cursor belongs to another root.');
623 const a = apiConfig(ctx.config);
624 const limit = Math.max(1, Math.min(1000, Number(req.query.limit) || 500));
625
626 let state;
627 try
628 {
629 state = await syncIndex.ensureFresh(root.abs, root.abs, { intervalMs: a.scanIntervalSeconds * 1000, maxEntries: a.maxSyncEntries });
630 } catch (err)
631 {
632 if (err && err.code === 'too_many_entries') throw new ApiError(507, 'too_many_entries', err.message);
633 throw err;
634 }
635
636 const toPath = (rel) => '/' + (rel ? `${root.id}/${rel}` : root.id);
637 const base = { r: root.id, k: rootTag(root), g: state.generation };
638
639 if (cursor && cursor.g !== state.generation) throw new ApiError(410, 'cursor_expired', 'Cursor is no longer valid; start again without a cursor.');
640
641 if (!cursor || cursor.m === 's')
642 {
643 const seq = cursor ? Number(cursor.s) : syncIndex.currentSeq();
644 const offset = cursor ? Number(cursor.o) || 0 : 0;
645 if (cursor && seq < state.min_seq) throw new ApiError(410, 'cursor_expired', 'Cursor is no longer valid; start again without a cursor.');
646 const rows = syncIndex.listEntries(root.abs, offset, limit);
647 const more = rows.length === limit;
648 return res.json({
649 reset: !cursor,
650 entries: rows.map((r) => ({
651 change: 'exists', id: r.file_id, path: toPath(r.rel_path), type: r.is_dir ? 'folder' : 'file',
652 size: r.size, mtime_ms: r.mtime_ms, version: r.version,
653 })),
654 cursor: encodeCursor(more ? { ...base, m: 's', s: seq, o: offset + rows.length } : { ...base, m: 'c', s: seq }),
655 has_more: more,
656 });
657 }
658
659 const after = Number(cursor.s) || 0;
660 if (after < state.min_seq) throw new ApiError(410, 'cursor_expired', 'Changes since this cursor were pruned; start again without a cursor.');
661 const rows = syncIndex.listChanges(root.abs, after, limit);
662 const last = rows.length ? rows[rows.length - 1].seq : after;
663 res.json({
664 reset: false,
665 entries: rows.map((r) => ({
666 change: r.change, id: r.file_id, path: toPath(r.rel_path),
667 old_path: r.old_rel_path != null ? toPath(r.old_rel_path) : undefined,
668 type: r.is_dir ? 'folder' : 'file',
669 size: r.size, mtime_ms: r.mtime_ms, version: r.version,
670 })),
671 cursor: encodeCursor({ ...base, m: 'c', s: last }),
672 has_more: rows.length === limit,
673 });
674}));
675
676// ---------------------------------------------------------------------------
677// Resumable uploads
678// ---------------------------------------------------------------------------
679
680function purgeExpiredUploads (uploadDir)
681{
682 const d = getDb();
683 const expired = d.prepare('SELECT id FROM api_uploads WHERE expires_at < ?').all([Date.now()]);
684 for (const r of expired)
685 {
686 fs.rmSync(path.join(uploadDir, `${r.id}.part`), { force: true });
687 d.prepare('DELETE FROM api_uploads WHERE id = ?').run([r.id]);
688 }
689}
690
691function getUploadFor (req, uploadDir)
692{
693 const id = String(req.params.id || '');
694 if (!/^[A-Za-z0-9_-]{10,64}$/.test(id)) throw new ApiError(404, 'not_found', 'Upload not found.');
695 const row = getDb().prepare('SELECT * FROM api_uploads WHERE id = ?').get([id]);
696 if (!row || row.username !== req.user.username || row.grant_id !== req.apiGrant.id || row.expires_at < Date.now())
697 {
698 throw new ApiError(404, 'not_found', 'Upload not found or expired.');
699 }
700 row.file = path.join(uploadDir, `${row.id}.part`);
701 return row;
702}
703
704function uploadJson (row)
705{
706 return { id: row.id, path: row.target, size: row.size, offset: row.received, expires_at: new Date(row.expires_at).toISOString() };
707}
708
709router.post('/uploads', requireScope('files:write'), json, wrap(async (req, res) =>
710{
711 const body = req.body || {};
712 const p = resolvePath(req.user, body.path, { write: true });
713 if (!p.rel) throw new ApiError(400, 'invalid_path', 'Cannot write to a root.');
714 p.reqUsername = req.user.username;
715 const a = apiConfig(p.config);
716 const size = Number(body.size);
717 if (!Number.isInteger(size) || size < 0) throw new ApiError(400, 'invalid_request', 'size (bytes) is required.');
718 if (size > a.maxUploadBytes) throw new ApiError(413, 'too_large', `Maximum upload size is ${a.maxUploadBytes} bytes.`);
719 const sha = body.sha256 ? String(body.sha256).toLowerCase() : null;
720 if (sha && !/^[0-9a-f]{64}$/.test(sha)) throw new ApiError(400, 'invalid_request', 'sha256 must be 64 hex characters.');
721 const pre = { ifMatch: parseEtag(body.if_match), ifNoneMatch: body.if_none_match === '*' ? '*' : null, overwrite: boolParam(body.overwrite) };
722 requireParentFolder(p);
723 checkPreconditions(p, pre);
724 checkQuota(p, size);
725
726 fs.mkdirSync(a.uploadDir, { recursive: true, mode: 0o700 });
727 purgeExpiredUploads(a.uploadDir);
728 const open = getDb().prepare('SELECT COUNT(*) AS n FROM api_uploads WHERE username = ?').get([req.user.username]).n;
729 if (open >= a.maxOpenUploadsPerUser) throw new ApiError(429, 'too_many_uploads', 'Too many unfinished uploads; finish or cancel some first.');
730
731 const id = crypto.randomBytes(18).toString('base64url');
732 const t = Date.now();
733 fs.writeFileSync(path.join(a.uploadDir, `${id}.part`), Buffer.alloc(0), { mode: 0o600 });
734 getDb().prepare(`INSERT INTO api_uploads (id, grant_id, username, target, size, received, sha256, mtime_ms, if_match, if_none_match, overwrite, created_at, expires_at)
735 VALUES (?, ?, ?, ?, ?, 0, ?, ?, ?, ?, ?, ?, ?)`)
736 .run([id, req.apiGrant.id, req.user.username, p.vpath, size, sha, parseMtime(body.mtime), pre.ifMatch, pre.ifNoneMatch ? 1 : 0, pre.overwrite ? 1 : 0, t, t + a.uploadTtlHours * 3600000]);
737 const row = getDb().prepare('SELECT * FROM api_uploads WHERE id = ?').get([id]);
738 res.set('Location', `/api/v1/uploads/${id}`);
739 res.set('Upload-Offset', '0');
740 res.status(201).json({ ...uploadJson(row), max_chunk_bytes: a.maxChunkBytes });
741}));
742
743router.get('/uploads/:id', requireScope('files:write'), wrap(async (req, res) =>
744{
745 const { config } = helpers.loadConfigAndRoots();
746 const row = getUploadFor(req, apiConfig(config).uploadDir);
747 res.set('Upload-Offset', String(row.received));
748 res.json(uploadJson(row));
749}));
750
751router.patch('/uploads/:id', requireScope('files:write'), wrap(async (req, res) =>
752{
753 const { config } = helpers.loadConfigAndRoots();
754 const a = apiConfig(config);
755 const row = getUploadFor(req, a.uploadDir);
756 const offset = Number(req.get('upload-offset'));
757 if (!Number.isInteger(offset)) throw new ApiError(400, 'invalid_request', 'Upload-Offset header is required.');
758 if (offset !== row.received)
759 {
760 res.set('Upload-Offset', String(row.received));
761 throw new ApiError(409, 'offset_mismatch', `Expected offset ${row.received}.`, { offset: row.received });
762 }
763 const remaining = row.size - row.received;
764 const max = Math.min(a.maxChunkBytes, remaining);
765 let written = 0;
766 try
767 {
768 await pipeline(
769 req,
770 limitStream(max, () => new ApiError(413, 'too_large', `Chunk too large (max ${max} bytes here).`)),
771 new Transform({ transform (chunk, enc, cb) { written += chunk.length; cb(null, chunk); } }),
772 fs.createWriteStream(row.file, { flags: 'r+', start: row.received }),
773 );
774 } catch (err)
775 {
776 // Keep only fully confirmed bytes; a client resumes from the stored offset.
777 fs.truncateSync(row.file, row.received);
778 throw err instanceof ApiError ? err : new ApiError(400, 'upload_interrupted', 'Chunk upload was interrupted.');
779 }
780 const received = row.received + written;
781 getDb().prepare('UPDATE api_uploads SET received = ? WHERE id = ?').run([received, row.id]);
782 res.set('Upload-Offset', String(received));
783 res.json({ ...uploadJson({ ...row, received }) });
784}));
785
786router.post('/uploads/:id/complete', requireScope('files:write'), wrap(async (req, res) =>
787{
788 const { config } = helpers.loadConfigAndRoots();
789 const a = apiConfig(config);
790 const row = getUploadFor(req, a.uploadDir);
791 if (row.received !== row.size) throw new ApiError(409, 'incomplete', `Only ${row.received} of ${row.size} bytes received.`, { offset: row.received });
792 const p = resolvePath(req.user, row.target, { write: true });
793 p.reqUsername = req.user.username;
794 const sha = await computeSha256(row.file);
795 if (row.sha256 && row.sha256 !== sha)
796 {
797 fs.rmSync(row.file, { force: true });
798 getDb().prepare('DELETE FROM api_uploads WHERE id = ?').run([row.id]);
799 throw new ApiError(422, 'checksum_mismatch', 'Uploaded content does not match sha256; upload discarded.');
800 }
801 requireParentFolder(p);
802 const pre = { ifMatch: row.if_match, ifNoneMatch: row.if_none_match ? '*' : null, overwrite: !!row.overwrite };
803 checkPreconditions(p, pre);
804 checkQuota(p, row.size);
805
806 const temp = tempPathIn(path.dirname(p.abs));
807 try
808 {
809 try { fs.renameSync(row.file, temp); } catch { fs.copyFileSync(row.file, temp); fs.rmSync(row.file, { force: true }); }
810 checkPreconditions(p, pre);
811 const existed = !!lstatOrNull(p.abs);
812 finalizeFile(temp, p, row.mtime_ms);
813 getDb().prepare('DELETE FROM api_uploads WHERE id = ?').run([row.id]);
814 const st = fs.lstatSync(p.abs);
815 rememberSha256(p.abs, st, sha);
816 recordUploadMeta(p.abs, req.user.username);
817 syncIndex.markDirty(p.root.abs);
818 const e = entryJson(p.root.id, p.rel, st, p.abs);
819 res.set('ETag', `"${e.version}"`);
820 res.status(existed ? 200 : 201).json(e);
821 } catch (err)
822 {
823 // Lost a race after staging (target changed meanwhile): discard; the client starts a new upload.
824 fs.rmSync(temp, { force: true });
825 getDb().prepare('DELETE FROM api_uploads WHERE id = ?').run([row.id]);
826 throw err;
827 }
828}));
829
830router.delete('/uploads/:id', requireScope('files:write'), wrap(async (req, res) =>
831{
832 const { config } = helpers.loadConfigAndRoots();
833 const row = getUploadFor(req, apiConfig(config).uploadDir);
834 fs.rmSync(row.file, { force: true });
835 getDb().prepare('DELETE FROM api_uploads WHERE id = ?').run([row.id]);
836 res.status(204).end();
837}));
838
839router.use((req, res) => res.status(404).json({ error: 'not_found', message: 'Unknown API endpoint.' }));
840
841router.ApiError = ApiError;
842module.exports = router;