X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FAttach.php;h=9009126868dd66460890c02b9d09c969bbaf9f04;hb=a662245c744f4d2faa1b533977f7d21b4de6a724;hp=92c76d3d0ac9a2e17e9b5d82e9fc0ce28005a44b;hpb=24d7ffa3fee7e3686589f29f005fb2c419c9985a;p=friendica.git diff --git a/src/Model/Attach.php b/src/Model/Attach.php index 92c76d3d0a..9009126868 100644 --- a/src/Model/Attach.php +++ b/src/Model/Attach.php @@ -1,70 +1,89 @@ . + * */ + namespace Friendica\Model; -use Friendica\BaseObject; use Friendica\Core\System; -use Friendica\Core\StorageManager; use Friendica\Database\DBA; use Friendica\Database\DBStructure; -use Friendica\Util\Security; +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; +use Friendica\Security\Security; /** * Class to handle attach dabatase table */ -class Attach extends BaseObject +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 */ private static function getFields() { - $allfields = DBStructure::definition(false); + $allfields = DBStructure::definition(DI::app()->getBasePath(), false); $fields = array_keys($allfields['attach']['fields']); array_splice($fields, array_search('data', $fields), 1); return $fields; } /** - * @brief Select rows from the attach table + * 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 - * @param array $params Array of several parameters + * @param array $fields Array of selected fields, empty for all + * @param array $conditions Array of fields for conditions + * @param array $params Array of several parameters * - * @return boolean|array + * @return array * - * @see \Friendica\Database\DBA::select + * @throws \Exception + * @see \Friendica\Database\DBA::selectToArray */ - public static function select(array $fields = [], array $conditions = [], array $params = []) + public static function selectToArray(array $fields = [], array $conditions = [], array $params = []) { if (empty($fields)) { - $selected = self::getFields(); + $fields = self::getFields(); } - $r = DBA::select('attach', $fields, $conditions, $params); - return DBA::toArray($r); + return DBA::selectToArray('attach', $fields, $conditions, $params); } /** - * @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 - * @param array $params Array of several parameters + * @param array $fields Array of selected fields, empty for all + * @param array $conditions Array of fields for conditions + * @param array $params Array of several parameters * * @return bool|array * - * @see \Friendica\Database\DBA::select + * @throws \Exception + * @see \Friendica\Database\DBA::select */ public static function selectFirst(array $fields = [], array $conditions = [], array $params = []) { @@ -76,11 +95,12 @@ class Attach extends BaseObject } /** - * @brief Check if attachment with given conditions exists + * Check if attachment with given conditions exists * - * @param array $conditions Array of extra conditions + * @param array $conditions Array of extra conditions * * @return boolean + * @throws \Exception */ public static function exists(array $conditions) { @@ -88,13 +108,14 @@ class Attach extends BaseObject } /** - * @brief Retrive a single record given the ID - * - * @param int $id Row id of the record - * + * Retrive a single record given the ID + * + * @param int $id Row id of the record + * * @return bool|array * - * @see \Friendica\Database\DBA::select + * @throws \Exception + * @see \Friendica\Database\DBA::select */ public static function getById($id) { @@ -102,13 +123,14 @@ class Attach extends BaseObject } /** - * @brief Retrive a single record given the ID - * - * @param int $id Row id of the record - * + * Retrive a single record given the ID + * + * @param int $id Row id of the record + * * @return bool|array * - * @see \Friendica\Database\DBA::select + * @throws \Exception + * @see \Friendica\Database\DBA::select */ public static function getByIdWithPermission($id) { @@ -130,32 +152,40 @@ class Attach extends BaseObject } /** - * @brief 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' - * + * 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' + * * @return string file data + * @throws \Exception */ 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 string $data Binary data * @param integer $uid User ID * @param string $filename Filename * @param string $filetype Mimetype. optional, default = '' @@ -166,8 +196,9 @@ class Attach extends BaseObject * @param string $deny_gid Permissions, denied greoup.optional, default = '' * * @return boolean/integer Row id on success, False on errors + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function store($data, $uid, $filename, $filetype = '' , $filesize = null, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') + public static function store($data, $uid, $filename, $filetype = '' , $filesize = null, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') { if ($filetype === '') { $filetype = Mimetype::getContentType($filename); @@ -177,12 +208,8 @@ class Attach extends BaseObject $filesize = strlen($data); } - $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(); @@ -200,7 +227,7 @@ class Attach extends BaseObject '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 ]; @@ -212,11 +239,19 @@ class Attach extends BaseObject } /** - * @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 + * @param string $filename + * @param string $allow_cid + * @param string $allow_gid + * @param string $deny_cid + * @param string $deny_gid * @return boolean True on success + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function storeFile($src, $uid, $filename = '', $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') + public static function storeFile($src, $uid, $filename = '', $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') { if ($filename === '') { $filename = basename($src); @@ -229,29 +264,32 @@ class Attach extends BaseObject /** - * @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 - * @param string $data File data to update. Optional, default null. + * @param Image $img Image data to update. Optional, default null. * @param array|boolean $old_fields Array with the old field values that are about to be replaced (true = update on duplicate) * - * @return boolean Was the update successfull? + * @return boolean Was the update successful? * - * @see \Friendica\Database\DBA::update + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @see \Friendica\Database\DBA::update */ - public static function update($fields, $conditions, $img = null, array $old_fields = []) + public static function update($fields, $conditions, Image $img = null, array $old_fields = []) { - if (!is_null($data)) { + if (!is_null($img)) { // get items to update - $items = self::select(['backend-class','backend-ref'], $conditions); + $items = self::selectToArray(['backend-class','backend-ref'], $conditions); foreach($items as $item) { - $backend_class = (string)$item['backend-class']; - if ($backend_class !== '') { - $fields['backend-ref'] = $backend_class::put($img->asString(), $item['backend-ref']); - } else { - $fields['data'] = $data; + 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]); } } } @@ -263,24 +301,29 @@ class Attach extends BaseObject /** - * @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 + * @param array $conditions Field condition(s) + * @param array $options Options array, Optional * * @return boolean * - * @see \Friendica\Database\DBA::delete + * @throws \Exception + * @see \Friendica\Database\DBA::delete */ public static function delete(array $conditions, array $options = []) { // get items to delete data info - $items = self::select(['backend-class','backend-ref'], $conditions); + $items = self::selectToArray(['backend-class','backend-ref'], $conditions); foreach($items as $item) { - $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]); } }