Files
wekan/server/initializeDirs.js
T
Lauri Ojansivu 31e569a18e Single-file WeKan for every platform with a Node.js bundle.
One executable per platform: that platform's published bundle repackaged
as a Node.js single executable application whose ZIP Node 26.11 mounts as
a virtual file system (useVfs + vfsArchive). Only FerretDB is copied out,
once and verified, to wekan-files/bin; native addons load from the VFS.

Everything WeKan writes is in wekan-files beside the executable, which
WRITABLE_PATH points at: attachments, avatars, db, bin, backup, logs,
versions. WeKan now treats a WRITABLE_PATH named wekan-files as the files
root (models/lib/filesRoot.js, shared by every place that derived it).

Started in a terminal it shows a menu: start, stop, status, version info
as Admin Panel / Version shows it, update to the newest release or change
to any other (checksum verified, backed up first, the old executable
kept), backup to wekan-files/backup/YYYY-MM-DD_HH-MM-SS, run at boot
(systemd, launchd, Task Scheduler, cron, Termux:Boot), log, settings.
Every entry is also a command. start runs the start-wekan.sh loop:
recovery, requested restore, FerretDB, recovery page, WeKan, restart.

Meteor's main.js chdir()s into the bundle, which a VFS cannot be, so the
server role keeps a virtual working directory. Node 26.11.1 breaks
fs.writev/readv while any VFS is mounted; they are rebuilt on fs.write
and fs.read.

single-file.yml builds every target in releases/single-file/targets.json
from its verified bundle (Macs on a Mac, to re-sign), starts it where a
runner exists with browser tests on amd64, and attaches what started.
Release All and Release All Missing call it.

Thanks to xet7 !
2026-10-13 21:08:09 +03:00

66 lines
2.6 KiB
JavaScript

// ============================================================================
// Initialize Upload Directories
// Runs at server startup before models are loaded to ensure ostrio:files
// can create necessary upload directories without permission errors.
// ============================================================================
import fs from 'fs';
import path from 'path';
const { isFilesRoot } = require('/models/lib/filesRoot');
// Compute upload subdirectory path:
// - On Docker (WRITABLE_PATH=/data): /data/files/attachments, /data/files/avatars
// - On Snap (WRITABLE_PATH=$SNAP_COMMON/files): $SNAP_COMMON/files/attachments, $SNAP_COMMON/files/avatars
// - On dev (WRITABLE_PATH=..): ../files/attachments, ../files/avatars
//
// Snap already appends '/files' to WRITABLE_PATH, so we detect and handle that case.
function getUploadPaths() {
const basePath = process.env.WRITABLE_PATH || process.cwd();
// Check if basePath already ends with '/files' (Snap case)
const endsWithFiles = isFilesRoot(basePath);
if (endsWithFiles) {
// Snap: WRITABLE_PATH is already $SNAP_COMMON/files, so append subdirectories directly
return {
attachments: path.join(basePath, 'attachments'),
avatars: path.join(basePath, 'avatars'),
};
} else {
// Docker & Dev: WRITABLE_PATH is /data or .., so append /files/{subdir}
return {
attachments: path.join(basePath, 'files', 'attachments'),
avatars: path.join(basePath, 'files', 'avatars'),
};
}
}
const uploadPaths = getUploadPaths();
const dirsToCreate = [uploadPaths.attachments, uploadPaths.avatars];
dirsToCreate.forEach(dirPath => {
try {
// Create directory recursively with proper permissions (0o755)
// Ignore error if it already exists
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true, mode: 0o755 });
if (process.env.DEBUG === 'true') {
console.info(`[initializeDirs] Created directory: ${dirPath}`);
}
} else {
// Verify the directory is writable
try {
fs.accessSync(dirPath, fs.constants.W_OK);
if (process.env.DEBUG === 'true') {
console.info(`[initializeDirs] Directory exists and is writable: ${dirPath}`);
}
} catch (accessError) {
console.warn(`[initializeDirs] WARNING: Directory exists but may not be writable: ${dirPath}`, accessError.message);
}
}
} catch (error) {
console.error(`[initializeDirs] FAILED to initialize directory: ${dirPath}`, error.message);
throw new Error(`Cannot create or access upload directory: ${dirPath}\n${error.message}`);
}
});