Update deleted= based on presence of item
This commit is contained in:
parent
d4a08ac45a
commit
36716f091c
@ -59,33 +59,44 @@ const imageExtensions = [ "jpg", "jpeg", "png", "gif", "nef" ],
|
||||
function getAssetInfoFromDisk(asset, filepath) {
|
||||
return exists(filepath).then(function(stats) {
|
||||
asset.filepath = filepath;
|
||||
|
||||
if (stats) {
|
||||
asset.stats = stats;
|
||||
}
|
||||
|
||||
/* Check if entry exists in DB */
|
||||
let parts = /^(.*)\/([^/]+)$/.exec(filepath), path, filename;
|
||||
let parts = /^(.*)\/([^/]+)$/.exec(filepath);
|
||||
if (parts) {
|
||||
path = parts[1] + "/";
|
||||
filename = parts[2];
|
||||
asset.path = parts[1] + "/";
|
||||
asset.filename = parts[2];
|
||||
} else {
|
||||
path = "";
|
||||
filename = filepath;
|
||||
asset.path = "";
|
||||
asset.filename = filepath;
|
||||
}
|
||||
|
||||
asset.name = asset.filename.replace(/\.[^.]*$/, "");
|
||||
|
||||
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
|
||||
},
|
||||
replacements: asset,
|
||||
type: photoDB.Sequelize.QueryTypes.SELECT,
|
||||
raw: true
|
||||
}).then(function(photos) {
|
||||
if (photos.length == 1) {
|
||||
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() {
|
||||
return asset;
|
||||
@ -138,27 +149,52 @@ require("../server/db/photos").then(function(db) {
|
||||
return getAssetInfoFromDB(asset, item.value);
|
||||
break;
|
||||
}
|
||||
}).then(function(asset) {
|
||||
}).then(function() {
|
||||
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;
|
||||
}
|
||||
|
||||
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 {
|
||||
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 " +
|
||||
"(albumId,filename,name,size) VALUES(:albumId,:filename,:name,:size)", {
|
||||
replacements: asset
|
||||
}).spread(function(results, metadata) {
|
||||
asset.id = metadata.lastID;
|
||||
});
|
||||
}).then(function() {
|
||||
if (asset.stats) {
|
||||
if (asset.modified > asset.stats.mtime) {
|
||||
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) {
|
||||
console.log("DB needs to be updated for " + item.value);
|
||||
}
|
||||
})
|
||||
}).catch(function(error) {
|
||||
console.log(error);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user