Update deleted= based on presence of item

This commit is contained in:
James Ketrenos 2018-10-08 14:24:33 -07:00
parent d4a08ac45a
commit 36716f091c

View File

@ -59,33 +59,44 @@ const imageExtensions = [ "jpg", "jpeg", "png", "gif", "nef" ],
function getAssetInfoFromDisk(asset, filepath) { function getAssetInfoFromDisk(asset, filepath) {
return exists(filepath).then(function(stats) { return exists(filepath).then(function(stats) {
asset.filepath = filepath; asset.filepath = filepath;
if (stats) { if (stats) {
asset.stats = stats; asset.stats = stats;
} }
/* Check if entry exists in DB */ /* Check if entry exists in DB */
let parts = /^(.*)\/([^/]+)$/.exec(filepath), path, filename; let parts = /^(.*)\/([^/]+)$/.exec(filepath);
if (parts) { if (parts) {
path = parts[1] + "/"; asset.path = parts[1] + "/";
filename = parts[2]; asset.filename = parts[2];
} else { } else {
path = ""; asset.path = "";
filename = filepath; asset.filename = filepath;
} }
asset.name = asset.filename.replace(/\.[^.]*$/, "");
return photoDB.sequelize.query("SELECT photos.*,(albums.path || photos.filename) AS filepath " + return photoDB.sequelize.query("SELECT photos.*,(albums.path || photos.filename) AS filepath " +
"FROM photos LEFT JOIN albums ON albums.id=photos.albumId " + "FROM photos LEFT JOIN albums ON albums.id=photos.albumId " +
"WHERE albums.path=:path AND photos.filename=:filename", { "WHERE albums.path=:path AND photos.filename=:filename", {
replacements: { replacements: asset,
path: path,
filename: filename
},
type: photoDB.Sequelize.QueryTypes.SELECT, type: photoDB.Sequelize.QueryTypes.SELECT,
raw: true raw: true
}).then(function(photos) { }).then(function(photos) {
if (photos.length == 1) { if (photos.length == 1) {
Object.assign(asset, photos[0]); Object.assign(asset, photos[0]);
} }
return photoDB.sequelize.query("SELECT * FROM albums WHERE path=:path", {
replacements: asset,
type: photoDB.Sequelize.QueryTypes.SELECT,
raw: true
}).then(function(albums) {
if (albums.length == 1) {
asset.album = albums[0];
asset.albumId = asset.album.id;
}
});
}); });
}).then(function() { }).then(function() {
return asset; return asset;
@ -138,27 +149,52 @@ require("../server/db/photos").then(function(db) {
return getAssetInfoFromDB(asset, item.value); return getAssetInfoFromDB(asset, item.value);
break; break;
} }
}).then(function(asset) { }).then(function() {
if (!asset.stats && !asset.id) { if (!asset.stats && !asset.id) {
console.log("Item " + item.value + " exists neither on disk nor in the DB."); throw "Item " + item.value + " exists neither on disk nor in the DB.";
}
}).then(function() {
if (asset.id) {
return; return;
} }
if (!asset.id) { console.log("Item " + asset.filepath + " does not exist in the DB.");
console.log("Item " + asset.filepath + " does not exist in the DB."); asset.size = asset.stats.size;
} asset.needsUpdate = true;
return photoDB.sequelize.query("INSERT INTO photos " +
if (!asset.stats) { "(albumId,filename,name,size) VALUES(:albumId,:filename,:name,:size)", {
console.log("Item " + asset.id + " does not exist on disk."); replacements: asset
} else { }).spread(function(results, metadata) {
asset.id = metadata.lastID;
});
}).then(function() {
if (asset.stats) {
if (asset.modified > asset.stats.mtime) { if (asset.modified > asset.stats.mtime) {
asset.needsUpdate = true; asset.needsUpdate = true;
} }
if (asset.deleted) {
console.log("...setting deleted=0 for " + asset.id);
return photoDB.sequelize.query("UPDATE photos SET deleted=0 WHERE id=:id", {
replacements: asset
});
}
return;
} }
console.log("Item " + asset.filepath + " does not exist on disk.");
if (!asset.deleted) {
console.log("...setting deleted=1 for " + asset.id);
return photoDB.sequelize.query("UPDATE photos SET deleted=1 WHERE id=:id", {
replacements: asset
});
}
}).then(function() {
if (asset.needsUpdate) { if (asset.needsUpdate) {
console.log("DB needs to be updated for " + item.value); console.log("DB needs to be updated for " + item.value);
} }
}) }).catch(function(error) {
console.log(error);
});
}); });
}); });