Only setup filesystem if needed for dashboard background service

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2020-11-18 09:03:56 +01:00
parent 0f1cc78389
commit e904da9d7a
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
1 changed files with 38 additions and 20 deletions

View File

@ -26,11 +26,18 @@ declare(strict_types=1);
namespace OCA\Dashboard\Service; namespace OCA\Dashboard\Service;
use InvalidArgumentException;
use OC\User\NoUserException;
use OCP\Files\File;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\Files\IRootFolder; use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use OCP\IConfig; use OCP\IConfig;
use OCP\Lock\LockedException;
use OCP\PreConditionNotMetException;
class BackgroundService { class BackgroundService {
public const THEMING_MODE_DARK = 'dark'; public const THEMING_MODE_DARK = 'dark';
@ -102,13 +109,13 @@ class BackgroundService {
] ]
]; ];
/** /**
* @var \OCP\Files\Folder * @var IRootFolder
*/ */
private $userFolder; private $rootFolder;
/** /**
* @var \OCP\Files\SimpleFS\ISimpleFolder * @var IAppData
*/ */
private $dashboardUserFolder; private $appData;
/** /**
* @var IConfig * @var IConfig
*/ */
@ -119,12 +126,8 @@ class BackgroundService {
if ($userId === null) { if ($userId === null) {
return; return;
} }
$this->userFolder = $rootFolder->getUserFolder($userId); $this->rootFolder = $rootFolder;
try { $this->appData = $appData;
$this->dashboardUserFolder = $appData->getFolder($userId);
} catch (NotFoundException $e) {
$this->dashboardUserFolder = $appData->newFolder($userId);
}
$this->config = $config; $this->config = $config;
$this->userId = $userId; $this->userId = $userId;
} }
@ -136,26 +139,29 @@ class BackgroundService {
/** /**
* @param $path * @param $path
* @throws NotFoundException * @throws NotFoundException
* @throws \OCP\Files\NotPermittedException * @throws NotPermittedException
* @throws \OCP\PreConditionNotMetException * @throws LockedException
* @throws PreConditionNotMetException
* @throws NoUserException
*/ */
public function setFileBackground($path): void { public function setFileBackground($path): void {
$this->config->setUserValue($this->userId, 'dashboard', 'background', 'custom'); $this->config->setUserValue($this->userId, 'dashboard', 'background', 'custom');
/** @var \OCP\Files\File $file */ $userFolder = $this->rootFolder->getUserFolder($this->userId);
$file = $this->userFolder->get($path); /** @var File $file */
$this->dashboardUserFolder->newFile('background.jpg', $file->fopen('r')); $file = $userFolder->get($path);
$this->getAppDataFolder()->newFile('background.jpg', $file->fopen('r'));
} }
public function setShippedBackground($fileName): void { public function setShippedBackground($fileName): void {
if (!array_key_exists($fileName, self::SHIPPED_BACKGROUNDS)) { if (!array_key_exists($fileName, self::SHIPPED_BACKGROUNDS)) {
throw new \InvalidArgumentException('The given file name is invalid'); throw new InvalidArgumentException('The given file name is invalid');
} }
$this->config->setUserValue($this->userId, 'dashboard', 'background', $fileName); $this->config->setUserValue($this->userId, 'dashboard', 'background', $fileName);
} }
public function setColorBackground(string $color): void { public function setColorBackground(string $color): void {
if (!preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) { if (!preg_match('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) {
throw new \InvalidArgumentException('The given color is invalid'); throw new InvalidArgumentException('The given color is invalid');
} }
$this->config->setUserValue($this->userId, 'dashboard', 'background', $color); $this->config->setUserValue($this->userId, 'dashboard', 'background', $color);
} }
@ -164,10 +170,22 @@ class BackgroundService {
$background = $this->config->getUserValue($this->userId, 'dashboard', 'background', 'default'); $background = $this->config->getUserValue($this->userId, 'dashboard', 'background', 'default');
if ($background === 'custom') { if ($background === 'custom') {
try { try {
return $this->dashboardUserFolder->getFile('background.jpg'); return $this->getAppDataFolder()->getFile('background.jpg');
} catch (NotFoundException $e) { } catch (NotFoundException | NotPermittedException $e) {
} }
} }
return null; return null;
} }
/**
* @return ISimpleFolder
* @throws NotPermittedException
*/
private function getAppDataFolder(): ISimpleFolder {
try {
return $this->appData->getFolder($this->userId);
} catch (NotFoundException $e) {
return $this->appData->newFolder($this->userId);
}
}
} }