Imaginary.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2020, Nextcloud, GmbH.
  4. *
  5. * @author Vincent Petry <vincent@nextcloud.com>
  6. * @author Carl Schwan <carl@carlschwan.eu>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  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, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Preview;
  24. use OCP\Files\File;
  25. use OCP\Http\Client\IClientService;
  26. use OCP\IConfig;
  27. use OCP\IImage;
  28. use OCP\Image;
  29. use OC\StreamImage;
  30. use Psr\Log\LoggerInterface;
  31. class Imaginary extends ProviderV2 {
  32. /** @var IConfig */
  33. private $config;
  34. /** @var IClientService */
  35. private $service;
  36. /** @var LoggerInterface */
  37. private $logger;
  38. public function __construct(array $config) {
  39. parent::__construct($config);
  40. $this->config = \OC::$server->get(IConfig::class);
  41. $this->service = \OC::$server->get(IClientService::class);
  42. $this->logger = \OC::$server->get(LoggerInterface::class);
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. public function getMimeType(): string {
  48. return self::supportedMimeTypes();
  49. }
  50. public static function supportedMimeTypes(): string {
  51. return '/image\/(bmp|x-bitmap|png|jpeg|gif|heic|svg|webp)/';
  52. }
  53. public function getCroppedThumbnail(File $file, int $maxX, int $maxY, bool $crop): ?IImage {
  54. $maxSizeForImages = $this->config->getSystemValue('preview_max_filesize_image', 50);
  55. $size = $file->getSize();
  56. if ($maxSizeForImages !== -1 && $size > ($maxSizeForImages * 1024 * 1024)) {
  57. return null;
  58. }
  59. $imaginaryUrl = $this->config->getSystemValueString('preview_imaginary_url', 'invalid');
  60. if ($imaginaryUrl === 'invalid') {
  61. $this->logger->error('Imaginary preview provider is enabled, but no url is configured. Please provide the url of your imaginary server to the \'preview_imaginary_url\' config variable.');
  62. return null;
  63. }
  64. $imaginaryUrl = rtrim($imaginaryUrl, '/');
  65. // Object store
  66. $stream = $file->fopen('r');
  67. $httpClient = $this->service->newClient();
  68. switch ($file->getMimeType()) {
  69. case 'image/gif':
  70. case 'image/png':
  71. $mimeType = 'png';
  72. break;
  73. default:
  74. $mimeType = 'jpeg';
  75. }
  76. $quality = $this->config->getAppValue('preview', 'jpeg_quality', '80');
  77. $operations = [
  78. [
  79. 'operation' => 'autorotate',
  80. ],
  81. [
  82. 'operation' => ($crop ? 'smartcrop' : 'fit'),
  83. 'params' => [
  84. 'width' => $maxX,
  85. 'height' => $maxY,
  86. 'stripmeta' => 'true',
  87. 'type' => $mimeType,
  88. 'norotation' => 'true',
  89. 'quality' => $quality,
  90. ]
  91. ]
  92. ];
  93. try {
  94. $response = $httpClient->post(
  95. $imaginaryUrl . '/pipeline', [
  96. 'query' => ['operations' => json_encode($operations)],
  97. 'stream' => true,
  98. 'content-type' => $file->getMimeType(),
  99. 'body' => $stream,
  100. 'nextcloud' => ['allow_local_address' => true],
  101. ]);
  102. } catch (\Exception $e) {
  103. $this->logger->error('Imaginary preview generation failed: ' . $e->getMessage(), [
  104. 'exception' => $e,
  105. ]);
  106. return null;
  107. }
  108. if ($response->getStatusCode() !== 200) {
  109. $this->logger->error('Imaginary preview generation failed: ' . json_decode($response->getBody())['message']);
  110. return null;
  111. }
  112. // This is not optimal but previews are distorted if the wrong width and height values are
  113. // used. Both dimension headers are only sent when passing the option "-return-size" to
  114. // Imaginary.
  115. if ($response->getHeader('Image-Width') && $response->getHeader('Image-Height')) {
  116. $image = new StreamImage(
  117. $response->getBody(),
  118. $response->getHeader('Content-Type'),
  119. (int)$response->getHeader('Image-Width'),
  120. (int)$response->getHeader('Image-Height'),
  121. );
  122. } else {
  123. $image = new Image();
  124. $image->loadFromFileHandle($response->getBody());
  125. }
  126. return $image->valid() ? $image : null;
  127. }
  128. /**
  129. * {@inheritDoc}
  130. */
  131. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  132. return $this->getCroppedThumbnail($file, $maxX, $maxY, false);
  133. }
  134. }