Imaginary.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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->getSystemValueInt('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. $preview_format = $this->config->getSystemValueString('preview_format', 'jpeg');
  95. switch ($preview_format) { // Change the format to the correct one
  96. case 'webp':
  97. $mimeType = 'webp';
  98. break;
  99. default:
  100. }
  101. $operations = [];
  102. if ($convert) {
  103. $operations[] = [
  104. 'operation' => 'convert',
  105. 'params' => [
  106. 'type' => $mimeType,
  107. ]
  108. ];
  109. } elseif ($autorotate) {
  110. $operations[] = [
  111. 'operation' => 'autorotate',
  112. ];
  113. }
  114. switch ($mimeType) {
  115. case 'jpeg':
  116. $quality = $this->config->getAppValue('preview', 'jpeg_quality', '80');
  117. break;
  118. case 'webp':
  119. $quality = $this->config->getAppValue('preview', 'webp_quality', '80');
  120. break;
  121. default:
  122. $quality = $this->config->getAppValue('preview', 'jpeg_quality', '80');
  123. }
  124. $operations[] = [
  125. 'operation' => ($crop ? 'smartcrop' : 'fit'),
  126. 'params' => [
  127. 'width' => $maxX,
  128. 'height' => $maxY,
  129. 'stripmeta' => 'true',
  130. 'type' => $mimeType,
  131. 'norotation' => 'true',
  132. 'quality' => $quality,
  133. ]
  134. ];
  135. try {
  136. $imaginaryKey = $this->config->getSystemValueString('preview_imaginary_key', '');
  137. $response = $httpClient->post(
  138. $imaginaryUrl . '/pipeline', [
  139. 'query' => ['operations' => json_encode($operations), 'key' => $imaginaryKey],
  140. 'stream' => true,
  141. 'content-type' => $file->getMimeType(),
  142. 'body' => $stream,
  143. 'nextcloud' => ['allow_local_address' => true],
  144. 'timeout' => 120,
  145. 'connect_timeout' => 3,
  146. ]);
  147. } catch (\Throwable $e) {
  148. $this->logger->info('Imaginary preview generation failed: ' . $e->getMessage(), [
  149. 'exception' => $e,
  150. ]);
  151. return null;
  152. }
  153. if ($response->getStatusCode() !== 200) {
  154. $this->logger->info('Imaginary preview generation failed: ' . json_decode($response->getBody())['message']);
  155. return null;
  156. }
  157. // This is not optimal but previews are distorted if the wrong width and height values are
  158. // used. Both dimension headers are only sent when passing the option "-return-size" to
  159. // Imaginary.
  160. if ($response->getHeader('Image-Width') && $response->getHeader('Image-Height')) {
  161. $image = new StreamImage(
  162. $response->getBody(),
  163. $response->getHeader('Content-Type'),
  164. (int)$response->getHeader('Image-Width'),
  165. (int)$response->getHeader('Image-Height'),
  166. );
  167. } else {
  168. $image = new Image();
  169. $image->loadFromFileHandle($response->getBody());
  170. }
  171. return $image->valid() ? $image : null;
  172. }
  173. /**
  174. * {@inheritDoc}
  175. */
  176. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  177. return $this->getCroppedThumbnail($file, $maxX, $maxY, false);
  178. }
  179. }