mirror of
https://github.com/friendica/friendica
synced 2025-04-28 03:10:11 +00:00
Split Storage usage and Storage configuration
This commit is contained in:
parent
ac9e5df614
commit
065b46c721
12 changed files with 379 additions and 208 deletions
|
@ -113,22 +113,6 @@ class Database implements IWritableStorage
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getOptions(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function saveOptions(array $data): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
|
|
@ -22,8 +22,6 @@
|
|||
namespace Friendica\Model\Storage;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\Config\IConfig;
|
||||
use Friendica\Core\L10n;
|
||||
use Friendica\Util\Strings;
|
||||
|
||||
/**
|
||||
|
@ -40,30 +38,17 @@ class Filesystem implements IWritableStorage
|
|||
{
|
||||
const NAME = 'Filesystem';
|
||||
|
||||
// Default base folder
|
||||
const DEFAULT_BASE_FOLDER = 'storage';
|
||||
|
||||
/** @var IConfig */
|
||||
private $config;
|
||||
|
||||
/** @var string */
|
||||
private $basePath;
|
||||
|
||||
/** @var L10n */
|
||||
private $l10n;
|
||||
|
||||
/**
|
||||
* Filesystem constructor.
|
||||
*
|
||||
* @param IConfig $config
|
||||
* @param L10n $l10n
|
||||
* @param string $filesystemPath
|
||||
*/
|
||||
public function __construct(IConfig $config, L10n $l10n)
|
||||
public function __construct(string $filesystemPath = FilesystemConfig::DEFAULT_BASE_FOLDER)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->l10n = $l10n;
|
||||
|
||||
$path = $this->config->get('storage', 'filesystem_path', self::DEFAULT_BASE_FOLDER);
|
||||
$path = $filesystemPath;
|
||||
$this->basePath = rtrim($path, '/');
|
||||
}
|
||||
|
||||
|
@ -176,37 +161,6 @@ class Filesystem implements IWritableStorage
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'storagepath' => [
|
||||
'input',
|
||||
$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')
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @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->basePath = $storagePath;
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
|
99
src/Model/Storage/FilesystemConfig.php
Normal file
99
src/Model/Storage/FilesystemConfig.php
Normal file
|
@ -0,0 +1,99 @@
|
|||
<?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\Model\Storage;
|
||||
|
||||
use Friendica\Core\Config\IConfig;
|
||||
use Friendica\Core\L10n;
|
||||
|
||||
/**
|
||||
* Filesystem based storage backend configuration
|
||||
*/
|
||||
class FilesystemConfig implements IStorageConfiguration
|
||||
{
|
||||
// Default base folder
|
||||
const DEFAULT_BASE_FOLDER = 'storage';
|
||||
|
||||
/** @var IConfig */
|
||||
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 IConfig $config
|
||||
* @param L10n $l10n
|
||||
*/
|
||||
public function __construct(IConfig $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 [];
|
||||
}
|
||||
}
|
78
src/Model/Storage/IStorageConfiguration.php
Normal file
78
src/Model/Storage/IStorageConfiguration.php
Normal 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\Model\Storage;
|
||||
|
||||
/**
|
||||
* The interface to use for configurable storage backends
|
||||
*/
|
||||
interface IStorageConfiguration
|
||||
{
|
||||
/**
|
||||
* 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;
|
||||
}
|
|
@ -50,54 +50,4 @@ interface IWritableStorage extends IStorage
|
|||
* @throws ReferenceStorageException in case the reference doesn't exist
|
||||
*/
|
||||
public function delete(string $reference);
|
||||
|
||||
/**
|
||||
* Get info about storage options
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* This method return an array with informations 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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue