|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. |
| 8 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 9 | + */ |
| 10 | + |
| 11 | +namespace OC\Preview; |
| 12 | + |
| 13 | +use OCP\Files\File; |
| 14 | +use OCP\Files\FileInfo; |
| 15 | +use OCP\IImage; |
| 16 | +use OCP\Image; |
| 17 | + |
| 18 | +class CDR extends ProviderV2 { |
| 19 | + #[\Override] |
| 20 | + public function getMimeType(): string { |
| 21 | + return '/application\/coreldraw/'; |
| 22 | + } |
| 23 | + #[\Override] |
| 24 | + public function isAvailable(FileInfo $file): bool { |
| 25 | + return $file->getSize() > 0; |
| 26 | + } |
| 27 | + #[\Override] |
| 28 | + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { |
| 29 | + if (!$this->isAvailable($file)) { |
| 30 | + return null; |
| 31 | + } |
| 32 | + |
| 33 | + $localFile = $this->getLocalFile($file); |
| 34 | + |
| 35 | + if ($localFile === false) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + $data = $this->extractThumbnail($localFile); |
| 40 | + if ($data === null) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + $image = new Image(); |
| 45 | + $image->loadFromData($data); |
| 46 | + |
| 47 | + if (!$image->valid()) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + $image->scaleDownToFit($maxX, $maxY); |
| 52 | + |
| 53 | + return $image; |
| 54 | + } |
| 55 | + /** * Extract ONLY thumbnail (no pages) */ |
| 56 | + private function extractThumbnail(string $file): ?string { |
| 57 | + $zip = new \ZipArchive(); |
| 58 | + |
| 59 | + if ($zip->open($file) !== true) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Newer CorelDRAW files store the embedded preview in previews/thumbnail.png. |
| 65 | + * |
| 66 | + * Older CorelDRAW files store the embedded BMP thumbnail in |
| 67 | + * metadata/thumbnails/thumbnail.bmp. |
| 68 | + */ |
| 69 | + foreach ([ |
| 70 | + 'previews/thumbnail.png', |
| 71 | + 'metadata/thumbnails/thumbnail.bmp', |
| 72 | + ] as $thumbnail) { |
| 73 | + $idx = $zip->locateName($thumbnail); |
| 74 | + |
| 75 | + if ($idx === false) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + $data = $zip->getFromIndex($idx); |
| 80 | + $zip->close(); |
| 81 | + |
| 82 | + return $data === false ? null : $data; |
| 83 | + } |
| 84 | + |
| 85 | + $zip->close(); |
| 86 | + |
| 87 | + return null; |
| 88 | + } |
| 89 | +} |
0 commit comments