ketr.photos/util/check.js
2018-10-08 10:56:14 -07:00

79 lines
1.8 KiB
JavaScript

"use strict";
const fs = require("fs"),
Promise = require("bluebird"),
config = require("config");
if (process.argv.length <= 2) {
console.log("usage: node check file|id [file|id] ... ");
process.exit(-1);
}
const items = process.argv.splice(2);
const picturesPath = config.get("picturesPath").replace(/\/$/, "");
for (let i = 0; i < items.length; i++) {
if (parseInt(items[i]) == items[i]) {
items[i] = {
type: "id",
value: items[i]
};
} else {
items[i] = {
type: "file",
value: items[i]
}
}
}
const exists = function(path) {
return stat(path).then(function() {
return true;
}).catch(function() {
return false;
});
}
const stat = function (_path) {
if (_path.indexOf(picturesPath.replace(/\/$/, "")) == 0) {
_path = _path.substring(picturesPath.length);
}
let path = picturesPath + _path;
return new Promise(function (resolve, reject) {
fs.stat(path, function (error, stats) {
if (error) {
return reject(error);
}
return resolve(stats);
});
});
}
require("../server/db/photos").then(function(db) {
const photoDB = db;
return Promise.mapSeries(items, function(item) {
switch (item.type) {
case "file":
return exists(item.value).then(function(exists) {
console.log("File '" + item.value + "' " + (exists ? "exists" : "does not exist."));
});
case "id":
return photoDB.sequelize.query("SELECT * FROM photos WHERE id=:id", {
replacements: {
id: item.value
},
type: photoDB.Sequelize.QueryTypes.SELECT,
raw: true
}).then(function(photos) {
if (photos.length == 0) {
console.log("Item " + item.value + " does not exist.");
} else {
console.log("Item " + item.value + " exists:", photos[0]);
}
});
}
});
});