1
0
Fork 0

- Fixing SystemResource

- Adding tests for StorageManager
- Updating doc
This commit is contained in:
Philipp Holzer 2020-01-06 17:42:28 +01:00
commit dbd5b5bb6e
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
12 changed files with 547 additions and 59 deletions

View file

@ -16,6 +16,7 @@ use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\DI;
use Friendica\Model\Storage\IStorage;
use Friendica\Model\Storage\SystemResource;
use Friendica\Object\Image;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Images;
@ -223,7 +224,7 @@ class Photo
$values = array_fill(0, count($fields), "");
$photo = array_combine($fields, $values);
$photo["backend-class"] = Storage\SystemResource::class;
$photo["backend-class"] = SystemResource::NAME;
$photo["backend-ref"] = $filename;
$photo["type"] = $mimetype;
$photo["cacheable"] = false;
@ -275,7 +276,7 @@ class Photo
if (DBA::isResult($existing_photo)) {
$backend_ref = (string)$existing_photo["backend-ref"];
$storage = DI::facStorage()->getByName((string)$existing_photo["backend-class"]);
$storage = DI::facStorage()->getByName($existing_photo["backend-class"] ?? '');
} else {
$storage = DI::storage();
}

View file

@ -0,0 +1,32 @@
<?php
namespace Friendica\Model\Storage;
use Friendica\Core\L10n\L10n;
use Psr\Log\LoggerInterface;
/**
* A general storage class which loads common dependencies and implements common methods
*/
abstract class AbstractStorage implements IStorage
{
/** @var L10n */
protected $l10n;
/** @var LoggerInterface */
protected $logger;
/**
* @param L10n $l10n
* @param LoggerInterface $logger
*/
public function __construct(L10n $l10n, LoggerInterface $logger)
{
$this->l10n = $l10n;
$this->logger = $logger;
}
public function __toString()
{
return static::getName();
}
}

View file

@ -6,35 +6,32 @@
namespace Friendica\Model\Storage;
use Friendica\Core\L10n;
use Friendica\Core\L10n\L10n;
use Psr\Log\LoggerInterface;
use Friendica\Database\Database as DBA;
/**
* @brief Database based storage system
*
* This class manage data stored in database table.
*/
class Database implements IStorage
class Database extends AbstractStorage
{
const NAME = 'Database';
/** @var \Friendica\Database\Database */
/** @var DBA */
private $dba;
/** @var LoggerInterface */
private $logger;
/** @var L10n\L10n */
private $l10n;
/**
* @param \Friendica\Database\Database $dba
* @param LoggerInterface $logger
* @param L10n\L10n $l10n
* @param DBA $dba
* @param LoggerInterface $logger
* @param L10n $l10n
*/
public function __construct(\Friendica\Database\Database $dba, LoggerInterface $logger, L10n\L10n $l10n)
public function __construct(DBA $dba, LoggerInterface $logger, L10n $l10n)
{
$this->dba = $dba;
$this->logger = $logger;
$this->l10n = $l10n;
parent::__construct($l10n, $logger);
$this->dba = $dba;
}
/**
@ -58,15 +55,15 @@ class Database implements IStorage
if ($reference !== '') {
$result = $this->dba->update('storage', ['data' => $data], ['id' => $reference]);
if ($result === false) {
$this->logger->warning('Failed to update data.', ['id' => $reference, 'errorCode' => $this->dba->errorNo(), 'errorMessage' => $this->dba->errorMessage()]);
$this->logger->warning('Failed to update data.', ['id' => $reference, 'errorCode' => $this->dba->errorNo(), 'errorMessage' => $this->dba->errorMessage()]);
throw new StorageException($this->l10n->t('Database storage failed to update %s', $reference));
}
}
return $reference;
} else {
$result = $this->dba->insert('storage', ['data' => $data]);
if ($result === false) {
$this->logger->warning('Failed to insert data.', ['errorCode' => $this->dba->errorNo(), 'errorMessage' => $this->dba->errorMessage()]);
$this->logger->warning('Failed to insert data.', ['errorCode' => $this->dba->errorNo(), 'errorMessage' => $this->dba->errorMessage()]);
throw new StorageException($this->l10n->t('Database storage failed to insert data'));
}
@ -101,7 +98,7 @@ class Database implements IStorage
/**
* @inheritDoc
*/
public function __toString()
public static function getName()
{
return self::NAME;
}

View file

@ -21,7 +21,7 @@ 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 implements IStorage
class Filesystem extends AbstractStorage
{
const NAME = 'Filesystem';
@ -30,10 +30,6 @@ class Filesystem implements IStorage
/** @var IConfiguration */
private $config;
/** @var LoggerInterface */
private $logger;
/** @var L10n */
private $l10n;
/** @var string */
private $basePath;
@ -47,9 +43,9 @@ class Filesystem implements IStorage
*/
public function __construct(IConfiguration $config, LoggerInterface $logger, L10n $l10n)
{
parent::__construct($l10n, $logger);
$this->config = $config;
$this->logger = $logger;
$this->l10n = $l10n;
$path = $this->config->get('storage', 'filesystem_path', self::DEFAULT_BASE_FOLDER);
$this->basePath = rtrim($path, '/');
@ -185,7 +181,7 @@ class Filesystem implements IStorage
/**
* @inheritDoc
*/
public function __toString()
public static function getName()
{
return self::NAME;
}

View file

@ -96,4 +96,11 @@ interface IStorage
* @return string
*/
public function __toString();
/**
* The name of the backend
*
* @return string
*/
public static function getName();
}

View file

@ -16,10 +16,15 @@ use \BadMethodCallException;
*/
class SystemResource implements IStorage
{
const NAME = 'SystemResource';
// Valid folders to look for resources
const VALID_FOLDERS = ["images"];
public static function get($filename)
/**
* @inheritDoc
*/
public function get(string $filename)
{
$folder = dirname($filename);
if (!in_array($folder, self::VALID_FOLDERS)) {
@ -31,25 +36,48 @@ class SystemResource implements IStorage
return file_get_contents($filename);
}
public static function put($data, $filename = "")
/**
* @inheritDoc
*/
public function put(string $data, string $filename = '')
{
throw new BadMethodCallException();
}
public static function delete($filename)
public function delete(string $filename)
{
throw new BadMethodCallException();
}
public static function getOptions()
/**
* @inheritDoc
*/
public function getOptions()
{
return [];
}
public static function saveOptions($data)
/**
* @inheritDoc
*/
public function saveOptions(array $data)
{
return [];
}
/**
* @inheritDoc
*/
public function __toString()
{
return self::NAME;
}
/**
* @inheritDoc
*/
public static function getName()
{
return self::NAME;
}
}