]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Photo.php
Revert Photo::getImageDataForPhoto return-type change
[friendica.git] / src / Model / Photo.php
index 30e666898777c5b4a9c245d5a31b611b9373b932..a38743d9d6adbfa5b3d10f13faedabd7b40db9a5 100644 (file)
@@ -28,6 +28,8 @@ use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
 use Friendica\DI;
 use Friendica\Model\Storage\ExternalResource;
+use Friendica\Model\Storage\ReferenceStorageException;
+use Friendica\Model\Storage\StorageException;
 use Friendica\Model\Storage\SystemResource;
 use Friendica\Object\Image;
 use Friendica\Util\DateTimeFormat;
@@ -186,6 +188,7 @@ class Photo
         * @return \Friendica\Object\Image
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
+        * @throws StorageException
         */
        public static function getImageDataForPhoto(array $photo)
        {
@@ -203,7 +206,12 @@ class Photo
                        $data = $i['data'];
                } else {
                        $backendRef = $photo['backend-ref'] ?? '';
-                       $data = $backendClass->get($backendRef);
+                       try {
+                               $data = $backendClass->get($backendRef);
+                       } catch (ReferenceStorageException $referenceStorageException) {
+                               DI::logger()->debug('No data found for photo', ['photo' => $photo, 'exception' => $referenceStorageException]);
+                               return null;
+                       }
                }
                return $data;
        }
@@ -339,7 +347,7 @@ class Photo
 
                if (DBA::isResult($existing_photo)) {
                        $backend_ref = (string)$existing_photo["backend-ref"];
-                       $storage = DI::storageManager()->getByName($existing_photo["backend-class"] ?? '');
+                       $storage = DI::storageManager()->getSelectableStorageByName($existing_photo["backend-class"] ?? '');
                } else {
                        $storage = DI::storage();
                }
@@ -403,11 +411,14 @@ class Photo
                $photos = DBA::select('photo', ['id', 'backend-class', 'backend-ref'], $conditions);
 
                while ($photo = DBA::fetch($photos)) {
-                       $backend_class = DI::storageManager()->getByName($photo['backend-class'] ?? '');
+                       $backend_class = DI::storageManager()->getSelectableStorageByName($photo['backend-class'] ?? '');
                        if (!empty($backend_class)) {
-                               if ($backend_class->delete($photo["backend-ref"] ?? '')) {
+                               try {
+                                       $backend_class->delete($item['backend-ref'] ?? '');
                                        // Delete the photos after they had been deleted successfully
                                        DBA::delete("photo", ['id' => $photo['id']]);
+                               } catch (ReferenceStorageException $referenceStorageException) {
+                                       DI::logger()->debug('phot doesn\'t exist.', ['conditions' => $conditions, 'exception' => $referenceStorageException]);
                                }
                        }
                }
@@ -437,7 +448,7 @@ class Photo
                        $photos = self::selectToArray(['backend-class', 'backend-ref'], $conditions);
 
                        foreach($photos as $photo) {
-                               $backend_class = DI::storageManager()->getByName($photo['backend-class'] ?? '');
+                               $backend_class = DI::storageManager()->getSelectableStorageByName($photo['backend-class'] ?? '');
                                if (!empty($backend_class)) {
                                        $fields["backend-ref"] = $backend_class->put($img->asString(), $photo['backend-ref']);
                                } else {
@@ -841,13 +852,28 @@ class Photo
         * @throws \Exception
         */
        public static function isLocal($name)
+       {
+               return (bool)self::getIdForName($name);
+       }
+
+       /**
+        * Return the id of a local photo
+        *
+        * @param string $name Picture link
+        * @return int
+        */
+       public static function getIdForName($name)
        {
                $data = self::getResourceData($name);
                if (empty($data)) {
-                       return false;
+                       return 0;
                }
 
-               return DBA::exists('photo', ['resource-id' => $data['guid'], 'scale' => $data['scale']]);
+               $photo = DBA::selectFirst('photo', ['id'], ['resource-id' => $data['guid'], 'scale' => $data['scale']]);
+               if (!empty($photo['id'])) {
+                       return $photo['id'];
+               }
+               return 0;
        }
 
        /**