Restructure Storage to new paradigm

This commit is contained in:
Philipp 2021-10-23 12:11:38 +02:00
parent 24f8ee8e67
commit 2ab0d06996
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
29 changed files with 229 additions and 199 deletions

View file

@ -0,0 +1,78 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Storage\Capability;
/**
* The interface to use for configurable storage backends
*/
interface ICanConfigureStorage
{
/**
* Get info about storage options
*
* @return array
*
* This method return an array with information about storage options
* from which the form presented to the user is build.
*
* The returned array is:
*
* [
* 'option1name' => [ ..info.. ],
* 'option2name' => [ ..info.. ],
* ...
* ]
*
* An empty array can be returned if backend doesn't have any options
*
* The info array for each option MUST be as follows:
*
* [
* 'type', // define the field used in form, and the type of data.
* // one of 'checkbox', 'combobox', 'custom', 'datetime',
* // 'input', 'intcheckbox', 'password', 'radio', 'richtext'
* // 'select', 'select_raw', 'textarea'
*
* 'label', // Translatable label of the field
* 'value', // Current value
* 'help text', // Translatable description for the field
* extra data // Optional. Depends on 'type':
* // select: array [ value => label ] of choices
* // intcheckbox: value of input element
* // select_raw: prebuild html string of < option > tags
* ]
*
* See https://github.com/friendica/friendica/wiki/Quick-Template-Guide
*/
public function getOptions(): array;
/**
* Validate and save options
*
* @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 function saveOptions(array $data): array;
}

View file

@ -0,0 +1,57 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Storage\Capability;
use Friendica\Core\Storage\Exception\ReferenceStorageException;
use Friendica\Core\Storage\Exception\StorageException;
/**
* Interface for basic storage backends
*/
interface ICanReadFromStorage
{
/**
* Get data from backend
*
* @param string $reference Data reference
*
* @return string
*
* @throws StorageException in case there's an unexpected error
* @throws ReferenceStorageException in case the reference doesn't exist
*/
public function get(string $reference): string;
/**
* The name of the backend
*
* @return string
*/
public function __toString(): string;
/**
* The name of the backend
*
* @return string
*/
public static function getName(): string;
}

View file

@ -0,0 +1,56 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Storage\Capability;
use Friendica\Core\Storage\Exception\ReferenceStorageException;
use Friendica\Core\Storage\Exception\StorageException;
/**
* Interface for writable storage backends
*
* Used for storages with CRUD functionality, mainly used for user data (e.g. photos, attachements).
* There's only one active writable storage possible. This type of storage is selectable by the current administrator.
*/
interface ICanWriteToStorage extends ICanReadFromStorage
{
/**
* Put data in backend as $ref. If $ref is not defined a new reference is created.
*
* @param string $data Data to save
* @param string $reference Data reference. Optional.
*
* @return string Saved data reference
*
* @throws StorageException in case there's an unexpected error
*/
public function put(string $data, string $reference = ""): string;
/**
* Remove data from backend
*
* @param string $reference Data reference
*
* @throws StorageException in case there's an unexpected error
* @throws ReferenceStorageException in case the reference doesn't exist
*/
public function delete(string $reference);
}

View file

@ -0,0 +1,29 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Storage\Exception;
/**
* Storage Exception in case of invalid storage class
*/
class InvalidClassStorageException extends StorageException
{
}

View file

@ -0,0 +1,29 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Storage\Exception;
/**
* Storage Exception in case of invalid references
*/
class ReferenceStorageException extends StorageException
{
}

View file

@ -0,0 +1,31 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Storage\Exception;
use Exception;
/**
* Storage Exception for unexpected failures
*/
class StorageException extends Exception
{
}

View file

@ -19,12 +19,20 @@
*
*/
namespace Friendica\Core;
namespace Friendica\Core\Storage\Repository;
use Exception;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\Storage\Exception\InvalidClassStorageException;
use Friendica\Core\Storage\Exception\ReferenceStorageException;
use Friendica\Core\Storage\Exception\StorageException;
use Friendica\Core\Storage\Capability\ICanReadFromStorage;
use Friendica\Core\Storage\Capability\ICanConfigureStorage;
use Friendica\Core\Storage\Capability\ICanWriteToStorage;
use Friendica\Database\Database;
use Friendica\Model\Storage;
use Friendica\Core\Storage\Type;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Psr\Log\LoggerInterface;
@ -42,15 +50,15 @@ class StorageManager
// Default storage backends
/** @var string[] */
const DEFAULT_BACKENDS = [
Storage\Filesystem::NAME,
Storage\Database::NAME,
Type\Filesystem::NAME,
Type\Database::NAME,
];
/** @var string[] List of valid backend classes */
private $validBackends;
/**
* @var Storage\IStorage[] A local cache for storage instances
* @var ICanReadFromStorage[] A local cache for storage instances
*/
private $backendInstances = [];
@ -63,7 +71,7 @@ class StorageManager
/** @var L10n */
private $l10n;
/** @var Storage\IWritableStorage */
/** @var ICanWriteToStorage */
private $currentBackend;
/**
@ -72,8 +80,8 @@ class StorageManager
* @param LoggerInterface $logger
* @param L10n $l10n
*
* @throws Storage\InvalidClassStorageException in case the active backend class is invalid
* @throws Storage\StorageException in case of unexpected errors during the active backend class loading
* @throws InvalidClassStorageException in case the active backend class is invalid
* @throws StorageException in case of unexpected errors during the active backend class loading
*/
public function __construct(Database $dba, IManageConfigValues $config, LoggerInterface $logger, L10n $l10n)
{
@ -92,9 +100,9 @@ class StorageManager
/**
* Return current storage backend class
*
* @return Storage\IWritableStorage
* @return ICanWriteToStorage
*/
public function getBackend()
public function getBackend(): ICanWriteToStorage
{
return $this->currentBackend;
}
@ -104,16 +112,16 @@ class StorageManager
*
* @param string $name Backend name
*
* @return Storage\IWritableStorage
* @return ICanWriteToStorage
*
* @throws Storage\InvalidClassStorageException in case there's no backend class for the name
* @throws Storage\StorageException in case of an unexpected failure during the hook call
* @throws InvalidClassStorageException in case there's no backend class for the name
* @throws StorageException in case of an unexpected failure during the hook call
*/
public function getWritableStorageByName(string $name): Storage\IWritableStorage
public function getWritableStorageByName(string $name): ICanWriteToStorage
{
$storage = $this->getByName($name, $this->validBackends);
if (!$storage instanceof Storage\IWritableStorage) {
throw new Storage\InvalidClassStorageException(sprintf('Backend %s is not writable', $name));
if (!$storage instanceof ICanWriteToStorage) {
throw new InvalidClassStorageException(sprintf('Backend %s is not writable', $name));
}
return $storage;
@ -124,19 +132,19 @@ class StorageManager
*
* @param string $name Backend name
*
* @return Storage\IStorageConfiguration|false
* @return ICanConfigureStorage|false
*
* @throws Storage\InvalidClassStorageException in case there's no backend class for the name
* @throws Storage\StorageException in case of an unexpected failure during the hook call
* @throws InvalidClassStorageException in case there's no backend class for the name
* @throws StorageException in case of an unexpected failure during the hook call
*/
public function getConfigurationByName(string $name)
{
switch ($name) {
// Try the filesystem backend
case Storage\Filesystem::getName():
return new Storage\FilesystemConfig($this->config, $this->l10n);
case Type\Filesystem::getName():
return new Type\FilesystemConfig($this->config, $this->l10n);
// try the database backend
case Storage\Database::getName():
case Type\Database::getName():
return false;
default:
$data = [
@ -145,13 +153,13 @@ class StorageManager
];
try {
Hook::callAll('storage_config', $data);
if (!($data['storage_config'] ?? null) instanceof Storage\IStorageConfiguration) {
throw new Storage\InvalidClassStorageException(sprintf('Configuration for backend %s was not found', $name));
if (!($data['storage_config'] ?? null) instanceof ICanConfigureStorage) {
throw new InvalidClassStorageException(sprintf('Configuration for backend %s was not found', $name));
}
return $data['storage_config'];
} catch (InternalServerErrorException $exception) {
throw new Storage\StorageException(sprintf('Failed calling hook::storage_config for backend %s', $name), $exception);
throw new StorageException(sprintf('Failed calling hook::storage_config for backend %s', $name), $exception);
}
}
}
@ -162,36 +170,36 @@ class StorageManager
* @param string $name Backend name
* @param string[]|null $validBackends possible, manual override of the valid backends
*
* @return Storage\IStorage
* @return ICanReadFromStorage
*
* @throws Storage\InvalidClassStorageException in case there's no backend class for the name
* @throws Storage\StorageException in case of an unexpected failure during the hook call
* @throws InvalidClassStorageException in case there's no backend class for the name
* @throws StorageException in case of an unexpected failure during the hook call
*/
public function getByName(string $name, array $validBackends = null): Storage\IStorage
public function getByName(string $name, array $validBackends = null): ICanReadFromStorage
{
// If there's no cached instance create a new instance
if (!isset($this->backendInstances[$name])) {
// If the current name isn't a valid backend (or the SystemResource instance) create it
if (!$this->isValidBackend($name, $validBackends)) {
throw new Storage\InvalidClassStorageException(sprintf('Backend %s is not valid', $name));
throw new InvalidClassStorageException(sprintf('Backend %s is not valid', $name));
}
switch ($name) {
// Try the filesystem backend
case Storage\Filesystem::getName():
$storageConfig = new Storage\FilesystemConfig($this->config, $this->l10n);
$this->backendInstances[$name] = new Storage\Filesystem($storageConfig->getStoragePath());
case Type\Filesystem::getName():
$storageConfig = new Type\FilesystemConfig($this->config, $this->l10n);
$this->backendInstances[$name] = new Type\Filesystem($storageConfig->getStoragePath());
break;
// try the database backend
case Storage\Database::getName():
$this->backendInstances[$name] = new Storage\Database($this->dba);
case Type\Database::getName():
$this->backendInstances[$name] = new Type\Database($this->dba);
break;
// at least, try if there's an addon for the backend
case Storage\SystemResource::getName():
$this->backendInstances[$name] = new Storage\SystemResource();
case \Friendica\Core\Storage\Type\SystemResource::getName():
$this->backendInstances[$name] = new \Friendica\Core\Storage\Type\SystemResource();
break;
case Storage\ExternalResource::getName():
$this->backendInstances[$name] = new Storage\ExternalResource();
case \Friendica\Core\Storage\Type\ExternalResource::getName():
$this->backendInstances[$name] = new \Friendica\Core\Storage\Type\ExternalResource();
break;
default:
$data = [
@ -200,13 +208,13 @@ class StorageManager
];
try {
Hook::callAll('storage_instance', $data);
if (!($data['storage'] ?? null) instanceof Storage\IStorage) {
throw new Storage\InvalidClassStorageException(sprintf('Backend %s was not found', $name));
if (!($data['storage'] ?? null) instanceof ICanReadFromStorage) {
throw new InvalidClassStorageException(sprintf('Backend %s was not found', $name));
}
$this->backendInstances[$data['name'] ?? $name] = $data['storage'];
} catch (InternalServerErrorException $exception) {
throw new Storage\StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception);
throw new StorageException(sprintf('Failed calling hook::storage_instance for backend %s', $name), $exception);
}
break;
}
@ -227,8 +235,8 @@ class StorageManager
{
$validBackends = $validBackends ?? array_merge($this->validBackends,
[
Storage\SystemResource::getName(),
Storage\ExternalResource::getName(),
Type\SystemResource::getName(),
Type\ExternalResource::getName(),
]);
return in_array($name, $validBackends);
}
@ -236,11 +244,11 @@ class StorageManager
/**
* Set current storage backend class
*
* @param Storage\IWritableStorage $storage The storage class
* @param ICanWriteToStorage $storage The storage class
*
* @return boolean True, if the set was successful
*/
public function setBackend(Storage\IWritableStorage $storage): bool
public function setBackend(ICanWriteToStorage $storage): bool
{
if ($this->config->set('storage', 'name', $storage::getName())) {
$this->currentBackend = $storage;
@ -271,9 +279,8 @@ class StorageManager
*/
public function register(string $class): bool
{
if (is_subclass_of($class, Storage\IStorage::class)) {
/** @var Storage\IStorage $class */
if (is_subclass_of($class, ICanReadFromStorage::class)) {
/** @var ICanReadFromStorage $class */
if ($this->isValidBackend($class::getName(), $this->validBackends)) {
return true;
}
@ -299,15 +306,14 @@ class StorageManager
*
* @return boolean True, if unregistering was successful
*
* @throws Storage\StorageException
* @throws StorageException
*/
public function unregister(string $class): bool
{
if (is_subclass_of($class, Storage\IStorage::class)) {
/** @var Storage\IStorage $class */
if (is_subclass_of($class, ICanReadFromStorage::class)) {
/** @var ICanReadFromStorage $class */
if ($this->currentBackend::getName() == $class::getName()) {
throw new Storage\StorageException(sprintf('Cannot unregister %s, because it\'s currently active.', $class::getName()));
throw new StorageException(sprintf('Cannot unregister %s, because it\'s currently active.', $class::getName()));
}
$key = array_search($class::getName(), $this->validBackends);
@ -336,18 +342,18 @@ class StorageManager
* Copy existing data to destination storage and delete from source.
* This method cannot move to legacy in-table `data` field.
*
* @param Storage\IWritableStorage $destination Destination storage class name
* @param array $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
* @param int $limit Limit of the process batch size, defaults to 5000
* @param ICanWriteToStorage $destination Destination storage class name
* @param array $tables Tables to look in for resources. Optional, defaults to ['photo', 'attach']
* @param int $limit Limit of the process batch size, defaults to 5000
*
* @return int Number of moved resources
* @throws Storage\StorageException
* @throws StorageException
* @throws Exception
*/
public function move(Storage\IWritableStorage $destination, array $tables = self::TABLES, int $limit = 5000): int
public function move(ICanWriteToStorage $destination, array $tables = self::TABLES, int $limit = 5000): int
{
if (!$this->isValidBackend($destination, $this->validBackends)) {
throw new Storage\StorageException(sprintf("Can't move to storage backend '%s'", $destination::getName()));
throw new StorageException(sprintf("Can't move to storage backend '%s'", $destination::getName()));
}
$moved = 0;
@ -369,10 +375,10 @@ class StorageManager
$source = $this->getWritableStorageByName($resource['backend-class'] ?? '');
$this->logger->info('Get data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
$data = $source->get($sourceRef);
} catch (Storage\InvalidClassStorageException $exception) {
} catch (InvalidClassStorageException $exception) {
$this->logger->info('Get data from DB resource field.', ['oldReference' => $sourceRef]);
$data = $resource['data'];
} catch (Storage\ReferenceStorageException $exception) {
} catch (ReferenceStorageException $exception) {
$this->logger->info('Invalid source reference.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
continue;
}
@ -385,7 +391,7 @@ class StorageManager
$this->logger->info('update row');
if ($this->dba->update($table, ['backend-class' => $destination::getName(), 'backend-ref' => $destinationRef, 'data' => ''], ['id' => $id])) {
if (!empty($source)) {
$this->logger->info('Delete data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
$this->logger->info('Deleted data from old backend.', ['oldBackend' => $source, 'oldReference' => $sourceRef]);
$source->delete($sourceRef);
}
$moved++;

View file

@ -0,0 +1,131 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Storage\Type;
use Exception;
use Friendica\Core\Storage\Exception\ReferenceStorageException;
use Friendica\Core\Storage\Exception\StorageException;
use Friendica\Core\Storage\Capability\ICanWriteToStorage;
use Friendica\Database\Database as DBA;
/**
* Database based storage system
*
* This class manage data stored in database table.
*/
class Database implements ICanWriteToStorage
{
const NAME = 'Database';
/** @var DBA */
private $dba;
/**
* @param DBA $dba
*/
public function __construct(DBA $dba)
{
$this->dba = $dba;
}
/**
* @inheritDoc
*/
public function get(string $reference): string
{
try {
$result = $this->dba->selectFirst('storage', ['data'], ['id' => $reference]);
if (!$this->dba->isResult($result)) {
throw new ReferenceStorageException(sprintf('Database storage cannot find data for reference %s', $reference));
}
return $result['data'];
} catch (Exception $exception) {
if ($exception instanceof ReferenceStorageException) {
throw $exception;
} else {
throw new StorageException(sprintf('Database storage failed to get %s', $reference), $exception->getCode(), $exception);
}
}
}
/**
* @inheritDoc
*/
public function put(string $data, string $reference = ''): string
{
if ($reference !== '') {
try {
$result = $this->dba->update('storage', ['data' => $data], ['id' => $reference]);
} catch (Exception $exception) {
throw new StorageException(sprintf('Database storage failed to update %s', $reference), $exception->getCode(), $exception);
}
if ($result === false) {
throw new StorageException(sprintf('Database storage failed to update %s', $reference), 500, new Exception($this->dba->errorMessage(), $this->dba->errorNo()));
}
return $reference;
} else {
try {
$result = $this->dba->insert('storage', ['data' => $data]);
} catch (Exception $exception) {
throw new StorageException(sprintf('Database storage failed to insert %s', $reference), $exception->getCode(), $exception);
}
if ($result === false) {
throw new StorageException(sprintf('Database storage failed to update %s', $reference), 500, new Exception($this->dba->errorMessage(), $this->dba->errorNo()));
}
return $this->dba->lastInsertId();
}
}
/**
* @inheritDoc
*/
public function delete(string $reference)
{
try {
if (!$this->dba->delete('storage', ['id' => $reference]) || $this->dba->affectedRows() === 0) {
throw new ReferenceStorageException(sprintf('Database storage failed to delete %s', $reference));
}
} catch (Exception $exception) {
if ($exception instanceof ReferenceStorageException) {
throw $exception;
} else {
throw new StorageException(sprintf('Database storage failed to delete %s', $reference), $exception->getCode(), $exception);
}
}
}
/**
* @inheritDoc
*/
public static function getName(): string
{
return self::NAME;
}
public function __toString(): string
{
return self::getName();
}
}

View file

@ -0,0 +1,81 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Storage\Type;
use Exception;
use Friendica\Core\Storage\Exception\ReferenceStorageException;
use Friendica\Core\Storage\Capability\ICanReadFromStorage;
use Friendica\Util\HTTPSignature;
/**
* External resource storage class
*
* This class is used to load external resources, like images.
* Is not intended to be selectable by admins as default storage class.
*/
class ExternalResource implements ICanReadFromStorage
{
const NAME = 'ExternalResource';
/**
* @inheritDoc
*/
public function get(string $reference): string
{
$data = json_decode($reference);
if (empty($data->url)) {
throw new ReferenceStorageException(sprintf('Invalid reference %s, cannot retrieve URL', $reference));
}
$parts = parse_url($data->url);
if (empty($parts['scheme']) || empty($parts['host'])) {
throw new ReferenceStorageException(sprintf('Invalid reference %s, cannot extract scheme and host', $reference));
}
try {
$fetchResult = HTTPSignature::fetchRaw($data->url, $data->uid, ['accept_content' => []]);
} catch (Exception $exception) {
throw new ReferenceStorageException(sprintf('External resource failed to get %s', $reference), $exception->getCode(), $exception);
}
if ($fetchResult->isSuccess()) {
return $fetchResult->getBody();
} else {
throw new ReferenceStorageException(sprintf('External resource failed to get %s', $reference), $fetchResult->getReturnCode(), new Exception($fetchResult->getBody()));
}
}
/**
* @inheritDoc
*/
public function __toString(): string
{
return self::NAME;
}
/**
* @inheritDoc
*/
public static function getName(): string
{
return self::NAME;
}
}

View file

@ -0,0 +1,185 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Storage\Type;
use Exception;
use Friendica\Core\Storage\Exception\ReferenceStorageException;
use Friendica\Core\Storage\Exception\StorageException;
use Friendica\Core\Storage\Capability\ICanWriteToStorage;
use Friendica\Util\Strings;
/**
* Filesystem based storage backend
*
* This class manage data on filesystem.
* Base folder for storage is set in storage.filesystem_path.
* Best would be for storage folder to be outside webserver folder, we are using a
* folder relative to code tree root as default to ease things for users in shared hostings.
* Each new resource gets a value as reference and is saved in a
* folder tree stucture created from that value.
*/
class Filesystem implements ICanWriteToStorage
{
const NAME = 'Filesystem';
/** @var string */
private $basePath;
/**
* Filesystem constructor.
*
* @param string $filesystemPath
*
* @throws StorageException in case the path doesn't exist or isn't writeable
*/
public function __construct(string $filesystemPath = FilesystemConfig::DEFAULT_BASE_FOLDER)
{
$path = $filesystemPath;
$this->basePath = rtrim($path, '/');
if (!is_dir($this->basePath) || !is_writable($this->basePath)) {
throw new StorageException(sprintf('Path "%s" does not exist or is not writeable.', $this->basePath));
}
}
/**
* Split data ref and return file path
*
* @param string $reference Data reference
*
* @return string
*/
private function pathForRef(string $reference): string
{
$fold1 = substr($reference, 0, 2);
$fold2 = substr($reference, 2, 2);
$file = substr($reference, 4);
return implode('/', [$this->basePath, $fold1, $fold2, $file]);
}
/**
* Create directory tree to store file, with .htaccess and index.html files
*
* @param string $file Path and filename
*
* @throws StorageException
*/
private function createFoldersForFile(string $file)
{
$path = dirname($file);
if (!is_dir($path)) {
if (!mkdir($path, 0770, true)) {
throw new StorageException(sprintf('Filesystem storage failed to create "%s". Check you write permissions.', $path));
}
}
while ($path !== $this->basePath) {
if (!is_file($path . '/index.html')) {
file_put_contents($path . '/index.html', '');
}
chmod($path . '/index.html', 0660);
chmod($path, 0770);
$path = dirname($path);
}
if (!is_file($path . '/index.html')) {
file_put_contents($path . '/index.html', '');
chmod($path . '/index.html', 0660);
}
}
/**
* @inheritDoc
*/
public function get(string $reference): string
{
$file = $this->pathForRef($reference);
if (!is_file($file)) {
throw new ReferenceStorageException(sprintf('Filesystem storage failed to get the file %s, The file is invalid', $reference));
}
$result = file_get_contents($file);
if ($result === false) {
throw new StorageException(sprintf('Filesystem storage failed to get data to "%s". Check your write permissions', $file));
}
return $result;
}
/**
* @inheritDoc
*/
public function put(string $data, string $reference = ''): string
{
if ($reference === '') {
try {
$reference = Strings::getRandomHex();
} catch (Exception $exception) {
throw new StorageException('Filesystem storage failed to generate a random hex', $exception->getCode(), $exception);
}
}
$file = $this->pathForRef($reference);
$this->createFoldersForFile($file);
$result = file_put_contents($file, $data);
// just in case the result is REALLY false, not zero or empty or anything else, throw the exception
if ($result === false) {
throw new StorageException(sprintf('Filesystem storage failed to save data to "%s". Check your write permissions', $file));
}
chmod($file, 0660);
return $reference;
}
/**
* @inheritDoc
*/
public function delete(string $reference)
{
$file = $this->pathForRef($reference);
if (!is_file($file)) {
throw new ReferenceStorageException(sprintf('File with reference "%s" doesn\'t exist', $reference));
}
if (!unlink($file)) {
throw new StorageException(sprintf('Cannot delete with file with reference "%s"', $reference));
}
}
/**
* @inheritDoc
*/
public static function getName(): string
{
return self::NAME;
}
public function __toString(): string
{
return self::getName();
}
}

View file

@ -0,0 +1,100 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Storage\Type;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\L10n;
use Friendica\Core\Storage\Capability\ICanConfigureStorage;
/**
* Filesystem based storage backend configuration
*/
class FilesystemConfig implements ICanConfigureStorage
{
// Default base folder
const DEFAULT_BASE_FOLDER = 'storage';
/** @var IManageConfigValues */
private $config;
/** @var string */
private $storagePath;
/** @var L10n */
private $l10n;
/**
* Returns the current storage path
*
* @return string
*/
public function getStoragePath(): string
{
return $this->storagePath;
}
/**
* Filesystem constructor.
*
* @param IManageConfigValues $config
* @param L10n $l10n
*/
public function __construct(IManageConfigValues $config, L10n $l10n)
{
$this->config = $config;
$this->l10n = $l10n;
$path = $this->config->get('storage', 'filesystem_path', self::DEFAULT_BASE_FOLDER);
$this->storagePath = rtrim($path, '/');
}
/**
* @inheritDoc
*/
public function getOptions(): array
{
return [
'storagepath' => [
'input',
$this->l10n->t('Storage base path'),
$this->storagePath,
$this->l10n->t('Folder where uploaded files are saved. For maximum security, This should be a path outside web server folder tree')
]
];
}
/**
* @inheritDoc
*/
public function saveOptions(array $data): array
{
$storagePath = $data['storagepath'] ?? '';
if ($storagePath === '' || !is_dir($storagePath)) {
return [
'storagepath' => $this->l10n->t('Enter a valid existing folder')
];
};
$this->config->set('storage', 'filesystem_path', $storagePath);
$this->storagePath = $storagePath;
return [];
}
}

View file

@ -0,0 +1,77 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Storage\Type;
use Friendica\Core\Storage\Exception\ReferenceStorageException;
use Friendica\Core\Storage\Exception\StorageException;
use Friendica\Core\Storage\Capability\ICanReadFromStorage;
/**
* System resource storage class
*
* This class is used to load system resources, like images.
* Is not intended to be selectable by admins as default storage class.
*/
class SystemResource implements ICanReadFromStorage
{
const NAME = 'SystemResource';
// Valid folders to look for resources
const VALID_FOLDERS = ["images"];
/**
* @inheritDoc
*/
public function get(string $reference): string
{
$folder = dirname($reference);
if (!in_array($folder, self::VALID_FOLDERS)) {
throw new ReferenceStorageException(sprintf('System Resource is invalid for reference %s, no valid folder found', $reference));
}
if (!file_exists($reference)) {
throw new StorageException(sprintf('System Resource is invalid for reference %s, the file doesn\'t exist', $reference));
}
$content = file_get_contents($reference);
if ($content === false) {
throw new StorageException(sprintf('Cannot get content for reference %s', $reference));
}
return $content;
}
/**
* @inheritDoc
*/
public function __toString(): string
{
return self::NAME;
}
/**
* @inheritDoc
*/
public static function getName(): string
{
return self::NAME;
}
}