summaryrefslogtreecommitdiffstats
path: root/apps/theming/lib/Controller/UserThemeController.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/theming/lib/Controller/UserThemeController.php')
-rw-r--r--apps/theming/lib/Controller/UserThemeController.php63
1 files changed, 61 insertions, 2 deletions
diff --git a/apps/theming/lib/Controller/UserThemeController.php b/apps/theming/lib/Controller/UserThemeController.php
index 71d78db4b3d..327029b26cd 100644
--- a/apps/theming/lib/Controller/UserThemeController.php
+++ b/apps/theming/lib/Controller/UserThemeController.php
@@ -30,9 +30,15 @@ declare(strict_types=1);
*/
namespace OCA\Theming\Controller;
+use OCA\Theming\AppInfo\Application;
use OCA\Theming\ITheme;
+use OCA\Theming\Service\BackgroundService;
use OCA\Theming\Service\ThemesService;
+use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
+use OCP\AppFramework\Http\FileDisplayResponse;
+use OCP\AppFramework\Http\JSONResponse;
+use OCP\AppFramework\Http\NotFoundResponse;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCSController;
@@ -47,6 +53,7 @@ class UserThemeController extends OCSController {
private IConfig $config;
private IUserSession $userSession;
private ThemesService $themesService;
+ private BackgroundService $backgroundService;
/**
* Config constructor.
@@ -55,11 +62,13 @@ class UserThemeController extends OCSController {
IRequest $request,
IConfig $config,
IUserSession $userSession,
- ThemesService $themesService) {
+ ThemesService $themesService,
+ BackgroundService $backgroundService) {
parent::__construct($appName, $request);
$this->config = $config;
$this->userSession = $userSession;
$this->themesService = $themesService;
+ $this->backgroundService = $backgroundService;
$this->userId = $userSession->getUser()->getUID();
}
@@ -91,7 +100,7 @@ class UserThemeController extends OCSController {
*/
public function disableTheme(string $themeId): DataResponse {
$theme = $this->validateTheme($themeId);
-
+
// Enable selected theme
$this->themesService->disableTheme($theme);
return new DataResponse();
@@ -124,4 +133,54 @@ class UserThemeController extends OCSController {
return $themes[$themeId];
}
+
+ /**
+ * @NoAdminRequired
+ * @NoCSRFRequired
+ */
+ public function getBackground(): Http\Response {
+ $file = $this->backgroundService->getBackground();
+ if ($file !== null) {
+ $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => $file->getMimeType()]);
+ $response->cacheFor(24 * 60 * 60, false, true);
+ return $response;
+ }
+ return new NotFoundResponse();
+ }
+
+ /**
+ * @NoAdminRequired
+ */
+ public function setBackground(string $type = 'default', string $value = ''): JSONResponse {
+ $currentVersion = (int)$this->config->getUserValue($this->userId, Application::APP_ID, '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);
+ } catch (\Throwable $e) {
+ return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
+ }
+ $currentVersion++;
+ $this->config->setUserValue($this->userId, Application::APP_ID, 'backgroundVersion', (string)$currentVersion);
+ return new JSONResponse([
+ 'type' => $type,
+ 'value' => $value,
+ 'version' => $this->config->getUserValue($this->userId, Application::APP_ID, 'backgroundVersion', $currentVersion)
+ ]);
+ }
}