File checks implemented.
This commit is contained in:
parent
d0ffda22b6
commit
d4a08ac45a
138
util/check.js
138
util/check.js
@ -10,7 +10,7 @@ if (process.argv.length <= 2) {
|
|||||||
}
|
}
|
||||||
const items = process.argv.splice(2);
|
const items = process.argv.splice(2);
|
||||||
|
|
||||||
const picturesPath = config.get("picturesPath").replace(/\/$/, "");
|
const picturesPath = config.get("picturesPath").replace(/\/$/, "") + "/";
|
||||||
|
|
||||||
for (let i = 0; i < items.length; i++) {
|
for (let i = 0; i < items.length; i++) {
|
||||||
if (parseInt(items[i]) == items[i]) {
|
if (parseInt(items[i]) == items[i]) {
|
||||||
@ -27,10 +27,10 @@ for (let i = 0; i < items.length; i++) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const exists = function(path) {
|
const exists = function(path) {
|
||||||
return stat(path).then(function() {
|
return stat(path).then(function(stats) {
|
||||||
return true;
|
return stats;
|
||||||
}).catch(function() {
|
}).catch(function() {
|
||||||
return false;
|
return null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,28 +51,114 @@ const stat = function (_path) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
require("../server/db/photos").then(function(db) {
|
const imageExtensions = [ "jpg", "jpeg", "png", "gif", "nef" ],
|
||||||
const photoDB = db;
|
imageRegExp = new RegExp("\.((" + imageExtensions.join(")|(") + "))$", "i"),
|
||||||
return Promise.mapSeries(items, function(item) {
|
videoExtensions = [ "mov", "avi", "mp4", "webm" ],
|
||||||
switch (item.type) {
|
videoRegExp = new RegExp("\.((" + videoExtensions.join(")|(") + "))$", "i");
|
||||||
case "file":
|
|
||||||
return exists(item.value).then(function(exists) {
|
function getAssetInfoFromDisk(asset, filepath) {
|
||||||
console.log("File '" + item.value + "' " + (exists ? "exists" : "does not exist."));
|
return exists(filepath).then(function(stats) {
|
||||||
});
|
asset.filepath = filepath;
|
||||||
case "id":
|
if (stats) {
|
||||||
return photoDB.sequelize.query("SELECT * FROM photos WHERE id=:id", {
|
asset.stats = stats;
|
||||||
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]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Check if entry exists in DB */
|
||||||
|
let parts = /^(.*)\/([^/]+)$/.exec(filepath), path, filename;
|
||||||
|
if (parts) {
|
||||||
|
path = parts[1] + "/";
|
||||||
|
filename = parts[2];
|
||||||
|
} else {
|
||||||
|
path = "";
|
||||||
|
filename = filepath;
|
||||||
|
}
|
||||||
|
|
||||||
|
return photoDB.sequelize.query("SELECT photos.*,(albums.path || photos.filename) AS filepath " +
|
||||||
|
"FROM photos LEFT JOIN albums ON albums.id=photos.albumId " +
|
||||||
|
"WHERE albums.path=:path AND photos.filename=:filename", {
|
||||||
|
replacements: {
|
||||||
|
path: path,
|
||||||
|
filename: filename
|
||||||
|
},
|
||||||
|
type: photoDB.Sequelize.QueryTypes.SELECT,
|
||||||
|
raw: true
|
||||||
|
}).then(function(photos) {
|
||||||
|
if (photos.length == 1) {
|
||||||
|
Object.assign(asset, photos[0]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}).then(function() {
|
||||||
|
return asset;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAssetInfoFromDB(asset, id) {
|
||||||
|
return photoDB.sequelize.query("SELECT photos.*,(albums.path || photos.filename) AS filepath " +
|
||||||
|
"FROM photos LEFT JOIN albums ON albums.id=photos.albumId " +
|
||||||
|
"WHERE photos.id=:id", {
|
||||||
|
replacements: {
|
||||||
|
id: id
|
||||||
|
},
|
||||||
|
type: photoDB.Sequelize.QueryTypes.SELECT,
|
||||||
|
raw: true
|
||||||
|
}).then(function(photos) {
|
||||||
|
if (photos.length == 1) {
|
||||||
|
Object.assign(asset, photos[0]);
|
||||||
|
}
|
||||||
|
}).then(function() {
|
||||||
|
if (!asset.filepath) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return exists(asset.filepath).then(function(stats) {
|
||||||
|
if (stats) {
|
||||||
|
asset.stats = stats;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}).then(function() {
|
||||||
|
return asset;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let photoDB;
|
||||||
|
|
||||||
|
require("../server/db/photos").then(function(db) {
|
||||||
|
photoDB = db;
|
||||||
|
|
||||||
|
return Promise.mapSeries(items, function(item) {
|
||||||
|
const asset = {};
|
||||||
|
|
||||||
|
return Promise.resolve().then(function() {
|
||||||
|
switch (item.type) {
|
||||||
|
case "file":
|
||||||
|
return getAssetInfoFromDisk(asset, item.value);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "id":
|
||||||
|
return getAssetInfoFromDB(asset, item.value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}).then(function(asset) {
|
||||||
|
if (!asset.stats && !asset.id) {
|
||||||
|
console.log("Item " + item.value + " exists neither on disk nor in the DB.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!asset.id) {
|
||||||
|
console.log("Item " + asset.filepath + " does not exist in the DB.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!asset.stats) {
|
||||||
|
console.log("Item " + asset.id + " does not exist on disk.");
|
||||||
|
} else {
|
||||||
|
if (asset.modified > asset.stats.mtime) {
|
||||||
|
asset.needsUpdate = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (asset.needsUpdate) {
|
||||||
|
console.log("DB needs to be updated for " + item.value);
|
||||||
|
}
|
||||||
|
})
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user