diff options
author | Roeland Jago Douma <rullzer@users.noreply.github.com> | 2020-04-08 19:33:35 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-08 19:33:35 +0200 |
commit | 37146c9b0f7450cb57d4e60fc05ed56faf0c761c (patch) | |
tree | 562a7d2c8e0ab99764d2132b64b691d3dc54c516 /lib/private | |
parent | cb2b38516f2a5e98400e5ed2cbcd17ceeb269b9b (diff) | |
parent | 5d27ed5e229595c7aaa2ce7835ab2b26a2caee24 (diff) | |
download | nextcloud-server-37146c9b0f7450cb57d4e60fc05ed56faf0c761c.tar.gz nextcloud-server-37146c9b0f7450cb57d4e60fc05ed56faf0c761c.zip |
Merge pull request #20319 from nextcloud/enh/noid/embedded-previews
Use embedded previews of ZIP-like files
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/Preview/Bundled.php | 56 | ||||
-rw-r--r-- | lib/private/Preview/Krita.php | 52 | ||||
-rw-r--r-- | lib/private/Preview/OpenDocument.php | 17 | ||||
-rw-r--r-- | lib/private/PreviewManager.php | 6 |
4 files changed, 129 insertions, 2 deletions
diff --git a/lib/private/Preview/Bundled.php b/lib/private/Preview/Bundled.php new file mode 100644 index 00000000000..afd286d895b --- /dev/null +++ b/lib/private/Preview/Bundled.php @@ -0,0 +1,56 @@ +<?php +/** + * @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net> + * + * @author Julius Härtl <jus@bitgrid.net> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Preview; + +use OC\Archive\ZIP; +use OCP\Files\File; +use OCP\IImage; + +/** + * Extracts a preview from files that embed them in an ZIP archive + */ +abstract class Bundled extends ProviderV2 { + + protected function extractThumbnail(File $file, $path): ?IImage { + $sourceTmp = \OC::$server->getTempManager()->getTemporaryFile(); + $targetTmp = \OC::$server->getTempManager()->getTemporaryFile(); + + try { + $content = $file->fopen('r'); + file_put_contents($sourceTmp, $content); + + $zip = new ZIP($sourceTmp); + $zip->extractFile($path, $targetTmp); + + $image = new \OC_Image(); + $image->loadFromFile($targetTmp); + $image->fixOrientation(); + + return $image; + } catch (\Exception $e) { + return null; + } + } + +} diff --git a/lib/private/Preview/Krita.php b/lib/private/Preview/Krita.php new file mode 100644 index 00000000000..39449145a14 --- /dev/null +++ b/lib/private/Preview/Krita.php @@ -0,0 +1,52 @@ +<?php +/** + * @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net> + * + * @author Julius Härtl <jus@bitgrid.net> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Preview; + +use OCP\Files\File; +use OCP\IImage; + +class Krita extends Bundled { + /** + * {@inheritDoc} + */ + public function getMimeType(): string { + return '/application\/x-krita/'; + } + + + /** + * @inheritDoc + */ + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { + $image = $this->extractThumbnail($file, 'mergedimage.png'); + if ($image->valid()) { + return $image; + } + $image = $this->extractThumbnail($file, 'preview.png'); + if ($image->valid()) { + return $image; + } + return null; + } +} diff --git a/lib/private/Preview/OpenDocument.php b/lib/private/Preview/OpenDocument.php index b795441d837..1db7b0be521 100644 --- a/lib/private/Preview/OpenDocument.php +++ b/lib/private/Preview/OpenDocument.php @@ -25,11 +25,26 @@ namespace OC\Preview; //.odt, .ott, .oth, .odm, .odg, .otg, .odp, .otp, .ods, .ots, .odc, .odf, .odb, .odi, .oxt -class OpenDocument extends Office { +use OCP\Files\File; +use OCP\IImage; + +class OpenDocument extends Bundled { /** * {@inheritDoc} */ public function getMimeType(): string { return '/application\/vnd.oasis.opendocument.*/'; } + + + /** + * @inheritDoc + */ + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { + $image = $this->extractThumbnail($file, 'Thumbnails/thumbnail.png'); + if ($image->valid()) { + return $image; + } + return null; + } } diff --git a/lib/private/PreviewManager.php b/lib/private/PreviewManager.php index 652a442deec..adfc04199e3 100644 --- a/lib/private/PreviewManager.php +++ b/lib/private/PreviewManager.php @@ -293,13 +293,15 @@ class PreviewManager implements IPreview { Preview\GIF::class, Preview\BMP::class, Preview\HEIC::class, - Preview\XBitmap::class + Preview\XBitmap::class, + Preview\Krita::class, ]; $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([ Preview\MarkDown::class, Preview\MP3::class, Preview\TXT::class, + Preview\OpenDocument::class, ], $imageProviders)); if (in_array(Preview\Image::class, $this->defaultProviders)) { @@ -339,7 +341,9 @@ class PreviewManager implements IPreview { $this->registerCoreProvider(Preview\GIF::class, '/image\/gif/'); $this->registerCoreProvider(Preview\BMP::class, '/image\/bmp/'); $this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/'); + $this->registerCoreProvider(Preview\Krita::class, '/application\/x-krita/'); $this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg/'); + $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); // SVG, Office and Bitmap require imagick if (extension_loaded('imagick')) { |