Add WebDavConfig

This commit is contained in:
Philipp 2021-10-04 11:40:05 +02:00
parent 52407530db
commit 2038eec6d7
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
3 changed files with 177 additions and 114 deletions

View file

@ -3,8 +3,6 @@
namespace Friendica\Addon\webdav_storage\src; namespace Friendica\Addon\webdav_storage\src;
use Exception; use Exception;
use Friendica\Core\Config\IConfig;
use Friendica\Core\L10n;
use Friendica\Model\Storage\IWritableStorage; use Friendica\Model\Storage\IWritableStorage;
use Friendica\Model\Storage\ReferenceStorageException; use Friendica\Model\Storage\ReferenceStorageException;
use Friendica\Model\Storage\StorageException; use Friendica\Model\Storage\StorageException;
@ -20,12 +18,6 @@ class WebDav implements IWritableStorage
{ {
const NAME = 'WebDav'; const NAME = 'WebDav';
/** @var L10n */
private $l10n;
/** @var IConfig */
private $config;
/** @var string */ /** @var string */
private $url; private $url;
@ -38,27 +30,23 @@ class WebDav implements IWritableStorage
/** @var array */ /** @var array */
private $authOptions; private $authOptions;
public function __construct(L10n $l10n, IConfig $config, IHTTPClient $client, LoggerInterface $logger) /**
* WebDav constructor
*
* @param string $url The full URL to the webdav endpoint (including the subdirectories)
* @param array $authOptions The authentication options for the http calls ( ['username', 'password', 'auth_type'] )
* @param IHTTPClient $client The http client for communicating with the WebDav endpoint
* @param LoggerInterface $logger The standard logging class
*/
public function __construct(string $url, array $authOptions, IHTTPClient $client, LoggerInterface $logger)
{ {
$this->l10n = $l10n;
$this->config = $config;
$this->client = $client; $this->client = $client;
$this->logger = $logger; $this->logger = $logger;
$this->authOptions = null; $this->authOptions = $authOptions;
$this->url = $url;
if (!empty($this->config->get('webdav', 'username'))) {
$this->authOptions = [
$this->config->get('webdav', 'username'),
(string)$this->config->get('webdav', 'password', ''),
$this->config->get('webdav', 'auth_type', 'basic')
];
}
$this->url = $this->config->get('webdav', 'url');
} }
/** /**
* Split data ref and return file path * Split data ref and return file path
* *
@ -262,95 +250,6 @@ class WebDav implements IWritableStorage
$this->checkAndDeletePath($file[1]); $this->checkAndDeletePath($file[1]);
} }
/**
* @inheritDoc
*/
public function getOptions(): array
{
$auths = [
'' => 'None',
'basic' => 'Basic',
'digest' => 'Digest',
];
return [
'url' => [
'input',
$this->l10n->t('URL'),
$this->url,
$this->l10n->t('URL to the Webdav endpoint, where files can be saved'),
true
],
'username' => [
'input',
$this->l10n->t('Username'),
$this->config->get('webdav', 'username', ''),
$this->l10n->t('Username to authenticate to the Webdav endpoint')
],
'password' => [
'password',
$this->l10n->t('Password'),
$this->config->get('webdav', 'username', ''),
$this->l10n->t('Password to authenticate to the Webdav endpoint')
],
'auth_type' => [
'select',
$this->l10n->t('Authentication type'),
$this->config->get('webdav', 'auth_type', ''),
$this->l10n->t('authentication type to the Webdav endpoint'),
$auths,
]
];
}
/**
* @inheritDoc
*/
public function saveOptions(array $data): array
{
$url = $data['url'] ?? '';
$username = $data['username'] ?? '';
$password = $data['password'] ?? '';
$auths = [
'' => 'None',
'basic' => 'Basic',
'digest' => 'Digest',
];
$authType = $data['auth_type'] ?? '';
if (!key_exists($authType, $auths)) {
return [
'auth_type' => $this->l10n->t('Authentication type is invalid.'),
];
}
$options = null;
if (!empty($username)) {
$options = [
$username,
$password,
$authType
];
}
if (!$this->client->head($url, [HTTPClientOptions::AUTH => $options])->isSuccess()) {
return [
'url' => $this->l10n->t('url is either invalid or not reachable'),
];
}
$this->config->set('webdav', 'url', $url);
$this->config->set('webdav', 'username', $username);
$this->config->set('webdav', 'password', $password);
$this->config->set('webdav', 'auth_type', $authType);
$this->url = $url;
return [];
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View file

@ -0,0 +1,156 @@
<?php
namespace Friendica\Addon\webdav_storage\src;
use Friendica\Core\Config\IConfig;
use Friendica\Core\L10n;
use Friendica\Model\Storage\IStorageConfiguration;
use Friendica\Network\HTTPClientOptions;
use Friendica\Network\IHTTPClient;
/**
* A WebDav Backend Storage class
*/
class WebDavConfig implements IStorageConfiguration
{
const NAME = 'WebDav';
/** @var L10n */
private $l10n;
/** @var IConfig */
private $config;
/** @var string */
private $url;
/** @var IHTTPClient */
private $client;
/** @var array */
private $authOptions;
/**
* @return string
*/
public function getUrl(): string
{
return $this->url;
}
/**
* @return array
*/
public function getAuthOptions(): array
{
return $this->authOptions;
}
public function __construct(L10n $l10n, IConfig $config, IHTTPClient $client)
{
$this->l10n = $l10n;
$this->config = $config;
$this->client = $client;
$this->authOptions = null;
if (!empty($this->config->get('webdav', 'username'))) {
$this->authOptions = [
$this->config->get('webdav', 'username'),
(string)$this->config->get('webdav', 'password', ''),
$this->config->get('webdav', 'auth_type', 'basic')
];
}
$this->url = $this->config->get('webdav', 'url');
}
/**
* @inheritDoc
*/
public function getOptions(): array
{
$auths = [
'' => 'None',
'basic' => 'Basic',
'digest' => 'Digest',
];
return [
'url' => [
'input',
$this->l10n->t('URL'),
$this->url,
$this->l10n->t('URL to the Webdav endpoint, where files can be saved'),
true
],
'username' => [
'input',
$this->l10n->t('Username'),
$this->config->get('webdav', 'username', ''),
$this->l10n->t('Username to authenticate to the Webdav endpoint')
],
'password' => [
'password',
$this->l10n->t('Password'),
$this->config->get('webdav', 'username', ''),
$this->l10n->t('Password to authenticate to the Webdav endpoint')
],
'auth_type' => [
'select',
$this->l10n->t('Authentication type'),
$this->config->get('webdav', 'auth_type', ''),
$this->l10n->t('authentication type to the Webdav endpoint'),
$auths,
]
];
}
/**
* @inheritDoc
*/
public function saveOptions(array $data): array
{
$url = $data['url'] ?? '';
$username = $data['username'] ?? '';
$password = $data['password'] ?? '';
$auths = [
'' => 'None',
'basic' => 'Basic',
'digest' => 'Digest',
];
$authType = $data['auth_type'] ?? '';
if (!key_exists($authType, $auths)) {
return [
'auth_type' => $this->l10n->t('Authentication type is invalid.'),
];
}
$options = null;
if (!empty($username)) {
$options = [
$username,
$password,
$authType
];
}
if (!$this->client->head($url, [HTTPClientOptions::AUTH => $options])->isSuccess()) {
return [
'url' => $this->l10n->t('url is either invalid or not reachable'),
];
}
$this->config->set('webdav', 'url', $url);
$this->config->set('webdav', 'username', $username);
$this->config->set('webdav', 'password', $password);
$this->config->set('webdav', 'auth_type', $authType);
$this->url = $url;
return [];
}
}

View file

@ -7,6 +7,7 @@
*/ */
use Friendica\Addon\webdav_storage\src\WebDav; use Friendica\Addon\webdav_storage\src\WebDav;
use Friendica\Addon\webdav_storage\src\WebDavConfig;
use Friendica\App; use Friendica\App;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\DI; use Friendica\DI;
@ -14,15 +15,22 @@ use Friendica\DI;
function webdav_storage_install($a) function webdav_storage_install($a)
{ {
Hook::register('storage_instance' , __FILE__, 'webdav_storage_instance'); Hook::register('storage_instance' , __FILE__, 'webdav_storage_instance');
Hook::register('storage_config' , __FILE__, 'webdav_storage_config');
DI::storageManager()->register(WebDav::class); DI::storageManager()->register(WebDav::class);
} }
function webdav_storage_uninstall() function webdav_storage_uninstall()
{ {
DI::storageManager()->unregister(WebDav::getName()); DI::storageManager()->unregister(WebDav::class);
} }
function webdav_storage_instance(App $a, array &$data) function webdav_storage_instance(App $a, array &$data)
{ {
$data['storage'] = new WebDav(DI::l10n(), DI::config(), DI::httpClient(), DI::logger()); $config = new WebDavConfig(DI::l10n(), DI::config(), DI::httpClient());
$data['storage'] = new WebDav($config->getUrl(), $config->getAuthOptions(), DI::httpClient(), DI::logger());
}
function webdav_storage_config(App $a, array &$data)
{
$data['storage_config'] = new WebDavConfig(DI::l10n(), DI::config(), DI::httpClient());
} }