Merge pull request #8123 from nupplaphil/bu/8103_photo_broken
Storage class fixings
This commit is contained in:
commit
3f1a3c83b7
5 changed files with 99 additions and 23 deletions
|
@ -130,7 +130,6 @@ HELP;
|
||||||
|
|
||||||
protected function doMove()
|
protected function doMove()
|
||||||
{
|
{
|
||||||
$tables = null;
|
|
||||||
if (count($this->args) < 1 || count($this->args) > 2) {
|
if (count($this->args) < 1 || count($this->args) > 2) {
|
||||||
throw new CommandArgsException('Invalid arguments');
|
throw new CommandArgsException('Invalid arguments');
|
||||||
}
|
}
|
||||||
|
@ -141,6 +140,8 @@ HELP;
|
||||||
throw new CommandArgsException('Invalid table');
|
throw new CommandArgsException('Invalid table');
|
||||||
}
|
}
|
||||||
$tables = [$table];
|
$tables = [$table];
|
||||||
|
} else {
|
||||||
|
$tables = StorageManager::TABLES;
|
||||||
}
|
}
|
||||||
|
|
||||||
$current = $this->storageManager->getBackend();
|
$current = $this->storageManager->getBackend();
|
||||||
|
|
|
@ -62,7 +62,8 @@ class StorageManager
|
||||||
|
|
||||||
$currentName = $this->config->get('storage', 'name', '');
|
$currentName = $this->config->get('storage', 'name', '');
|
||||||
|
|
||||||
$this->currentBackend = $this->getByName($currentName);
|
// you can only use user backends as a "default" backend, so the second parameter is true
|
||||||
|
$this->currentBackend = $this->getByName($currentName, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,18 +80,21 @@ class StorageManager
|
||||||
* @brief Return storage backend class by registered name
|
* @brief Return storage backend class by registered name
|
||||||
*
|
*
|
||||||
* @param string|null $name Backend name
|
* @param string|null $name Backend name
|
||||||
* @param boolean $userBackend Just return instances in case it's a user backend (e.g. not SystemResource)
|
* @param boolean $onlyUserBackend True, if just user specific instances should be returrned (e.g. not SystemResource)
|
||||||
*
|
*
|
||||||
* @return Storage\IStorage|null null if no backend registered at $name
|
* @return Storage\IStorage|null null if no backend registered at $name
|
||||||
*
|
*
|
||||||
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
|
||||||
*/
|
*/
|
||||||
public function getByName(string $name = null, $userBackend = true)
|
public function getByName(string $name = null, $onlyUserBackend = false)
|
||||||
{
|
{
|
||||||
|
// @todo 2020.09 Remove this call after 2 releases
|
||||||
|
$name = $this->checkLegacyBackend($name);
|
||||||
|
|
||||||
// If there's no cached instance create a new instance
|
// If there's no cached instance create a new instance
|
||||||
if (!isset($this->backendInstances[$name])) {
|
if (!isset($this->backendInstances[$name])) {
|
||||||
// If the current name isn't a valid backend (or the SystemResource instance) create it
|
// If the current name isn't a valid backend (or the SystemResource instance) create it
|
||||||
if ($this->isValidBackend($name, $userBackend)) {
|
if ($this->isValidBackend($name, $onlyUserBackend)) {
|
||||||
switch ($name) {
|
switch ($name) {
|
||||||
// Try the filesystem backend
|
// Try the filesystem backend
|
||||||
case Storage\Filesystem::getName():
|
case Storage\Filesystem::getName():
|
||||||
|
@ -129,14 +133,33 @@ class StorageManager
|
||||||
* Checks, if the storage is a valid backend
|
* Checks, if the storage is a valid backend
|
||||||
*
|
*
|
||||||
* @param string|null $name The name or class of the backend
|
* @param string|null $name The name or class of the backend
|
||||||
* @param boolean $userBackend True, if just user backend should get returned (e.g. not SystemResource)
|
* @param boolean $onlyUserBackend True, if just user backend should get returned (e.g. not SystemResource)
|
||||||
*
|
*
|
||||||
* @return boolean True, if the backend is a valid backend
|
* @return boolean True, if the backend is a valid backend
|
||||||
*/
|
*/
|
||||||
public function isValidBackend(string $name = null, bool $userBackend = true)
|
public function isValidBackend(string $name = null, bool $onlyUserBackend = false)
|
||||||
{
|
{
|
||||||
return array_key_exists($name, $this->backends) ||
|
return array_key_exists($name, $this->backends) ||
|
||||||
(!$userBackend && $name === Storage\SystemResource::getName());
|
(!$onlyUserBackend && $name === Storage\SystemResource::getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check for legacy backend storage class names (= full model class name)
|
||||||
|
*
|
||||||
|
* @todo 2020.09 Remove this function after 2 releases, because there shouldn't be any legacy backend classes left
|
||||||
|
*
|
||||||
|
* @param string|null $name a potential, legacy storage name ("Friendica\Model\Storage\...")
|
||||||
|
*
|
||||||
|
* @return string|null The current storage name
|
||||||
|
*/
|
||||||
|
private function checkLegacyBackend(string $name = null)
|
||||||
|
{
|
||||||
|
if (stristr($name, 'Friendica\Model\Storage\\')) {
|
||||||
|
$this->logger->notice('Using deprecated storage class value', ['name' => $name]);
|
||||||
|
return substr($name, 24);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -148,12 +171,12 @@ class StorageManager
|
||||||
*/
|
*/
|
||||||
public function setBackend(string $name = null)
|
public function setBackend(string $name = null)
|
||||||
{
|
{
|
||||||
if (!$this->isValidBackend($name)) {
|
if (!$this->isValidBackend($name, false)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->config->set('storage', 'name', $name)) {
|
if ($this->config->set('storage', 'name', $name)) {
|
||||||
$this->currentBackend = $this->getByName($name);
|
$this->currentBackend = $this->getByName($name, false);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
@ -239,8 +262,8 @@ class StorageManager
|
||||||
*/
|
*/
|
||||||
public function move(Storage\IStorage $destination, array $tables = self::TABLES, int $limit = 5000)
|
public function move(Storage\IStorage $destination, array $tables = self::TABLES, int $limit = 5000)
|
||||||
{
|
{
|
||||||
if ($destination === null) {
|
if (!$this->isValidBackend($destination, true)) {
|
||||||
throw new Storage\StorageException('Can\'t move to NULL storage backend');
|
throw new Storage\StorageException(sprintf("Can't move to storage backend '%s'", $destination::getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
$moved = 0;
|
$moved = 0;
|
||||||
|
|
|
@ -125,7 +125,10 @@ class Filesystem extends AbstractStorage
|
||||||
|
|
||||||
$this->createFoldersForFile($file);
|
$this->createFoldersForFile($file);
|
||||||
|
|
||||||
if (!file_put_contents($file, $data)) {
|
$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) {
|
||||||
$this->logger->warning('Failed to write data.', ['file' => $file]);
|
$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));
|
throw new StorageException($this->l10n->t('Filesystem storage failed to save data to "%s". Check your write permissions', $file));
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,10 +109,42 @@ class StorageManagerTest extends DatabaseTest
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Data array for legacy backends
|
||||||
|
*
|
||||||
|
* @todo 2020.09 After 2 releases, remove the legacy functionality and these data array with it
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function dataLegacyBackends()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'legacyDatabase' => [
|
||||||
|
'name' => 'Friendica\Model\Storage\Database',
|
||||||
|
'assert' => Storage\Database::class,
|
||||||
|
'assertName' => Storage\Database::NAME,
|
||||||
|
'userBackend' => true,
|
||||||
|
],
|
||||||
|
'legacyFilesystem' => [
|
||||||
|
'name' => 'Friendica\Model\Storage\Filesystem',
|
||||||
|
'assert' => Storage\Filesystem::class,
|
||||||
|
'assertName' => Storage\Filesystem::NAME,
|
||||||
|
'userBackend' => true,
|
||||||
|
],
|
||||||
|
'legacySystemResource' => [
|
||||||
|
'name' => 'Friendica\Model\Storage\SystemResource',
|
||||||
|
'assert' => Storage\SystemResource::class,
|
||||||
|
'assertName' => Storage\SystemResource::NAME,
|
||||||
|
'userBackend' => false,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the getByName() method
|
* Test the getByName() method
|
||||||
*
|
*
|
||||||
* @dataProvider dataStorages
|
* @dataProvider dataStorages
|
||||||
|
* @dataProvider dataLegacyBackends
|
||||||
*/
|
*/
|
||||||
public function testGetByName($name, $assert, $assertName, $userBackend)
|
public function testGetByName($name, $assert, $assertName, $userBackend)
|
||||||
{
|
{
|
||||||
|
@ -123,7 +155,6 @@ class StorageManagerTest extends DatabaseTest
|
||||||
if (!empty($assert)) {
|
if (!empty($assert)) {
|
||||||
$this->assertInstanceOf(Storage\IStorage::class, $storage);
|
$this->assertInstanceOf(Storage\IStorage::class, $storage);
|
||||||
$this->assertInstanceOf($assert, $storage);
|
$this->assertInstanceOf($assert, $storage);
|
||||||
$this->assertEquals($name, $storage::getName());
|
|
||||||
} else {
|
} else {
|
||||||
$this->assertNull($storage);
|
$this->assertNull($storage);
|
||||||
}
|
}
|
||||||
|
@ -139,7 +170,11 @@ class StorageManagerTest extends DatabaseTest
|
||||||
{
|
{
|
||||||
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
|
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
|
||||||
|
|
||||||
$this->assertEquals($userBackend, $storageManager->isValidBackend($name));
|
// true in every of the backends
|
||||||
|
$this->assertEquals(!empty($assertName), $storageManager->isValidBackend($name));
|
||||||
|
|
||||||
|
// if userBackend is set to true, filter out e.g. SystemRessource
|
||||||
|
$this->assertEquals($userBackend, $storageManager->isValidBackend($name, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -174,6 +209,7 @@ class StorageManagerTest extends DatabaseTest
|
||||||
* Test the method getBackend() with a pre-configured backend
|
* Test the method getBackend() with a pre-configured backend
|
||||||
*
|
*
|
||||||
* @dataProvider dataStorages
|
* @dataProvider dataStorages
|
||||||
|
* @dataProvider dataLegacyBackends
|
||||||
*/
|
*/
|
||||||
public function testPresetBackend($name, $assert, $assertName, $userBackend)
|
public function testPresetBackend($name, $assert, $assertName, $userBackend)
|
||||||
{
|
{
|
||||||
|
@ -261,4 +297,17 @@ class StorageManagerTest extends DatabaseTest
|
||||||
$this->assertNotEmpty($data);
|
$this->assertNotEmpty($data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test moving data to a WRONG storage
|
||||||
|
*
|
||||||
|
* @expectedException \Friendica\Model\Storage\StorageException
|
||||||
|
* @expectedExceptionMessage Can't move to storage backend 'SystemResource'
|
||||||
|
*/
|
||||||
|
public function testMoveStorageWrong()
|
||||||
|
{
|
||||||
|
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
|
||||||
|
$storage = $storageManager->getByName(Storage\SystemResource::getName());
|
||||||
|
$storageManager->move($storage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -425,8 +425,8 @@ function update_1330()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update attachments and photos
|
// Update attachments and photos
|
||||||
if (!DBA::p("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 22) WHERE `photo`.`backend-class` LIKE 'Friendica\\Model\\Storage\\%'") ||
|
if (!DBA::p("UPDATE `photo` SET `photo`.`backend-class` = SUBSTR(`photo`.`backend-class`, 25) WHERE `photo`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'") ||
|
||||||
!DBA::p("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 22) WHERE `attach`.`backend-class` LIKE 'Friendica\\Model\\Storage\\%'")) {
|
!DBA::p("UPDATE `attach` SET `attach`.`backend-class` = SUBSTR(`attach`.`backend-class`, 25) WHERE `attach`.`backend-class` LIKE 'Friendica\\\Model\\\Storage\\\%' ESCAPE '|'")) {
|
||||||
return Update::FAILED;
|
return Update::FAILED;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue