Refactor IStorage

This commit is contained in:
Philipp 2021-08-01 14:00:48 +02:00
parent 5dcdf2322e
commit 29c7552df5
No known key found for this signature in database
GPG key ID: 9A28B7D4FF5667BD
11 changed files with 121 additions and 55 deletions

View file

@ -25,6 +25,7 @@ use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\DI;
use Friendica\Model\Storage\ReferenceStorageException;
use Friendica\Object\Image;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Mimetype;
@ -173,7 +174,12 @@ class Attach
return $i['data'];
} else {
$backendRef = $item['backend-ref'];
return $backendClass->get($backendRef);
try {
return $backendClass->get($backendRef);
} catch (ReferenceStorageException $referenceStorageException) {
DI::logger()->debug('No data found for item', ['item' => $item, 'exception' => $referenceStorageException]);
return '';
}
}
}
@ -278,7 +284,7 @@ class Attach
$items = self::selectToArray(['backend-class','backend-ref'], $conditions);
foreach($items as $item) {
$backend_class = DI::storageManager()->getByName($item['backend-class'] ?? '');
$backend_class = DI::storageManager()->getSelectableStorageByName($item['backend-class'] ?? '');
if (!empty($backend_class)) {
$fields['backend-ref'] = $backend_class->put($img->asString(), $item['backend-ref'] ?? '');
} else {
@ -310,9 +316,13 @@ class Attach
$items = self::selectToArray(['backend-class','backend-ref'], $conditions);
foreach($items as $item) {
$backend_class = DI::storageManager()->getByName($item['backend-class'] ?? '');
$backend_class = DI::storageManager()->getSelectableStorageByName($item['backend-class'] ?? '');
if (!empty($backend_class)) {
$backend_class->delete($item['backend-ref'] ?? '');
try {
$backend_class->delete($item['backend-ref'] ?? '');
} catch (ReferenceStorageException $referenceStorageException) {
DI::logger()->debug('Item doesn\'t exist.', ['conditions' => $conditions, 'exception' => $referenceStorageException]);
}
}
}

View file

@ -28,6 +28,8 @@ use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\DI;
use Friendica\Model\Storage\ExternalResource;
use Friendica\Model\Storage\ReferenceStorageException;
use Friendica\Model\Storage\StorageException;
use Friendica\Model\Storage\SystemResource;
use Friendica\Object\Image;
use Friendica\Util\DateTimeFormat;
@ -183,9 +185,10 @@ class Photo
*
* @param array $photo Photo data. Needs at least 'id', 'type', 'backend-class', 'backend-ref'
*
* @return \Friendica\Object\Image
* @return \Friendica\Object\Image|string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
* @throws StorageException
*/
public static function getImageDataForPhoto(array $photo)
{
@ -198,12 +201,17 @@ class Photo
// legacy data storage in "data" column
$i = self::selectFirst(['data'], ['id' => $photo['id']]);
if ($i === false) {
return null;
return '';
}
$data = $i['data'];
} else {
$backendRef = $photo['backend-ref'] ?? '';
$data = $backendClass->get($backendRef);
try {
$data = $backendClass->get($backendRef);
} catch (ReferenceStorageException $referenceStorageException) {
DI::logger()->debug('No data found for photo', ['photo' => $photo, 'exception' => $referenceStorageException]);
return '';
}
}
return $data;
}
@ -339,7 +347,7 @@ class Photo
if (DBA::isResult($existing_photo)) {
$backend_ref = (string)$existing_photo["backend-ref"];
$storage = DI::storageManager()->getByName($existing_photo["backend-class"] ?? '');
$storage = DI::storageManager()->getSelectableStorageByName($existing_photo["backend-class"] ?? '');
} else {
$storage = DI::storage();
}
@ -403,11 +411,14 @@ class Photo
$photos = DBA::select('photo', ['id', 'backend-class', 'backend-ref'], $conditions);
while ($photo = DBA::fetch($photos)) {
$backend_class = DI::storageManager()->getByName($photo['backend-class'] ?? '');
$backend_class = DI::storageManager()->getSelectableStorageByName($photo['backend-class'] ?? '');
if (!empty($backend_class)) {
if ($backend_class->delete($photo["backend-ref"] ?? '')) {
try {
$backend_class->delete($item['backend-ref'] ?? '');
// Delete the photos after they had been deleted successfully
DBA::delete("photo", ['id' => $photo['id']]);
} catch (ReferenceStorageException $referenceStorageException) {
DI::logger()->debug('phot doesn\'t exist.', ['conditions' => $conditions, 'exception' => $referenceStorageException]);
}
}
}
@ -437,7 +448,7 @@ class Photo
$photos = self::selectToArray(['backend-class', 'backend-ref'], $conditions);
foreach($photos as $photo) {
$backend_class = DI::storageManager()->getByName($photo['backend-class'] ?? '');
$backend_class = DI::storageManager()->getSelectableStorageByName($photo['backend-class'] ?? '');
if (!empty($backend_class)) {
$fields["backend-ref"] = $backend_class->put($img->asString(), $photo['backend-ref']);
} else {

View file

@ -52,12 +52,12 @@ class ExternalResource implements IStorage
try {
$fetchResult = HTTPSignature::fetchRaw($data->url, $data->uid, ['accept_content' => '']);
} catch (Exception $exception) {
throw new StorageException(sprintf('External resource failed to get %s', $reference), $exception->getCode(), $exception);
throw new ReferenceStorageException(sprintf('External resource failed to get %s', $reference), $exception->getCode(), $exception);
}
if ($fetchResult->isSuccess()) {
return $fetchResult->getBody();
} else {
throw new StorageException(sprintf('External resource failed to get %s', $reference), $fetchResult->getReturnCode(), new Exception($fetchResult->getBody()));
throw new ReferenceStorageException(sprintf('External resource failed to get %s', $reference), $fetchResult->getReturnCode(), new Exception($fetchResult->getBody()));
}
}

View file

@ -131,6 +131,8 @@ class Filesystem implements ISelectableStorage
if ($result === false) {
throw new StorageException(sprintf('Filesystem storage failed to get data to "%s". Check your write permissions', $file));
}
return $result;
}
/**