Make Storage testable & add tests

- Making StorageManager dynamic (DI::facStorage())
- Making concrete Storage dynamic (DI::storage())
- Add tests for Storage backend and failure handling
- Bumping Level-2/Dice to "dev-master" until new release
- Using Storage-Names instead of Storage-Classes in config (includes migration)
This commit is contained in:
nupplaPhil 2020-01-05 01:58:49 +01:00
parent a5895f8623
commit 08edeae2f9
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
18 changed files with 744 additions and 242 deletions

View file

@ -6,9 +6,8 @@
namespace Friendica\Model\Storage;
use Friendica\Core\Logger;
use Friendica\Core\L10n;
use Friendica\Database\DBA;
use Psr\Log\LoggerInterface;
/**
* @brief Database based storage system
@ -17,47 +16,93 @@ use Friendica\Database\DBA;
*/
class Database implements IStorage
{
public static function get($ref)
const NAME = 'Database';
/** @var \Friendica\Database\Database */
private $dba;
/** @var LoggerInterface */
private $logger;
/** @var L10n\L10n */
private $l10n;
/**
* @param \Friendica\Database\Database $dba
* @param LoggerInterface $logger
* @param L10n\L10n $l10n
*/
public function __construct(\Friendica\Database\Database $dba, LoggerInterface $logger, L10n\L10n $l10n)
{
$r = DBA::selectFirst('storage', ['data'], ['id' => $ref]);
if (!DBA::isResult($r)) {
$this->dba = $dba;
$this->logger = $logger;
$this->l10n = $l10n;
}
/**
* @inheritDoc
*/
public function get(string $reference)
{
$result = $this->dba->selectFirst('storage', ['data'], ['id' => $reference]);
if (!$this->dba->isResult($result)) {
return '';
}
return $r['data'];
return $result['data'];
}
public static function put($data, $ref = '')
/**
* @inheritDoc
*/
public function put(string $data, string $reference = '')
{
if ($ref !== '') {
$r = DBA::update('storage', ['data' => $data], ['id' => $ref]);
if ($r === false) {
Logger::log('Failed to update data with id ' . $ref . ': ' . DBA::errorNo() . ' : ' . DBA::errorMessage());
throw new StorageException(L10n::t('Database storage failed to update %s', $ref));
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()]);
throw new StorageException($this->l10n->t('Database storage failed to update %s', $reference));
}
return $ref;
return $reference;
} else {
$r = DBA::insert('storage', ['data' => $data]);
if ($r === false) {
Logger::log('Failed to insert data: ' . DBA::errorNo() . ' : ' . DBA::errorMessage());
throw new StorageException(L10n::t('Database storage failed to insert data'));
$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()]);
throw new StorageException($this->l10n->t('Database storage failed to insert data'));
}
return DBA::lastInsertId();
return $this->dba->lastInsertId();
}
}
public static function delete($ref)
/**
* @inheritDoc
*/
public function delete(string $reference)
{
return DBA::delete('storage', ['id' => $ref]);
return $this->dba->delete('storage', ['id' => $reference]);
}
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;
}
}

View file

