From c3d52e87b9585cadb735195c8d94fad6fa5c6f28 Mon Sep 17 00:00:00 2001 From: ansuz Date: Fri, 12 Apr 2019 17:16:32 +0200 Subject: [PATCH] use configured paths in scripts instead of hardcoded strings --- scripts/check-account-deletion.js | 6 +++++- scripts/delete-inactive.js | 10 +++++++--- scripts/expire-channels.js | 11 +++++------ scripts/load-config.js | 7 +++++++ scripts/pinned.js | 2 ++ scripts/pinneddata.js | 24 +++++++++++++++--------- 6 files changed, 41 insertions(+), 19 deletions(-) create mode 100644 scripts/load-config.js diff --git a/scripts/check-account-deletion.js b/scripts/check-account-deletion.js index 34930d1eb..459fad4e9 100644 --- a/scripts/check-account-deletion.js +++ b/scripts/check-account-deletion.js @@ -3,6 +3,8 @@ const Fs = require('fs'); const nThen = require('nthen'); const Pinned = require('./pinned'); const Nacl = require('tweetnacl'); +const Path = require('path'); +const Config = require('./load-config'); const hashesFromPinFile = (pinFile, fileName) => { var pins = {}; @@ -54,7 +56,9 @@ let data = []; let pinned = []; nThen((waitFor) => { - let f = '../pins/' + edPublic.slice(0, 2) + '/' + edPublic + '.ndjson'; + var pinPath = Config.pinPath || './pins'; + + let f = Path.join(pinPath, edPublic.slice(0, 2), edPublic + '.ndjson'); Fs.readFile(f, waitFor((err, content) => { if (err) { throw err; } pinned = hashesFromPinFile(content.toString('utf8'), f); diff --git a/scripts/delete-inactive.js b/scripts/delete-inactive.js index 80d2bb09a..bf64509ed 100644 --- a/scripts/delete-inactive.js +++ b/scripts/delete-inactive.js @@ -5,9 +5,9 @@ const Saferphore = require("saferphore"); const PinnedData = require('./pinneddata'); let config; try { - config = require('../config/config'); + config = require('./config/config'); } catch (e) { - config = require('../config/config.example'); + config = require('./config/config.example'); } if (!config.inactiveTime || typeof(config.inactiveTime) !== "number") { return; } @@ -16,7 +16,11 @@ let inactiveTime = +new Date() - (config.inactiveTime * 24 * 3600 * 1000); let inactiveConfig = { unpinned: true, olderthan: inactiveTime, - blobsolderthan: inactiveTime + blobsolderthan: inactiveTime, + + filePath: config.filePath, + blobPath: config.blobPath, + pinPath: config.pinPath, }; let toDelete; nThen(function (waitFor) { diff --git a/scripts/expire-channels.js b/scripts/expire-channels.js index ec25d10a7..38e0c10c1 100644 --- a/scripts/expire-channels.js +++ b/scripts/expire-channels.js @@ -3,12 +3,7 @@ var Path = require("path"); var nThen = require("nthen"); -var config; -try { - config = require('../config/config'); -} catch (e) { - config = require('../config/config.example'); -} +var config = require("./load-config"); var FileStorage = require('../' + config.storage || './storage/file'); var root = Path.resolve('../' + config.taskPath || './tasks'); @@ -52,10 +47,12 @@ var handleTask = function (str, path, cb) { nThen(function (waitFor) { switch (command) { case 'EXPIRE': + // FIXME noisy! console.log("expiring: %s", args[0]); store.removeChannel(args[0], waitFor()); break; default: + // FIXME noisy console.log("unknown command", command); } }).nThen(function () { @@ -83,6 +80,7 @@ nt = nThen(function (w) { }).nThen(function () { dirs.forEach(function (dir, dIdx) { queue(function (w) { + // FIXME noisy! console.log('recursing into %s', dir); Fs.readdir(Path.join(root, dir), w(function (e, list) { list.forEach(function (fn) { @@ -90,6 +88,7 @@ nt = nThen(function (w) { var filePath = Path.join(root, dir, fn); var cb = w(); + // FIXME noisy! console.log("processing file at %s", filePath); Fs.readFile(filePath, 'utf8', function (e, str) { if (e) { diff --git a/scripts/load-config.js b/scripts/load-config.js new file mode 100644 index 000000000..d32f9d31e --- /dev/null +++ b/scripts/load-config.js @@ -0,0 +1,7 @@ +var config; +try { + config = require("../config/config"); +} catch (e) { + config = require("../config/config.example"); +} +module.exports = config; diff --git a/scripts/pinned.js b/scripts/pinned.js index 0f52b4482..2c00310a4 100644 --- a/scripts/pinned.js +++ b/scripts/pinned.js @@ -11,6 +11,8 @@ let dirList; const fileList = []; const pinned = {}; +// FIXME this seems to be duplicated in a few places. +// make it a library and put it in ./lib/ const checkPinStatus = (pinFile, fileName) => { var pins = {}; pinFile.split('\n').filter((x)=>(x)).map((l) => JSON.parse(l)).forEach((l) => { diff --git a/scripts/pinneddata.js b/scripts/pinneddata.js index 4c30f0a67..5f3d4e1ec 100644 --- a/scripts/pinneddata.js +++ b/scripts/pinneddata.js @@ -2,6 +2,7 @@ const Fs = require('fs'); const Semaphore = require('saferphore'); const nThen = require('nthen'); +const Path = require('path'); /* takes contents of a pinFile (UTF8 string) @@ -63,9 +64,14 @@ const pinned = {}; // map of pinned files // define a function: 'load' which takes a config // and a callback module.exports.load = function (config, cb) { + var filePath = config.filePath || './datastore'; + var blobPath = config.blobPath || './blob'; + var pinPath = config.pinPath || './pins'; + + nThen((waitFor) => { // read the subdirectories in the datastore - Fs.readdir('../datastore', waitFor((err, list) => { + Fs.readdir(filePath, waitFor((err, list) => { if (err) { throw err; } dirList = list; })); @@ -76,15 +82,15 @@ module.exports.load = function (config, cb) { sema.take((returnAfter) => { // get the list of files in every subdirectory // and push them to 'fileList' - Fs.readdir('../datastore/' + f, waitFor(returnAfter((err, list2) => { + Fs.readdir(Path.join(filePath, f), waitFor(returnAfter((err, list2) => { if (err) { throw err; } - list2.forEach((ff) => { fileList.push('../datastore/' + f + '/' + ff); }); + list2.forEach((ff) => { fileList.push(Path.join(filePath, f, ff)); }); }))); }); }); }).nThen((waitFor) => { // read the subdirectories in 'blob' - Fs.readdir('../blob', waitFor((err, list) => { + Fs.readdir(blobPath, waitFor((err, list) => { if (err) { throw err; } // overwrite dirList dirList = list; @@ -96,9 +102,9 @@ module.exports.load = function (config, cb) { sema.take((returnAfter) => { // get the list of files in every subdirectory // and push them to 'fileList' - Fs.readdir('../blob/' + f, waitFor(returnAfter((err, list2) => { + Fs.readdir(Path.join(blobPath, f), waitFor(returnAfter((err, list2) => { if (err) { throw err; } - list2.forEach((ff) => { fileList.push('../blob/' + f + '/' + ff); }); + list2.forEach((ff) => { fileList.push(Path.join(blobPath, f, ff)); }); }))); }); }); @@ -118,7 +124,7 @@ module.exports.load = function (config, cb) { }); }).nThen((waitFor) => { // read the subdirectories in the pinstore - Fs.readdir('../pins', waitFor((err, list) => { + Fs.readdir(pinPath, waitFor((err, list) => { if (err) { throw err; } dirList = list; })); @@ -131,9 +137,9 @@ module.exports.load = function (config, cb) { sema.take((returnAfter) => { // get the list of files in every subdirectory // and push them to 'fileList' (which is empty because we keep reusing it) - Fs.readdir('../pins/' + f, waitFor(returnAfter((err, list2) => { + Fs.readdir(Path.join(pinPath, f), waitFor(returnAfter((err, list2) => { if (err) { throw err; } - list2.forEach((ff) => { fileList.push('../pins/' + f + '/' + ff); }); + list2.forEach((ff) => { fileList.push(Path.join(pinPath, f, ff)); }); }))); }); });