Skip to content

Commit 37146c9

Browse files
authored
Merge pull request #20319 from nextcloud/enh/noid/embedded-previews
Use embedded previews of ZIP-like files
2 parents cb2b385 + 5d27ed5 commit 37146c9

7 files changed

Lines changed: 138 additions & 12 deletions

File tree

config/config.sample.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,6 @@
989989
* - OC\Preview\MSOffice2003
990990
* - OC\Preview\MSOffice2007
991991
* - OC\Preview\MSOfficeDoc
992-
* - OC\Preview\OpenDocument
993992
* - OC\Preview\PDF
994993
* - OC\Preview\Photoshop
995994
* - OC\Preview\Postscript
@@ -998,14 +997,6 @@
998997
* - OC\Preview\TIFF
999998
* - OC\Preview\Font
1000999
*
1001-
* The following providers are not available in Microsoft Windows:
1002-
*
1003-
* - OC\Preview\Movie
1004-
* - OC\Preview\MSOfficeDoc
1005-
* - OC\Preview\MSOffice2003
1006-
* - OC\Preview\MSOffice2007
1007-
* - OC\Preview\OpenDocument
1008-
* - OC\Preview\StarOffice
10091000
*
10101001
* Defaults to the following providers:
10111002
*
@@ -1018,6 +1009,8 @@
10181009
* - OC\Preview\PNG
10191010
* - OC\Preview\TXT
10201011
* - OC\Preview\XBitmap
1012+
* - OC\Preview\OpenDocument
1013+
* - OC\Preview\Krita
10211014
*/
10221015
'enabledPreviewProviders' => [
10231016
'OC\Preview\PNG',
@@ -1028,7 +1021,9 @@
10281021
'OC\Preview\XBitmap',
10291022
'OC\Preview\MP3',
10301023
'OC\Preview\TXT',
1031-
'OC\Preview\MarkDown'
1024+
'OC\Preview\MarkDown',
1025+
'OC\Preview\OpenDocument',
1026+
'OC\Preview\Krita',
10321027
],
10331028

10341029
/**

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,7 @@
11241124
'OC\\Preview\\BMP' => $baseDir . '/lib/private/Preview/BMP.php',
11251125
'OC\\Preview\\BackgroundCleanupJob' => $baseDir . '/lib/private/Preview/BackgroundCleanupJob.php',
11261126
'OC\\Preview\\Bitmap' => $baseDir . '/lib/private/Preview/Bitmap.php',
1127+
'OC\\Preview\\Bundled' => $baseDir . '/lib/private/Preview/Bundled.php',
11271128
'OC\\Preview\\Font' => $baseDir . '/lib/private/Preview/Font.php',
11281129
'OC\\Preview\\GIF' => $baseDir . '/lib/private/Preview/GIF.php',
11291130
'OC\\Preview\\Generator' => $baseDir . '/lib/private/Preview/Generator.php',
@@ -1132,6 +1133,7 @@
11321133
'OC\\Preview\\Illustrator' => $baseDir . '/lib/private/Preview/Illustrator.php',
11331134
'OC\\Preview\\Image' => $baseDir . '/lib/private/Preview/Image.php',
11341135
'OC\\Preview\\JPEG' => $baseDir . '/lib/private/Preview/JPEG.php',
1136+
'OC\\Preview\\Krita' => $baseDir . '/lib/private/Preview/Krita.php',
11351137
'OC\\Preview\\MP3' => $baseDir . '/lib/private/Preview/MP3.php',
11361138
'OC\\Preview\\MSOffice2003' => $baseDir . '/lib/private/Preview/MSOffice2003.php',
11371139
'OC\\Preview\\MSOffice2007' => $baseDir . '/lib/private/Preview/MSOffice2007.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
11531153
'OC\\Preview\\BMP' => __DIR__ . '/../../..' . '/lib/private/Preview/BMP.php',
11541154
'OC\\Preview\\BackgroundCleanupJob' => __DIR__ . '/../../..' . '/lib/private/Preview/BackgroundCleanupJob.php',
11551155
'OC\\Preview\\Bitmap' => __DIR__ . '/../../..' . '/lib/private/Preview/Bitmap.php',
1156+
'OC\\Preview\\Bundled' => __DIR__ . '/../../..' . '/lib/private/Preview/Bundled.php',
11561157
'OC\\Preview\\Font' => __DIR__ . '/../../..' . '/lib/private/Preview/Font.php',
11571158
'OC\\Preview\\GIF' => __DIR__ . '/../../..' . '/lib/private/Preview/GIF.php',
11581159
'OC\\Preview\\Generator' => __DIR__ . '/../../..' . '/lib/private/Preview/Generator.php',
@@ -1161,6 +1162,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
11611162
'OC\\Preview\\Illustrator' => __DIR__ . '/../../..' . '/lib/private/Preview/Illustrator.php',
11621163
'OC\\Preview\\Image' => __DIR__ . '/../../..' . '/lib/private/Preview/Image.php',
11631164
'OC\\Preview\\JPEG' => __DIR__ . '/../../..' . '/lib/private/Preview/JPEG.php',
1165+
'OC\\Preview\\Krita' => __DIR__ . '/../../..' . '/lib/private/Preview/Krita.php',
11641166
'OC\\Preview\\MP3' => __DIR__ . '/../../..' . '/lib/private/Preview/MP3.php',
11651167
'OC\\Preview\\MSOffice2003' => __DIR__ . '/../../..' . '/lib/private/Preview/MSOffice2003.php',
11661168
'OC\\Preview\\MSOffice2007' => __DIR__ . '/../../..' . '/lib/private/Preview/MSOffice2007.php',

lib/private/Preview/Bundled.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2020 Julius Härtl <[email protected]>
4+
*
5+
* @author Julius Härtl <[email protected]>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
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
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OC\Preview;
25+
26+
use OC\Archive\ZIP;
27+
use OCP\Files\File;
28+
use OCP\IImage;
29+
30+
/**
31+
* Extracts a preview from files that embed them in an ZIP archive
32+
*/
33+
abstract class Bundled extends ProviderV2 {
34+
35+
protected function extractThumbnail(File $file, $path): ?IImage {
36+
$sourceTmp = \OC::$server->getTempManager()->getTemporaryFile();
37+
$targetTmp = \OC::$server->getTempManager()->getTemporaryFile();
38+
39+
try {
40+
$content = $file->fopen('r');
41+
file_put_contents($sourceTmp, $content);
42+
43+
$zip = new ZIP($sourceTmp);
44+
$zip->extractFile($path, $targetTmp);
45+
46+
$image = new \OC_Image();
47+
$image->loadFromFile($targetTmp);
48+
$image->fixOrientation();
49+
50+
return $image;
51+
} catch (\Exception $e) {
52+
return null;
53+
}
54+
}
55+
56+
}

