RepairLogoDimension.php 2.2 KB

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