]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Photo.php
Revert Photo::getImageDataForPhoto return-type change
[friendica.git] / src / Model / Photo.php
index f79409e3f5c332f1115002d9b265f0388fbd2813..a38743d9d6adbfa5b3d10f13faedabd7b40db9a5 100644 (file)
@@ -27,6 +27,9 @@ use Friendica\Core\System;
 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;
@@ -185,6 +188,7 @@ class Photo
         * @return \Friendica\Object\Image
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
+        * @throws StorageException
         */
        public static function getImageDataForPhoto(array $photo)
        {
@@ -202,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;
        }
@@ -244,13 +253,17 @@ class Photo
         * Construct a photo array for a system resource image
         *
         * @param string $filename Image file name relative to code root
-        * @param string $mimetype Image mime type. Defaults to "image/jpeg"
+        * @param string $mimetype Image mime type. Is guessed by file name when empty.
         *
         * @return array
         * @throws \Exception
         */
-       public static function createPhotoForSystemResource($filename, $mimetype = "image/jpeg")
+       public static function createPhotoForSystemResource($filename, $mimetype = '')
        {
+               if (empty($mimetype)) {
+                       $mimetype = Images::guessTypeByExtension($filename);
+               }
+
                $fields = self::getFields();
                $values = array_fill(0, count($fields), "");
 
@@ -263,6 +276,33 @@ class Photo
                return $photo;
        }
 
+       /**
+        * Construct a photo array for an external resource image
+        *
+        * @param string $url      Image URL
+        * @param int    $uid      User ID of the requesting person
+        * @param string $mimetype Image mime type. Is guessed by file name when empty.
+        *
+        * @return array
+        * @throws \Exception
+        */
+       public static function createPhotoForExternalResource($url, $uid = 0, $mimetype = '')
+       {
+               if (empty($mimetype)) {
+                       $mimetype = Images::guessTypeByExtension($url);
+               }
+
+               $fields = self::getFields();
+               $values = array_fill(0, count($fields), "");
+
+               $photo                  = array_combine($fields, $values);
+               $photo['backend-class'] = ExternalResource::NAME;
+               $photo['backend-ref']   = json_encode(['url' => $url, 'uid' => $uid]);
+               $photo['type']          = $mimetype;
+               $photo['cacheable']     = true;
+
+               return $photo;
+       }
 
        /**
         * store photo metadata in db and binary in default backend
@@ -307,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();
                }
@@ -371,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]);
                                }
                        }
                }
@@ -405,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 {
@@ -772,30 +815,33 @@ class Photo
        }
 
        /**
-        * Returns the GUID from picture links
+        * Fetch the guid and scale from picture links
         *
         * @param string $name Picture link
-        * @return string GUID
-        * @throws \Exception
+        * @return array
         */
-       public static function getGUID($name)
+       public static function getResourceData(string $name):array
        {
                $base = DI::baseUrl()->get();
 
                $guid = str_replace([Strings::normaliseLink($base), '/photo/'], '', Strings::normaliseLink($name));
 
+               if (parse_url($guid, PHP_URL_SCHEME)) {
+                       return [];
+               }
+
                $guid = self::stripExtension($guid);
                if (substr($guid, -2, 1) != "-") {
-                       return '';
+                       return [];
                }
 
                $scale = intval(substr($guid, -1, 1));
                if (!is_numeric($scale)) {
-                       return '';
+                       return [];
                }
 
                $guid = substr($guid, 0, -2);
-               return $guid;
+               return ['guid' => $guid, 'scale' => $scale];
        }
 
        /**
@@ -807,13 +853,27 @@ class Photo
         */
        public static function isLocal($name)
        {
-               $guid = self::getGUID($name);
+               return (bool)self::getIdForName($name);
+       }
 
-               if (empty($guid)) {
-                       return false;
+       /**
+        * 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 0;
                }
 
-               return DBA::exists('photo', ['resource-id' => $guid]);
+               $photo = DBA::selectFirst('photo', ['id'], ['resource-id' => $data['guid'], 'scale' => $data['scale']]);
+               if (!empty($photo['id'])) {
+                       return $photo['id'];
+               }
+               return 0;
        }
 
        /**