]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Photo.php
Detection of local requests
[friendica.git] / src / Model / Photo.php
index acc6b0d19731a80b6d724fbae5d466ec8c588772..30e666898777c5b4a9c245d5a31b611b9373b932 100644 (file)
@@ -245,13 +245,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), "");
 
@@ -268,21 +272,26 @@ class Photo
         * Construct a photo array for an external resource image
         *
         * @param string $url      Image URL
-        * @param string $mimetype Image mime type. Defaults to "image/jpeg"
+        * @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, $mimetype = "image/jpeg")
+       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']   = $url;
+               $photo['backend-ref']   = json_encode(['url' => $url, 'uid' => $uid]);
                $photo['type']          = $mimetype;
-               $photo['cacheable']     = false;
+               $photo['cacheable']     = true;
 
                return $photo;
        }
@@ -795,30 +804,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];
        }
 
        /**
@@ -830,13 +842,12 @@ class Photo
         */
        public static function isLocal($name)
        {
-               $guid = self::getGUID($name);
-
-               if (empty($guid)) {
+               $data = self::getResourceData($name);
+               if (empty($data)) {
                        return false;
                }
 
-               return DBA::exists('photo', ['resource-id' => $guid]);
+               return DBA::exists('photo', ['resource-id' => $data['guid'], 'scale' => $data['scale']]);
        }
 
        /**