Skip to content

Commit 11173a9

Browse files
committed
Scope the appdata theming storage for global and users
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
1 parent f49ccd1 commit 11173a9

6 files changed

Lines changed: 58 additions & 24 deletions

File tree

apps/theming/lib/Controller/UserThemeController.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ public function getBackground(): Http\Response {
157157
*/
158158
public function setBackground(string $type = 'default', string $value = ''): JSONResponse {
159159
$currentVersion = (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'backgroundVersion', '0');
160+
160161
try {
161162
switch ($type) {
162163
case 'shipped':
@@ -179,14 +180,14 @@ public function setBackground(string $type = 'default', string $value = ''): JSO
179180
} catch (\Throwable $e) {
180181
return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
181182
}
183+
182184
$currentVersion++;
183185
$this->config->setUserValue($this->userId, Application::APP_ID, 'backgroundVersion', (string)$currentVersion);
184-
// FIXME replace with user-specific cachebuster increase https://github.com/nextcloud/server/issues/34472
185-
$this->themingDefaults->increaseCacheBuster();
186+
186187
return new JSONResponse([
187188
'type' => $type,
188189
'value' => $value,
189-
'version' => $this->config->getUserValue($this->userId, Application::APP_ID, 'backgroundVersion', $currentVersion)
190+
'version' => $currentVersion,
190191
]);
191192
}
192193
}

apps/theming/lib/ImageManager.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,13 @@ public function __construct(IConfig $config,
6666
IURLGenerator $urlGenerator,
6767
ICacheFactory $cacheFactory,
6868
ILogger $logger,
69-
ITempManager $tempManager
70-
) {
69+
ITempManager $tempManager) {
7170
$this->config = $config;
72-
$this->appData = $appData;
7371
$this->urlGenerator = $urlGenerator;
7472
$this->cacheFactory = $cacheFactory;
7573
$this->logger = $logger;
7674
$this->tempManager = $tempManager;
75+
$this->appData = $appData;
7776
}
7877

7978
public function getImageUrl(string $key, bool $useSvg = true): string {
@@ -106,7 +105,7 @@ public function getImageUrlAbsolute(string $key, bool $useSvg = true): string {
106105
*/
107106
public function getImage(string $key, bool $useSvg = true): ISimpleFile {
108107
$logo = $this->config->getAppValue('theming', $key . 'Mime', '');
109-
$folder = $this->appData->getFolder('images');
108+
$folder = $this->getRootFolder()->getFolder('images');
110109
if ($logo === '' || !$folder->fileExists($key)) {
111110
throw new NotFoundException();
112111
}
@@ -158,9 +157,9 @@ public function getCustomImages(): array {
158157
public function getCacheFolder(): ISimpleFolder {
159158
$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
160159
try {
161-
$folder = $this->appData->getFolder($cacheBusterValue);
160+
$folder = $this->getRootFolder()->getFolder($cacheBusterValue);
162161
} catch (NotFoundException $e) {
163-
$folder = $this->appData->newFolder($cacheBusterValue);
162+
$folder = $this->getRootFolder()->newFolder($cacheBusterValue);
164163
$this->cleanup();
165164
}
166165
return $folder;
@@ -202,13 +201,13 @@ public function setCachedImage(string $filename, string $data): ISimpleFile {
202201
public function delete(string $key): void {
203202
/* ignore exceptions, since we don't want to fail hard if something goes wrong during cleanup */
204203
try {
205-
$file = $this->appData->getFolder('images')->getFile($key);
204+
$file = $this->getRootFolder()->getFolder('images')->getFile($key);
206205
$file->delete();
207206
} catch (NotFoundException $e) {
208207
} catch (NotPermittedException $e) {
209208
}
210209
try {
211-
$file = $this->appData->getFolder('images')->getFile($key . '.png');
210+
$file = $this->getRootFolder()->getFolder('images')->getFile($key . '.png');
212211
$file->delete();
213212
} catch (NotFoundException $e) {
214213
} catch (NotPermittedException $e) {
@@ -219,9 +218,9 @@ public function updateImage(string $key, string $tmpFile): string {
219218
$this->delete($key);
220219

221220
try {
222-
$folder = $this->appData->getFolder('images');
221+
$folder = $this->getRootFolder()->getFolder('images');
223222
} catch (NotFoundException $e) {
224-
$folder = $this->appData->newFolder('images');
223+
$folder = $this->getRootFolder()->newFolder('images');
225224
}
226225

227226
$target = $folder->newFile($key);
@@ -288,7 +287,7 @@ private function getSupportedUploadImageFormats(string $key): array {
288287
*/
289288
public function cleanup() {
290289
$currentFolder = $this->getCacheFolder();
291-
$folders = $this->appData->getDirectoryListing();
290+
$folders = $this->getRootFolder()->getDirectoryListing();
292291
foreach ($folders as $folder) {
293292
if ($folder->getName() !== 'images' && $folder->getName() !== $currentFolder->getName()) {
294293
$folder->delete();
@@ -316,4 +315,12 @@ public function shouldReplaceIcons() {
316315
$cache->set('shouldReplaceIcons', $value);
317316
return $value;
318317
}
318+
319+
private function getRootFolder(): ISimpleFolder {
320+
try {
321+
return $this->appData->getFolder('global');
322+
} catch (NotFoundException $e) {
323+
return $this->appData->newFolder('global');
324+
}
325+
}
319326
}

apps/theming/lib/Listener/BeforeTemplateRenderedListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function handle(Event $event): void {
9191

9292
$this->initialState->provideInitialState(
9393
'backgroundVersion',
94-
$this->config->getUserValue($userId, Application::APP_ID, 'backgroundVersion', 0),
94+
(int)$this->config->getUserValue($userId, Application::APP_ID, 'backgroundVersion', '0'),
9595
);
9696

9797
$this->initialState->provideInitialState(

apps/theming/lib/Service/BackgroundService.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,19 @@ class BackgroundService {
136136
private string $userId;
137137
private IAppDataFactory $appDataFactory;
138138

139-
public function __construct(
140-
IRootFolder $rootFolder,
141-
IAppDataFactory $appDataFactory,
142-
IConfig $config,
143-
?string $userId
144-
) {
139+
public function __construct(IRootFolder $rootFolder,
140+
IAppData $appData,
141+
IConfig $config,
142+
?string $userId,
143+
IAppDataFactory $appDataFactory) {
145144
if ($userId === null) {
146145
return;
147146
}
147+
148148
$this->rootFolder = $rootFolder;
149-
$this->appData = $appDataFactory->get(Application::APP_ID);
150149
$this->config = $config;
151150
$this->userId = $userId;
151+
$this->appData = $appData;
152152
$this->appDataFactory = $appDataFactory;
153153
}
154154

@@ -167,12 +167,15 @@ public function setDefaultBackground(): void {
167167
public function setFileBackground($path): void {
168168
$this->config->setUserValue($this->userId, Application::APP_ID, 'background', 'custom');
169169
$userFolder = $this->rootFolder->getUserFolder($this->userId);
170+
170171
/** @var File $file */
171172
$file = $userFolder->get($path);
172173
$image = new \OCP\Image();
174+
173175
if ($image->loadFromFileHandle($file->fopen('r')) === false) {
174176
throw new InvalidArgumentException('Invalid image file');
175177
}
178+
176179
$this->getAppDataFolder()->newFile('background.jpg', $file->fopen('r'));
177180
}
178181

@@ -207,14 +210,21 @@ public function getBackground(): ?ISimpleFile {
207210
}
208211

209212
/**
213+
* Storing the data in appdata/theming/users/USERID
214+
*
210215
* @return ISimpleFolder
211216
* @throws NotPermittedException
212217
*/
213218
private function getAppDataFolder(): ISimpleFolder {
214219
try {
215-
return $this->appData->getFolder($this->userId);
220+
$rootFolder = $this->appData->getFolder('users');
221+
} catch (NotFoundException $e) {
222+
$rootFolder = $this->appData->newFolder('users');
223+
}
224+
try {
225+
return $rootFolder->getFolder($this->userId);
216226
} catch (NotFoundException $e) {
217-
return $this->appData->newFolder($this->userId);
227+
return $rootFolder->newFolder($this->userId);
218228
}
219229
}
220230
}

apps/theming/src/UserThemes.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export default {
173173
this.updateGlobalStyles()
174174
this.$emit('update:background')
175175
},
176+
176177
updateGlobalStyles() {
177178
// Override primary-invert-if-bright and color-primary-text if background is set
178179
const isBackgroundBright = shippedBackgroundList[this.background]?.theming === 'dark'

lib/private/Files/SimpleFS/SimpleFolder.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,19 @@ public function newFile(string $name, $content = null): ISimpleFile {
9191
return new SimpleFile($file);
9292
}
9393
}
94+
95+
public function getFolder(string $name): ISimpleFolder {
96+
$folder = $this->folder->get($name);
97+
98+
if (!($folder instanceof Folder)) {
99+
throw new NotFoundException();
100+
}
101+
102+
return new SimpleFolder($folder);
103+
}
104+
105+
public function newFolder(string $path): ISimpleFolder {
106+
$folder = $this->folder->newFolder($path);
107+
return new SimpleFolder($folder);
108+
}
94109
}

0 commit comments

Comments
 (0)