]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Attach.php
Changes:
[friendica.git] / src / Model / Attach.php
index 6182727c9762ad85d63c6365941bc8636129c5aa..b2cc229c3d00101884dc3c618b1ac0b10fa49c32 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -25,6 +25,8 @@ use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
 use Friendica\DI;
+use Friendica\Core\Storage\Exception\InvalidClassStorageException;
+use Friendica\Core\Storage\Exception\ReferenceStorageException;
 use Friendica\Object\Image;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Mimetype;
@@ -42,7 +44,7 @@ class Attach
         * @return array field list
         * @throws \Exception
         */
-       private static function getFields()
+       private static function getFields(): array
        {
                $allfields = DBStructure::definition(DI::app()->getBasePath(), false);
                $fields = array_keys($allfields['attach']['fields']);
@@ -57,7 +59,7 @@ class Attach
         * @param array $conditions Array of fields for conditions
         * @param array $params     Array of several parameters
         *
-        * @return array
+        * @return array|bool
         *
         * @throws \Exception
         * @see   \Friendica\Database\DBA::selectToArray
@@ -100,7 +102,7 @@ class Attach
         * @return boolean
         * @throws \Exception
         */
-       public static function exists(array $conditions)
+       public static function exists(array $conditions): bool
        {
                return DBA::exists('attach', $conditions);
        }
@@ -115,7 +117,7 @@ class Attach
         * @throws \Exception
         * @see   \Friendica\Database\DBA::select
         */
-       public static function getById($id)
+       public static function getById(int $id)
        {
                return self::selectFirst([], ['id' => $id]);
        }
@@ -130,7 +132,7 @@ class Attach
         * @throws \Exception
         * @see   \Friendica\Database\DBA::select
         */
-       public static function getByIdWithPermission($id)
+       public static function getByIdWithPermission(int $id)
        {
                $r = self::selectFirst(['uid'], ['id' => $id]);
                if ($r === false) {
@@ -154,26 +156,29 @@ class Attach
         *
         * @param array $item Attachment data. Needs at least 'id', 'backend-class', 'backend-ref'
         *
-        * @return string  file data
+        * @return string|null file data or null on failure
         * @throws \Exception
         */
-       public static function getData($item)
+       public static function getData(array $item)
        {
                if (!empty($item['data'])) {
                        return $item['data'];
                }
 
-               $backendClass = DI::storageManager()->getByName($item['backend-class'] ?? '');
-               if (empty($backendClass)) {
+               try {
+                       $backendClass = DI::storageManager()->getByName($item['backend-class'] ?? '');
+                       $backendRef   = $item['backend-ref'];
+                       return $backendClass->get($backendRef);
+               } catch (InvalidClassStorageException $storageException) {
                        // legacy data storage in 'data' column
                        $i = self::selectFirst(['data'], ['id' => $item['id']]);
                        if ($i === false) {
                                return null;
                        }
                        return $i['data'];
-               } else {
-                       $backendRef = $item['backend-ref'];
-                       return $backendClass->get($backendRef);
+               } catch (ReferenceStorageException $referenceStorageException) {
+                       DI::logger()->debug('No data found for item', ['item' => $item, 'exception' => $referenceStorageException]);
+                       return '';
                }
        }
 
@@ -190,10 +195,10 @@ class Attach
         * @param string  $deny_cid  Permissions, denied contacts.optional, default = ''
         * @param string  $deny_gid  Permissions, denied greoup.optional, default = ''
         *
-        * @return boolean/integer Row id on success, False on errors
+        * @return boolean|integer Row id on success, False on errors
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public static function store($data, $uid, $filename, $filetype = '' , $filesize = null, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
+       public static function store(string $data, int $uid, string $filename, string $filetype = '' , int $filesize = null, string $allow_cid = '', string $allow_gid = '', string $deny_cid = '', string $deny_gid = '')
        {
                if ($filetype === '') {
                        $filetype = Mimetype::getContentType($filename);
@@ -236,17 +241,17 @@ class Attach
        /**
         * Store new file metadata in db and binary in default backend from existing file
         *
-        * @param        $src
-        * @param        $uid
-        * @param string $filename
+        * @param string $src Source file name
+        * @param int    $uid User id
+        * @param string $filename Optional file name
         * @param string $allow_cid
         * @param string $allow_gid
         * @param string $deny_cid
         * @param string $deny_gid
-        * @return boolean True on success
+        * @return boolean|int Insert id or false on failure
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public static function storeFile($src, $uid, $filename = '', $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
+       public static function storeFile(string $src, int $uid, string $filename = '', string $allow_cid = '', string $allow_gid = '', string $deny_cid = '', string $deny_gid = '')
        {
                if ($filename === '') {
                        $filename = basename($src);
@@ -271,18 +276,20 @@ class Attach
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @see   \Friendica\Database\DBA::update
         */
-       public static function update($fields, $conditions, Image $img = null, array $old_fields = [])
+       public static function update(array $fields, array $conditions, Image $img = null, array $old_fields = []): bool
        {
                if (!is_null($img)) {
                        // get items to update
                        $items = self::selectToArray(['backend-class','backend-ref'], $conditions);
 
                        foreach($items as $item) {
-                               $backend_class = DI::storageManager()->getByName($item['backend-class'] ?? '');
-                               if (!empty($backend_class)) {
+                               try {
+                                       $backend_class         = DI::storageManager()->getWritableStorageByName($item['backend-class'] ?? '');
                                        $fields['backend-ref'] = $backend_class->put($img->asString(), $item['backend-ref'] ?? '');
-                               } else {
-                                       $fields['data'] = $img->asString();
+                               } catch (InvalidClassStorageException $storageException) {
+                                       DI::logger()->debug('Storage class not found.', ['conditions' => $conditions, 'exception' => $storageException]);
+                               } catch (ReferenceStorageException $referenceStorageException) {
+                                       DI::logger()->debug('Item doesn\'t exist.', ['conditions' => $conditions, 'exception' => $referenceStorageException]);
                                }
                        }
                }
@@ -304,15 +311,19 @@ class Attach
         * @throws \Exception
         * @see   \Friendica\Database\DBA::delete
         */
-       public static function delete(array $conditions, array $options = [])
+       public static function delete(array $conditions, array $options = []): bool
        {
                // get items to delete data info
                $items = self::selectToArray(['backend-class','backend-ref'], $conditions);
 
                foreach($items as $item) {
-                       $backend_class = DI::storageManager()->getByName($item['backend-class'] ?? '');
-                       if (!empty($backend_class)) {
+                       try {
+                               $backend_class = DI::storageManager()->getWritableStorageByName($item['backend-class'] ?? '');
                                $backend_class->delete($item['backend-ref'] ?? '');
+                       } catch (InvalidClassStorageException $storageException) {
+                               DI::logger()->debug('Storage class not found.', ['conditions' => $conditions, 'exception' => $storageException]);
+                       } catch (ReferenceStorageException $referenceStorageException) {
+                               DI::logger()->debug('Item doesn\'t exist.', ['conditions' => $conditions, 'exception' => $referenceStorageException]);
                        }
                }