Don't delete photo when used elsewhere

This commit is contained in:
Tobias Reich 2014-08-22 22:54:33 +02:00
parent 47e3219966
commit 988a9075f3

View File

@ -210,12 +210,15 @@ class Photo extends Module {
} }
private function exists($checksum) { private function exists($checksum, $photoID = null) {
# Check dependencies # Check dependencies
self::dependencies(isset($this->database, $checksum)); self::dependencies(isset($this->database, $checksum));
$query = "SELECT id, url, thumbUrl FROM lychee_photos WHERE checksum = '$checksum' LIMIT 1;"; # Exclude $photoID from select when $photoID is set
if (isset($photoID)) $query = "SELECT id, url, thumbUrl FROM lychee_photos WHERE checksum = '$checksum' AND id <> '$photoID' LIMIT 1;";
else $query = "SELECT id, url, thumbUrl FROM lychee_photos WHERE checksum = '$checksum' LIMIT 1;";
$result = $this->database->query($query); $result = $this->database->query($query);
if (!$result) { if (!$result) {
@ -820,7 +823,7 @@ class Photo extends Module {
$this->plugins(__METHOD__, 0, func_get_args()); $this->plugins(__METHOD__, 0, func_get_args());
# Get photos # Get photos
$photos = $this->database->query("SELECT id, url, thumbUrl FROM lychee_photos WHERE id IN ($this->photoIDs);"); $photos = $this->database->query("SELECT id, url, thumbUrl, checksum FROM lychee_photos WHERE id IN ($this->photoIDs);");
if (!$photos) { if (!$photos) {
Log::error($this->database, __METHOD__, __LINE__, $this->database->error); Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
return false; return false;
@ -829,6 +832,10 @@ class Photo extends Module {
# For each photo # For each photo
while ($photo = $photos->fetch_object()) { while ($photo = $photos->fetch_object()) {
# Check if other photos are referring to this images
# If so, only delete the db entry
if ($this->exists($photo->checksum, $photo->id)===false) {
# Get retina thumb url # Get retina thumb url
$thumbUrl2x = explode(".", $photo->thumbUrl); $thumbUrl2x = explode(".", $photo->thumbUrl);
$thumbUrl2x = $thumbUrl2x[0] . '@2x.' . $thumbUrl2x[1]; $thumbUrl2x = $thumbUrl2x[0] . '@2x.' . $thumbUrl2x[1];
@ -851,6 +858,8 @@ class Photo extends Module {
return false; return false;
} }
}
# Delete db entry # Delete db entry
$delete = $this->database->query("DELETE FROM lychee_photos WHERE id = '$photo->id';"); $delete = $this->database->query("DELETE FROM lychee_photos WHERE id = '$photo->id';");
if (!$delete) { if (!$delete) {