Imaginary.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Preview;
  7. use OC\StreamImage;
  8. use OCP\Files\File;
  9. use OCP\Http\Client\IClientService;
  10. use OCP\IConfig;
  11. use OCP\IImage;
  12. use OCP\Image;
  13. use Psr\Log\LoggerInterface;
  14. class Imaginary extends ProviderV2 {
  15. /** @var IConfig */
  16. private $config;
  17. /** @var IClientService */
  18. private $service;
  19. /** @var LoggerInterface */
  20. private $logger;
  21. public function __construct(array $config) {
  22. parent::__construct($config);
  23. $this->config = \OC::$server->get(IConfig::class);
  24. $this->service = \OC::$server->get(IClientService::class);
  25. $this->logger = \OC::$server->get(LoggerInterface::class);
  26. }
  27. /**
  28. * {@inheritDoc}
  29. */
  30. public function getMimeType(): string {
  31. return self::supportedMimeTypes();
  32. }
  33. public static function supportedMimeTypes(): string {
  34. return '/(image\/(bmp|x-bitmap|png|jpeg|gif|heic|heif|svg\+xml|tiff|webp)|application\/illustrator)/';
  35. }
  36. public function getCroppedThumbnail(File $file, int $maxX, int $maxY, bool $crop): ?IImage {
  37. $maxSizeForImages = $this->config->getSystemValueInt('preview_max_filesize_image', 50);
  38. $size = $file->getSize();
  39. if ($maxSizeForImages !== -1 && $size > ($maxSizeForImages * 1024 * 1024)) {
  40. return null;
  41. }
  42. $imaginaryUrl = $this->config->getSystemValueString('preview_imaginary_url', 'invalid');
  43. if ($imaginaryUrl === 'invalid') {
  44. $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.');
  45. return null;
  46. }
  47. $imaginaryUrl = rtrim($imaginaryUrl, '/');
  48. // Object store
  49. $stream = $file->fopen('r');
  50. if (!$stream || !is_resource($stream) || feof($stream)) {
  51. return null;
  52. }
  53. $httpClient = $this->service->newClient();
  54. $convert = false;
  55. $autorotate = true;
  56. switch ($file->getMimeType()) {
  57. case 'image/heic':
  58. // Autorotate seems to be broken for Heic so disable for that
  59. $autorotate = false;
  60. $mimeType = 'jpeg';
  61. break;
  62. case 'image/gif':
  63. case 'image/png':
  64. $mimeType = 'png';
  65. break;
  66. case 'image/svg+xml':
  67. case 'application/pdf':
  68. case 'application/illustrator':
  69. $convert = true;
  70. // Converted files do not need to be autorotated
  71. $autorotate = false;
  72. $mimeType = 'png';
  73. break;
  74. default:
  75. $mimeType = 'jpeg';
  76. }
  77. $preview_format = $this->config->getSystemValueString('preview_format', 'jpeg');
  78. switch ($preview_format) { // Change the format to the correct one
  79. case 'webp':
  80. $mimeType = 'webp';
  81. break;
  82. default:
  83. }
  84. $operations = [];
  85. if ($convert) {
  86. $operations[] = [
  87. 'operation' => 'convert',
  88. 'params' => [
  89. 'type' => $mimeType,
  90. ]
  91. ];
  92. } elseif ($autorotate) {
  93. $operations[] = [
  94. 'operation' => 'autorotate',
  95. ];
  96. }
  97. switch ($mimeType) {
  98. case 'jpeg':
  99. $quality = $this->config->getAppValue('preview', 'jpeg_quality', '80');
  100. break;
  101. case 'webp':
  102. $quality = $this->config->getAppValue('preview', 'webp_quality', '80');
  103. break;
  104. default:
  105. $quality = $this->config->getAppValue('preview', 'jpeg_quality', '80');
  106. }
  107. $operations[] = [
  108. 'operation' => ($crop ? 'smartcrop' : 'fit'),
  109. 'params' => [
  110. 'width' => $maxX,
  111. 'height' => $maxY,
  112. 'stripmeta' => 'true',
  113. 'type' => $mimeType,
  114. 'norotation' => 'true',
  115. 'quality' => $quality,
  116. ]
  117. ];
  118. try {
  119. $imaginaryKey = $this->config->getSystemValueString('preview_imaginary_key', '');
  120. $response = $httpClient->post(
  121. $imaginaryUrl . '/pipeline', [
  122. 'query' => ['operations' => json_encode($operations), 'key' => $imaginaryKey],
  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->info('Imaginary preview generation failed: ' . $e->getMessage(), [
  132. 'exception' => $e,
  133. ]);
  134. return null;
  135. }
  136. if ($response->getStatusCode() !== 200) {
  137. $this->logger->info('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. }