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,26 +832,32 @@ class Photo extends Module {
# For each photo # For each photo
while ($photo = $photos->fetch_object()) { while ($photo = $photos->fetch_object()) {
# Get retina thumb url # Check if other photos are referring to this images
$thumbUrl2x = explode(".", $photo->thumbUrl); # If so, only delete the db entry
$thumbUrl2x = $thumbUrl2x[0] . '@2x.' . $thumbUrl2x[1]; if ($this->exists($photo->checksum, $photo->id)===false) {
# Delete big # Get retina thumb url
if (file_exists(LYCHEE_UPLOADS_BIG . $photo->url)&&!unlink(LYCHEE_UPLOADS_BIG . $photo->url)) { $thumbUrl2x = explode(".", $photo->thumbUrl);
Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/big/'); $thumbUrl2x = $thumbUrl2x[0] . '@2x.' . $thumbUrl2x[1];
return false;
}
# Delete thumb # Delete big
if (file_exists(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)&&!unlink(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)) { if (file_exists(LYCHEE_UPLOADS_BIG . $photo->url)&&!unlink(LYCHEE_UPLOADS_BIG . $photo->url)) {
Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/thumb/'); Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/big/');
return false; return false;
} }
# Delete thumb
if (file_exists(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)&&!unlink(LYCHEE_UPLOADS_THUMB . $photo->thumbUrl)) {
Log::error($this->database, __METHOD__, __LINE__, 'Could not delete photo in uploads/thumb/');
return false;
}
# Delete thumb@2x
if (file_exists(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)&&!unlink(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)) {
Log::error($this->database, __METHOD__, __LINE__, 'Could not delete high-res photo in uploads/thumb/');
return false;
}
# Delete thumb@2x
if (file_exists(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)&&!unlink(LYCHEE_UPLOADS_THUMB . $thumbUrl2x)) {
Log::error($this->database, __METHOD__, __LINE__, 'Could not delete high-res photo in uploads/thumb/');
return false;
} }
# Delete db entry # Delete db entry