Imaginary.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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|heif|svg\+xml|tiff|webp)|application\/(pdf|illustrator))/';
  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. if (!$stream || !is_resource($stream) || feof($stream)) {
  68. return null;
  69. }
  70. $httpClient = $this->service->newClient();
  71. $convert = false;
  72. $autorotate = true;
  73. switch ($file->getMimeType()) {
  74. case 'image/heic':
  75. // Autorotate seems to be broken for Heic so disable for that
  76. $autorotate = false;
  77. $mimeType = 'jpeg';
  78. break;
  79. case 'image/gif':
  80. case 'image/png':
  81. $mimeType = 'png';
  82. break;
  83. case 'image/svg+xml':
  84. case 'application/pdf':
  85. case 'application/illustrator':
  86. $convert = true;
  87. // Converted files do not need to be autorotated
  88. $autorotate = false;
  89. $mimeType = 'png';
  90. break;
  91. default:
  92. $mimeType = 'jpeg';
  93. }
  94. $operations = [];
  95. if ($convert) {
  96. $operations[] = [
  97. 'operation' => 'convert',
  98. 'params' => [
  99. 'type' => $mimeType,
  100. ]
  101. ];
  102. } elseif ($autorotate) {
  103. $operations[] = [
  104. 'operation' => 'autorotate',
  105. ];
  106. }
  107. $quality = $this->config->getAppValue('preview', 'jpeg_quality', '80');
  108. $operations[] = [
  109. 'operation' => ($crop ? 'smartcrop' : 'fit'),
  110. 'params' => [
  111. 'width' => $maxX,
  112. 'height' => $maxY,
  113. 'stripmeta' => 'true',
  114. 'type' => $mimeType,
  115. 'norotation' => 'true',
  116. 'quality' => $quality,
  117. ]
  118. ];
  119. try {
  120. $response = $httpClient->post(
  121. $imaginaryUrl . '/pipeline', [
  122. 'query' => ['operations' => json_encode($operations)],
  123. 'stream' => true,
  124. 'content-type' => $file->getMimeType(),
  125. 'body' => $stream,
  126. 'nextcloud' => ['allow_local_address' => true],
  127. 'timeout' => 120,
  128. 'connect_timeout' => 3,
  129. ]);
  130. } catch (\Throwable $e) {
  131. $this->logger->error('Imaginary preview generation failed: ' . $e->getMessage(), [
  132. 'exception' => $e,
  133. ]);
  134. return null;
  135. }
  136. if ($response->getStatusCode() !== 200) {
  137. $this->logger->error('Imaginary preview generation failed: ' . json_decode($response->getBody())['message']);
  138. return null;
  139. }
  140. // This is not optimal but previews are distorted if the wrong width and height values are
  141. // used. Both dimension headers are only sent when passing the option "-return-size" to
  142. // Imaginary.
  143. if ($response->getHeader('Image-Width') && $response->getHeader('Image-Height')) {
  144. $image = new StreamImage(
  145. $response->getBody(),
  146. $response->getHeader('Content-Type'),
  147. (int)$response->getHeader('Image-Width'),
  148. (int)$response->getHeader('Image-Height'),
  149. );
  150. } else {
  151. $image = new Image();
  152. $image->loadFromFileHandle($response->getBody());
  153. }
  154. return $image->valid() ? $image : null;
  155. }
  156. /**
  157. * {@inheritDoc}
  158. */
  159. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  160. return $this->getCroppedThumbnail($file, $maxX, $maxY, false);
  161. }
  162. }