diff --git a/apps/dashboard/appinfo/routes.php b/apps/dashboard/appinfo/routes.php index 704311bddf..c4df1f732f 100644 --- a/apps/dashboard/appinfo/routes.php +++ b/apps/dashboard/appinfo/routes.php @@ -29,6 +29,6 @@ return [ ['name' => 'dashboard#index', 'url' => '/', 'verb' => 'GET'], ['name' => 'dashboard#updateLayout', 'url' => '/layout', 'verb' => 'POST'], ['name' => 'dashboard#getBackground', 'url' => '/background', 'verb' => 'GET'], - ['name' => 'dashboard#setBackground', 'url' => '/background', 'verb' => 'POST'], + ['name' => 'dashboard#setBackground', 'url' => '/background/{type}', 'verb' => 'POST'], ] ]; diff --git a/apps/dashboard/lib/Controller/DashboardController.php b/apps/dashboard/lib/Controller/DashboardController.php index bd83ca4584..3f4c9fdbfa 100644 --- a/apps/dashboard/lib/Controller/DashboardController.php +++ b/apps/dashboard/lib/Controller/DashboardController.php @@ -108,6 +108,7 @@ class DashboardController extends Controller { $this->inititalStateService->provideInitialState('dashboard', 'firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1'); $this->inititalStateService->provideInitialState('dashboard', 'shippedBackgrounds', BackgroundService::SHIPPED_BACKGROUNDS); $this->inititalStateService->provideInitialState('dashboard', 'background', $this->config->getUserValue($this->userId, 'dashboard', 'background', 'default')); + $this->inititalStateService->provideInitialState('dashboard', 'version', $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', 0)); $this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0'); return new TemplateResponse('dashboard', 'index'); @@ -126,18 +127,35 @@ class DashboardController extends Controller { /** * @NoAdminRequired */ - public function setBackground($path = null, $url = null, $shipped = null): JSONResponse { - if ($shipped !== null) { - $this->backgroundService->setShippedBackground($shipped); - } else if ($path !== null) { - $this->backgroundService->setFileBackground($path); - } else if ($url !== null) { - $this->backgroundService->setUrlBackground($url); - } else { - $this->backgroundService->setDefaultBackground(); + public function setBackground(string $type = 'default', $value): JSONResponse { + $currentVersion = $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', 0); + try { + switch ($type) { + case 'shipped': + $this->backgroundService->setShippedBackground($value); + break; + case 'custom': + $this->backgroundService->setFileBackground($value); + break; + case 'color': + $this->backgroundService->setColorBackground($value); + break; + case 'default': + $this->backgroundService->setDefaultBackground(); + break; + default: + return new JSONResponse(['error' => 'Invalid type provided'], Http::STATUS_BAD_REQUEST); + } + } catch (\InvalidArgumentException $e) { + return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); } - - return new JSONResponse([]); + $currentVersion++; + $this->config->setUserValue($this->userId, 'dashboard', 'backgroundVersion', $currentVersion); + return new JSONResponse([ + 'type' => $type, + 'value' => $value, + 'version' => $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', $currentVersion) + ]); } /** diff --git a/apps/dashboard/lib/Service/BackgroundService.php b/apps/dashboard/lib/Service/BackgroundService.php index a651d11618..d4de491aea 100644 --- a/apps/dashboard/lib/Service/BackgroundService.php +++ b/apps/dashboard/lib/Service/BackgroundService.php @@ -74,19 +74,17 @@ class BackgroundService { } public function setShippedBackground($fileName) { + if (!in_array($fileName, self::SHIPPED_BACKGROUNDS)) { + throw new \InvalidArgumentException('The given file name is invalid'); + } $this->config->setUserValue($this->userId, 'dashboard', 'background', $fileName); } - public function setUrlBackground($url) { - $this->config->setUserValue($this->userId, 'dashboard', 'background', 'custom'); - if (substr($url, 0, 1) === '/') { - $url = \OC::$server->getURLGenerator()->getAbsoluteURL($url); + public function setColorBackground(string $color) { + if (!preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) { + throw new \InvalidArgumentException('The given color is invalid'); } - - $client = \OC::$server->getHTTPClientService()->newClient(); - $response = $client->get($url); - $content = $response->getBody(); - $newFile = $this->dashboardUserFolder->newFile('background.jpg', $content); + $this->config->setUserValue($this->userId, 'dashboard', 'background', $color); } public function getBackground() { diff --git a/apps/dashboard/src/App.vue b/apps/dashboard/src/App.vue index e266d64c6d..c59e473237 100644 --- a/apps/dashboard/src/App.vue +++ b/apps/dashboard/src/App.vue @@ -1,5 +1,5 @@