Skip to content

Commit 15ff980

Browse files
authored
Merge pull request #23029 from nextcloud/backport/23024/stable20
[stable20] Add occ command to set theming values
2 parents 7496a10 + 19390a4 commit 15ff980

8 files changed

Lines changed: 302 additions & 163 deletions

File tree

apps/theming/appinfo/info.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@
2525
<admin>OCA\Theming\Settings\Admin</admin>
2626
<admin-section>OCA\Theming\Settings\Section</admin-section>
2727
</settings>
28+
<commands>
29+
<command>OCA\Theming\Command\UpdateConfig</command>
30+
</commands>
2831
</info>
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
4+
*
5+
* @author Julius Härtl <jus@bitgrid.net>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCA\Theming\Command;
25+
26+
use OCA\Theming\ImageManager;
27+
use OCA\Theming\ThemingDefaults;
28+
use OCP\IConfig;
29+
use Symfony\Component\Console\Command\Command;
30+
use Symfony\Component\Console\Input\InputArgument;
31+
use Symfony\Component\Console\Input\InputInterface;
32+
use Symfony\Component\Console\Input\InputOption;
33+
use Symfony\Component\Console\Output\OutputInterface;
34+
35+
class UpdateConfig extends Command {
36+
public const SUPPORTED_KEYS = [
37+
'name', 'url', 'imprintUrl', 'privacyUrl', 'slogan', 'color'
38+
];
39+
40+
public const SUPPORTED_IMAGE_KEYS = [
41+
'background', 'logo', 'favicon', 'logoheader'
42+
];
43+
44+
private $themingDefaults;
45+
private $imageManager;
46+
private $config;
47+
48+
public function __construct(ThemingDefaults $themingDefaults, ImageManager $imageManager, IConfig $config) {
49+
parent::__construct();
50+
51+
$this->themingDefaults = $themingDefaults;
52+
$this->imageManager = $imageManager;
53+
$this->config = $config;
54+
}
55+
56+
protected function configure() {
57+
$this
58+
->setName('theming:config')
59+
->setDescription('Set theming app config values')
60+
->addArgument(
61+
'key',
62+
InputArgument::OPTIONAL,
63+
'Key to update the theming app configuration (leave empty to get a list of all configured values)' . PHP_EOL .
64+
'One of: ' . implode(', ', self::SUPPORTED_KEYS)
65+
)
66+
->addArgument(
67+
'value',
68+
InputArgument::OPTIONAL,
69+
'Value to set (leave empty to obtain the current value)'
70+
)
71+
->addOption(
72+
'reset',
73+
'r',
74+
InputOption::VALUE_NONE,
75+
'Reset the given config key to default'
76+
);
77+
}
78+
79+
80+
protected function execute(InputInterface $input, OutputInterface $output): int {
81+
$key = $input->getArgument('key');
82+
$value = $input->getArgument('value');
83+
84+
if ($key === null) {
85+
$output->writeln('Current theming config:');
86+
foreach (self::SUPPORTED_KEYS as $key) {
87+
$value = $this->config->getAppValue('theming', $key, '');
88+
$output->writeln('- ' . $key . ': ' . $value . '');
89+
}
90+
foreach (self::SUPPORTED_IMAGE_KEYS as $key) {
91+
$value = $this->config->getAppValue('theming', $key . 'Mime', '');
92+
$output->writeln('- ' . $key . ': ' . $value . '');
93+
}
94+
return 0;
95+
}
96+
97+
if (!in_array($key, self::SUPPORTED_KEYS, true)) {
98+
$output->writeln('<error>Invalid config key provided</error>');
99+
return 1;
100+
}
101+
102+
if ($input->getOption('reset')) {
103+
$defaultValue = $this->themingDefaults->undo($key);
104+
$output->writeln('<info>Reset ' . $key . ' to ' . $defaultValue . '</info>');
105+
return 0;
106+
}
107+
108+
if ($value === null) {
109+
$value = $this->config->getAppValue('theming', $key, '');
110+
if ($value !== '') {
111+
$output->writeln('<info>' . $key . ' is currently set to ' . $value . '</info>');
112+
} else {
113+
$output->writeln('<info>' . $key . ' is currently not set</info>');
114+
}
115+
return 0;
116+
}
117+
118+
if (in_array($key, self::SUPPORTED_IMAGE_KEYS, true)) {
119+
if (file_exists(__DIR__ . $value)) {
120+
$value = __DIR__ . $value;
121+
}
122+
if (!file_exists($value)) {
123+
$output->writeln('<error>File could not be found: ' . $value . '</error>');
124+
return 1;
125+
}
126+
$value = $this->imageManager->updateImage($key, $value);
127+
$key = $key . 'Mime';
128+
}
129+
130+
$this->themingDefaults->set($key, $value);
131+
$output->writeln('<info>Updated ' . $key . ' to ' . $value . '</info>');
132+
133+
return 0;
134+
}
135+
}

