]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Storage/Filesystem.php
Insert a `user-contact` for every contact
[friendica.git] / src / Model / Storage / Filesystem.php
index 15689f7f2533a8f9571076410d50ed9cf280ff85..c6c939bd464353154eab1193916cdd2c79ecb1d9 100644 (file)
@@ -1,15 +1,30 @@
 <?php
 /**
- * @file src/Model/Storage/Filesystem.php
- * Storage backend system
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
  */
 
 namespace Friendica\Model\Storage;
 
-use Friendica\Core\Config\IConfiguration;
-use Friendica\Core\L10n\L10n;
+use Exception;
+use Friendica\Core\Config\IConfig;
+use Friendica\Core\L10n;
 use Friendica\Util\Strings;
-use Psr\Log\LoggerInterface;
 
 /**
  * Filesystem based storage backend
@@ -21,31 +36,32 @@ use Psr\Log\LoggerInterface;
  * Each new resource gets a value as reference and is saved in a
  * folder tree stucture created from that value.
  */
-class Filesystem extends AbstractStorage
+class Filesystem implements IWritableStorage
 {
        const NAME = 'Filesystem';
 
        // Default base folder
        const DEFAULT_BASE_FOLDER = 'storage';
 
-       /** @var IConfiguration */
+       /** @var IConfig */
        private $config;
 
        /** @var string */
        private $basePath;
 
+       /** @var L10n */
+       private $l10n;
+
        /**
         * Filesystem constructor.
         *
-        * @param IConfiguration  $config
-        * @param LoggerInterface $logger
+        * @param IConfig         $config
         * @param L10n            $l10n
         */
-       public function __construct(IConfiguration $config, LoggerInterface $logger, L10n $l10n)
+       public function __construct(IConfig $config, L10n $l10n)
        {
-               parent::__construct($l10n, $logger);
-
                $this->config = $config;
+               $this->l10n   = $l10n;
 
                $path           = $this->config->get('storage', 'filesystem_path', self::DEFAULT_BASE_FOLDER);
                $this->basePath = rtrim($path, '/');
@@ -58,7 +74,7 @@ class Filesystem extends AbstractStorage
         *
         * @return string
         */
-       private function pathForRef(string $reference)
+       private function pathForRef(string $reference): string
        {
                $fold1 = substr($reference, 0, 2);
                $fold2 = substr($reference, 2, 2);
@@ -69,7 +85,7 @@ class Filesystem extends AbstractStorage
 
 
        /**
-        * Create dirctory tree to store file, with .htaccess and index.html files
+        * Create directory tree to store file, with .htaccess and index.html files
         *
         * @param string $file Path and filename
         *
@@ -81,8 +97,7 @@ class Filesystem extends AbstractStorage
 
                if (!is_dir($path)) {
                        if (!mkdir($path, 0770, true)) {
-                               $this->logger->warning('Failed to create dir.', ['path' => $path]);
-                               throw new StorageException($this->l10n->t('Filesystem storage failed to create "%s". Check you write permissions.', $path));
+                               throw new StorageException(sprintf('Filesystem storage failed to create "%s". Check you write permissions.', $path));
                        }
                }
 
@@ -103,23 +118,33 @@ class Filesystem extends AbstractStorage
        /**
         * @inheritDoc
         */
-       public function get(string $reference)
+       public function get(string $reference): string
        {
                $file = $this->pathForRef($reference);
                if (!is_file($file)) {
-                       return '';
+                       throw new ReferenceStorageException(sprintf('Filesystem storage failed to get the file %s, The file is invalid', $reference));
                }
 
-               return file_get_contents($file);
+               $result = file_get_contents($file);
+
+               if ($result === false) {
+                       throw new StorageException(sprintf('Filesystem storage failed to get data to "%s". Check your write permissions', $file));
+               }
+
+               return $result;
        }
 
        /**
         * @inheritDoc
         */
-       public function put(string $data, string $reference = '')
+       public function put(string $data, string $reference = ''): string
        {
                if ($reference === '') {
-                       $reference = Strings::getRandomHex();
+                       try {
+                               $reference = Strings::getRandomHex();
+                       } catch (Exception $exception) {
+                               throw new StorageException('Filesystem storage failed to generate a random hex', $exception->getCode(), $exception);
+                       }
                }
                $file = $this->pathForRef($reference);
 
@@ -129,8 +154,7 @@ class Filesystem extends AbstractStorage
 
                // just in case the result is REALLY false, not zero or empty or anything else, throw the exception
                if ($result === false) {
-                       $this->logger->warning('Failed to write data.', ['file' => $file]);
-                       throw new StorageException($this->l10n->t('Filesystem storage failed to save data to "%s". Check your write permissions', $file));
+                       throw new StorageException(sprintf('Filesystem storage failed to save data to "%s". Check your write permissions', $file));
                }
 
                chmod($file, 0660);
@@ -143,17 +167,19 @@ class Filesystem extends AbstractStorage
        public function delete(string $reference)
        {
                $file = $this->pathForRef($reference);
-               // return true if file doesn't exists. we want to delete it: success with zero work!
                if (!is_file($file)) {
-                       return true;
+                       throw new ReferenceStorageException(sprintf('File with reference "%s" doesn\'t exist', $reference));
+               }
+
+               if (!unlink($file)) {
+                       throw new StorageException(sprintf('Cannot delete with file with reference "%s"', $reference));
                }
-               return unlink($file);
        }
 
        /**
         * @inheritDoc
         */
-       public function getOptions()
+       public function getOptions(): array
        {
                return [
                        'storagepath' => [
@@ -168,7 +194,7 @@ class Filesystem extends AbstractStorage
        /**
         * @inheritDoc
         */
-       public function saveOptions(array $data)
+       public function saveOptions(array $data): array
        {
                $storagePath = $data['storagepath'] ?? '';
                if ($storagePath === '' || !is_dir($storagePath)) {
@@ -184,8 +210,13 @@ class Filesystem extends AbstractStorage
        /**
         * @inheritDoc
         */
-       public static function getName()
+       public static function getName(): string
        {
                return self::NAME;
        }
+
+       public function __toString()
+       {
+               return self::getName();
+       }
 }