99 lines
2.6 KiB
JavaScript
99 lines
2.6 KiB
JavaScript
"use strict";
|
|
|
|
const Promise = require("bluebird"),
|
|
fs = require("fs"),
|
|
config = require("config");
|
|
|
|
let scanning = 0;
|
|
|
|
let photoDB = null;
|
|
|
|
const picturesPath = config.get("picturesPath");
|
|
|
|
function scanFile(path, file, stats) {
|
|
return new Promise(function(resolve, reject) {
|
|
console.log("Scanning file: " + path + "/" + file);
|
|
return resolve(true);
|
|
});
|
|
}
|
|
|
|
function scanDir(path) {
|
|
return new Promise(function(resolve, reject) {
|
|
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(false);
|
|
}
|
|
|
|
if (stats.isDirectory()) {
|
|
return scanDir(filepath, stats).then(function(entry) {
|
|
return resolve(true);
|
|
});
|
|
}
|
|
|
|
/* stats.isFile() */
|
|
return scanFile(path, file, stats).then(function(entry) {
|
|
if (!entry) {
|
|
return resolve(false);
|
|
}
|
|
|
|
const replacements = {
|
|
path: path.slice(picturesPath.length),
|
|
filename: file,
|
|
added: new Date()
|
|
};
|
|
|
|
return photoDB.sequelize.query("SELECT id FROM photos WHERE path=:path AND filename=:filename", {
|
|
replacements: replacements,
|
|
type: photoDB.sequelize.QueryTypes.SELECT
|
|
}).then(function(photo) {
|
|
if (photo.length == 0) {
|
|
return photoDB.sequelize.query("INSERT INTO photos " +
|
|
"SET path=:path,filename=:filename,added=DATE(:added)", {
|
|
replacements: replacements
|
|
});
|
|
}
|
|
}).then(function() {
|
|
return resolve(true);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}, {
|
|
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();
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
const startStamp = Date.now();
|
|
|
|
module.exports = {
|
|
scan: function (db) {
|
|
photoDB = db;
|
|
return scanDir(picturesPath);
|
|
}
|
|
};
|