Skip to content

Commit

Permalink
Scope the appdata theming storage for global and users
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <[email protected]>
  • Loading branch information
skjnldsv committed Oct 14, 2022
1 parent f49ccd1 commit 11173a9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 24 deletions.
7 changes: 4 additions & 3 deletions apps/theming/lib/Controller/UserThemeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public function getBackground(): Http\Response {
*/
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':
Expand All @@ -179,14 +180,14 @@ public function setBackground(string $type = 'default', string $value = ''): JSO
} 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);
// FIXME replace with user-specific cachebuster increase https://github.com/nextcloud/server/issues/34472
$this->themingDefaults->increaseCacheBuster();

return new JSONResponse([
'type' => $type,
'value' => $value,
'version' => $this->config->getUserValue($this->userId, Application::APP_ID, 'backgroundVersion', $currentVersion)
'version' => $currentVersion,
]);
}
}
29 changes: 18 additions & 11 deletions apps/theming/lib/ImageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,13 @@ public function __construct(IConfig $config,
IURLGenerator $urlGenerator,
ICacheFactory $cacheFactory,
ILogger $logger,
ITempManager $tempManager
) {
ITempManager $tempManager) {
$this->config = $config;
$this->appData = $appData;
$this->urlGenerator = $urlGenerator;
$this->cacheFactory = $cacheFactory;
$this->logger = $logger;
$this->tempManager = $tempManager;
$this->appData = $appData;
}

