X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FAttach.php;h=9009126868dd66460890c02b9d09c969bbaf9f04;hb=a662245c744f4d2faa1b533977f7d21b4de6a724;hp=c1d5c033bda368ed0af2700f09870e294bd7a2c6;hpb=3c76826793feeb933dab7a07dab7cc7eb2efc451;p=friendica.git diff --git a/src/Model/Attach.php b/src/Model/Attach.php index c1d5c033bd..9009126868 100644 --- a/src/Model/Attach.php +++ b/src/Model/Attach.php @@ -1,21 +1,36 @@ . + * */ + namespace Friendica\Model; -use Friendica\Core\StorageManager; use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Database\DBStructure; use Friendica\DI; -use Friendica\Model\Storage\IStorage; +use Friendica\Core\Storage\Exception\InvalidClassStorageException; +use Friendica\Core\Storage\Exception\ReferenceStorageException; use Friendica\Object\Image; use Friendica\Util\DateTimeFormat; use Friendica\Util\Mimetype; -use Friendica\Util\Security; +use Friendica\Security\Security; /** * Class to handle attach dabatase table @@ -24,7 +39,7 @@ class Attach { /** - * @brief Return a list of fields that are associated with the attach table + * Return a list of fields that are associated with the attach table * * @return array field list * @throws \Exception @@ -38,7 +53,7 @@ class Attach } /** - * @brief Select rows from the attach table and return them as array + * Select rows from the attach table and return them as array * * @param array $fields Array of selected fields, empty for all * @param array $conditions Array of fields for conditions @@ -59,7 +74,7 @@ class Attach } /** - * @brief Retrieve a single record from the attach table + * Retrieve a single record from the attach table * * @param array $fields Array of selected fields, empty for all * @param array $conditions Array of fields for conditions @@ -80,7 +95,7 @@ class Attach } /** - * @brief Check if attachment with given conditions exists + * Check if attachment with given conditions exists * * @param array $conditions Array of extra conditions * @@ -93,7 +108,7 @@ class Attach } /** - * @brief Retrive a single record given the ID + * Retrive a single record given the ID * * @param int $id Row id of the record * @@ -108,7 +123,7 @@ class Attach } /** - * @brief Retrive a single record given the ID + * Retrive a single record given the ID * * @param int $id Row id of the record * @@ -137,7 +152,7 @@ class Attach } /** - * @brief Get file data for given row id. null if row id does not exist + * Get file data for given row id. null if row id does not exist * * @param array $item Attachment data. Needs at least 'id', 'backend-class', 'backend-ref' * @@ -146,22 +161,29 @@ class Attach */ public static function getData($item) { - if ($item['backend-class'] == '') { + if (!empty($item['data'])) { + return $item['data']; + } + + 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 { - $backendClass = $item['backend-class']; - $backendRef = $item['backend-ref']; - return $backendClass::get($backendRef); + } catch (ReferenceStorageException $referenceStorageException) { + DI::logger()->debug('No data found for item', ['item' => $item, 'exception' => $referenceStorageException]); + return ''; } } /** - * @brief Store new file metadata in db and binary in default backend + * Store new file metadata in db and binary in default backend * * @param string $data Binary data * @param integer $uid User ID @@ -186,13 +208,8 @@ class Attach $filesize = strlen($data); } - /** @var IStorage $backend_class */ - $backend_class = StorageManager::getBackend(); - $backend_ref = ''; - if ($backend_class !== '') { - $backend_ref = $backend_class::put($data); - $data = ''; - } + $backend_ref = DI::storage()->put($data); + $data = ''; $hash = System::createGUID(64); $created = DateTimeFormat::utcNow(); @@ -210,7 +227,7 @@ class Attach 'allow_gid' => $allow_gid, 'deny_cid' => $deny_cid, 'deny_gid' => $deny_gid, - 'backend-class' => $backend_class, + 'backend-class' => (string)DI::storage(), 'backend-ref' => $backend_ref ]; @@ -222,7 +239,7 @@ class Attach } /** - * @brief Store new file metadata in db and binary in default backend from existing file + * Store new file metadata in db and binary in default backend from existing file * * @param $src * @param $uid @@ -247,7 +264,7 @@ class Attach /** - * @brief Update an attached file + * Update an attached file * * @param array $fields Contains the fields that are updated * @param array $conditions Condition array with the key values @@ -266,12 +283,13 @@ class Attach $items = self::selectToArray(['backend-class','backend-ref'], $conditions); foreach($items as $item) { - /** @var IStorage $backend_class */ - $backend_class = (string)$item['backend-class']; - if ($backend_class !== '') { - $fields['backend-ref'] = $backend_class::put($img->asString(), $item['backend-ref']); - } else { - $fields['data'] = $img->asString(); + try { + $backend_class = DI::storageManager()->getWritableStorageByName($item['backend-class'] ?? ''); + $fields['backend-ref'] = $backend_class->put($img->asString(), $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]); } } } @@ -283,7 +301,7 @@ class Attach /** - * @brief Delete info from table and data from storage + * Delete info from table and data from storage * * @param array $conditions Field condition(s) * @param array $options Options array, Optional @@ -299,10 +317,13 @@ class Attach $items = self::selectToArray(['backend-class','backend-ref'], $conditions); foreach($items as $item) { - /** @var IStorage $backend_class */ - $backend_class = (string)$item['backend-class']; - if ($backend_class !== '') { - $backend_class::delete($item['backend-ref']); + 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]); } }