]> git.mxchange.org Git - friendica.git/commitdiff
Pluggable storage backends: first steps
authorfabrixxm <fabrix.xm@gmail.com>
Tue, 20 Nov 2018 22:15:03 +0000 (23:15 +0100)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 21 Jan 2019 13:57:28 +0000 (08:57 -0500)
- add backend related columns in photo table
- add system resource storage class
- add code to load image data from backend class
- return "nosign" image as photo meta with SystemResource backend

config/dbstructure.config.php
src/Model/Photo.php
src/Model/Storage/SystemResource.php [new file with mode: 0644]
src/Module/Photo.php

index e90a35f38a7f29176c0e0747e75dff7b044a1425..70be49481991560f20d03cb1fb7af89f44e94b1a 100644 (file)
@@ -34,7 +34,7 @@
 use Friendica\Database\DBA;
 
 if (!defined('DB_UPDATE_VERSION')) {
-       define('DB_UPDATE_VERSION', 1293);
+       define('DB_UPDATE_VERSION', 1294);
 }
 
 return [
@@ -955,6 +955,8 @@ return [
                        "allow_gid" => ["type" => "mediumtext", "comment" => "Access Control - list of allowed groups"],
                        "deny_cid" => ["type" => "mediumtext", "comment" => "Access Control - list of denied contact.id"],
                        "deny_gid" => ["type" => "mediumtext", "comment" => "Access Control - list of denied groups"],
+                       "backend-class" => ["type" => "tinytext", "default" => "", "comment" => "Storage backend class"],
+                       "backend-ref" => ["type" => "text", "default" => "", "comment" => "Storage backend data reference"]
                ],
                "indexes" => [
                        "PRIMARY" => ["id"],
index 298ca39201a5aa2c9581d04e864f947b3e23ed78..078ef87024d0fca2022bea92eb6cc4eaa5731996 100644 (file)
@@ -58,7 +58,7 @@ class Photo extends BaseObject
        public static function selectFirst(array $fields = [], array $condition = [], array $params = [])
        {
                if (empty($fields)) {
-                       $selected = self::getFields();
+                       $fields = self::getFields();
                }
 
                return DBA::selectFirst("photo", $fields, $condition, $params);
@@ -68,7 +68,7 @@ class Photo extends BaseObject
         * @brief Get a single photo given resource id and scale
         *
         * This method checks for permissions. Returns associative array
-        * on success, a generic "red sign" data if user has no permission,
+        * on success, "no sign" image info, if user has no permission,
         * false if photo does not exists
         *
         * @param string  $resourceid  Rescource ID for the photo
@@ -92,7 +92,7 @@ class Photo extends BaseObject
 
                $photo = self::selectFirst([], $condition, $params);
                if ($photo === false) {
-                       return false; ///TODO: Return info for red sign image
+                       return self::createPhotoForSystemResource("images/nosign.jpg");
                }
                return $photo;
        }
@@ -116,13 +116,26 @@ class Photo extends BaseObject
         *
         * @return \Friendica\Object\Image
         */
-       public static function getImageForPhotoId($id)
+       public static function getImageForPhoto($photo)
        {
-               $i = self::selectFirst(["data", "type"],["id"=>$id]);
-               if ($i===false) {
+               $data = "";
+               if ($photo["backend-class"] == "") {
+                       // legacy data storage in "data" column
+                       $i = self::selectFirst(["data"], ["id"=>$photo["id"]]);
+                       if ($i===false) {
+                               return null;
+                       }
+                       $data = $i["data"];
+               } else {
+                       $backendClass = $photo["backend-class"];
+                       $backendRef = $photo["backend-ref"];
+                       $data = $backendClass::get($backendRef);
+               }
+
+               if ($data === "") {
                        return null;
                }
-               return new Image($i["data"], $i["type"]);
+               return new Image($data, $photo["type"]);
        }
 
        /**
@@ -138,6 +151,25 @@ class Photo extends BaseObject
                return $fields;
        }
 
+       /**
+        * @brief 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"
+        *
+        * @return array
+        */
+       public static function createPhotoForSystemResource($filename, $mimetype = "image/jpeg")
+       {
+               $fields = self::getFields();
+               $values = array_fill(0, count($fields), "");
+               $photo = array_combine($fields, $values);
+               $photo["backend-class"] = "\Friendica\Model\Storage\SystemResource";
+               $photo["backend-ref"] = $filename;
+               $photo["type"] = $mimetype;
+               $photo['cacheable'] = false;
+               return $photo;
+       }
 
 
        /**
diff --git a/src/Model/Storage/SystemResource.php b/src/Model/Storage/SystemResource.php
new file mode 100644 (file)
index 0000000..203e9a2
--- /dev/null
@@ -0,0 +1,39 @@
+<?php
+/**
+ * @file src/Model/Storage/SystemStorage.php
+ */
+
+namespace Friendica\Model\Storage;
+
+/**
+ * @brief System resource storage class
+ *
+ * This class is used to load system resources, like images.
+ * Is not itended to be selectable by admins as default storage class.
+ */
+class SystemResource
+{
+       // Valid folders to look for resources
+       const VALID_FOLDERS = [ "images" ];
+
+       /**
+        * @brief get data
+        *
+        * @param string  $resourceid
+        *
+        * @return string
+        */
+       static function get($filename)
+       {
+               $folder = dirname($filename);
+               if (!in_array($folder, self::VALID_FOLDERS)) return "";
+               if (!file_exists($filename)) return "";
+               return file_get_contents($filename);
+       }
+
+       static function put($filename, $data)
+       {
+               throw new \BadMethodCallException();
+       }
+}
+
index 0fddb8276a0e2df72d5b07cda646c295e09f9acc..f65ca698b0836f14d6a3549669299a5266311ed1 100644 (file)
@@ -77,9 +77,9 @@ class Photo extends BaseModule
                        killme();
                }
 
-               $cacheable = ($photo["allow_cid"].$photo["allow_gid"].$photo["deny_cid"].$photo["deny_gid"] === "") || defaults($photo, "cacheable", false);
+               $cacheable = ($photo["allow_cid"].$photo["allow_gid"].$photo["deny_cid"].$photo["deny_gid"] === "") && (isset($photo["cacheable"])?$photo["cacheable"]:true);
 
-               $img = MPhoto::getImageForPhotoId($photo["id"]);
+               $img = MPhoto::getImageForPhoto($photo);
 
                if (is_null($img) || !$img->isValid()) {
                        Logger::log("Invalid photo with id {$photo['id']}.");