@ -6,10 +6,10 @@
namespace Friendica\Model\Storage;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\Logger;
use Friendica\Core\Config\IConfiguration;
use Friendica\Core\L10n\L10n;
use Friendica\Util\Strings;
use Psr\Log\LoggerInterface;
/**
* @brief Filesystem based storage backend
@ -23,50 +23,74 @@ use Friendica\Util\Strings;
*/
class Filesystem implements IStorage
{
const NAME = 'Filesystem';
// Default base folder
const DEFAULT_BASE_FOLDER = 'storage';
private static function getBasePath()
/** @var IConfiguration */
private $config;
/** @var LoggerInterface */
private $logger;
/** @var L10n */
private $l10n;
/** @var string */
private $basePath;
/**
* Filesystem constructor.
*
* @param IConfiguration $config
* @param LoggerInterface $logger
* @param L10n $l10n
*/
public function __construct(IConfiguration $config, LoggerInterface $logger, L10n $l10n)
{
$path = Config::get('storage', 'filesystem_path', self::DEFAULT_BASE_FOLDER);
return rtrim($path, '/');
$this->config = $config;
$this->logger = $logger;
$this->l10n = $l10n;
$path = $this->config->get('storage', 'filesystem_path', self::DEFAULT_BASE_FOLDER);
$this->basePath = rtrim($path, '/');
}
/**
* @brief Split data ref and return file path
* @param string $ref Data reference
*
* @param string $reference Data reference
*
* @return string
*/
private static function pathForRef($ref)
private function pathForRef(string $reference)
{
$base = self::getBasePath();
$fold1 = substr($ref, 0, 2);
$fold2 = substr($ref, 2, 2);
$file = substr($ref, 4);
$fold1 = substr($reference, 0, 2);
$fold2 = substr($reference, 2, 2);
$file = substr($reference, 4);
return implode('/', [$base, $fold1, $fold2, $file]);
return implode('/', [$this->basePath, $fold1, $fold2, $file]);
}
/**
* @brief Create dirctory tree to store file, with .htaccess and index.html files
*
* @param string $file Path and filename
*
* @throws StorageException
*/
private static function createFoldersForFile($file)
private function createFoldersForFile(string $file)
{
$path = dirname($file);
if (!is_dir($path)) {
if (!mkdir($path, 0770, true)) {
Logger::log('Failed to create dirs ' . $path);
throw new StorageException(L10n::t('Filesystem storage failed to create "%s". Check you write permissions.', $path));
$this->logger->warning('Failed to create dir.', ['path' => $path]);
throw new StorageException($this->l10n->t('Filesystem storage failed to create "%s". Check you write permissions.', $path));
}
}
$base = self::getBasePath();
while ($path !== $base) {
while ($path !== $this->basePath) {
if (!is_file($path . '/index.html')) {
file_put_contents($path . '/index.html', '');
}
@ -80,9 +104,12 @@ class Filesystem implements IStorage
}
}
public static function get($ref)
/**
* @inheritDoc
*/
public function get(string $reference)
{
$file = self::pathForRef($ref);
$file = $this->pathForRef($reference);
if (!is_file($file)) {
return '';
}
@ -90,27 +117,33 @@ class Filesystem implements IStorage
return file_get_contents($file);
}
public static function put($data, $ref = '')
/**
* @inheritDoc
*/
public function put(string $data, string $reference = '')
{
if ($ref === '') {
$ref = Strings::getRandomHex();
if ($reference === '') {
$reference = Strings::getRandomHex();
}
$file = self::pathForRef($ref);
$file = $this->pathForRef($reference);
self::createFoldersForFile($file);
$this->createFoldersForFile($file);
$r = file_put_contents($file, $data);
if ($r === FALSE) {
Logger::log('Failed to write data to ' . $file);
throw new StorageException(L10n::t('Filesystem storage failed to save data to "%s". Check your write permissions', $file));
if ((file_exists($file) && !is_writable($file)) || !file_put_contents($file, $data)) {
$this->logger->warning('Failed to write data.', ['file' => $file]);
throw new StorageException($this->l10n->t('Filesystem storage failed to save data to "%s". Check your write permissions', $file));
}
chmod($file, 0660);
return $ref;
return $reference;
}
public static function delete($ref)
/**
* @inheritDoc
*/
public function delete(string $reference)
{
$file = self::pathForRef($ref);
$file = $this->pathForRef($reference);
// return true if file doesn't exists. we want to delete it: success with zero work!
if (!is_file($file)) {
return true;
@ -118,28 +151,42 @@ class Filesystem implements IStorage
return unlink($file);
}
public static function getOptions()
/**
* @inheritDoc
*/
public function getOptions()
{
return [
'storagepath' => [
'input',
L10n::t('Storage base path'),
self::getBasePath(),
L10n::t('Folder where uploaded files are saved. For maximum security, This should be a path outside web server folder tree')
$this->l10n->t('Storage base path'),
$this->basePath,
$this->l10n->t('Folder where uploaded files are saved. For maximum security, This should be a path outside web server folder tree')
]
];
}
public static function saveOptions($data)
/**
* @inheritDoc
*/
public function saveOptions(array $data)
{
$storagepath = $data['storagepath'] ?? '';
if ($storagepath === '' || !is_dir($storagepath)) {
$storagePath = $data['storagepath'] ?? '';
if ($storagePath === '' || !is_dir($storagePath)) {
return [
'storagepath' => L10n::t('Enter a valid existing folder')
'storagepath' => $this->l10n->t('Enter a valid existing folder')
];
};
Config::set('storage', 'filesystem_path', $storagepath);
$this->config->set('storage', 'filesystem_path', $storagePath);
$this->basePath = $storagePath;
return [];
}
/**
* @inheritDoc
*/
public function __toString()
{
return self::NAME;
}
}

View file

@ -13,26 +13,32 @@ interface IStorage
{
/**
* @brief Get data from backend
* @param string $ref Data reference
*
* @param string $reference Data reference
*
* @return string
*/
public static function get($ref);
*/
public function get(string $reference);
/**
* @brief Put data in backend as $ref. If $ref is not defined a new reference is created.
* @param string $data Data to save
* @param string $ref Data referece. Optional.
* @return string Saved data referece
*
* @param string $data Data to save
* @param string $reference Data reference. Optional.
*
* @return string Saved data reference
*/
public static function put($data, $ref = "");
public function put(string $data, string $reference = "");
/**
* @brief Remove data from backend
* @param string $ref Data referece
*
* @param string $reference Data reference
*
* @return boolean True on success
*/
public static function delete($ref);
public function delete(string $reference);
/**
* @brief Get info about storage options
*
@ -71,19 +77,23 @@ interface IStorage
*
* See https://github.com/friendica/friendica/wiki/Quick-Template-Guide
*/
public static function getOptions();
public function getOptions();
/**
* @brief Validate and save options
*
* @param array $data Array [optionname => value] to be saved
* @param array $data Array [optionname => value] to be saved
*
* @return array Validation errors: [optionname => error message]
*
* Return array must be empty if no error.
*/
public static function saveOptions($data);
public function saveOptions(array $data);
/**
* The name of the backend
*
* @return string
*/
public function __toString();
}