From 3b3c4e8cc7d062268b96e93357227baddbfe4902 Mon Sep 17 00:00:00 2001 From: fabrixxm Date: Tue, 20 Nov 2018 23:15:03 +0100 Subject: [PATCH] Pluggable storage backends: first steps - 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 | 4 ++- src/Model/Photo.php | 46 +++++++++++++++++++++++----- src/Model/Storage/SystemResource.php | 39 +++++++++++++++++++++++ src/Module/Photo.php | 4 +-- 4 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 src/Model/Storage/SystemResource.php diff --git a/config/dbstructure.config.php b/config/dbstructure.config.php index e90a35f38a..70be494819 100644 --- a/config/dbstructure.config.php +++ b/config/dbstructure.config.php @@ -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"], diff --git a/src/Model/Photo.php b/src/Model/Photo.php index 298ca39201..078ef87024 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -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 index 0000000000..203e9a2892 --- /dev/null +++ b/src/Model/Storage/SystemResource.php @@ -0,0 +1,39 @@ +isValid()) { Logger::log("Invalid photo with id {$photo['id']}.");