]> git.mxchange.org Git - friendica.git/commitdiff
Add Database storage backend
authorfabrixxm <fabrix.xm@gmail.com>
Thu, 29 Nov 2018 07:36:39 +0000 (08:36 +0100)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 21 Jan 2019 14:11:33 +0000 (09:11 -0500)
This storage store files data in a separate database table

config/dbstructure.config.php
database.sql
src/Model/Storage/Database.php [new file with mode: 0644]

index 0c24d8a737cae6d9ad03fedac6dfa95210d58215..ac9127f7668bbe574607bb95f923902945350cd7 100644 (file)
@@ -34,7 +34,7 @@
 use Friendica\Database\DBA;
 
 if (!defined('DB_UPDATE_VERSION')) {
-       define('DB_UPDATE_VERSION', 1294);
+       define('DB_UPDATE_VERSION', 1295);
 }
 
 return [
@@ -1380,5 +1380,16 @@ return [
                        "done_priority_next_try" => ["done", "priority", "next_try"],
                        "done_next_try" => ["done", "next_try"]
                ]
+       ],
+       "storage" => [
+               "comment" => "Data stored by Database storage backend",
+               "fields" => [
+                       "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "Auto incremented image data id"],
+                       "data" => ["type" => "longblob", "not null" => "1", "comment" => "file data"]
+               ],
+               "indexes" => [
+                       "PRIMARY" => ["id"]
+               ]
        ]
 ];
+
index 208714908d13ff186c07fe57e534d4f2614c84af..1143ac6d9b0111b172dfbe9ee136a7ee5b20b1df 100644 (file)
@@ -1,6 +1,6 @@
 -- ------------------------------------------
 -- Friendica 2019.03-dev (The Tazmans Flax-lily)
--- DB_UPDATE_VERSION 1294
+-- DB_UPDATE_VERSION 1295
 -- ------------------------------------------
 
 
@@ -1274,4 +1274,13 @@ CREATE TABLE IF NOT EXISTS `workerqueue` (
         INDEX `done_next_try` (`done`,`next_try`)
 ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Background tasks queue entries';
 
+--
+-- TABLE storage
+--
+CREATE TABLE IF NOT EXISTS `storage` (
+       `id` int unsigned NOT NULL auto_increment COMMENT 'Auto incremented image data id',
+       `data` longblob NOT NULL COMMENT 'file data',
+        PRIMARY KEY(`id`)
+) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Data stored by Database storage backend';
+
 
diff --git a/src/Model/Storage/Database.php b/src/Model/Storage/Database.php
new file mode 100644 (file)
index 0000000..39a1cb6
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+/**
+ * @file src/Model/Storage/Filesystem.php
+ * @brief Storage backend system
+ */
+
+namespace Friendica\Model\Storage;
+
+use Friendica\Core\Logger;
+use Friendica\Core\L10n;
+use Friendica\Core\System;
+use Friendica\Database\DBA;
+
+/**
+ * @brief Database based storage system
+ *
+ * This class manage data stored in database table.
+ */
+class Database implements IStorage
+{
+       public static function get($ref)
+       {
+               $r = DBA::selectFirst('storage', ['data'], ['id' => $ref]);
+               if (!DBA::isResult($r)) {
+                       return '';
+               }
+
+               return $r['data'];
+       }
+
+       public static function put($data, $ref = '')
+       {
+               if ($ref !== '') {
+                       $r = DBA::update('storage', ['data' => $data], ['id' => $ref]);
+                       if ($r === false) {
+                               Logger::log('Failed to update data with id ' . $ref . ': ' . DBA::errorNo() . ' : ' . DBA::errorMessage());
+                               throw new StorageException(L10n::t('Database storage failed to update %s', $ref));
+                       }
+                       return $ref;
+               } else {
+                       $r = DBA::insert('storage', ['data' => $data]);
+                       if ($r === false) {
+                               Logger::log('Failed to insert data: ' . DBA::errorNo() . ' : ' . DBA::errorMessage());
+                               throw new StorageException(L10n::t('Database storage failed to insert data'));
+                       }
+                       return DBA::lastInsertId();
+               }
+       }
+
+       public static function delete($ref)
+       {
+               return DBA::delete('storage', ['id' => $ref]);
+       }
+}
\ No newline at end of file