@@ -47,542 +47,542 @@ |
||
| 47 | 47 | |
| 48 | 48 | class Generator { |
| 49 | 49 | |
| 50 | - /** @var IPreview */ |
|
| 51 | - private $previewManager; |
|
| 52 | - /** @var IConfig */ |
|
| 53 | - private $config; |
|
| 54 | - /** @var IAppData */ |
|
| 55 | - private $appData; |
|
| 56 | - /** @var GeneratorHelper */ |
|
| 57 | - private $helper; |
|
| 58 | - /** @var EventDispatcherInterface */ |
|
| 59 | - private $eventDispatcher; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @param IConfig $config |
|
| 63 | - * @param IPreview $previewManager |
|
| 64 | - * @param IAppData $appData |
|
| 65 | - * @param GeneratorHelper $helper |
|
| 66 | - * @param EventDispatcherInterface $eventDispatcher |
|
| 67 | - */ |
|
| 68 | - public function __construct( |
|
| 69 | - IConfig $config, |
|
| 70 | - IPreview $previewManager, |
|
| 71 | - IAppData $appData, |
|
| 72 | - GeneratorHelper $helper, |
|
| 73 | - EventDispatcherInterface $eventDispatcher |
|
| 74 | - ) { |
|
| 75 | - $this->config = $config; |
|
| 76 | - $this->previewManager = $previewManager; |
|
| 77 | - $this->appData = $appData; |
|
| 78 | - $this->helper = $helper; |
|
| 79 | - $this->eventDispatcher = $eventDispatcher; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Returns a preview of a file |
|
| 84 | - * |
|
| 85 | - * The cache is searched first and if nothing usable was found then a preview is |
|
| 86 | - * generated by one of the providers |
|
| 87 | - * |
|
| 88 | - * @param File $file |
|
| 89 | - * @param int $width |
|
| 90 | - * @param int $height |
|
| 91 | - * @param bool $crop |
|
| 92 | - * @param string $mode |
|
| 93 | - * @param string $mimeType |
|
| 94 | - * @return ISimpleFile |
|
| 95 | - * @throws NotFoundException |
|
| 96 | - * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
| 97 | - */ |
|
| 98 | - public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
|
| 99 | - $specification = [ |
|
| 100 | - 'width' => $width, |
|
| 101 | - 'height' => $height, |
|
| 102 | - 'crop' => $crop, |
|
| 103 | - 'mode' => $mode, |
|
| 104 | - ]; |
|
| 105 | - $this->eventDispatcher->dispatch( |
|
| 106 | - IPreview::EVENT, |
|
| 107 | - new GenericEvent($file, $specification) |
|
| 108 | - ); |
|
| 109 | - |
|
| 110 | - // since we only ask for one preview, and the generate method return the last one it created, it returns the one we want |
|
| 111 | - return $this->generatePreviews($file, [$specification], $mimeType); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Generates previews of a file |
|
| 116 | - * |
|
| 117 | - * @param File $file |
|
| 118 | - * @param array $specifications |
|
| 119 | - * @param string $mimeType |
|
| 120 | - * @return ISimpleFile the last preview that was generated |
|
| 121 | - * @throws NotFoundException |
|
| 122 | - * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
| 123 | - */ |
|
| 124 | - public function generatePreviews(File $file, array $specifications, $mimeType = null) { |
|
| 125 | - //Make sure that we can read the file |
|
| 126 | - if (!$file->isReadable()) { |
|
| 127 | - throw new NotFoundException('Cannot read file'); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - if ($mimeType === null) { |
|
| 131 | - $mimeType = $file->getMimeType(); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - $previewFolder = $this->getPreviewFolder($file); |
|
| 135 | - |
|
| 136 | - $previewVersion = ''; |
|
| 137 | - if ($file instanceof IVersionedPreviewFile) { |
|
| 138 | - $previewVersion = $file->getPreviewVersion() . '-'; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - // If imaginary is enabled, and we request a small thumbnail, |
|
| 142 | - // let's not generate the max preview for performance reasons |
|
| 143 | - if (count($specifications) === 1 |
|
| 144 | - && ($specifications[0]['width'] <= 256 || $specifications[0]['height'] <= 256) |
|
| 145 | - && preg_match(Imaginary::supportedMimeTypes(), $mimeType) |
|
| 146 | - && $this->config->getSystemValueString('preview_imaginary_url', 'invalid') !== 'invalid') { |
|
| 147 | - $crop = $specifications[0]['crop'] ?? false; |
|
| 148 | - $preview = $this->getSmallImagePreview($previewFolder, $file, $mimeType, $previewVersion, $crop); |
|
| 149 | - |
|
| 150 | - if ($preview->getSize() === 0) { |
|
| 151 | - $preview->delete(); |
|
| 152 | - throw new NotFoundException('Cached preview size 0, invalid!'); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - return $preview; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - // Get the max preview and infer the max preview sizes from that |
|
| 159 | - $maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType, $previewVersion); |
|
| 160 | - $maxPreviewImage = null; // only load the image when we need it |
|
| 161 | - if ($maxPreview->getSize() === 0) { |
|
| 162 | - $maxPreview->delete(); |
|
| 163 | - throw new NotFoundException('Max preview size 0, invalid!'); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - [$maxWidth, $maxHeight] = $this->getPreviewSize($maxPreview, $previewVersion); |
|
| 167 | - |
|
| 168 | - $preview = null; |
|
| 169 | - |
|
| 170 | - foreach ($specifications as $specification) { |
|
| 171 | - $width = $specification['width'] ?? -1; |
|
| 172 | - $height = $specification['height'] ?? -1; |
|
| 173 | - $crop = $specification['crop'] ?? false; |
|
| 174 | - $mode = $specification['mode'] ?? IPreview::MODE_FILL; |
|
| 175 | - |
|
| 176 | - // If both width and height are -1 we just want the max preview |
|
| 177 | - if ($width === -1 && $height === -1) { |
|
| 178 | - $width = $maxWidth; |
|
| 179 | - $height = $maxHeight; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - // Calculate the preview size |
|
| 183 | - [$width, $height] = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight); |
|
| 184 | - |
|
| 185 | - // No need to generate a preview that is just the max preview |
|
| 186 | - if ($width === $maxWidth && $height === $maxHeight) { |
|
| 187 | - // ensure correct return value if this was the last one |
|
| 188 | - $preview = $maxPreview; |
|
| 189 | - continue; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - // Try to get a cached preview. Else generate (and store) one |
|
| 193 | - try { |
|
| 194 | - try { |
|
| 195 | - $preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion); |
|
| 196 | - } catch (NotFoundException $e) { |
|
| 197 | - if (!$this->previewManager->isMimeSupported($mimeType)) { |
|
| 198 | - throw new NotFoundException(); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - if ($maxPreviewImage === null) { |
|
| 202 | - $maxPreviewImage = $this->helper->getImage($maxPreview); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - $preview = $this->generatePreview($previewFolder, $maxPreviewImage, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion); |
|
| 206 | - } |
|
| 207 | - } catch (\InvalidArgumentException $e) { |
|
| 208 | - throw new NotFoundException("", 0, $e); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - if ($preview->getSize() === 0) { |
|
| 212 | - $preview->delete(); |
|
| 213 | - throw new NotFoundException('Cached preview size 0, invalid!'); |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - // Free memory being used by the embedded image resource. Without this the image is kept in memory indefinitely. |
|
| 218 | - // Garbage Collection does NOT free this memory. We have to do it ourselves. |
|
| 219 | - if ($maxPreviewImage instanceof \OC_Image) { |
|
| 220 | - $maxPreviewImage->destroy(); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - return $preview; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Generate a small image straight away without generating a max preview first |
|
| 228 | - * Preview generated is 256x256 |
|
| 229 | - */ |
|
| 230 | - private function getSmallImagePreview(ISimpleFolder $previewFolder, File $file, string $mimeType, string $prefix, bool $crop) { |
|
| 231 | - $nodes = $previewFolder->getDirectoryListing(); |
|
| 232 | - |
|
| 233 | - foreach ($nodes as $node) { |
|
| 234 | - $name = $node->getName(); |
|
| 235 | - if (($prefix === '' || strpos($name, $prefix) === 0) |
|
| 236 | - && (str_starts_with($name, '256-256-crop') && $crop || str_starts_with($name, '256-256') && !$crop)) { |
|
| 237 | - return $node; |
|
| 238 | - } |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $previewProviders = $this->previewManager->getProviders(); |
|
| 242 | - foreach ($previewProviders as $supportedMimeType => $providers) { |
|
| 243 | - // Filter out providers that does not support this mime |
|
| 244 | - if (!preg_match($supportedMimeType, $mimeType)) { |
|
| 245 | - continue; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - foreach ($providers as $providerClosure) { |
|
| 249 | - $provider = $this->helper->getProvider($providerClosure); |
|
| 250 | - if (!($provider instanceof IProviderV2)) { |
|
| 251 | - continue; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - if (!$provider->isAvailable($file)) { |
|
| 255 | - continue; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - $preview = $this->helper->getThumbnail($provider, $file, 256, 256, true); |
|
| 259 | - |
|
| 260 | - if (!($preview instanceof IImage)) { |
|
| 261 | - continue; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - // Try to get the extension. |
|
| 265 | - try { |
|
| 266 | - $ext = $this->getExtention($preview->dataMimeType()); |
|
| 267 | - } catch (\InvalidArgumentException $e) { |
|
| 268 | - // Just continue to the next iteration if this preview doesn't have a valid mimetype |
|
| 269 | - continue; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - $path = $this->generatePath(256, 256, $crop, $preview->dataMimeType(), $prefix); |
|
| 273 | - try { |
|
| 274 | - $file = $previewFolder->newFile($path); |
|
| 275 | - if ($preview instanceof IStreamImage) { |
|
| 276 | - $file->putContent($preview->resource()); |
|
| 277 | - } else { |
|
| 278 | - $file->putContent($preview->data()); |
|
| 279 | - } |
|
| 280 | - } catch (NotPermittedException $e) { |
|
| 281 | - throw new NotFoundException(); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - return $file; |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * @param ISimpleFolder $previewFolder |
|
| 291 | - * @param File $file |
|
| 292 | - * @param string $mimeType |
|
| 293 | - * @param string $prefix |
|
| 294 | - * @return ISimpleFile |
|
| 295 | - * @throws NotFoundException |
|
| 296 | - */ |
|
| 297 | - private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType, $prefix) { |
|
| 298 | - $nodes = $previewFolder->getDirectoryListing(); |
|
| 299 | - |
|
| 300 | - foreach ($nodes as $node) { |
|
| 301 | - $name = $node->getName(); |
|
| 302 | - if (($prefix === '' || strpos($name, $prefix) === 0) && strpos($name, 'max')) { |
|
| 303 | - return $node; |
|
| 304 | - } |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - $previewProviders = $this->previewManager->getProviders(); |
|
| 308 | - foreach ($previewProviders as $supportedMimeType => $providers) { |
|
| 309 | - // Filter out providers that does not support this mime |
|
| 310 | - if (!preg_match($supportedMimeType, $mimeType)) { |
|
| 311 | - continue; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - foreach ($providers as $providerClosure) { |
|
| 315 | - $provider = $this->helper->getProvider($providerClosure); |
|
| 316 | - if (!($provider instanceof IProviderV2)) { |
|
| 317 | - continue; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - if (!$provider->isAvailable($file)) { |
|
| 321 | - continue; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - $maxWidth = $this->config->getSystemValueInt('preview_max_x', 4096); |
|
| 325 | - $maxHeight = $this->config->getSystemValueInt('preview_max_y', 4096); |
|
| 326 | - |
|
| 327 | - $preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight); |
|
| 328 | - |
|
| 329 | - if (!($preview instanceof IImage)) { |
|
| 330 | - continue; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - // Try to get the extention. |
|
| 334 | - try { |
|
| 335 | - $ext = $this->getExtention($preview->dataMimeType()); |
|
| 336 | - } catch (\InvalidArgumentException $e) { |
|
| 337 | - // Just continue to the next iteration if this preview doesn't have a valid mimetype |
|
| 338 | - continue; |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - $path = $prefix . (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext; |
|
| 342 | - try { |
|
| 343 | - $file = $previewFolder->newFile($path); |
|
| 344 | - if ($preview instanceof IStreamImage) { |
|
| 345 | - $file->putContent($preview->resource()); |
|
| 346 | - } else { |
|
| 347 | - $file->putContent($preview->data()); |
|
| 348 | - } |
|
| 349 | - } catch (NotPermittedException $e) { |
|
| 350 | - throw new NotFoundException(); |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - return $file; |
|
| 354 | - } |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - throw new NotFoundException(); |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * @param ISimpleFile $file |
|
| 362 | - * @param string $prefix |
|
| 363 | - * @return int[] |
|
| 364 | - */ |
|
| 365 | - private function getPreviewSize(ISimpleFile $file, string $prefix = '') { |
|
| 366 | - $size = explode('-', substr($file->getName(), strlen($prefix))); |
|
| 367 | - return [(int)$size[0], (int)$size[1]]; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * @param int $width |
|
| 372 | - * @param int $height |
|
| 373 | - * @param bool $crop |
|
| 374 | - * @param string $mimeType |
|
| 375 | - * @param string $prefix |
|
| 376 | - * @return string |
|
| 377 | - */ |
|
| 378 | - private function generatePath($width, $height, $crop, $mimeType, $prefix) { |
|
| 379 | - $path = $prefix . (string)$width . '-' . (string)$height; |
|
| 380 | - if ($crop) { |
|
| 381 | - $path .= '-crop'; |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - $ext = $this->getExtention($mimeType); |
|
| 385 | - $path .= '.' . $ext; |
|
| 386 | - return $path; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * @param int $width |
|
| 392 | - * @param int $height |
|
| 393 | - * @param bool $crop |
|
| 394 | - * @param string $mode |
|
| 395 | - * @param int $maxWidth |
|
| 396 | - * @param int $maxHeight |
|
| 397 | - * @return int[] |
|
| 398 | - */ |
|
| 399 | - private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) { |
|
| 400 | - |
|
| 401 | - /* |
|
| 50 | + /** @var IPreview */ |
|
| 51 | + private $previewManager; |
|
| 52 | + /** @var IConfig */ |
|
| 53 | + private $config; |
|
| 54 | + /** @var IAppData */ |
|
| 55 | + private $appData; |
|
| 56 | + /** @var GeneratorHelper */ |
|
| 57 | + private $helper; |
|
| 58 | + /** @var EventDispatcherInterface */ |
|
| 59 | + private $eventDispatcher; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @param IConfig $config |
|
| 63 | + * @param IPreview $previewManager |
|
| 64 | + * @param IAppData $appData |
|
| 65 | + * @param GeneratorHelper $helper |
|
| 66 | + * @param EventDispatcherInterface $eventDispatcher |
|
| 67 | + */ |
|
| 68 | + public function __construct( |
|
| 69 | + IConfig $config, |
|
| 70 | + IPreview $previewManager, |
|
| 71 | + IAppData $appData, |
|
| 72 | + GeneratorHelper $helper, |
|
| 73 | + EventDispatcherInterface $eventDispatcher |
|
| 74 | + ) { |
|
| 75 | + $this->config = $config; |
|
| 76 | + $this->previewManager = $previewManager; |
|
| 77 | + $this->appData = $appData; |
|
| 78 | + $this->helper = $helper; |
|
| 79 | + $this->eventDispatcher = $eventDispatcher; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Returns a preview of a file |
|
| 84 | + * |
|
| 85 | + * The cache is searched first and if nothing usable was found then a preview is |
|
| 86 | + * generated by one of the providers |
|
| 87 | + * |
|
| 88 | + * @param File $file |
|
| 89 | + * @param int $width |
|
| 90 | + * @param int $height |
|
| 91 | + * @param bool $crop |
|
| 92 | + * @param string $mode |
|
| 93 | + * @param string $mimeType |
|
| 94 | + * @return ISimpleFile |
|
| 95 | + * @throws NotFoundException |
|
| 96 | + * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
| 97 | + */ |
|
| 98 | + public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { |
|
| 99 | + $specification = [ |
|
| 100 | + 'width' => $width, |
|
| 101 | + 'height' => $height, |
|
| 102 | + 'crop' => $crop, |
|
| 103 | + 'mode' => $mode, |
|
| 104 | + ]; |
|
| 105 | + $this->eventDispatcher->dispatch( |
|
| 106 | + IPreview::EVENT, |
|
| 107 | + new GenericEvent($file, $specification) |
|
| 108 | + ); |
|
| 109 | + |
|
| 110 | + // since we only ask for one preview, and the generate method return the last one it created, it returns the one we want |
|
| 111 | + return $this->generatePreviews($file, [$specification], $mimeType); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Generates previews of a file |
|
| 116 | + * |
|
| 117 | + * @param File $file |
|
| 118 | + * @param array $specifications |
|
| 119 | + * @param string $mimeType |
|
| 120 | + * @return ISimpleFile the last preview that was generated |
|
| 121 | + * @throws NotFoundException |
|
| 122 | + * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
| 123 | + */ |
|
| 124 | + public function generatePreviews(File $file, array $specifications, $mimeType = null) { |
|
| 125 | + //Make sure that we can read the file |
|
| 126 | + if (!$file->isReadable()) { |
|
| 127 | + throw new NotFoundException('Cannot read file'); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + if ($mimeType === null) { |
|
| 131 | + $mimeType = $file->getMimeType(); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + $previewFolder = $this->getPreviewFolder($file); |
|
| 135 | + |
|
| 136 | + $previewVersion = ''; |
|
| 137 | + if ($file instanceof IVersionedPreviewFile) { |
|
| 138 | + $previewVersion = $file->getPreviewVersion() . '-'; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + // If imaginary is enabled, and we request a small thumbnail, |
|
| 142 | + // let's not generate the max preview for performance reasons |
|
| 143 | + if (count($specifications) === 1 |
|
| 144 | + && ($specifications[0]['width'] <= 256 || $specifications[0]['height'] <= 256) |
|
| 145 | + && preg_match(Imaginary::supportedMimeTypes(), $mimeType) |
|
| 146 | + && $this->config->getSystemValueString('preview_imaginary_url', 'invalid') !== 'invalid') { |
|
| 147 | + $crop = $specifications[0]['crop'] ?? false; |
|
| 148 | + $preview = $this->getSmallImagePreview($previewFolder, $file, $mimeType, $previewVersion, $crop); |
|
| 149 | + |
|
| 150 | + if ($preview->getSize() === 0) { |
|
| 151 | + $preview->delete(); |
|
| 152 | + throw new NotFoundException('Cached preview size 0, invalid!'); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + return $preview; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + // Get the max preview and infer the max preview sizes from that |
|
| 159 | + $maxPreview = $this->getMaxPreview($previewFolder, $file, $mimeType, $previewVersion); |
|
| 160 | + $maxPreviewImage = null; // only load the image when we need it |
|
| 161 | + if ($maxPreview->getSize() === 0) { |
|
| 162 | + $maxPreview->delete(); |
|
| 163 | + throw new NotFoundException('Max preview size 0, invalid!'); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + [$maxWidth, $maxHeight] = $this->getPreviewSize($maxPreview, $previewVersion); |
|
| 167 | + |
|
| 168 | + $preview = null; |
|
| 169 | + |
|
| 170 | + foreach ($specifications as $specification) { |
|
| 171 | + $width = $specification['width'] ?? -1; |
|
| 172 | + $height = $specification['height'] ?? -1; |
|
| 173 | + $crop = $specification['crop'] ?? false; |
|
| 174 | + $mode = $specification['mode'] ?? IPreview::MODE_FILL; |
|
| 175 | + |
|
| 176 | + // If both width and height are -1 we just want the max preview |
|
| 177 | + if ($width === -1 && $height === -1) { |
|
| 178 | + $width = $maxWidth; |
|
| 179 | + $height = $maxHeight; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + // Calculate the preview size |
|
| 183 | + [$width, $height] = $this->calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight); |
|
| 184 | + |
|
| 185 | + // No need to generate a preview that is just the max preview |
|
| 186 | + if ($width === $maxWidth && $height === $maxHeight) { |
|
| 187 | + // ensure correct return value if this was the last one |
|
| 188 | + $preview = $maxPreview; |
|
| 189 | + continue; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + // Try to get a cached preview. Else generate (and store) one |
|
| 193 | + try { |
|
| 194 | + try { |
|
| 195 | + $preview = $this->getCachedPreview($previewFolder, $width, $height, $crop, $maxPreview->getMimeType(), $previewVersion); |
|
| 196 | + } catch (NotFoundException $e) { |
|
| 197 | + if (!$this->previewManager->isMimeSupported($mimeType)) { |
|
| 198 | + throw new NotFoundException(); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + if ($maxPreviewImage === null) { |
|
| 202 | + $maxPreviewImage = $this->helper->getImage($maxPreview); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + $preview = $this->generatePreview($previewFolder, $maxPreviewImage, $width, $height, $crop, $maxWidth, $maxHeight, $previewVersion); |
|
| 206 | + } |
|
| 207 | + } catch (\InvalidArgumentException $e) { |
|
| 208 | + throw new NotFoundException("", 0, $e); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + if ($preview->getSize() === 0) { |
|
| 212 | + $preview->delete(); |
|
| 213 | + throw new NotFoundException('Cached preview size 0, invalid!'); |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + // Free memory being used by the embedded image resource. Without this the image is kept in memory indefinitely. |
|
| 218 | + // Garbage Collection does NOT free this memory. We have to do it ourselves. |
|
| 219 | + if ($maxPreviewImage instanceof \OC_Image) { |
|
| 220 | + $maxPreviewImage->destroy(); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + return $preview; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Generate a small image straight away without generating a max preview first |
|
| 228 | + * Preview generated is 256x256 |
|
| 229 | + */ |
|
| 230 | + private function getSmallImagePreview(ISimpleFolder $previewFolder, File $file, string $mimeType, string $prefix, bool $crop) { |
|
| 231 | + $nodes = $previewFolder->getDirectoryListing(); |
|
| 232 | + |
|
| 233 | + foreach ($nodes as $node) { |
|
| 234 | + $name = $node->getName(); |
|
| 235 | + if (($prefix === '' || strpos($name, $prefix) === 0) |
|
| 236 | + && (str_starts_with($name, '256-256-crop') && $crop || str_starts_with($name, '256-256') && !$crop)) { |
|
| 237 | + return $node; |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $previewProviders = $this->previewManager->getProviders(); |
|
| 242 | + foreach ($previewProviders as $supportedMimeType => $providers) { |
|
| 243 | + // Filter out providers that does not support this mime |
|
| 244 | + if (!preg_match($supportedMimeType, $mimeType)) { |
|
| 245 | + continue; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + foreach ($providers as $providerClosure) { |
|
| 249 | + $provider = $this->helper->getProvider($providerClosure); |
|
| 250 | + if (!($provider instanceof IProviderV2)) { |
|
| 251 | + continue; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + if (!$provider->isAvailable($file)) { |
|
| 255 | + continue; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + $preview = $this->helper->getThumbnail($provider, $file, 256, 256, true); |
|
| 259 | + |
|
| 260 | + if (!($preview instanceof IImage)) { |
|
| 261 | + continue; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + // Try to get the extension. |
|
| 265 | + try { |
|
| 266 | + $ext = $this->getExtention($preview->dataMimeType()); |
|
| 267 | + } catch (\InvalidArgumentException $e) { |
|
| 268 | + // Just continue to the next iteration if this preview doesn't have a valid mimetype |
|
| 269 | + continue; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + $path = $this->generatePath(256, 256, $crop, $preview->dataMimeType(), $prefix); |
|
| 273 | + try { |
|
| 274 | + $file = $previewFolder->newFile($path); |
|
| 275 | + if ($preview instanceof IStreamImage) { |
|
| 276 | + $file->putContent($preview->resource()); |
|
| 277 | + } else { |
|
| 278 | + $file->putContent($preview->data()); |
|
| 279 | + } |
|
| 280 | + } catch (NotPermittedException $e) { |
|
| 281 | + throw new NotFoundException(); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + return $file; |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * @param ISimpleFolder $previewFolder |
|
| 291 | + * @param File $file |
|
| 292 | + * @param string $mimeType |
|
| 293 | + * @param string $prefix |
|
| 294 | + * @return ISimpleFile |
|
| 295 | + * @throws NotFoundException |
|
| 296 | + */ |
|
| 297 | + private function getMaxPreview(ISimpleFolder $previewFolder, File $file, $mimeType, $prefix) { |
|
| 298 | + $nodes = $previewFolder->getDirectoryListing(); |
|
| 299 | + |
|
| 300 | + foreach ($nodes as $node) { |
|
| 301 | + $name = $node->getName(); |
|
| 302 | + if (($prefix === '' || strpos($name, $prefix) === 0) && strpos($name, 'max')) { |
|
| 303 | + return $node; |
|
| 304 | + } |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + $previewProviders = $this->previewManager->getProviders(); |
|
| 308 | + foreach ($previewProviders as $supportedMimeType => $providers) { |
|
| 309 | + // Filter out providers that does not support this mime |
|
| 310 | + if (!preg_match($supportedMimeType, $mimeType)) { |
|
| 311 | + continue; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + foreach ($providers as $providerClosure) { |
|
| 315 | + $provider = $this->helper->getProvider($providerClosure); |
|
| 316 | + if (!($provider instanceof IProviderV2)) { |
|
| 317 | + continue; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + if (!$provider->isAvailable($file)) { |
|
| 321 | + continue; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + $maxWidth = $this->config->getSystemValueInt('preview_max_x', 4096); |
|
| 325 | + $maxHeight = $this->config->getSystemValueInt('preview_max_y', 4096); |
|
| 326 | + |
|
| 327 | + $preview = $this->helper->getThumbnail($provider, $file, $maxWidth, $maxHeight); |
|
| 328 | + |
|
| 329 | + if (!($preview instanceof IImage)) { |
|
| 330 | + continue; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + // Try to get the extention. |
|
| 334 | + try { |
|
| 335 | + $ext = $this->getExtention($preview->dataMimeType()); |
|
| 336 | + } catch (\InvalidArgumentException $e) { |
|
| 337 | + // Just continue to the next iteration if this preview doesn't have a valid mimetype |
|
| 338 | + continue; |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + $path = $prefix . (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext; |
|
| 342 | + try { |
|
| 343 | + $file = $previewFolder->newFile($path); |
|
| 344 | + if ($preview instanceof IStreamImage) { |
|
| 345 | + $file->putContent($preview->resource()); |
|
| 346 | + } else { |
|
| 347 | + $file->putContent($preview->data()); |
|
| 348 | + } |
|
| 349 | + } catch (NotPermittedException $e) { |
|
| 350 | + throw new NotFoundException(); |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + return $file; |
|
| 354 | + } |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + throw new NotFoundException(); |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * @param ISimpleFile $file |
|
| 362 | + * @param string $prefix |
|
| 363 | + * @return int[] |
|
| 364 | + */ |
|
| 365 | + private function getPreviewSize(ISimpleFile $file, string $prefix = '') { |
|
| 366 | + $size = explode('-', substr($file->getName(), strlen($prefix))); |
|
| 367 | + return [(int)$size[0], (int)$size[1]]; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * @param int $width |
|
| 372 | + * @param int $height |
|
| 373 | + * @param bool $crop |
|
| 374 | + * @param string $mimeType |
|
| 375 | + * @param string $prefix |
|
| 376 | + * @return string |
|
| 377 | + */ |
|
| 378 | + private function generatePath($width, $height, $crop, $mimeType, $prefix) { |
|
| 379 | + $path = $prefix . (string)$width . '-' . (string)$height; |
|
| 380 | + if ($crop) { |
|
| 381 | + $path .= '-crop'; |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + $ext = $this->getExtention($mimeType); |
|
| 385 | + $path .= '.' . $ext; |
|
| 386 | + return $path; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * @param int $width |
|
| 392 | + * @param int $height |
|
| 393 | + * @param bool $crop |
|
| 394 | + * @param string $mode |
|
| 395 | + * @param int $maxWidth |
|
| 396 | + * @param int $maxHeight |
|
| 397 | + * @return int[] |
|
| 398 | + */ |
|
| 399 | + private function calculateSize($width, $height, $crop, $mode, $maxWidth, $maxHeight) { |
|
| 400 | + |
|
| 401 | + /* |
|
| 402 | 402 | * If we are not cropping we have to make sure the requested image |
| 403 | 403 | * respects the aspect ratio of the original. |
| 404 | 404 | */ |
| 405 | - if (!$crop) { |
|
| 406 | - $ratio = $maxHeight / $maxWidth; |
|
| 405 | + if (!$crop) { |
|
| 406 | + $ratio = $maxHeight / $maxWidth; |
|
| 407 | 407 | |
| 408 | - if ($width === -1) { |
|
| 409 | - $width = $height / $ratio; |
|
| 410 | - } |
|
| 411 | - if ($height === -1) { |
|
| 412 | - $height = $width * $ratio; |
|
| 413 | - } |
|
| 408 | + if ($width === -1) { |
|
| 409 | + $width = $height / $ratio; |
|
| 410 | + } |
|
| 411 | + if ($height === -1) { |
|
| 412 | + $height = $width * $ratio; |
|
| 413 | + } |
|
| 414 | 414 | |
| 415 | - $ratioH = $height / $maxHeight; |
|
| 416 | - $ratioW = $width / $maxWidth; |
|
| 415 | + $ratioH = $height / $maxHeight; |
|
| 416 | + $ratioW = $width / $maxWidth; |
|
| 417 | 417 | |
| 418 | - /* |
|
| 418 | + /* |
|
| 419 | 419 | * Fill means that the $height and $width are the max |
| 420 | 420 | * Cover means min. |
| 421 | 421 | */ |
| 422 | - if ($mode === IPreview::MODE_FILL) { |
|
| 423 | - if ($ratioH > $ratioW) { |
|
| 424 | - $height = $width * $ratio; |
|
| 425 | - } else { |
|
| 426 | - $width = $height / $ratio; |
|
| 427 | - } |
|
| 428 | - } elseif ($mode === IPreview::MODE_COVER) { |
|
| 429 | - if ($ratioH > $ratioW) { |
|
| 430 | - $width = $height / $ratio; |
|
| 431 | - } else { |
|
| 432 | - $height = $width * $ratio; |
|
| 433 | - } |
|
| 434 | - } |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - if ($height !== $maxHeight && $width !== $maxWidth) { |
|
| 438 | - /* |
|
| 422 | + if ($mode === IPreview::MODE_FILL) { |
|
| 423 | + if ($ratioH > $ratioW) { |
|
| 424 | + $height = $width * $ratio; |
|
| 425 | + } else { |
|
| 426 | + $width = $height / $ratio; |
|
| 427 | + } |
|
| 428 | + } elseif ($mode === IPreview::MODE_COVER) { |
|
| 429 | + if ($ratioH > $ratioW) { |
|
| 430 | + $width = $height / $ratio; |
|
| 431 | + } else { |
|
| 432 | + $height = $width * $ratio; |
|
| 433 | + } |
|
| 434 | + } |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + if ($height !== $maxHeight && $width !== $maxWidth) { |
|
| 438 | + /* |
|
| 439 | 439 | * Scale to the nearest power of four |
| 440 | 440 | */ |
| 441 | - $pow4height = 4 ** ceil(log($height) / log(4)); |
|
| 442 | - $pow4width = 4 ** ceil(log($width) / log(4)); |
|
| 443 | - |
|
| 444 | - // Minimum size is 64 |
|
| 445 | - $pow4height = max($pow4height, 64); |
|
| 446 | - $pow4width = max($pow4width, 64); |
|
| 447 | - |
|
| 448 | - $ratioH = $height / $pow4height; |
|
| 449 | - $ratioW = $width / $pow4width; |
|
| 450 | - |
|
| 451 | - if ($ratioH < $ratioW) { |
|
| 452 | - $width = $pow4width; |
|
| 453 | - $height /= $ratioW; |
|
| 454 | - } else { |
|
| 455 | - $height = $pow4height; |
|
| 456 | - $width /= $ratioH; |
|
| 457 | - } |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - /* |
|
| 441 | + $pow4height = 4 ** ceil(log($height) / log(4)); |
|
| 442 | + $pow4width = 4 ** ceil(log($width) / log(4)); |
|
| 443 | + |
|
| 444 | + // Minimum size is 64 |
|
| 445 | + $pow4height = max($pow4height, 64); |
|
| 446 | + $pow4width = max($pow4width, 64); |
|
| 447 | + |
|
| 448 | + $ratioH = $height / $pow4height; |
|
| 449 | + $ratioW = $width / $pow4width; |
|
| 450 | + |
|
| 451 | + if ($ratioH < $ratioW) { |
|
| 452 | + $width = $pow4width; |
|
| 453 | + $height /= $ratioW; |
|
| 454 | + } else { |
|
| 455 | + $height = $pow4height; |
|
| 456 | + $width /= $ratioH; |
|
| 457 | + } |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + /* |
|
| 461 | 461 | * Make sure the requested height and width fall within the max |
| 462 | 462 | * of the preview. |
| 463 | 463 | */ |
| 464 | - if ($height > $maxHeight) { |
|
| 465 | - $ratio = $height / $maxHeight; |
|
| 466 | - $height = $maxHeight; |
|
| 467 | - $width /= $ratio; |
|
| 468 | - } |
|
| 469 | - if ($width > $maxWidth) { |
|
| 470 | - $ratio = $width / $maxWidth; |
|
| 471 | - $width = $maxWidth; |
|
| 472 | - $height /= $ratio; |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - return [(int)round($width), (int)round($height)]; |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * @param ISimpleFolder $previewFolder |
|
| 480 | - * @param ISimpleFile $maxPreview |
|
| 481 | - * @param int $width |
|
| 482 | - * @param int $height |
|
| 483 | - * @param bool $crop |
|
| 484 | - * @param int $maxWidth |
|
| 485 | - * @param int $maxHeight |
|
| 486 | - * @param string $prefix |
|
| 487 | - * @return ISimpleFile |
|
| 488 | - * @throws NotFoundException |
|
| 489 | - * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
| 490 | - */ |
|
| 491 | - private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight, $prefix) { |
|
| 492 | - $preview = $maxPreview; |
|
| 493 | - if (!$preview->valid()) { |
|
| 494 | - throw new \InvalidArgumentException('Failed to generate preview, failed to load image'); |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - if ($crop) { |
|
| 498 | - if ($height !== $preview->height() && $width !== $preview->width()) { |
|
| 499 | - //Resize |
|
| 500 | - $widthR = $preview->width() / $width; |
|
| 501 | - $heightR = $preview->height() / $height; |
|
| 502 | - |
|
| 503 | - if ($widthR > $heightR) { |
|
| 504 | - $scaleH = $height; |
|
| 505 | - $scaleW = $maxWidth / $heightR; |
|
| 506 | - } else { |
|
| 507 | - $scaleH = $maxHeight / $widthR; |
|
| 508 | - $scaleW = $width; |
|
| 509 | - } |
|
| 510 | - $preview = $preview->preciseResizeCopy((int)round($scaleW), (int)round($scaleH)); |
|
| 511 | - } |
|
| 512 | - $cropX = (int)floor(abs($width - $preview->width()) * 0.5); |
|
| 513 | - $cropY = (int)floor(abs($height - $preview->height()) * 0.5); |
|
| 514 | - $preview = $preview->cropCopy($cropX, $cropY, $width, $height); |
|
| 515 | - } else { |
|
| 516 | - $preview = $maxPreview->resizeCopy(max($width, $height)); |
|
| 517 | - } |
|
| 518 | - |
|
| 519 | - |
|
| 520 | - $path = $this->generatePath($width, $height, $crop, $preview->dataMimeType(), $prefix); |
|
| 521 | - try { |
|
| 522 | - $file = $previewFolder->newFile($path); |
|
| 523 | - $file->putContent($preview->data()); |
|
| 524 | - } catch (NotPermittedException $e) { |
|
| 525 | - throw new NotFoundException(); |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - return $file; |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - /** |
|
| 532 | - * @param ISimpleFolder $previewFolder |
|
| 533 | - * @param int $width |
|
| 534 | - * @param int $height |
|
| 535 | - * @param bool $crop |
|
| 536 | - * @param string $mimeType |
|
| 537 | - * @param string $prefix |
|
| 538 | - * @return ISimpleFile |
|
| 539 | - * |
|
| 540 | - * @throws NotFoundException |
|
| 541 | - */ |
|
| 542 | - private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType, $prefix) { |
|
| 543 | - $path = $this->generatePath($width, $height, $crop, $mimeType, $prefix); |
|
| 544 | - |
|
| 545 | - return $previewFolder->getFile($path); |
|
| 546 | - } |
|
| 547 | - |
|
| 548 | - /** |
|
| 549 | - * Get the specific preview folder for this file |
|
| 550 | - * |
|
| 551 | - * @param File $file |
|
| 552 | - * @return ISimpleFolder |
|
| 553 | - * |
|
| 554 | - * @throws InvalidPathException |
|
| 555 | - * @throws NotFoundException |
|
| 556 | - * @throws NotPermittedException |
|
| 557 | - */ |
|
| 558 | - private function getPreviewFolder(File $file) { |
|
| 559 | - // Obtain file id outside of try catch block to prevent the creation of an existing folder |
|
| 560 | - $fileId = (string)$file->getId(); |
|
| 561 | - |
|
| 562 | - try { |
|
| 563 | - $folder = $this->appData->getFolder($fileId); |
|
| 564 | - } catch (NotFoundException $e) { |
|
| 565 | - $folder = $this->appData->newFolder($fileId); |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - return $folder; |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - /** |
|
| 572 | - * @param string $mimeType |
|
| 573 | - * @return null|string |
|
| 574 | - * @throws \InvalidArgumentException |
|
| 575 | - */ |
|
| 576 | - private function getExtention($mimeType) { |
|
| 577 | - switch ($mimeType) { |
|
| 578 | - case 'image/png': |
|
| 579 | - return 'png'; |
|
| 580 | - case 'image/jpeg': |
|
| 581 | - return 'jpg'; |
|
| 582 | - case 'image/gif': |
|
| 583 | - return 'gif'; |
|
| 584 | - default: |
|
| 585 | - throw new \InvalidArgumentException('Not a valid mimetype: "' . $mimeType . '"'); |
|
| 586 | - } |
|
| 587 | - } |
|
| 464 | + if ($height > $maxHeight) { |
|
| 465 | + $ratio = $height / $maxHeight; |
|
| 466 | + $height = $maxHeight; |
|
| 467 | + $width /= $ratio; |
|
| 468 | + } |
|
| 469 | + if ($width > $maxWidth) { |
|
| 470 | + $ratio = $width / $maxWidth; |
|
| 471 | + $width = $maxWidth; |
|
| 472 | + $height /= $ratio; |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + return [(int)round($width), (int)round($height)]; |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * @param ISimpleFolder $previewFolder |
|
| 480 | + * @param ISimpleFile $maxPreview |
|
| 481 | + * @param int $width |
|
| 482 | + * @param int $height |
|
| 483 | + * @param bool $crop |
|
| 484 | + * @param int $maxWidth |
|
| 485 | + * @param int $maxHeight |
|
| 486 | + * @param string $prefix |
|
| 487 | + * @return ISimpleFile |
|
| 488 | + * @throws NotFoundException |
|
| 489 | + * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) |
|
| 490 | + */ |
|
| 491 | + private function generatePreview(ISimpleFolder $previewFolder, IImage $maxPreview, $width, $height, $crop, $maxWidth, $maxHeight, $prefix) { |
|
| 492 | + $preview = $maxPreview; |
|
| 493 | + if (!$preview->valid()) { |
|
| 494 | + throw new \InvalidArgumentException('Failed to generate preview, failed to load image'); |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + if ($crop) { |
|
| 498 | + if ($height !== $preview->height() && $width !== $preview->width()) { |
|
| 499 | + //Resize |
|
| 500 | + $widthR = $preview->width() / $width; |
|
| 501 | + $heightR = $preview->height() / $height; |
|
| 502 | + |
|
| 503 | + if ($widthR > $heightR) { |
|
| 504 | + $scaleH = $height; |
|
| 505 | + $scaleW = $maxWidth / $heightR; |
|
| 506 | + } else { |
|
| 507 | + $scaleH = $maxHeight / $widthR; |
|
| 508 | + $scaleW = $width; |
|
| 509 | + } |
|
| 510 | + $preview = $preview->preciseResizeCopy((int)round($scaleW), (int)round($scaleH)); |
|
| 511 | + } |
|
| 512 | + $cropX = (int)floor(abs($width - $preview->width()) * 0.5); |
|
| 513 | + $cropY = (int)floor(abs($height - $preview->height()) * 0.5); |
|
| 514 | + $preview = $preview->cropCopy($cropX, $cropY, $width, $height); |
|
| 515 | + } else { |
|
| 516 | + $preview = $maxPreview->resizeCopy(max($width, $height)); |
|
| 517 | + } |
|
| 518 | + |
|
| 519 | + |
|
| 520 | + $path = $this->generatePath($width, $height, $crop, $preview->dataMimeType(), $prefix); |
|
| 521 | + try { |
|
| 522 | + $file = $previewFolder->newFile($path); |
|
| 523 | + $file->putContent($preview->data()); |
|
| 524 | + } catch (NotPermittedException $e) { |
|
| 525 | + throw new NotFoundException(); |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + return $file; |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + /** |
|
| 532 | + * @param ISimpleFolder $previewFolder |
|
| 533 | + * @param int $width |
|
| 534 | + * @param int $height |
|
| 535 | + * @param bool $crop |
|
| 536 | + * @param string $mimeType |
|
| 537 | + * @param string $prefix |
|
| 538 | + * @return ISimpleFile |
|
| 539 | + * |
|
| 540 | + * @throws NotFoundException |
|
| 541 | + */ |
|
| 542 | + private function getCachedPreview(ISimpleFolder $previewFolder, $width, $height, $crop, $mimeType, $prefix) { |
|
| 543 | + $path = $this->generatePath($width, $height, $crop, $mimeType, $prefix); |
|
| 544 | + |
|
| 545 | + return $previewFolder->getFile($path); |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + /** |
|
| 549 | + * Get the specific preview folder for this file |
|
| 550 | + * |
|
| 551 | + * @param File $file |
|
| 552 | + * @return ISimpleFolder |
|
| 553 | + * |
|
| 554 | + * @throws InvalidPathException |
|
| 555 | + * @throws NotFoundException |
|
| 556 | + * @throws NotPermittedException |
|
| 557 | + */ |
|
| 558 | + private function getPreviewFolder(File $file) { |
|
| 559 | + // Obtain file id outside of try catch block to prevent the creation of an existing folder |
|
| 560 | + $fileId = (string)$file->getId(); |
|
| 561 | + |
|
| 562 | + try { |
|
| 563 | + $folder = $this->appData->getFolder($fileId); |
|
| 564 | + } catch (NotFoundException $e) { |
|
| 565 | + $folder = $this->appData->newFolder($fileId); |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + return $folder; |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + /** |
|
| 572 | + * @param string $mimeType |
|
| 573 | + * @return null|string |
|
| 574 | + * @throws \InvalidArgumentException |
|
| 575 | + */ |
|
| 576 | + private function getExtention($mimeType) { |
|
| 577 | + switch ($mimeType) { |
|
| 578 | + case 'image/png': |
|
| 579 | + return 'png'; |
|
| 580 | + case 'image/jpeg': |
|
| 581 | + return 'jpg'; |
|
| 582 | + case 'image/gif': |
|
| 583 | + return 'gif'; |
|
| 584 | + default: |
|
| 585 | + throw new \InvalidArgumentException('Not a valid mimetype: "' . $mimeType . '"'); |
|
| 586 | + } |
|
| 587 | + } |
|
| 588 | 588 | } |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | |
| 136 | 136 | $previewVersion = ''; |
| 137 | 137 | if ($file instanceof IVersionedPreviewFile) { |
| 138 | - $previewVersion = $file->getPreviewVersion() . '-'; |
|
| 138 | + $previewVersion = $file->getPreviewVersion().'-'; |
|
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | // If imaginary is enabled, and we request a small thumbnail, |
@@ -338,7 +338,7 @@ discard block |
||
| 338 | 338 | continue; |
| 339 | 339 | } |
| 340 | 340 | |
| 341 | - $path = $prefix . (string)$preview->width() . '-' . (string)$preview->height() . '-max.' . $ext; |
|
| 341 | + $path = $prefix.(string) $preview->width().'-'.(string) $preview->height().'-max.'.$ext; |
|
| 342 | 342 | try { |
| 343 | 343 | $file = $previewFolder->newFile($path); |
| 344 | 344 | if ($preview instanceof IStreamImage) { |
@@ -364,7 +364,7 @@ discard block |
||
| 364 | 364 | */ |
| 365 | 365 | private function getPreviewSize(ISimpleFile $file, string $prefix = '') { |
| 366 | 366 | $size = explode('-', substr($file->getName(), strlen($prefix))); |
| 367 | - return [(int)$size[0], (int)$size[1]]; |
|
| 367 | + return [(int) $size[0], (int) $size[1]]; |
|
| 368 | 368 | } |
| 369 | 369 | |
| 370 | 370 | /** |
@@ -376,13 +376,13 @@ discard block |
||
| 376 | 376 | * @return string |
| 377 | 377 | */ |
| 378 | 378 | private function generatePath($width, $height, $crop, $mimeType, $prefix) { |
| 379 | - $path = $prefix . (string)$width . '-' . (string)$height; |
|
| 379 | + $path = $prefix.(string) $width.'-'.(string) $height; |
|
| 380 | 380 | if ($crop) { |
| 381 | 381 | $path .= '-crop'; |
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | $ext = $this->getExtention($mimeType); |
| 385 | - $path .= '.' . $ext; |
|
| 385 | + $path .= '.'.$ext; |
|
| 386 | 386 | return $path; |
| 387 | 387 | } |
| 388 | 388 | |
@@ -472,7 +472,7 @@ discard block |
||
| 472 | 472 | $height /= $ratio; |
| 473 | 473 | } |
| 474 | 474 | |
| 475 | - return [(int)round($width), (int)round($height)]; |
|
| 475 | + return [(int) round($width), (int) round($height)]; |
|
| 476 | 476 | } |
| 477 | 477 | |
| 478 | 478 | /** |
@@ -507,10 +507,10 @@ discard block |
||
| 507 | 507 | $scaleH = $maxHeight / $widthR; |
| 508 | 508 | $scaleW = $width; |
| 509 | 509 | } |
| 510 | - $preview = $preview->preciseResizeCopy((int)round($scaleW), (int)round($scaleH)); |
|
| 510 | + $preview = $preview->preciseResizeCopy((int) round($scaleW), (int) round($scaleH)); |
|
| 511 | 511 | } |
| 512 | - $cropX = (int)floor(abs($width - $preview->width()) * 0.5); |
|
| 513 | - $cropY = (int)floor(abs($height - $preview->height()) * 0.5); |
|
| 512 | + $cropX = (int) floor(abs($width - $preview->width()) * 0.5); |
|
| 513 | + $cropY = (int) floor(abs($height - $preview->height()) * 0.5); |
|
| 514 | 514 | $preview = $preview->cropCopy($cropX, $cropY, $width, $height); |
| 515 | 515 | } else { |
| 516 | 516 | $preview = $maxPreview->resizeCopy(max($width, $height)); |
@@ -557,7 +557,7 @@ discard block |
||
| 557 | 557 | */ |
| 558 | 558 | private function getPreviewFolder(File $file) { |
| 559 | 559 | // Obtain file id outside of try catch block to prevent the creation of an existing folder |
| 560 | - $fileId = (string)$file->getId(); |
|
| 560 | + $fileId = (string) $file->getId(); |
|
| 561 | 561 | |
| 562 | 562 | try { |
| 563 | 563 | $folder = $this->appData->getFolder($fileId); |
@@ -582,7 +582,7 @@ discard block |
||
| 582 | 582 | case 'image/gif': |
| 583 | 583 | return 'gif'; |
| 584 | 584 | default: |
| 585 | - throw new \InvalidArgumentException('Not a valid mimetype: "' . $mimeType . '"'); |
|
| 585 | + throw new \InvalidArgumentException('Not a valid mimetype: "'.$mimeType.'"'); |
|
| 586 | 586 | } |
| 587 | 587 | } |
| 588 | 588 | } |