Skip to content

Commit 2aca8e0

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

6 files changed

Lines changed: 47 additions & 16 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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,18 @@ 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+
76+
try {
77+
$this->appData = $appData->getFolder('global');
78+
} catch (NotFoundException $e) {
79+
$this->appData = $appData->newFolder('global');
80+
}
7781
}
7882

7983
public function getImageUrl(string $key, bool $useSvg = true): string {

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)