apps/theming/lib/Controller/ThemingController.php

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,6 @@ private function isValidUrl(string $url): bool {
214214
* @throws NotPermittedException
215215
*/
216216
public function uploadImage(): DataResponse {
217-
// logo / background
218-
// new: favicon logo-header
219-
//
220217
$key = $this->request->getParam('key');
221218
$image = $this->request->getUploadedFile('image');
222219
$error = null;
@@ -249,52 +246,22 @@ public function uploadImage(): DataResponse {
249246
);
250247
}
251248

252-
$name = '';
253249
try {
254-
$folder = $this->appData->getFolder('images');
255-
} catch (NotFoundException $e) {
256-
$folder = $this->appData->newFolder('images');
257-
}
258-
259-
$this->imageManager->delete($key);
260-
261-
$target = $folder->newFile($key);
262-
$supportedFormats = $this->getSupportedUploadImageFormats($key);
263-
$detectedMimeType = mime_content_type($image['tmp_name']);
264-
if (!in_array($image['type'], $supportedFormats) || !in_array($detectedMimeType, $supportedFormats)) {
250+
$mime = $this->imageManager->updateImage($key, $image['tmp_name']);
251+
$this->themingDefaults->set($key . 'Mime', $mime);
252+
} catch (\Exception $e) {
265253
return new DataResponse(
266254
[
267255
'data' => [
268-
'message' => $this->l10n->t('Unsupported image type'),
256+
'message' => $e->getMessage()
269257
],
270258
'status' => 'failure',
271259
],
272260
Http::STATUS_UNPROCESSABLE_ENTITY
273261
);
274262
}
275263

276-
if ($key === 'background' && strpos($detectedMimeType, 'image/svg') === false) {
277-
// Optimize the image since some people may upload images that will be
278-
// either to big or are not progressive rendering.
279-
$newImage = @imagecreatefromstring(file_get_contents($image['tmp_name'], 'r'));
280-
281-
$tmpFile = $this->tempManager->getTemporaryFile();
282-
$newWidth = imagesx($newImage) < 4096 ? imagesx($newImage) : 4096;
283-
$newHeight = imagesy($newImage) / (imagesx($newImage) / $newWidth);
284-
$outputImage = imagescale($newImage, $newWidth, $newHeight);
285-
286-
imageinterlace($outputImage, 1);
287-
imagejpeg($outputImage, $tmpFile, 75);
288-
imagedestroy($outputImage);
289-
290-
$target->putContent(file_get_contents($tmpFile, 'r'));
291-
} else {
292-
$target->putContent(file_get_contents($image['tmp_name'], 'r'));
293-
}
294264
$name = $image['name'];
295-
296-
$this->themingDefaults->set($key.'Mime', $image['type']);
297-
298265
$cssCached = $this->scssCacher->process(\OC::$SERVERROOT, 'core/css/css-variables.scss', 'core');
299266

300267
return new DataResponse(
@@ -311,24 +278,6 @@ public function uploadImage(): DataResponse {
311278
);
312279
}
313280

314-
/**
315-
* Returns a list of supported mime types for image uploads.
316-
* "favicon" images are only allowed to be SVG when imagemagick with SVG support is available.
317-
*
318-
* @param string $key The image key, e.g. "favicon"
319-
* @return array
320-
*/
321-
private function getSupportedUploadImageFormats(string $key): array {
322-
$supportedFormats = ['image/jpeg', 'image/png', 'image/gif',];
323-
324-
if ($key !== 'favicon' || $this->imageManager->shouldReplaceIcons() === true) {
325-
$supportedFormats[] = 'image/svg+xml';
326-
$supportedFormats[] = 'image/svg';
327-
}
328-
329-
return $supportedFormats;
330-
}
331-
332281
/**
333282
* Revert setting to default value
334283
*
@@ -341,11 +290,6 @@ public function undo(string $setting): DataResponse {
341290
// reprocess server scss for preview
342291
$cssCached = $this->scssCacher->process(\OC::$SERVERROOT, 'core/css/css-variables.scss', 'core');
343292

344-
if (strpos($setting, 'Mime') !== -1) {
345-
$imageKey = str_replace('Mime', '', $setting);
346-
$this->imageManager->delete($imageKey);
347-
}
348-
349293
return new DataResponse(
350294
[
351295
'data' =>

apps/theming/lib/ImageManager.php

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use OCP\ICacheFactory;
3737
use OCP\IConfig;
3838
use OCP\ILogger;
39+
use OCP\ITempManager;
3940
use OCP\IURLGenerator;
4041

4142
class ImageManager {
@@ -52,27 +53,22 @@ class ImageManager {
5253
private $cacheFactory;
5354
/** @var ILogger */
5455
private $logger;
56+
/** @var ITempManager */
57+
private $tempManager;
5558

56-
/**
57-
* ImageManager constructor.
58-
*
59-
* @param IConfig $config
60-
* @param IAppData $appData
61-
* @param IURLGenerator $urlGenerator
62-
* @param ICacheFactory $cacheFactory
63-
* @param ILogger $logger
64-
*/
6559
public function __construct(IConfig $config,
6660
IAppData $appData,
6761
IURLGenerator $urlGenerator,
6862
ICacheFactory $cacheFactory,
69-
ILogger $logger
63+
ILogger $logger,
64+
ITempManager $tempManager
7065
) {
7166
$this->config = $config;
7267
$this->appData = $appData;
7368
$this->urlGenerator = $urlGenerator;
7469
$this->cacheFactory = $cacheFactory;
7570
$this->logger = $logger;
71+
$this->tempManager = $tempManager;
7672
}
7773

7874
public function getImageUrl(string $key, bool $useSvg = true): string {
@@ -211,6 +207,62 @@ public function delete(string $key) {
211207
}
212208
}
213209

210+
public function updateImage(string $key, string $tmpFile) {
211+
$this->delete($key);
212+
213+
try {
214+
$folder = $this->appData->getFolder('images');
215+
} catch (NotFoundException $e) {
216+
$folder = $this->appData->newFolder('images');
217+
}
218+
219+
$target = $folder->newFile($key);
220+
$supportedFormats = $this->getSupportedUploadImageFormats($key);
221+
$detectedMimeType = mime_content_type($tmpFile);
222+
if (!in_array($detectedMimeType, $supportedFormats, true)) {
223+
throw new \Exception('Unsupported image type');
224+
}
225+
226+
if ($key === 'background' && strpos($detectedMimeType, 'image/svg') === false) {
227+
// Optimize the image since some people may upload images that will be
228+
// either to big or are not progressive rendering.
229+
$newImage = @imagecreatefromstring(file_get_contents($tmpFile));
230+
231+
$tmpFile = $this->tempManager->getTemporaryFile();
232+
$newWidth = (int)(imagesx($newImage) < 4096 ? imagesx($newImage) : 4096);
233+
$newHeight = (int)(imagesy($newImage) / (imagesx($newImage) / $newWidth));
234+
$outputImage = imagescale($newImage, $newWidth, $newHeight);
235+
236+
imageinterlace($outputImage, 1);
237+
imagejpeg($outputImage, $tmpFile, 75);
238+
imagedestroy($outputImage);
239+
240+
$target->putContent(file_get_contents($tmpFile));
241+
} else {
242+
$target->putContent(file_get_contents($tmpFile));
243+
}
244+
245+
return $detectedMimeType;
246+
}
247+
248+
/**
249+
* Returns a list of supported mime types for image uploads.
250+
* "favicon" images are only allowed to be SVG when imagemagick with SVG support is available.
251+
*
252+
* @param string $key The image key, e.g. "favicon"
253+
* @return array
254+
*/
255+
private function getSupportedUploadImageFormats(string $key): array {
256+
$supportedFormats = ['image/jpeg', 'image/png', 'image/gif'];
257+
258+
if ($key !== 'favicon' || $this->shouldReplaceIcons() === true) {
259+
$supportedFormats[] = 'image/svg+xml';
260+
$supportedFormats[] = 'image/svg';
261+
}
262+
263+
return $supportedFormats;
264+
}
265+
214266
/**
215267
* remove cached files that are not required any longer
216268
*

apps/theming/lib/ThemingDefaults.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ public function undo($setting) {
398398
$this->config->deleteAppValue('theming', $setting);
399399
$this->increaseCacheBuster();
400400

401+
$returnValue = '';
401402
switch ($setting) {
402403
case 'name':
403404
$returnValue = $this->getEntity();
@@ -411,8 +412,11 @@ public function undo($setting) {
411412
case 'color':
412413
$returnValue = $this->getColorPrimary();
413414
break;
414-
default:
415-
$returnValue = '';
415+
case 'logo':
416+
case 'logoheader':
417+
case 'background':
418+
case 'favicon':
419+
$this->imageManager->delete($setting);
416420
break;
417421
}
418422

0 commit comments

Comments
 (0)