'use strict'; // Public, versioned file API for sync clients (phones, desktops, services). // Auth: Bearer access tokens from /oauth/token or personal access tokens. // Every request is checked with the same permission helpers as the web UI, // so a token can never see or change more than its user. const express = require('express'); const fs = require('fs'); const path = require('path'); const crypto = require('crypto'); const { pipeline } = require('stream/promises'); const { Transform } = require('stream'); const store = require('./store'); const syncIndex = require('./sync-index'); const { getDb } = require('../db'); const { getClientIp } = require('../auth'); const { logError } = require('../logger'); const { publicBaseUrl } = require('../public-url'); const filesRouter = require('../routes/files'); const { deleteIndexUnder, rewriteIndexPaths, reindexDirectoryTree } = require('../search-index'); const { enqueueFileHash } = require('../file-hash-queue'); const helpers = filesRouter.helpers; const router = express.Router(); const json = express.json({ limit: '256kb' }); const USER_ROOT_ID = '~'; const GB = 1024 ** 3; const MB = 1024 ** 2; const DEFAULTS = { maxUploadBytes: 10 * GB, maxChunkBytes: 64 * MB, maxOpenUploadsPerUser: 20, uploadTtlHours: 24, maxHashBytes: 4 * GB, scanIntervalSeconds: 15, maxSyncEntries: 500000, }; const API_UPLOAD_DIR_DEFAULT = path.join(__dirname, '../../api-uploads'); // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- class ApiError extends Error { constructor (status, code, message, extra) { super(message); this.status = status; this.code = code; this.extra = extra; } } function apiConfig (config) { const raw = config && typeof config.api === 'object' && config.api ? config.api : {}; const out = { ...DEFAULTS }; for (const k of Object.keys(DEFAULTS)) { const v = Number(raw[k]); if (Number.isFinite(v) && v > 0) out[k] = v; } out.uploadDir = typeof raw.uploadDir === 'string' && raw.uploadDir.trim() ? raw.uploadDir.trim() : API_UPLOAD_DIR_DEFAULT; return out; } function sendError (res, err) { if (!(err instanceof ApiError)) { logError('API v1 request failed', err); err = new ApiError(500, 'internal_error', 'Internal server error.'); } if (res.headersSent) { res.destroy(); return; } res.status(err.status).json({ error: err.code, message: err.message, ...(err.extra || {}) }); } const wrap = (fn) => (req, res) => Promise.resolve(fn(req, res)).catch((err) => sendError(res, err)); function parseEtag (value) { const v = String(value || '').trim(); if (!v) return null; return v.replace(/^W\//, '').replace(/^"|"$/g, ''); } function loadApiUser (username) { const row = getDb().prepare('SELECT id, username, is_admin, permissions FROM users WHERE username = ?').get([username]); if (!row) return null; let permissions = {}; try { permissions = JSON.parse(row.permissions || '{}'); } catch { permissions = {}; } return { id: row.id, username: row.username, isAdmin: row.is_admin === 1, permissions }; } function authenticate (req, res, next) { const header = req.get('authorization') || ''; const token = header.startsWith('Bearer ') ? header.slice(7).trim() : ''; const resolved = store.resolveBearer(token); const user = resolved ? loadApiUser(resolved.grant.username) : null; if (!resolved || !user) { res.set('WWW-Authenticate', 'Bearer error="invalid_token"'); return res.status(401).json({ error: 'invalid_token', message: 'Missing, invalid, expired or revoked access token.' }); } store.touchGrant(resolved.grant.id, getClientIp(req)); req.user = user; req.apiGrant = resolved.grant; req.apiScopes = resolved.scopes; next(); } function requireScope (scope) { return (req, res, next) => { if (!req.apiScopes.includes(scope)) { res.set('WWW-Authenticate', `Bearer error="insufficient_scope", scope="${scope}"`); return res.status(403).json({ error: 'insufficient_scope', message: `This token lacks the ${scope} scope.` }); } next(); }; } /** Roots visible to the user, keyed by a stable id used in virtual paths. */ function rootsForUser (user) { const { config, roots } = helpers.loadConfigAndRoots(); const aliases = config.rootAliases && typeof config.rootAliases === 'object' ? config.rootAliases : {}; const list = []; const userFolder = helpers.getUserFolderPath(config, user.username); if (userFolder) { try { fs.mkdirSync(userFolder, { recursive: true }); } catch { /* non-fatal */ } list.push({ id: USER_ROOT_ID, name: 'My Folder', abs: path.resolve(userFolder), writable: true, isUserFolder: true }); } const seen = new Set(list.map((r) => r.id)); for (const r of roots) { const label = path.basename(r); if (!helpers.canRead(user, label) || seen.has(label)) continue; const info = helpers.resolveRootForUser(r, roots, user, config); seen.add(label); list.push({ id: label, name: String(aliases[path.resolve(r)] || '').trim() || label, abs: path.resolve(r), writable: !!(info && helpers.canWriteInfo(user, info)), isUserFolder: false, }); } return { config, roots, list }; } function validSegment (s) { return s && s !== '.' && s !== '..' && !s.includes('\0') && !s.includes('\\') && Buffer.byteLength(s) <= 255; } /** * Resolve a virtual path ("//a/b") for the user. Throws ApiError. * Returns { root, rel, abs, vpath, config, info }. */ function resolvePath (user, vpath, { write = false, ctx } = {}) { const raw = String(vpath == null ? '' : vpath); if (!raw.startsWith('/')) throw new ApiError(400, 'invalid_path', 'Paths must start with //.'); if (raw.length > 4096) throw new ApiError(400, 'invalid_path', 'Path too long.'); const parts = raw.split('/').slice(1).filter((p, i, arr) => !(p === '' && i === arr.length - 1)); if (!parts.length || parts.some((p) => !validSegment(p))) throw new ApiError(400, 'invalid_path', 'Invalid path segment.'); const c = ctx || rootsForUser(user); const root = c.list.find((r) => r.id === parts[0]); if (!root) throw new ApiError(404, 'not_found', 'No such root.'); const rel = parts.slice(1).join('/'); const abs = rel ? path.join(root.abs, ...parts.slice(1)) : root.abs; // Same checks as the web UI, so the API can never exceed the user's rights. const info = helpers.resolveRootForUser(abs, c.roots, user, c.config); if (!info || !helpers.canReadInfo(user, info)) throw new ApiError(404, 'not_found', 'Not found.'); if (write && !helpers.canWriteInfo(user, info)) throw new ApiError(403, 'forbidden', 'You do not have write access here.'); // Refuse paths that escape the root through symlinks. let probe = abs; while (probe !== root.abs && !fs.existsSync(probe)) probe = path.dirname(probe); try { const realRoot = fs.realpathSync(root.abs); const realProbe = fs.realpathSync(probe); if (realProbe !== realRoot && !realProbe.startsWith(realRoot + path.sep)) throw new ApiError(404, 'not_found', 'Not found.'); } catch (err) { if (err instanceof ApiError) throw err; } return { root, rel, abs, vpath: '/' + [root.id, ...parts.slice(1)].join('/'), config: c.config, info, ctx: c }; } function lstatOrNull (abs) { try { return fs.lstatSync(abs); } catch { return null; } } function knownSha256 (abs, stat) { const row = getDb().prepare('SELECT size, mtime_ms, sha256 FROM api_hashes WHERE abs_path = ?').get([abs]); if (row && Number(row.size) === stat.size && Number(row.mtime_ms) === Math.floor(stat.mtimeMs)) return row.sha256; return null; } function rememberSha256 (abs, stat, sha) { getDb().prepare(`INSERT INTO api_hashes (abs_path, size, mtime_ms, sha256) VALUES (?, ?, ?, ?) ON CONFLICT(abs_path) DO UPDATE SET size = excluded.size, mtime_ms = excluded.mtime_ms, sha256 = excluded.sha256`) .run([abs, stat.size, Math.floor(stat.mtimeMs), sha]); } async function computeSha256 (abs) { const h = crypto.createHash('sha256'); await pipeline(fs.createReadStream(abs), h); return h.digest('hex'); } function entryJson (rootId, rel, stat, abs) { const e = syncIndex.entryFromStat(rel, stat); const out = { id: e.file_id, path: '/' + (rel ? `${rootId}/${rel}` : rootId), name: rel ? path.posix.basename(rel) : rootId, type: e.is_dir ? 'folder' : 'file', size: e.size, mtime: new Date(e.mtime_ms).toISOString(), mtime_ms: e.mtime_ms, version: e.version, }; if (!e.is_dir) out.sha256 = abs ? knownSha256(abs, stat) : null; return out; } function currentEntry (p) { const st = lstatOrNull(p.abs); return st ? entryJson(p.root.id, p.rel, st, p.abs) : null; } /** * Optimistic concurrency for writes to `p`: * - If-None-Match: * -> target must not exist (412) * - If-Match: -> target must exist with that version (412) * - neither -> 409 when the target exists, unless overwrite=true */ function checkPreconditions (p, { ifMatch, ifNoneMatch, overwrite }) { const st = lstatOrNull(p.abs); const current = st ? entryJson(p.root.id, p.rel, st, p.abs) : null; if (ifNoneMatch === '*' && st) throw new ApiError(412, 'precondition_failed', 'Target already exists.', { current }); if (ifMatch) { if (!st) throw new ApiError(412, 'precondition_failed', 'Target no longer exists.', { current: null }); if (ifMatch !== '*' && ifMatch !== current.version) throw new ApiError(412, 'precondition_failed', 'Target was changed by someone else.', { current }); } if (st && st.isDirectory()) throw new ApiError(409, 'conflict', 'A folder exists at this path.', { current }); if (st && !ifMatch && !overwrite) throw new ApiError(409, 'conflict', 'Target exists. Send If-Match with its version, or overwrite=true.', { current }); return st; } function requireParentFolder (p) { const parent = path.dirname(p.abs); const st = lstatOrNull(parent); if (!st || !st.isDirectory()) throw new ApiError(409, 'parent_missing', 'Parent folder does not exist.'); } function boolParam (v) { return v === true || v === 'true' || v === '1' || v === 1; } function parseMtime (v) { if (v == null || v === '') return null; const n = Number(v); if (Number.isFinite(n) && n > 0) return n; const d = Date.parse(String(v)); if (Number.isFinite(d)) return d; throw new ApiError(400, 'invalid_request', 'mtime must be epoch milliseconds or an ISO date.'); } function recordUploadMeta (abs, username) { const db = getDb(); const uploadedAt = new Date().toISOString(); const existing = db.prepare('SELECT file_path FROM file_meta WHERE file_path = ?').get([abs]); if (existing) { db.prepare('UPDATE file_meta SET uploader = ?, sha256 = NULL, uploaded_at = ? WHERE file_path = ?').run([username, uploadedAt, abs]); } else { db.prepare('INSERT INTO file_meta (file_path, uploader, downloaders, watched_by, uploaded_at, sha256) VALUES (?, ?, ?, ?, ?, ?)') .run([abs, username, '[]', '{}', uploadedAt, null]); } try { enqueueFileHash(abs); } catch { /* best effort */ } Promise.resolve().then(() => reindexDirectoryTree(path.dirname(abs))).catch(() => {}); } function tempPathIn (dir) { return path.join(dir, `${syncIndex.TEMP_PREFIX}${crypto.randomBytes(8).toString('hex')}.part`); } function limitStream (max, onTooBig) { let total = 0; return new Transform({ transform (chunk, enc, cb) { total += chunk.length; if (total > max) return cb(onTooBig()); cb(null, chunk); }, }); } /** Move a finished temp file into place atomically and apply mtime. */ function finalizeFile (tempAbs, p, mtimeMs) { if (mtimeMs) { const t = new Date(mtimeMs); fs.utimesSync(tempAbs, t, t); } fs.renameSync(tempAbs, p.abs); } function checkQuota (p, addedBytes) { if (!p.info || !p.info.isUserFolder) return; const msg = helpers.checkUserFolderSizeLimit(p.config, p.reqUsername, addedBytes); if (msg) throw new ApiError(507, 'quota_exceeded', msg); } // --------------------------------------------------------------------------- // Routes // --------------------------------------------------------------------------- router.use((req, res, next) => { res.set('Access-Control-Allow-Origin', '*'); res.set('Access-Control-Allow-Headers', 'Authorization, Content-Type, If-Match, If-None-Match, Range, If-Range, Upload-Offset, X-Mtime'); res.set('Access-Control-Allow-Methods', 'GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS'); res.set('Access-Control-Expose-Headers', 'ETag, Content-Range, Accept-Ranges, Upload-Offset, Location, Content-Length'); if (req.method === 'OPTIONS') return res.status(204).end(); next(); }); router.get('/', (req, res) => { const base = publicBaseUrl(req); res.json({ name: 'filemanager', api: 'v1', openapi: `${base}/api/v1/openapi.json`, oauth: `${base}/.well-known/oauth-authorization-server` }); }); router.get('/openapi.json', (req, res) => { const spec = JSON.parse(fs.readFileSync(path.join(__dirname, '../../docs/openapi.json'), 'utf8')); const base = publicBaseUrl(req); spec.servers = [{ url: `${base}/api/v1` }]; const flow = spec.components.securitySchemes.oauth2.flows.authorizationCode; flow.authorizationUrl = `${base}/oauth/authorize`; flow.tokenUrl = `${base}/oauth/token`; flow.refreshUrl = `${base}/oauth/token`; res.json(spec); }); router.use(authenticate); router.get('/me', (req, res) => { const { config, list } = rootsForUser(req.user); const a = apiConfig(config); res.json({ username: req.user.username, scopes: req.apiScopes, grant: { id: req.apiGrant.id, kind: req.apiGrant.kind, name: req.apiGrant.name }, roots: list.map((r) => ({ id: r.id, name: r.name, writable: r.writable && req.apiScopes.includes('files:write') })), limits: { maxUploadBytes: a.maxUploadBytes, maxChunkBytes: a.maxChunkBytes, maxListLimit: 1000, maxDeltaLimit: 1000 }, trash: false, }); }); router.get('/roots', requireScope('files:read'), (req, res) => { const { list } = rootsForUser(req.user); res.json({ roots: list.map((r) => ({ id: r.id, name: r.name, path: '/' + r.id, writable: r.writable && req.apiScopes.includes('files:write') })) }); }); router.get('/files/metadata', requireScope('files:read'), wrap(async (req, res) => { const p = resolvePath(req.user, req.query.path); const st = lstatOrNull(p.abs); if (!st || st.isSymbolicLink()) throw new ApiError(404, 'not_found', 'Not found.'); if (boolParam(req.query.hash) && st.isFile() && !knownSha256(p.abs, st)) { const a = apiConfig(p.config); if (st.size > a.maxHashBytes) throw new ApiError(413, 'too_large', `Hashing on demand is limited to ${a.maxHashBytes} bytes.`); const sha = await computeSha256(p.abs); const st2 = lstatOrNull(p.abs); if (st2 && st2.size === st.size && st2.mtimeMs === st.mtimeMs) rememberSha256(p.abs, st2, sha); } const e = currentEntry(p); res.set('ETag', `"${e.version}"`); res.json(e); })); router.get('/files/list', requireScope('files:read'), wrap(async (req, res) => { const p = resolvePath(req.user, req.query.path); const st = lstatOrNull(p.abs); if (!st) throw new ApiError(404, 'not_found', 'Not found.'); if (!st.isDirectory()) throw new ApiError(400, 'not_a_folder', 'Path is not a folder.'); const limit = Math.max(1, Math.min(1000, Number(req.query.limit) || 500)); const offset = Math.max(0, Number(req.query.page_token) || 0); const names = (await fs.promises.readdir(p.abs)).filter((n) => !syncIndex.isIgnoredName(n)).sort(); const page = names.slice(offset, offset + limit); const entries = []; for (const n of page) { const childAbs = path.join(p.abs, n); const cst = lstatOrNull(childAbs); if (!cst || cst.isSymbolicLink() || (!cst.isFile() && !cst.isDirectory())) continue; entries.push(entryJson(p.root.id, p.rel ? `${p.rel}/${n}` : n, cst, childAbs)); } const next = offset + limit < names.length ? String(offset + limit) : null; res.json({ folder: currentEntry(p), entries, next_page_token: next }); })); function downloadHandler (req, res) { let p; try { p = resolvePath(req.user, req.query.path); } catch (err) { return sendError(res, err); } const st = lstatOrNull(p.abs); 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.')); const e = entryJson(p.root.id, p.rel, st, p.abs); res.set('ETag', `"${e.version}"`); res.set('X-File-Id', e.id); if (e.sha256) res.set('Digest', `sha-256=${Buffer.from(e.sha256, 'hex').toString('base64')}`); res.set('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(e.name)}`); const inm = String(req.get('if-none-match') || '').split(',').map(parseEtag); if (inm.includes(e.version) || inm.includes('*')) return res.status(304).end(); res.sendFile(p.abs, { dotfiles: 'allow', etag: false, lastModified: true, acceptRanges: true, cacheControl: false }, (err) => { if (err && !res.headersSent) sendError(res, new ApiError(err.status || 500, 'download_failed', 'Download failed.')); }); } router.get('/files/content', requireScope('files:read'), downloadHandler); router.head('/files/content', requireScope('files:read'), downloadHandler); router.put('/files/content', requireScope('files:write'), wrap(async (req, res) => { const p = resolvePath(req.user, req.query.path, { write: true }); if (!p.rel) throw new ApiError(400, 'invalid_path', 'Cannot write to a root.'); p.reqUsername = req.user.username; const a = apiConfig(p.config); const pre = { ifMatch: parseEtag(req.get('if-match')), ifNoneMatch: parseEtag(req.get('if-none-match')), overwrite: boolParam(req.query.overwrite) }; const mtime = parseMtime(req.query.mtime || req.get('x-mtime')); requireParentFolder(p); checkPreconditions(p, pre); const declared = req.get('content-length') != null ? Number(req.get('content-length')) : null; if (declared != null && declared > a.maxUploadBytes) throw new ApiError(413, 'too_large', `Maximum upload size is ${a.maxUploadBytes} bytes; use /uploads for large files.`); if (declared != null) checkQuota(p, declared); const temp = tempPathIn(path.dirname(p.abs)); const hash = crypto.createHash('sha256'); let size = 0; try { await pipeline( req, limitStream(a.maxUploadBytes, () => new ApiError(413, 'too_large', `Maximum upload size is ${a.maxUploadBytes} bytes.`)), new Transform({ transform (chunk, enc, cb) { size += chunk.length; hash.update(chunk); cb(null, chunk); } }), fs.createWriteStream(temp, { flags: 'wx' }), ); const sha = hash.digest('hex'); const expected = req.get('x-content-sha256'); if (expected && expected.toLowerCase() !== sha) throw new ApiError(422, 'checksum_mismatch', 'Uploaded content does not match X-Content-SHA256.'); if (declared == null) checkQuota(p, size); checkPreconditions(p, pre); // re-check right before the atomic replace const existed = !!lstatOrNull(p.abs); finalizeFile(temp, p, mtime); const st = fs.lstatSync(p.abs); rememberSha256(p.abs, st, sha); recordUploadMeta(p.abs, req.user.username); syncIndex.markDirty(p.root.abs); const e = entryJson(p.root.id, p.rel, st, p.abs); res.set('ETag', `"${e.version}"`); res.status(existed ? 200 : 201).json(e); } finally { fs.rmSync(temp, { force: true }); } })); router.post('/folders', requireScope('files:write'), json, wrap(async (req, res) => { const p = resolvePath(req.user, req.body && req.body.path, { write: true }); if (!p.rel) throw new ApiError(409, 'conflict', 'Root already exists.'); const st = lstatOrNull(p.abs); if (st) { if (st.isDirectory() && boolParam(req.body.exist_ok)) return res.status(200).json(currentEntry(p)); throw new ApiError(409, 'conflict', 'Something already exists at this path.', { current: currentEntry(p) }); } if (boolParam(req.body.parents)) { fs.mkdirSync(p.abs, { recursive: true }); } else { requireParentFolder(p); fs.mkdirSync(p.abs); } syncIndex.markDirty(p.root.abs); res.status(201).json(currentEntry(p)); })); router.post('/files/move', requireScope('files:write'), json, wrap(async (req, res) => { const body = req.body || {}; const ctx = rootsForUser(req.user); const from = resolvePath(req.user, body.from, { write: true, ctx }); const to = resolvePath(req.user, body.to, { write: true, ctx }); if (!from.rel || !to.rel) throw new ApiError(400, 'invalid_path', 'Roots cannot be moved or replaced.'); const st = lstatOrNull(from.abs); if (!st) throw new ApiError(404, 'not_found', 'Source not found.'); const ifMatch = parseEtag(body.if_match || req.get('if-match')); const cur = entryJson(from.root.id, from.rel, st, from.abs); if (ifMatch && ifMatch !== '*' && ifMatch !== cur.version) throw new ApiError(412, 'precondition_failed', 'Source was changed by someone else.', { current: cur }); if (from.abs === to.abs) return res.json(cur); if (to.abs.startsWith(from.abs + path.sep)) throw new ApiError(400, 'invalid_path', 'Cannot move a folder into itself.'); requireParentFolder(to); const dst = lstatOrNull(to.abs); // Case-only renames on case-insensitive filesystems resolve to the same inode. const sameNode = dst && dst.ino === st.ino && dst.dev === st.dev; if (dst && !sameNode) { if (!boolParam(body.overwrite)) throw new ApiError(409, 'conflict', 'Destination exists.', { current: currentEntry(to) }); if (dst.isDirectory() || st.isDirectory()) throw new ApiError(409, 'conflict', 'Folders are never overwritten.', { current: currentEntry(to) }); } if (to.info && to.info.isUserFolder && from.root.abs !== to.root.abs) { to.reqUsername = req.user.username; checkQuota(to, st.isDirectory() ? 0 : st.size); } helpers.movePathWithFallback(from.abs, to.abs); helpers.rewriteMetaPaths(from.abs, to.abs); try { rewriteIndexPaths(from.abs, to.abs); } catch { /* best effort */ } getDb().prepare('DELETE FROM api_hashes WHERE abs_path = ?').run([from.abs]); syncIndex.markDirty(from.root.abs); syncIndex.markDirty(to.root.abs); res.json(currentEntry(to)); })); router.delete('/files', requireScope('files:write'), wrap(async (req, res) => { const p = resolvePath(req.user, req.query.path, { write: true }); if (!p.rel) throw new ApiError(400, 'invalid_path', 'Roots cannot be deleted.'); 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.'); const st = lstatOrNull(p.abs); if (!st) throw new ApiError(404, 'not_found', 'Not found.'); const ifMatch = parseEtag(req.get('if-match')); const cur = entryJson(p.root.id, p.rel, st, p.abs); if (ifMatch && ifMatch !== '*' && ifMatch !== cur.version) throw new ApiError(412, 'precondition_failed', 'Target was changed by someone else.', { current: cur }); if (st.isDirectory()) { const children = fs.readdirSync(p.abs); if (children.length && !boolParam(req.query.recursive)) throw new ApiError(409, 'folder_not_empty', 'Folder is not empty; pass recursive=true.'); fs.rmSync(p.abs, { recursive: true, force: true }); } else { fs.unlinkSync(p.abs); } getDb().prepare('DELETE FROM file_meta WHERE file_path = ? OR file_path LIKE ?').run([p.abs, p.abs + path.sep + '%']); getDb().prepare('DELETE FROM api_hashes WHERE abs_path = ? OR abs_path LIKE ?').run([p.abs, p.abs + path.sep + '%']); try { deleteIndexUnder(p.abs); } catch { /* best effort */ } syncIndex.markDirty(p.root.abs); res.status(204).end(); })); // --------------------------------------------------------------------------- // Delta / change feed // --------------------------------------------------------------------------- function encodeCursor (obj) { return Buffer.from(JSON.stringify(obj)).toString('base64url'); } function decodeCursor (raw) { try { const c = JSON.parse(Buffer.from(String(raw), 'base64url').toString('utf8')); if (c && typeof c === 'object') return c; } catch { /* fallthrough */ } throw new ApiError(400, 'invalid_cursor', 'Malformed cursor.'); } function rootTag (root) { return crypto.createHash('sha256').update(root.abs).digest('base64url').slice(0, 12); } router.get('/delta', requireScope('files:read'), wrap(async (req, res) => { const ctx = rootsForUser(req.user); let rootId = req.query.root; let cursor = null; if (req.query.cursor) { cursor = decodeCursor(req.query.cursor); rootId = rootId || cursor.r; } const root = ctx.list.find((r) => r.id === rootId); if (!root) throw new ApiError(404, 'not_found', 'Unknown root; pass root= from /roots.'); if (cursor && (cursor.r !== root.id || cursor.k !== rootTag(root))) throw new ApiError(400, 'invalid_cursor', 'Cursor belongs to another root.'); const a = apiConfig(ctx.config); const limit = Math.max(1, Math.min(1000, Number(req.query.limit) || 500)); let state; try { state = await syncIndex.ensureFresh(root.abs, root.abs, { intervalMs: a.scanIntervalSeconds * 1000, maxEntries: a.maxSyncEntries }); } catch (err) { if (err && err.code === 'too_many_entries') throw new ApiError(507, 'too_many_entries', err.message); throw err; } const toPath = (rel) => '/' + (rel ? `${root.id}/${rel}` : root.id); const base = { r: root.id, k: rootTag(root), g: state.generation }; if (cursor && cursor.g !== state.generation) throw new ApiError(410, 'cursor_expired', 'Cursor is no longer valid; start again without a cursor.'); if (!cursor || cursor.m === 's') { const seq = cursor ? Number(cursor.s) : syncIndex.currentSeq(); const offset = cursor ? Number(cursor.o) || 0 : 0; if (cursor && seq < state.min_seq) throw new ApiError(410, 'cursor_expired', 'Cursor is no longer valid; start again without a cursor.'); const rows = syncIndex.listEntries(root.abs, offset, limit); const more = rows.length === limit; return res.json({ reset: !cursor, entries: rows.map((r) => ({ change: 'exists', id: r.file_id, path: toPath(r.rel_path), type: r.is_dir ? 'folder' : 'file', size: r.size, mtime_ms: r.mtime_ms, version: r.version, })), cursor: encodeCursor(more ? { ...base, m: 's', s: seq, o: offset + rows.length } : { ...base, m: 'c', s: seq }), has_more: more, }); } const after = Number(cursor.s) || 0; if (after < state.min_seq) throw new ApiError(410, 'cursor_expired', 'Changes since this cursor were pruned; start again without a cursor.'); const rows = syncIndex.listChanges(root.abs, after, limit); const last = rows.length ? rows[rows.length - 1].seq : after; res.json({ reset: false, entries: rows.map((r) => ({ change: r.change, id: r.file_id, path: toPath(r.rel_path), old_path: r.old_rel_path != null ? toPath(r.old_rel_path) : undefined, type: r.is_dir ? 'folder' : 'file', size: r.size, mtime_ms: r.mtime_ms, version: r.version, })), cursor: encodeCursor({ ...base, m: 'c', s: last }), has_more: rows.length === limit, }); })); // --------------------------------------------------------------------------- // Resumable uploads // --------------------------------------------------------------------------- function purgeExpiredUploads (uploadDir) { const d = getDb(); const expired = d.prepare('SELECT id FROM api_uploads WHERE expires_at < ?').all([Date.now()]); for (const r of expired) { fs.rmSync(path.join(uploadDir, `${r.id}.part`), { force: true }); d.prepare('DELETE FROM api_uploads WHERE id = ?').run([r.id]); } } function getUploadFor (req, uploadDir) { const id = String(req.params.id || ''); if (!/^[A-Za-z0-9_-]{10,64}$/.test(id)) throw new ApiError(404, 'not_found', 'Upload not found.'); const row = getDb().prepare('SELECT * FROM api_uploads WHERE id = ?').get([id]); if (!row || row.username !== req.user.username || row.grant_id !== req.apiGrant.id || row.expires_at < Date.now()) { throw new ApiError(404, 'not_found', 'Upload not found or expired.'); } row.file = path.join(uploadDir, `${row.id}.part`); return row; } function uploadJson (row) { return { id: row.id, path: row.target, size: row.size, offset: row.received, expires_at: new Date(row.expires_at).toISOString() }; } router.post('/uploads', requireScope('files:write'), json, wrap(async (req, res) => { const body = req.body || {}; const p = resolvePath(req.user, body.path, { write: true }); if (!p.rel) throw new ApiError(400, 'invalid_path', 'Cannot write to a root.'); p.reqUsername = req.user.username; const a = apiConfig(p.config); const size = Number(body.size); if (!Number.isInteger(size) || size < 0) throw new ApiError(400, 'invalid_request', 'size (bytes) is required.'); if (size > a.maxUploadBytes) throw new ApiError(413, 'too_large', `Maximum upload size is ${a.maxUploadBytes} bytes.`); const sha = body.sha256 ? String(body.sha256).toLowerCase() : null; if (sha && !/^[0-9a-f]{64}$/.test(sha)) throw new ApiError(400, 'invalid_request', 'sha256 must be 64 hex characters.'); const pre = { ifMatch: parseEtag(body.if_match), ifNoneMatch: body.if_none_match === '*' ? '*' : null, overwrite: boolParam(body.overwrite) }; requireParentFolder(p); checkPreconditions(p, pre); checkQuota(p, size); fs.mkdirSync(a.uploadDir, { recursive: true, mode: 0o700 }); purgeExpiredUploads(a.uploadDir); const open = getDb().prepare('SELECT COUNT(*) AS n FROM api_uploads WHERE username = ?').get([req.user.username]).n; if (open >= a.maxOpenUploadsPerUser) throw new ApiError(429, 'too_many_uploads', 'Too many unfinished uploads; finish or cancel some first.'); const id = crypto.randomBytes(18).toString('base64url'); const t = Date.now(); fs.writeFileSync(path.join(a.uploadDir, `${id}.part`), Buffer.alloc(0), { mode: 0o600 }); 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) VALUES (?, ?, ?, ?, ?, 0, ?, ?, ?, ?, ?, ?, ?)`) .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]); const row = getDb().prepare('SELECT * FROM api_uploads WHERE id = ?').get([id]); res.set('Location', `/api/v1/uploads/${id}`); res.set('Upload-Offset', '0'); res.status(201).json({ ...uploadJson(row), max_chunk_bytes: a.maxChunkBytes }); })); router.get('/uploads/:id', requireScope('files:write'), wrap(async (req, res) => { const { config } = helpers.loadConfigAndRoots(); const row = getUploadFor(req, apiConfig(config).uploadDir); res.set('Upload-Offset', String(row.received)); res.json(uploadJson(row)); })); router.patch('/uploads/:id', requireScope('files:write'), wrap(async (req, res) => { const { config } = helpers.loadConfigAndRoots(); const a = apiConfig(config); const row = getUploadFor(req, a.uploadDir); const offset = Number(req.get('upload-offset')); if (!Number.isInteger(offset)) throw new ApiError(400, 'invalid_request', 'Upload-Offset header is required.'); if (offset !== row.received) { res.set('Upload-Offset', String(row.received)); throw new ApiError(409, 'offset_mismatch', `Expected offset ${row.received}.`, { offset: row.received }); } const remaining = row.size - row.received; const max = Math.min(a.maxChunkBytes, remaining); let written = 0; try { await pipeline( req, limitStream(max, () => new ApiError(413, 'too_large', `Chunk too large (max ${max} bytes here).`)), new Transform({ transform (chunk, enc, cb) { written += chunk.length; cb(null, chunk); } }), fs.createWriteStream(row.file, { flags: 'r+', start: row.received }), ); } catch (err) { // Keep only fully confirmed bytes; a client resumes from the stored offset. fs.truncateSync(row.file, row.received); throw err instanceof ApiError ? err : new ApiError(400, 'upload_interrupted', 'Chunk upload was interrupted.'); } const received = row.received + written; getDb().prepare('UPDATE api_uploads SET received = ? WHERE id = ?').run([received, row.id]); res.set('Upload-Offset', String(received)); res.json({ ...uploadJson({ ...row, received }) }); })); router.post('/uploads/:id/complete', requireScope('files:write'), wrap(async (req, res) => { const { config } = helpers.loadConfigAndRoots(); const a = apiConfig(config); const row = getUploadFor(req, a.uploadDir); if (row.received !== row.size) throw new ApiError(409, 'incomplete', `Only ${row.received} of ${row.size} bytes received.`, { offset: row.received }); const p = resolvePath(req.user, row.target, { write: true }); p.reqUsername = req.user.username; const sha = await computeSha256(row.file); if (row.sha256 && row.sha256 !== sha) { fs.rmSync(row.file, { force: true }); getDb().prepare('DELETE FROM api_uploads WHERE id = ?').run([row.id]); throw new ApiError(422, 'checksum_mismatch', 'Uploaded content does not match sha256; upload discarded.'); } requireParentFolder(p); const pre = { ifMatch: row.if_match, ifNoneMatch: row.if_none_match ? '*' : null, overwrite: !!row.overwrite }; checkPreconditions(p, pre); checkQuota(p, row.size); const temp = tempPathIn(path.dirname(p.abs)); try { try { fs.renameSync(row.file, temp); } catch { fs.copyFileSync(row.file, temp); fs.rmSync(row.file, { force: true }); } checkPreconditions(p, pre); const existed = !!lstatOrNull(p.abs); finalizeFile(temp, p, row.mtime_ms); getDb().prepare('DELETE FROM api_uploads WHERE id = ?').run([row.id]); const st = fs.lstatSync(p.abs); rememberSha256(p.abs, st, sha); recordUploadMeta(p.abs, req.user.username); syncIndex.markDirty(p.root.abs); const e = entryJson(p.root.id, p.rel, st, p.abs); res.set('ETag', `"${e.version}"`); res.status(existed ? 200 : 201).json(e); } catch (err) { // Lost a race after staging (target changed meanwhile): discard; the client starts a new upload. fs.rmSync(temp, { force: true }); getDb().prepare('DELETE FROM api_uploads WHERE id = ?').run([row.id]); throw err; } })); router.delete('/uploads/:id', requireScope('files:write'), wrap(async (req, res) => { const { config } = helpers.loadConfigAndRoots(); const row = getUploadFor(req, apiConfig(config).uploadDir); fs.rmSync(row.file, { force: true }); getDb().prepare('DELETE FROM api_uploads WHERE id = ?').run([row.id]); res.status(204).end(); })); router.use((req, res) => res.status(404).json({ error: 'not_found', message: 'Unknown API endpoint.' })); router.ApiError = ApiError; module.exports = router;