Imaginary.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 OC\StreamImage;
  29. use Psr\Log\LoggerInterface;
  30. class Imaginary extends ProviderV2 {
  31. /** @var IConfig */
  32. private $config;
  33. /** @var IClientService */
  34. private $service;
  35. /** @var LoggerInterface */
  36. private $logger;
  37. public function __construct(array $config) {
  38. parent::__construct($config);
  39. $this->config = \OC::$server->get(IConfig::class);
  40. $this->service = \OC::$server->get(IClientService::class);
  41. $this->logger = \OC::$server->get(LoggerInterface::class);
  42. }
  43. /**
  44. * {@inheritDoc}
  45. */
  46. public function getMimeType(): string {
  47. return self::supportedMimeTypes();
  48. }
  49. public static function supportedMimeTypes(): string {
  50. return '/image\/(bmp|x-bitmap|png|jpeg|gif|heic|svg|webp)/';
  51. }
  52. public function getCroppedThumbnail(File $file, int $maxX, int $maxY, bool $crop): ?IImage {
  53. $maxSizeForImages = $this->config->getSystemValue('preview_max_filesize_image', 50);
  54. $size = $file->getSize();
  55. if ($maxSizeForImages !== -1 && $size > ($maxSizeForImages * 1024 * 1024)) {
  56. return null;
  57. }
  58. $imaginaryUrl = $this->config->getSystemValueString('preview_imaginary_url', 'invalid');
  59. if ($imaginaryUrl === 'invalid') {
  60. $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.');
  61. return null;
  62. }
  63. $imaginaryUrl = rtrim($imaginaryUrl, '/');
  64. // Object store
  65. $stream = $file->fopen('r');
  66. $httpClient = $this->service->newClient();
  67. switch ($file->getMimeType()) {
  68. case 'image/gif':
  69. case 'image/png':
  70. $mimeType = 'png';
  71. break;
  72. default:
  73. $mimeType = 'jpeg';
  74. }
  75. $operations = [
  76. [
  77. 'operation' => 'autorotate',
  78. ],
  79. [
  80. 'operation' => ($crop ? 'smartcrop' : 'fit'),
  81. 'params' => [
  82. 'width' => $maxX,
  83. 'height' => $maxY,
  84. 'stripmeta' => 'true',
  85. 'type' => $mimeType,
  86. 'norotation' => 'true',
  87. ]
  88. ]
  89. ];
  90. try {
  91. $response = $httpClient->post(
  92. $imaginaryUrl . '/pipeline', [
  93. 'query' => ['operations' => json_encode($operations)],
  94. 'stream' => true,
  95. 'content-type' => $file->getMimeType(),
  96. 'body' => $stream,
  97. 'nextcloud' => ['allow_local_address' => true],
  98. ]);
  99. } catch (\Exception $e) {
  100. $this->logger->error('Imaginary preview generation failed: ' . $e->getMessage(), [
  101. 'exception' => $e,
  102. ]);
  103. return null;
  104. }
  105. if ($response->getStatusCode() !== 200) {
  106. $this->logger->error('Imaginary preview generation failed: ' . json_decode($response->getBody())['message']);
  107. return null;
  108. }
  109. if ($response->getHeader('X-Image-Width') && $response->getHeader('X-Image-Height')) {
  110. $maxX = (int)$response->getHeader('X-Image-Width');
  111. $maxY = (int)$response->getHeader('X-Image-Height');
  112. }
  113. $image = new StreamImage($response->getBody(), $response->getHeader('Content-Type'), $maxX, $maxY);
  114. return $image->valid() ? $image : null;
  115. }
  116. /**
  117. * {@inheritDoc}
  118. */
  119. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  120. return $this->getCroppedThumbnail($file, $maxX, $maxY, false);
  121. }
  122. }