use configured paths in scripts instead of hardcoded strings

pull/1/head
ansuz 6 years ago
parent 36637c4a7f
commit c3d52e87b9

@ -3,6 +3,8 @@ const Fs = require('fs');
const nThen = require('nthen'); const nThen = require('nthen');
const Pinned = require('./pinned'); const Pinned = require('./pinned');
const Nacl = require('tweetnacl'); const Nacl = require('tweetnacl');
const Path = require('path');
const Config = require('./load-config');
const hashesFromPinFile = (pinFile, fileName) => { const hashesFromPinFile = (pinFile, fileName) => {
var pins = {}; var pins = {};
@ -54,7 +56,9 @@ let data = [];
let pinned = []; let pinned = [];
nThen((waitFor) => { 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) => { Fs.readFile(f, waitFor((err, content) => {
if (err) { throw err; } if (err) { throw err; }
pinned = hashesFromPinFile(content.toString('utf8'), f); pinned = hashesFromPinFile(content.toString('utf8'), f);

@ -5,9 +5,9 @@ const Saferphore = require("saferphore");
const PinnedData = require('./pinneddata'); const PinnedData = require('./pinneddata');
let config; let config;
try { try {
config = require('../config/config'); config = require('./config/config');
} catch (e) { } catch (e) {
config = require('../config/config.example'); config = require('./config/config.example');
} }
if (!config.inactiveTime || typeof(config.inactiveTime) !== "number") { return; } if (!config.inactiveTime || typeof(config.inactiveTime) !== "number") { return; }
@ -16,7 +16,11 @@ let inactiveTime = +new Date() - (config.inactiveTime * 24 * 3600 * 1000);
let inactiveConfig = { let inactiveConfig = {
unpinned: true, unpinned: true,
olderthan: inactiveTime, olderthan: inactiveTime,
blobsolderthan: inactiveTime blobsolderthan: inactiveTime,
filePath: config.filePath,
blobPath: config.blobPath,
pinPath: config.pinPath,
}; };
let toDelete; let toDelete;
nThen(function (waitFor) { nThen(function (waitFor) {

@ -3,12 +3,7 @@ var Path = require("path");
var nThen = require("nthen"); var nThen = require("nthen");
var config; var config = require("./load-config");
try {
config = require('../config/config');
} catch (e) {
config = require('../config/config.example');
}
var FileStorage = require('../' + config.storage || './storage/file'); var FileStorage = require('../' + config.storage || './storage/file');
var root = Path.resolve('../' + config.taskPath || './tasks'); var root = Path.resolve('../' + config.taskPath || './tasks');
@ -52,10 +47,12 @@ var handleTask = function (str, path, cb) {
nThen(function (waitFor) { nThen(function (waitFor) {
switch (command) { switch (command) {
case 'EXPIRE': case 'EXPIRE':
// FIXME noisy!
console.log("expiring: %s", args[0]); console.log("expiring: %s", args[0]);
store.removeChannel(args[0], waitFor()); store.removeChannel(args[0], waitFor());
break; break;
default: default:
// FIXME noisy
console.log("unknown command", command); console.log("unknown command", command);
} }
}).nThen(function () { }).nThen(function () {
@ -83,6 +80,7 @@ nt = nThen(function (w) {
}).nThen(function () { }).nThen(function () {
dirs.forEach(function (dir, dIdx) { dirs.forEach(function (dir, dIdx) {
queue(function (w) { queue(function (w) {
// FIXME noisy!
console.log('recursing into %s', dir); console.log('recursing into %s', dir);
Fs.readdir(Path.join(root, dir), w(function (e, list) { Fs.readdir(Path.join(root, dir), w(function (e, list) {
list.forEach(function (fn) { list.forEach(function (fn) {
@ -90,6 +88,7 @@ nt = nThen(function (w) {
var filePath = Path.join(root, dir, fn); var filePath = Path.join(root, dir, fn);
var cb = w(); var cb = w();
// FIXME noisy!
console.log("processing file at %s", filePath); console.log("processing file at %s", filePath);
Fs.readFile(filePath, 'utf8', function (e, str) { Fs.readFile(filePath, 'utf8', function (e, str) {
if (e) { if (e) {

@ -0,0 +1,7 @@
var config;
try {
config = require("../config/config");
} catch (e) {
config = require("../config/config.example");
}
module.exports = config;

@ -11,6 +11,8 @@ let dirList;
const fileList = []; const fileList = [];
const pinned = {}; 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) => { const checkPinStatus = (pinFile, fileName) => {
var pins = {}; var pins = {};
pinFile.split('\n').filter((x)=>(x)).map((l) => JSON.parse(l)).forEach((l) => { pinFile.split('\n').filter((x)=>(x)).map((l) => JSON.parse(l)).forEach((l) => {

@ -2,6 +2,7 @@
const Fs = require('fs'); const Fs = require('fs');
const Semaphore = require('saferphore'); const Semaphore = require('saferphore');
const nThen = require('nthen'); const nThen = require('nthen');
const Path = require('path');
/* /*
takes contents of a pinFile (UTF8 string) 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 // define a function: 'load' which takes a config
// and a callback // and a callback
module.exports.load = function (config, cb) { module.exports.load = function (config, cb) {
var filePath = config.filePath || './datastore';
var blobPath = config.blobPath || './blob';
var pinPath = config.pinPath || './pins';
nThen((waitFor) => { nThen((waitFor) => {
// read the subdirectories in the datastore // read the subdirectories in the datastore
Fs.readdir('../datastore', waitFor((err, list) => { Fs.readdir(filePath, waitFor((err, list) => {
if (err) { throw err; } if (err) { throw err; }
dirList = list; dirList = list;
})); }));
@ -76,15 +82,15 @@ module.exports.load = function (config, cb) {
sema.take((returnAfter) => { sema.take((returnAfter) => {
// get the list of files in every subdirectory // get the list of files in every subdirectory
// and push them to 'fileList' // 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; } if (err) { throw err; }
list2.forEach((ff) => { fileList.push('../datastore/' + f + '/' + ff); }); list2.forEach((ff) => { fileList.push(Path.join(filePath, f, ff)); });
}))); })));
}); });
}); });
}).nThen((waitFor) => { }).nThen((waitFor) => {
// read the subdirectories in 'blob' // read the subdirectories in 'blob'
Fs.readdir('../blob', waitFor((err, list) => { Fs.readdir(blobPath, waitFor((err, list) => {
if (err) { throw err; } if (err) { throw err; }
// overwrite dirList // overwrite dirList
dirList = list; dirList = list;
@ -96,9 +102,9 @@ module.exports.load = function (config, cb) {
sema.take((returnAfter) => { sema.take((returnAfter) => {
// get the list of files in every subdirectory // get the list of files in every subdirectory
// and push them to 'fileList' // 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; } 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) => { }).nThen((waitFor) => {
// read the subdirectories in the pinstore // read the subdirectories in the pinstore
Fs.readdir('../pins', waitFor((err, list) => { Fs.readdir(pinPath, waitFor((err, list) => {
if (err) { throw err; } if (err) { throw err; }
dirList = list; dirList = list;
})); }));
@ -131,9 +137,9 @@ module.exports.load = function (config, cb) {
sema.take((returnAfter) => { sema.take((returnAfter) => {
// get the list of files in every subdirectory // get the list of files in every subdirectory
// and push them to 'fileList' (which is empty because we keep reusing it) // 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; } if (err) { throw err; }
list2.forEach((ff) => { fileList.push('../pins/' + f + '/' + ff); }); list2.forEach((ff) => { fileList.push(Path.join(pinPath, f, ff)); });
}))); })));
}); });
}); });

Loading…
Cancel
Save