82 lines
2.1 KiB
JavaScript
82 lines
2.1 KiB
JavaScript
"use strict";
|
|
|
|
const db = require("sequelize"),
|
|
Promise = require("bluebird"),
|
|
fs = require("fs"),
|
|
config = require("config");
|
|
|
|
let scanning = 0;
|
|
|
|
const picturesPath = config.get("picturesPath");
|
|
|
|
function scanFile(path, stats) {
|
|
return new Promise(function(resolve, reject) {
|
|
const meta = { filepath: path };
|
|
console.log("Scanning file: " + path);
|
|
meta.created = stats.ctime;
|
|
return resolve(meta);
|
|
});
|
|
}
|
|
|
|
function scanDir(path) {
|
|
return new Promise(function(resolve, reject) {
|
|
const meta = { filepath: path, files: [], paths: [] };
|
|
|
|
console.log("Scanning path " + path);
|
|
|
|
fs.readdir(path, function(err, files) {
|
|
if (err) {
|
|
console.warn(" Could not readdir " + path);
|
|
return resolve(null);
|
|
}
|
|
|
|
scanning++;
|
|
|
|
return Promise.map(files, function(file) {
|
|
let filepath = path + "/" + file;
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
fs.stat(filepath, function(err, stats) {
|
|
if (err) {
|
|
console.warn("Could not stat " + filepath);
|
|
return resolve(null);
|
|
}
|
|
|
|
if (stats.isDirectory()) {
|
|
return scanDir(filepath, stats).then(function(entry) {
|
|
if (entry && (entry.files.length || entry.paths.length)) {
|
|
meta.paths.push(entry);
|
|
}
|
|
return resolve(entry);
|
|
});
|
|
}
|
|
|
|
/* stats.isFile() */
|
|
return scanFile(filepath, stats).then(function(entry) {
|
|
if (entry) {
|
|
meta.files.push(entry);
|
|
}
|
|
return resolve(entry);
|
|
});
|
|
});
|
|
});
|
|
}, {
|
|
concurrency: 10
|
|
}).then(function() {
|
|
scanning--;
|
|
if (scanning == 0) {
|
|
const endStamp = Date.now();
|
|
console.log("Scanning completed in " + Math.round(((endStamp - startStamp))) + "ms.");
|
|
}
|
|
}).then(function() {
|
|
return resolve(meta);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
const startStamp = Date.now();
|
|
|
|
module.exports = scanDir(picturesPath);
|