2023-10-20 22:35:26 +08:00
|
|
|
// SPDX-FileCopyrightText: 2023 XWiki CryptPad Team <contact@cryptpad.org> and contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
2017-12-06 00:48:30 +08:00
|
|
|
/* jshint esversion: 6, node: true */
|
|
|
|
const Fs = require('fs');
|
2019-04-11 16:27:52 +08:00
|
|
|
const Path = require("path");
|
2017-12-06 00:48:30 +08:00
|
|
|
const Semaphore = require('saferphore');
|
2019-04-11 16:27:52 +08:00
|
|
|
const Once = require("../lib/once");
|
2017-12-06 00:48:30 +08:00
|
|
|
const nThen = require('nthen');
|
2019-04-17 20:46:46 +08:00
|
|
|
const Pins = require("../lib/pins");
|
2017-12-06 00:48:30 +08:00
|
|
|
|
|
|
|
const sema = Semaphore.create(20);
|
|
|
|
|
|
|
|
let dirList;
|
|
|
|
const fileList = [];
|
|
|
|
const pinned = {};
|
|
|
|
|
2018-04-04 18:06:59 +08:00
|
|
|
module.exports.load = function (cb, config) {
|
2019-04-11 16:27:52 +08:00
|
|
|
var pinPath = config.pinPath || './pins';
|
|
|
|
var done = Once(cb);
|
|
|
|
|
2017-12-06 00:48:30 +08:00
|
|
|
nThen((waitFor) => {
|
2019-04-11 16:27:52 +08:00
|
|
|
// recurse over the configured pinPath, or the default
|
|
|
|
Fs.readdir(pinPath, waitFor((err, list) => {
|
2017-12-20 01:21:29 +08:00
|
|
|
if (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
dirList = [];
|
2019-04-11 16:27:52 +08:00
|
|
|
return; // this ends up calling back with an empty object
|
2017-12-20 01:21:29 +08:00
|
|
|
}
|
2019-04-11 16:27:52 +08:00
|
|
|
waitFor.abort();
|
|
|
|
return void done(err);
|
2017-12-20 01:21:29 +08:00
|
|
|
}
|
2017-12-06 00:48:30 +08:00
|
|
|
dirList = list;
|
|
|
|
}));
|
|
|
|
}).nThen((waitFor) => {
|
|
|
|
dirList.forEach((f) => {
|
|
|
|
sema.take((returnAfter) => {
|
2019-04-11 16:27:52 +08:00
|
|
|
// iterate over all the subdirectories in the pin store
|
|
|
|
Fs.readdir(Path.join(pinPath, f), waitFor(returnAfter((err, list2) => {
|
|
|
|
if (err) {
|
|
|
|
waitFor.abort();
|
|
|
|
return void done(err);
|
|
|
|
}
|
2018-04-04 18:06:59 +08:00
|
|
|
list2.forEach((ff) => {
|
|
|
|
if (config && config.exclude && config.exclude.indexOf(ff) > -1) { return; }
|
2019-04-11 16:27:52 +08:00
|
|
|
fileList.push(Path.join(pinPath, f, ff));
|
2018-04-04 18:06:59 +08:00
|
|
|
});
|
2017-12-06 00:48:30 +08:00
|
|
|
})));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).nThen((waitFor) => {
|
|
|
|
fileList.forEach((f) => {
|
|
|
|
sema.take((returnAfter) => {
|
|
|
|
Fs.readFile(f, waitFor(returnAfter((err, content) => {
|
2019-04-11 16:27:52 +08:00
|
|
|
if (err) {
|
|
|
|
waitFor.abort();
|
|
|
|
return void done(err);
|
|
|
|
}
|
2019-04-17 20:46:46 +08:00
|
|
|
const hashes = Pins.calculateFromLog(content.toString('utf8'), f);
|
2017-12-06 00:48:30 +08:00
|
|
|
hashes.forEach((x) => {
|
|
|
|
(pinned[x] = pinned[x] || {})[f.replace(/.*\/([^/]*).ndjson$/, (x, y)=>y)] = 1;
|
|
|
|
});
|
|
|
|
})));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).nThen(() => {
|
2019-04-11 16:27:52 +08:00
|
|
|
done(void 0, pinned);
|
2017-12-06 00:48:30 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!module.parent) {
|
2019-04-11 16:27:52 +08:00
|
|
|
module.exports.load(function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return void console.error(err);
|
|
|
|
}
|
|
|
|
|
2017-12-06 00:48:30 +08:00
|
|
|
Object.keys(data).forEach(function (x) {
|
|
|
|
console.log(x + ' ' + JSON.stringify(data[x]));
|
|
|
|
});
|
2019-04-11 16:27:52 +08:00
|
|
|
}, {
|
2019-04-17 19:48:39 +08:00
|
|
|
pinPath: require("../lib/load-config").pinPath
|
2017-12-06 00:48:30 +08:00
|
|
|
});
|
2018-04-04 18:06:59 +08:00
|
|
|
}
|