public function getImageUrl(string $key, bool $useSvg = true): string {
Expand Down Expand Up @@ -106,7 +105,7 @@ public function getImageUrlAbsolute(string $key, bool $useSvg = true): string {
*/
public function getImage(string $key, bool $useSvg = true): ISimpleFile {
$logo = $this->config->getAppValue('theming', $key . 'Mime', '');
$folder = $this->appData->getFolder('images');
$folder = $this->getRootFolder()->getFolder('images');
if ($logo === '' || !$folder->fileExists($key)) {
throw new NotFoundException();
}
Expand Down Expand Up @@ -158,9 +157,9 @@ public function getCustomImages(): array {
public function getCacheFolder(): ISimpleFolder {
$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
try {
$folder = $this->appData->getFolder($cacheBusterValue);
$folder = $this->getRootFolder()->getFolder($cacheBusterValue);
} catch (NotFoundException $e) {
$folder = $this->appData->newFolder($cacheBusterValue);
$folder = $this->getRootFolder()->newFolder($cacheBusterValue);
$this->cleanup();
}
return $folder;
Expand Down Expand Up @@ -202,13 +201,13 @@ public function setCachedImage(string $filename, string $data): ISimpleFile {
public function delete(string $key): void {
/* ignore exceptions, since we don't want to fail hard if something goes wrong during cleanup */
try {
$file = $this->appData->getFolder('images')->getFile($key);
$file = $this->getRootFolder()->getFolder('images')->getFile($key);
$file->delete();
} catch (NotFoundException $e) {
} catch (NotPermittedException $e) {
}
try {
$file = $this->appData->getFolder('images')->getFile($key . '.png');
$file = $this->getRootFolder()->getFolder('images')->getFile($key . '.png');
$file->delete();
} catch (NotFoundException $e) {
} catch (NotPermittedException $e) {
Expand All @@ -219,9 +218,9 @@ public function updateImage(string $key, string $tmpFile): string {
$this->delete($key);

try {
$folder = $this->appData->getFolder('images');
$folder = $this->getRootFolder()->getFolder('images');
} catch (NotFoundException $e) {
$folder = $this->appData->newFolder('images');
$folder = $this->getRootFolder()->newFolder('images');
}

$target = $folder->newFile($key);
Expand Down Expand Up @@ -288,7 +287,7 @@ private function getSupportedUploadImageFormats(string $key): array {
*/
public function cleanup() {
$currentFolder = $this->getCacheFolder();
$folders = $this->appData->getDirectoryListing();
$folders = $this->getRootFolder()->getDirectoryListing();
foreach ($folders as $folder) {
if ($folder->getName() !== 'images' && $folder->getName() !== $currentFolder->getName()) {
$folder->delete();
Expand Down Expand Up @@ -316,4 +315,12 @@ public function shouldReplaceIcons() {
$cache->set('shouldReplaceIcons', $value);
return $value;
}

private function getRootFolder(): ISimpleFolder {
try {
return $this->appData->getFolder('global');
} catch (NotFoundException $e) {
return $this->appData->newFolder('global');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function handle(Event $event): void {

$this->initialState->provideInitialState(
'backgroundVersion',
$this->config->getUserValue($userId, Application::APP_ID, 'backgroundVersion', 0),
(int)$this->config->getUserValue($userId, Application::APP_ID, 'backgroundVersion', '0'),
);

$this->initialState->provideInitialState(
Expand Down
28 changes: 19 additions & 9 deletions apps/theming/lib/Service/BackgroundService.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,19 @@ class BackgroundService {
private string $userId;
private IAppDataFactory $appDataFactory;

public function __construct(
IRootFolder $rootFolder,
IAppDataFactory $appDataFactory,
IConfig $config,
?string $userId
) {
public function __construct(IRootFolder $rootFolder,
IAppData $appData,
IConfig $config,
?string $userId,
IAppDataFactory $appDataFactory) {
if ($userId === null) {
return;
}

$this->rootFolder = $rootFolder;
$this->appData = $appDataFactory->get(Application::APP_ID);
$this->config = $config;
$this->userId = $userId;
$this->appData = $appData;
$this->appDataFactory = $appDataFactory;
}

Expand All @@ -167,12 +167,15 @@ public function setDefaultBackground(): void {
public function setFileBackground($path): void {
$this->config->setUserValue($this->userId, Application::APP_ID, 'background', 'custom');
$userFolder = $this->rootFolder->getUserFolder($this->userId);

/** @var File $file */
$file = $userFolder->get($path);
$image = new \OCP\Image();

if ($image->loadFromFileHandle($file->fopen('r')) === false) {
throw new InvalidArgumentException('Invalid image file');
}

$this->getAppDataFolder()->newFile('background.jpg', $file->fopen('r'));
}

Expand Down Expand Up @@ -207,14 +210,21 @@ public function getBackground(): ?ISimpleFile {
}

/**
* Storing the data in appdata/theming/users/USERID
*
* @return ISimpleFolder
* @throws NotPermittedException
*/
private function getAppDataFolder(): ISimpleFolder {
try {
return $this->appData->getFolder($this->userId);
$rootFolder = $this->appData->getFolder('users');
} catch (NotFoundException $e) {
$rootFolder = $this->appData->newFolder('users');
}
try {
return $rootFolder->getFolder($this->userId);
} catch (NotFoundException $e) {
return $this->appData->newFolder($this->userId);
return $rootFolder->newFolder($this->userId);
}
}
}
1 change: 1 addition & 0 deletions apps/theming/src/UserThemes.vue
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export default {
this.updateGlobalStyles()
this.$emit('update:background')
},

updateGlobalStyles() {
// Override primary-invert-if-bright and color-primary-text if background is set
const isBackgroundBright = shippedBackgroundList[this.background]?.theming === 'dark'
Expand Down
15 changes: 15 additions & 0 deletions lib/private/Files/SimpleFS/SimpleFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,19 @@ public function newFile(string $name, $content = null): ISimpleFile {
return new SimpleFile($file);
}
}

public function getFolder(string $name): ISimpleFolder {
$folder = $this->folder->get($name);

if (!($folder instanceof Folder)) {
throw new NotFoundException();
}

return new SimpleFolder($folder);
}

public function newFolder(string $path): ISimpleFolder {
$folder = $this->folder->newFolder($path);
return new SimpleFolder($folder);
}
}

0 comments on commit 11173a9

Please sign in to comment.