|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | +namespace OC\Repair; |
| 10 | + |
| 11 | +use OCA\Theming\ImageManager; |
| 12 | +use OCP\IConfig; |
| 13 | +use OCP\Migration\IOutput; |
| 14 | +use OCP\Migration\IRepairStep; |
| 15 | +use OCP\Server; |
| 16 | + |
| 17 | +class RepairLogoDimension implements IRepairStep { |
| 18 | + public function __construct( |
| 19 | + protected IConfig $config, |
| 20 | + ) { |
| 21 | + } |
| 22 | + |
| 23 | + public function getName(): string { |
| 24 | + return 'Cache logo dimension to fix size in emails on Outlook'; |
| 25 | + } |
| 26 | + |
| 27 | + public function run(IOutput $output): void { |
| 28 | + $logoDimensions = $this->config->getAppValue('theming', 'logoDimensions'); |
| 29 | + if (preg_match('/^\d+x\d+$/', $logoDimensions)) { |
| 30 | + $output->info('Logo dimensions are already known'); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + try { |
| 35 | + /** @var ImageManager $imageManager */ |
| 36 | + $imageManager = Server::get(ImageManager::class); |
| 37 | + } catch (\Throwable) { |
| 38 | + $output->info('Theming is disabled'); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + if (!$imageManager->hasImage('logo')) { |
| 43 | + $output->info('Theming is not used to provide a logo'); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + $simpleFile = $imageManager->getImage('logo', false); |
| 48 | + |
| 49 | + $image = @imagecreatefromstring($simpleFile->getContent()); |
| 50 | + |
| 51 | + $dimensions = ''; |
| 52 | + if ($image !== false) { |
| 53 | + $dimensions = imagesx($image) . 'x' . imagesy($image); |
| 54 | + } elseif (str_starts_with($simpleFile->getMimeType(), 'image/svg')) { |
| 55 | + $matched = preg_match('/viewbox=["\']\d* \d* (\d*\.?\d*) (\d*\.?\d*)["\']/i', $simpleFile->getContent(), $matches); |
| 56 | + if ($matched) { |
| 57 | + $dimensions = $matches[1] . 'x' . $matches[2]; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (!$dimensions) { |
| 62 | + $output->warning('Failed to read dimensions from logo'); |
| 63 | + $this->config->deleteAppValue('theming', 'logoDimensions'); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + $dimensions = imagesx($image) . 'x' . imagesy($image); |
| 68 | + $this->config->setAppValue('theming', 'logoDimensions', $dimensions); |
| 69 | + $output->info('Updated logo dimensions: ' . $dimensions); |
| 70 | + } |
| 71 | +} |
0 commit comments