lib/private/Preview/Krita.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2020 Julius Härtl <[email protected]>
4+
*
5+
* @author Julius Härtl <[email protected]>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
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
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OC\Preview;
25+
26+
use OCP\Files\File;
27+
use OCP\IImage;
28+
29+
class Krita extends Bundled {
30+
/**
31+
* {@inheritDoc}
32+
*/
33+
public function getMimeType(): string {
34+
return '/application\/x-krita/';
35+
}
36+
37+
38+
/**
39+
* @inheritDoc
40+
*/
41+
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
42+
$image = $this->extractThumbnail($file, 'mergedimage.png');
43+
if ($image->valid()) {
44+
return $image;
45+
}
46+
$image = $this->extractThumbnail($file, 'preview.png');
47+
if ($image->valid()) {
48+
return $image;
49+
}
50+
return null;
51+
}
52+
}

lib/private/Preview/OpenDocument.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,26 @@
2525
namespace OC\Preview;
2626

2727
//.odt, .ott, .oth, .odm, .odg, .otg, .odp, .otp, .ods, .ots, .odc, .odf, .odb, .odi, .oxt
28-
class OpenDocument extends Office {
28+
use OCP\Files\File;
29+
use OCP\IImage;
30+
31+
class OpenDocument extends Bundled {
2932
/**
3033
* {@inheritDoc}
3134
*/
3235
public function getMimeType(): string {
3336
return '/application\/vnd.oasis.opendocument.*/';
3437
}
38+
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
44+
$image = $this->extractThumbnail($file, 'Thumbnails/thumbnail.png');
45+
if ($image->valid()) {
46+
return $image;
47+
}
48+
return null;
49+
}
3550
}

lib/private/PreviewManager.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,15 @@ protected function getEnabledDefaultProvider() {
293293
Preview\GIF::class,
294294
Preview\BMP::class,
295295
Preview\HEIC::class,
296-
Preview\XBitmap::class
296+
Preview\XBitmap::class,
297+
Preview\Krita::class,
297298
];
298299

299300
$this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([
300301
Preview\MarkDown::class,
301302
Preview\MP3::class,
302303
Preview\TXT::class,
304+
Preview\OpenDocument::class,
303305
], $imageProviders));
304306

305307
if (in_array(Preview\Image::class, $this->defaultProviders)) {
@@ -339,7 +341,9 @@ protected function registerCoreProviders() {
339341
$this->registerCoreProvider(Preview\GIF::class, '/image\/gif/');
340342
$this->registerCoreProvider(Preview\BMP::class, '/image\/bmp/');
341343
$this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/');
344+
$this->registerCoreProvider(Preview\Krita::class, '/application\/x-krita/');
342345
$this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg/');
346+
$this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/');
343347

344348
// SVG, Office and Bitmap require imagick
345349
if (extension_loaded('imagick')) {

0 commit comments

Comments
 (0)