Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix paths #90

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/server/lib/services/cfg.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const stat = promisify(fs.stat);
const readFile = promisify(fs.readFile);
const mkdir = promisify(fs.mkdir);

const configFile = require("./paths.js").get().cfgFile;
const paths = require("./paths.js");

const defaults = {
listeners: [{
Expand Down Expand Up @@ -38,6 +38,7 @@ const hiddenOpts = ["dev"];
const cfg = module.exports = {};

cfg.init = (config) => new Promise(async (resolve, reject) => {
const configFile = paths.get().cfgFile;
if (typeof config === "object" && config !== null) {
config = Object.assign({}, defaults, config);
return resolve(config);
Expand All @@ -49,7 +50,7 @@ cfg.init = (config) => new Promise(async (resolve, reject) => {
config = defaults;
await mkdir(dirname(configFile), {recursive: true});

await write(config);
await write(configFile, config);
resolve(config);
} else {
return reject(err);
Expand Down Expand Up @@ -79,7 +80,7 @@ cfg.init = (config) => new Promise(async (resolve, reject) => {
delete config[key];
}
});
await write(config);
await write(configFile, config);
return resolve(config);
} catch (err) {
// TODO: can we print helpful information here?
Expand All @@ -88,7 +89,7 @@ cfg.init = (config) => new Promise(async (resolve, reject) => {
}
});

function write(config) {
function write(configFile, config) {
return new Promise((resolve, reject) => {
fs.writeFile(configFile, JSON.stringify(config, null, 2), (err) => {
if (err) {
Expand Down
6 changes: 5 additions & 1 deletion packages/server/lib/services/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ const crypto = require("crypto");
const path = require("path");

const log = require("./log.js");
const dbFile = require("./paths.js").get().db;
const paths = require("./paths.js");
const defaults = {users: {}, sessions: {}, links: {}};

let database, watching;

db.load = function(callback) {
const dbFile = paths.get().db;
fs.stat(dbFile, err => {
if (err) {
if (err.code === "ENOENT") {
Expand Down Expand Up @@ -77,6 +78,7 @@ db.load = function(callback) {
};

db.parse = function(cb) {
const dbFile = paths.get().db;
fs.readFile(dbFile, "utf8", (err, data) => {
if (err) return cb(err);

Expand Down Expand Up @@ -147,6 +149,7 @@ db.authUser = function(user, pass) {
};

db.watch = function(config) {
const dbFile = paths.get().db;
chokidar.watch(dbFile, {
ignoreInitial: true,
usePolling: Boolean(config.pollingInterval),
Expand All @@ -165,6 +168,7 @@ db.watch = function(config) {

// TODO: async
function write() {
const dbFile = paths.get().db;
watching = false;
fs.writeFileSync(dbFile, JSON.stringify(database, null, 2));

Expand Down
4 changes: 2 additions & 2 deletions packages/server/lib/services/filetree.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const rfdc = require("rfdc");
const util = require("util");

const log = require("./log.js");
const paths = require("./paths.js").get();
const paths = require("./paths.js");
const utils = require("./utils.js");

const clone = rfdc();
Expand All @@ -32,7 +32,7 @@ filetree.init = function(config) {
};

filetree.watch = function() {
chokidar.watch(paths.files, {
chokidar.watch(paths.get().files, {
alwaysStat: true,
ignoreInitial: true,
usePolling: Boolean(cfg.pollingInterval),
Expand Down
46 changes: 23 additions & 23 deletions packages/server/lib/services/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ const {stat, mkdir, readdir, readFile, writeFile} = require("fs").promises;
const {promisify} = require("util");

const log = require("./log.js");
const paths = require("./paths.js").get();
const paths = require("./paths.js");
const utils = require("./utils.js");

const themesPath = path.join(paths.client, "/node_modules/codemirror/theme");
const modesPath = path.join(paths.client, "/node_modules/codemirror/mode");
const cachePath = process.env.DROPPY_CACHE_PATH ?? path.join(paths.homedir, "/.droppy/cache/cache.json");
const themesPath = path.join(paths.get().client, "/node_modules/codemirror/theme");
const modesPath = path.join(paths.get().client, "/node_modules/codemirror/mode");
const cachePath = process.env.DROPPY_CACHE_PATH ?? path.join(paths.get().homedir, "/.droppy/cache/cache.json");

const pkg = require("../../package.json");

Expand Down Expand Up @@ -107,10 +107,10 @@ try {

resources.files = {
css: [
`${paths.client}/lib/style.css`,
`${paths.client}/lib/sprites.css`,
`${paths.client}/lib/tooltips.css`,
`${paths.client}/lib/clienttheme.css`,
`${paths.get().client}/lib/style.css`,
`${paths.get().client}/lib/sprites.css`,
`${paths.get().client}/lib/tooltips.css`,
`${paths.get().client}/lib/clienttheme.css`,
],
js: [
"node_modules/handlebars/dist/handlebars.runtime.min.js",
Expand Down Expand Up @@ -229,8 +229,8 @@ async function isCacheFresh(cb) {
const files = [];
for (const type of Object.keys(resources.files)) {
resources.files[type].forEach(file => {
if (fs.existsSync(path.join(paths.client, file))) {
files.push(path.join(paths.client, file));
if (fs.existsSync(path.join(paths.get().client, file))) {
files.push(path.join(paths.get().client, file));
} else {
files.push(file);
}
Expand All @@ -239,15 +239,15 @@ async function isCacheFresh(cb) {

for (const file of Object.keys(libs)) {
if (typeof libs[file] === "string") {
if (fs.existsSync(path.join(paths.client, libs[file]))) {
files.push(path.join(paths.client, libs[file]));
if (fs.existsSync(path.join(paths.get().client, libs[file]))) {
files.push(path.join(paths.get().client, libs[file]));
} else {
files.push(libs[file]);
}
} else {
libs[file].forEach(file => {
if (fs.existsSync(path.join(paths.client, file))) {
files.push(path.join(paths.client, file));
if (fs.existsSync(path.join(paths.get().client, file))) {
files.push(path.join(paths.get().client, file));
} else {
files.push(file);
}
Expand Down Expand Up @@ -324,7 +324,7 @@ async function readThemes() {
themes[name.replace(/\.css$/, "")] = Buffer.from(await minifyCSS(String(data)));
}

const droppyTheme = await readFile(path.join(paths.client, "/lib/cmtheme.css"));
const droppyTheme = await readFile(path.join(paths.get().client, "/lib/cmtheme.css"));
themes.droppy = Buffer.from(await minifyCSS(String(droppyTheme)));

return themes;
Expand All @@ -334,7 +334,7 @@ async function readModes() {
const modes = {};

// parse meta.js from CM for supported modes
const js = await readFile(path.join(paths.client, "/node_modules/codemirror/mode/meta.js"));
const js = await readFile(path.join(paths.get().client, "/node_modules/codemirror/mode/meta.js"));

// Extract modes from CodeMirror
const sandbox = {CodeMirror: {}};
Expand All @@ -357,7 +357,7 @@ async function readLibs() {

for (const [dest, files] of Object.entries(libs)) {
lib[dest] = Buffer.concat(await Promise.all(files.map(file => {
return readFile(path.join(paths.client, file));
return readFile(path.join(paths.get().client, file));
})));
}

Expand Down Expand Up @@ -397,8 +397,8 @@ function templates() {
"templates=Handlebars.templates=Handlebars.templates||{};";
const suffix = "Handlebars.partials=Handlebars.templates})();";

return prefix + fs.readdirSync(paths.templates).map(file => {
const p = path.join(paths.templates, file);
return prefix + fs.readdirSync(paths.get().templates).map(file => {
const p = path.join(paths.get().templates, file);
const name = file.replace(/\..+$/, "");
let html = htmlMinifier.minify(fs.readFileSync(p, "utf8"), opts.htmlMinifier);

Expand All @@ -421,8 +421,8 @@ function templates() {
resources.compileJS = async function() {
let js = "";
resources.files.js.forEach(file => {
if (fs.existsSync(path.join(paths.client, file))) {
js += `${fs.readFileSync(path.join(paths.client, file), "utf8")};`;
if (fs.existsSync(path.join(paths.get().client, file))) {
js += `${fs.readFileSync(path.join(paths.get().client, file), "utf8")};`;
} else {
js += `${fs.readFileSync(file, "utf8")};`;
}
Expand Down Expand Up @@ -458,7 +458,7 @@ resources.compileCSS = async function() {
};

resources.compileHTML = async function(res) {
let html = fs.readFileSync(path.join(paths.client, "lib", "index.html"), "utf8");
let html = fs.readFileSync(path.join(paths.get().client, "lib", "index.html"), "utf8");
html = html.replace("<!-- {{svg}} -->", svg());

let auth = html.replace("{{type}}", "a");
Expand All @@ -485,7 +485,7 @@ async function compileAll() {
// Read misc files
for (const file of resources.files.other) {
const name = path.basename(file);
const fullPath = path.join(paths.client, file);
const fullPath = path.join(paths.get().client, file);
const data = fs.readFileSync(fullPath);
res[name] = {data, etag: etag(data), mime: utils.contentType(name)};
}
Expand Down
30 changes: 15 additions & 15 deletions packages/server/lib/services/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const db = require("./db.js");
const filetree = require("./filetree.js");
const log = require("./log.js");
const manifest = require("./manifest.js");
const paths = require("./paths.js").get();
const paths = require("./paths.js");
const pkg = require("../../package.json");
const resources = require("./resources.js");
const utils = require("./utils.js");
Expand Down Expand Up @@ -54,24 +54,24 @@ module.exports = async function droppy(opts, isStandalone, dev, callback) {
].join(" "), [
blue("config"),
"at",
green(paths.config)
green(paths.get().config)
].join(" "), [
blue("files"),
"at",
green(paths.files)
green(paths.get().files)
].join(" ")
);
}
setupProcess(isStandalone);

try {
await promisify((cb) => {
utils.mkdir([paths.files, paths.config], cb);
utils.mkdir([paths.get().files, paths.get().config], cb);
})();

await promisify((cb) => {
if (isStandalone) {
fs.writeFile(paths.pid, String(process.pid), cb);
fs.writeFile(paths.get().pid, String(process.pid), cb);
} else {
cb();
}
Expand Down Expand Up @@ -967,7 +967,7 @@ function handleUploadRequest(req, res) {
const tmpPath = utils.addUploadTempExt(filename);
rootNames.add(utils.rootname(tmpPath));

const dst = path.join(paths.files, dstDir, tmpPath);
const dst = path.join(paths.get().files, dstDir, tmpPath);
utils.mkdir(path.dirname(dst), () => {
fs.stat(dst, err => {
if (err && err.code === "ENOENT") {
Expand Down Expand Up @@ -999,8 +999,8 @@ function handleUploadRequest(req, res) {

// move temp files into place
await Promise.all([...rootNames].map(async p => {
const srcPath = path.join(paths.files, dstDir, p);
const dstPath = path.join(paths.files, dstDir, utils.removeUploadTempExt(p));
const srcPath = path.join(paths.get().files, dstDir, p);
const dstPath = path.join(paths.get().files, dstDir, utils.removeUploadTempExt(p));
await promisify(utils.move)(srcPath, dstPath);
}));

Expand All @@ -1014,7 +1014,7 @@ function handleUploadRequest(req, res) {

// remove all uploaded temp files on cancel
await Promise.all([...rootNames].map(async p => {
await promisify(utils.rm)(path.join(paths.files, dstDir, p));
await promisify(utils.rm)(path.join(paths.get().files, dstDir, p));
}));

filetree.updateDir(dstDir);
Expand Down Expand Up @@ -1085,7 +1085,7 @@ function removeClientPerDir(sid, vId) {
}

function debug() {
require("chokidar").watch(paths.client, {
require("chokidar").watch(paths.get().client, {
alwaysStat: true,
ignoreInitial: true
}).on("change", file => {
Expand Down Expand Up @@ -1129,7 +1129,7 @@ function cleanupLinks(callback) {
return;
}
// check for links where the target does not exist anymore
fs.stat(path.join(paths.files, location), (error, stats) => {
fs.stat(path.join(paths.get().files, location), (error, stats) => {
if (!stats || error) {
log.debug(`deleting nonexistant link: ${shareLink}`);
delete links[shareLink];
Expand Down Expand Up @@ -1222,7 +1222,7 @@ function streamFile(req, res, filepath, download, stats, shareLink) {

// send expects a url-encoded argument
sendFile(req, encodeURIComponent(utils.removeFilesPath(filepath).substring(1)), {
root: paths.files,
root: paths.get().files,
dotfiles: "allow",
index: false,
etag: false,
Expand Down Expand Up @@ -1267,8 +1267,8 @@ async function tlsSetup(opts, cb) {
return cb(new Error("Missing TLS option 'cert'"));
}

const cert = await readFile(path.resolve(paths.config, ut(opts.cert)), "utf8");
const key = await readFile(path.resolve(paths.config, ut(opts.key)), "utf8");
const cert = await readFile(path.resolve(paths.get().config, ut(opts.cert)), "utf8");
const key = await readFile(path.resolve(paths.get().config, ut(opts.key)), "utf8");

cb(null, {cert, key});
}
Expand Down Expand Up @@ -1316,6 +1316,6 @@ function endProcess(signal) {
}
});
if (count > 0) log.info(`Closed ${count} WebSocket${count > 1 ? "s" : ""}`);
try { fs.unlinkSync(paths.pid); } catch {}
try { fs.unlinkSync(paths.get().pid); } catch {}
process.exit(0);
}
4 changes: 2 additions & 2 deletions packages/server/lib/services/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const svgstore = require("@droppyjs/svgstore");
const fs = require("fs");
const path = require("path");
const paths = require("./paths.js").get();
const paths = require("./paths.js");

module.exports = function svg() {
const sprites = svgstore({
Expand All @@ -12,7 +12,7 @@ module.exports = function svg() {
},
});

fs.readdirSync(paths.svg).forEach(file => {
fs.readdirSync(paths.get().svg).forEach(file => {
sprites.add(`i-${file.replace(/\.svg/, "")}`, fs.readFileSync(path.join(paths.svg, file)));
});

Expand Down
12 changes: 6 additions & 6 deletions packages/server/lib/services/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const util = require("util");
const validate = require("valid-filename");
const {mkdir, stat, lstat, copyFile, readdir, access} = require("fs").promises;

const paths = require("./paths.js").get();
const paths = require("./paths.js");

const forceBinaryTypes = [
"pdf",
Expand Down Expand Up @@ -134,19 +134,19 @@ utils.normalizePath = function(p) {
};

utils.addFilesPath = function(p) {
return p === "/" ? paths.files : path.join(`${paths.files}/${p}`);
return p === "/" ? paths.get().files : path.join(`${paths.get().files}/${p}`);
};

utils.removeFilesPath = function(p) {
if (p.length > paths.files.length) {
return utils.normalizePath(p.substring(paths.files.length));
} else if (p === paths.files) {
if (p.length > paths.get().files.length) {
return utils.normalizePath(p.substring(paths.get().files.length));
} else if (p === paths.get().files) {
return "/";
}
};

utils.sanitizePathsInString = function(str) {
return (str || "").replace(new RegExp(escapeStringRegexp(paths.files), "g"), "");
return (str || "").replace(new RegExp(escapeStringRegexp(paths.get().files), "g"), "");
};

utils.isPathSane = function(p, isURL) {
Expand Down
Loading