Fix delete photos to also delete faces and other foreign keys
Signed-off-by: James Ketrenos <james_git@ketrenos.com>
This commit is contained in:
parent
132803eba3
commit
39069e660e
@ -365,16 +365,19 @@ router.get("/status/:token", function(req, res) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 1. Look if there are duplicates.
|
* 1. Look if there are duplicates.
|
||||||
* 2. Update all other duplicates to be duplicates of the first image that remains
|
* 2. Update all other duplicates to be duplicates of the first image that
|
||||||
|
* remains
|
||||||
* 3. If no duplicates, DELETE the entry from referencing tables:
|
* 3. If no duplicates, DELETE the entry from referencing tables:
|
||||||
* photohashes, faces
|
* photohashes, faces, and any face{descriptor,distance} using that face
|
||||||
* 4. If there are duplicates, update the HASH (and any FACE) entry to point to the first image that remains
|
* 4. If there are duplicates, update the HASH (and any FACE) entry to point
|
||||||
|
* to the first image that remains
|
||||||
* 5. Delete the entry from photos
|
* 5. Delete the entry from photos
|
||||||
* 6. Delete the scaled, thumb, and original from disk
|
* 6. Delete the scaled, thumb, and original from disk
|
||||||
*/
|
*/
|
||||||
const deletePhoto = function(photo) {
|
const deletePhoto = function(photo) {
|
||||||
return photoDB.sequelize.transaction(function(transaction) {
|
return photoDB.sequelize.transaction(function(transaction) {
|
||||||
return photoDB.sequelize.query("SELECT id FROM photos WHERE duplicate=:id", {
|
return photoDB.sequelize.query(
|
||||||
|
"SELECT id FROM photos WHERE duplicate=:id", {
|
||||||
replacements: photo,
|
replacements: photo,
|
||||||
type: photoDB.Sequelize.QueryTypes.SELECT,
|
type: photoDB.Sequelize.QueryTypes.SELECT,
|
||||||
raw: true
|
raw: true
|
||||||
@ -393,7 +396,8 @@ const deletePhoto = function(photo) {
|
|||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Update all other duplicates to be duplicates of the first image that remains
|
// 2. Update all other duplicates to be duplicates of the first image
|
||||||
|
// that remains
|
||||||
console.log("Updating " + needsUpdate + " to point to " + first.id);
|
console.log("Updating " + needsUpdate + " to point to " + first.id);
|
||||||
return photoDB.sequelize.query(
|
return photoDB.sequelize.query(
|
||||||
"UPDATE photos SET duplicate=:first WHERE id IN (:needsUpdate)", {
|
"UPDATE photos SET duplicate=:first WHERE id IN (:needsUpdate)", {
|
||||||
@ -409,10 +413,43 @@ const deletePhoto = function(photo) {
|
|||||||
if (!first) {
|
if (!first) {
|
||||||
console.log("Deleting " + photo.id + " from photohash.");
|
console.log("Deleting " + photo.id + " from photohash.");
|
||||||
// 3. If no duplicates, DELETE the entry from photohashes and faces
|
// 3. If no duplicates, DELETE the entry from photohashes and faces
|
||||||
|
// and any face{descriptor,distance} using that face
|
||||||
return photoDB.sequelize.query(
|
return photoDB.sequelize.query(
|
||||||
"DELETE FROM photohashes WHERE photoId=:id", {
|
"DELETE FROM photohashes WHERE photoId=:id", {
|
||||||
replacements: photo,
|
replacements: photo,
|
||||||
transaction: transaction
|
transaction: transaction
|
||||||
|
}).then(() => {
|
||||||
|
return photoDB.sequelize.query(
|
||||||
|
"SELECT id FROM faces WHERE photoId=:id", {
|
||||||
|
replacements: photo,
|
||||||
|
type: photoDB.Sequelize.QueryTypes.SELECT,
|
||||||
|
raw: true,
|
||||||
|
transaction: transaction
|
||||||
|
}).then((faces) => {
|
||||||
|
if (faces.length == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const ids = faces.map(face => face.id);
|
||||||
|
console.log(`Deleting faces: ${JSON.stringify(ids, null, 2)}`);
|
||||||
|
return photoDB.sequelize.query(
|
||||||
|
"DELETE FROM facedistances " +
|
||||||
|
"WHERE face1Id IN (:ids) " +
|
||||||
|
"OR face2Id IN (:ids)", {
|
||||||
|
replacements: { ids },
|
||||||
|
transaction: transaction
|
||||||
|
}).then(() => {
|
||||||
|
return photoDB.sequelize.query(
|
||||||
|
"DELETE FROM facedescriptors WHERE faceId IN (:ids)", {
|
||||||
|
replacements: { ids },
|
||||||
|
transaction: transaction
|
||||||
|
}).then(() => {
|
||||||
|
return photoDB.sequelize.query(
|
||||||
|
"DELETE FROM faces WHERE id IN (:ids)", {
|
||||||
|
replacements: { ids },
|
||||||
|
transaction: transaction
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
console.log("Deleting " + photo.id + " from faces.");
|
console.log("Deleting " + photo.id + " from faces.");
|
||||||
// 3. If no duplicates, DELETE the entry from photohashes
|
// 3. If no duplicates, DELETE the entry from photohashes
|
||||||
@ -422,6 +459,7 @@ const deletePhoto = function(photo) {
|
|||||||
transaction: transaction
|
transaction: transaction
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
console.log("Updating photohash for " + photo.id + " to point to " + first.id);
|
console.log("Updating photohash for " + photo.id + " to point to " + first.id);
|
||||||
// 4. If there are duplicates, update the HASH entry to point to the first image that remains
|
// 4. If there are duplicates, update the HASH entry to point to the first image that remains
|
||||||
@ -461,6 +499,8 @@ const deletePhoto = function(photo) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
}).then(() => {
|
||||||
|
console.log(`Completed deleting ${photo.id}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user