use configured pinPath and handle errors when checking pin status

pull/1/head
ansuz 6 years ago
parent 6d015a2b9f
commit e17bad7fb7

@ -245,6 +245,7 @@ var loadUserPins = function (Env, publicKey, cb) {
}; };
var unpin = function (channel) { var unpin = function (channel) {
// TODO delete?
pins[channel] = false; pins[channel] = false;
}; };
@ -263,6 +264,7 @@ var loadUserPins = function (Env, publicKey, cb) {
parsed[1].forEach(unpin); parsed[1].forEach(unpin);
break; break;
case 'RESET': case 'RESET':
// TODO just wipe out the object?
Object.keys(pins).forEach(unpin); Object.keys(pins).forEach(unpin);
if (parsed[1] && parsed[1].length) { if (parsed[1] && parsed[1].length) {
@ -600,9 +602,21 @@ var sumChannelSizes = function (sizes) {
// inform that the // inform that the
var loadChannelPins = function (Env) { var loadChannelPins = function (Env) {
Pinned.load(function (data) { Pinned.load(function (err, data) {
if (err) {
Log.error("LOAD_CHANNEL_PINS", err);
// FIXME not sure what should be done here instead
Env.pinnedPads = {};
Env.evPinnedPadsReady.fire();
return;
}
Env.pinnedPads = data; Env.pinnedPads = data;
Env.evPinnedPadsReady.fire(); Env.evPinnedPadsReady.fire();
}, {
pinPath: Env.paths.pin,
}); });
}; };
var addPinned = function ( var addPinned = function (

@ -1,6 +1,8 @@
/* jshint esversion: 6, node: true */ /* jshint esversion: 6, node: true */
const Fs = require('fs'); const Fs = require('fs');
const Path = require("path");
const Semaphore = require('saferphore'); const Semaphore = require('saferphore');
const Once = require("../lib/once");
const nThen = require('nthen'); const nThen = require('nthen');
const sema = Semaphore.create(20); const sema = Semaphore.create(20);
@ -9,7 +11,7 @@ let dirList;
const fileList = []; const fileList = [];
const pinned = {}; const pinned = {};
const hashesFromPinFile = (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) => {
switch (l[0]) { switch (l[0]) {
@ -34,25 +36,34 @@ const hashesFromPinFile = (pinFile, fileName) => {
}; };
module.exports.load = function (cb, config) { module.exports.load = function (cb, config) {
var pinPath = config.pinPath || './pins';
var done = Once(cb);
nThen((waitFor) => { nThen((waitFor) => {
Fs.readdir('../pins', waitFor((err, list) => { // recurse over the configured pinPath, or the default
Fs.readdir(pinPath, waitFor((err, list) => {
if (err) { if (err) {
if (err.code === 'ENOENT') { if (err.code === 'ENOENT') {
dirList = []; dirList = [];
return; return; // this ends up calling back with an empty object
} }
throw err; waitFor.abort();
return void done(err);
} }
dirList = list; dirList = list;
})); }));
}).nThen((waitFor) => { }).nThen((waitFor) => {
dirList.forEach((f) => { dirList.forEach((f) => {
sema.take((returnAfter) => { sema.take((returnAfter) => {
Fs.readdir('../pins/' + f, waitFor(returnAfter((err, list2) => { // iterate over all the subdirectories in the pin store
if (err) { throw err; } Fs.readdir(Path.join(pinPath, f), waitFor(returnAfter((err, list2) => {
if (err) {
waitFor.abort();
return void done(err);
}
list2.forEach((ff) => { list2.forEach((ff) => {
if (config && config.exclude && config.exclude.indexOf(ff) > -1) { return; } if (config && config.exclude && config.exclude.indexOf(ff) > -1) { return; }
fileList.push('../pins/' + f + '/' + ff); fileList.push(Path.join(pinPath, f, ff));
}); });
}))); })));
}); });
@ -61,8 +72,11 @@ module.exports.load = function (cb, config) {
fileList.forEach((f) => { fileList.forEach((f) => {
sema.take((returnAfter) => { sema.take((returnAfter) => {
Fs.readFile(f, waitFor(returnAfter((err, content) => { Fs.readFile(f, waitFor(returnAfter((err, content) => {
if (err) { throw err; } if (err) {
const hashes = hashesFromPinFile(content.toString('utf8'), f); waitFor.abort();
return void done(err);
}
const hashes = checkPinStatus(content.toString('utf8'), f);
hashes.forEach((x) => { hashes.forEach((x) => {
(pinned[x] = pinned[x] || {})[f.replace(/.*\/([^/]*).ndjson$/, (x, y)=>y)] = 1; (pinned[x] = pinned[x] || {})[f.replace(/.*\/([^/]*).ndjson$/, (x, y)=>y)] = 1;
}); });
@ -70,14 +84,20 @@ module.exports.load = function (cb, config) {
}); });
}); });
}).nThen(() => { }).nThen(() => {
cb(pinned); done(void 0, pinned);
}); });
}; };
if (!module.parent) { if (!module.parent) {
module.exports.load(function (data) { module.exports.load(function (err, data) {
if (err) {
return void console.error(err);
}
Object.keys(data).forEach(function (x) { Object.keys(data).forEach(function (x) {
console.log(x + ' ' + JSON.stringify(data[x])); console.log(x + ' ' + JSON.stringify(data[x]));
}); });
}, {
pinPath: require("../config/config").pinPath
}); });
} }

Loading…
Cancel
Save