Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use embedded previews of ZIP-like files #20319

Merged
merged 5 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,6 @@
* - OC\Preview\MSOffice2003
* - OC\Preview\MSOffice2007
* - OC\Preview\MSOfficeDoc
* - OC\Preview\OpenDocument
* - OC\Preview\PDF
* - OC\Preview\Photoshop
* - OC\Preview\Postscript
Expand All @@ -998,14 +997,6 @@
* - OC\Preview\TIFF
* - OC\Preview\Font
*
* The following providers are not available in Microsoft Windows:
*
* - OC\Preview\Movie
* - OC\Preview\MSOfficeDoc
* - OC\Preview\MSOffice2003
* - OC\Preview\MSOffice2007
* - OC\Preview\OpenDocument
* - OC\Preview\StarOffice
*
* Defaults to the following providers:
*
Expand All @@ -1018,6 +1009,8 @@
* - OC\Preview\PNG
* - OC\Preview\TXT
* - OC\Preview\XBitmap
* - OC\Preview\OpenDocument
* - OC\Preview\Krita
*/
'enabledPreviewProviders' => [
'OC\Preview\PNG',
Expand All @@ -1028,7 +1021,9 @@
'OC\Preview\XBitmap',
'OC\Preview\MP3',
'OC\Preview\TXT',
'OC\Preview\MarkDown'
'OC\Preview\MarkDown',
'OC\Preview\OpenDocument',
'OC\Preview\Krita',
],

/**
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@
'OC\\Preview\\BMP' => $baseDir . '/lib/private/Preview/BMP.php',
'OC\\Preview\\BackgroundCleanupJob' => $baseDir . '/lib/private/Preview/BackgroundCleanupJob.php',
'OC\\Preview\\Bitmap' => $baseDir . '/lib/private/Preview/Bitmap.php',
'OC\\Preview\\Bundled' => $baseDir . '/lib/private/Preview/Bundled.php',
'OC\\Preview\\Font' => $baseDir . '/lib/private/Preview/Font.php',
'OC\\Preview\\GIF' => $baseDir . '/lib/private/Preview/GIF.php',
'OC\\Preview\\Generator' => $baseDir . '/lib/private/Preview/Generator.php',
Expand All @@ -1132,6 +1133,7 @@
'OC\\Preview\\Illustrator' => $baseDir . '/lib/private/Preview/Illustrator.php',
'OC\\Preview\\Image' => $baseDir . '/lib/private/Preview/Image.php',
'OC\\Preview\\JPEG' => $baseDir . '/lib/private/Preview/JPEG.php',
'OC\\Preview\\Krita' => $baseDir . '/lib/private/Preview/Krita.php',
'OC\\Preview\\MP3' => $baseDir . '/lib/private/Preview/MP3.php',
'OC\\Preview\\MSOffice2003' => $baseDir . '/lib/private/Preview/MSOffice2003.php',
'OC\\Preview\\MSOffice2007' => $baseDir . '/lib/private/Preview/MSOffice2007.php',
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Preview\\BMP' => __DIR__ . '/../../..' . '/lib/private/Preview/BMP.php',
'OC\\Preview\\BackgroundCleanupJob' => __DIR__ . '/../../..' . '/lib/private/Preview/BackgroundCleanupJob.php',
'OC\\Preview\\Bitmap' => __DIR__ . '/../../..' . '/lib/private/Preview/Bitmap.php',
'OC\\Preview\\Bundled' => __DIR__ . '/../../..' . '/lib/private/Preview/Bundled.php',
'OC\\Preview\\Font' => __DIR__ . '/../../..' . '/lib/private/Preview/Font.php',
'OC\\Preview\\GIF' => __DIR__ . '/../../..' . '/lib/private/Preview/GIF.php',
'OC\\Preview\\Generator' => __DIR__ . '/../../..' . '/lib/private/Preview/Generator.php',
Expand All @@ -1161,6 +1162,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Preview\\Illustrator' => __DIR__ . '/../../..' . '/lib/private/Preview/Illustrator.php',
'OC\\Preview\\Image' => __DIR__ . '/../../..' . '/lib/private/Preview/Image.php',
'OC\\Preview\\JPEG' => __DIR__ . '/../../..' . '/lib/private/Preview/JPEG.php',
'OC\\Preview\\Krita' => __DIR__ . '/../../..' . '/lib/private/Preview/Krita.php',
'OC\\Preview\\MP3' => __DIR__ . '/../../..' . '/lib/private/Preview/MP3.php',
'OC\\Preview\\MSOffice2003' => __DIR__ . '/../../..' . '/lib/private/Preview/MSOffice2003.php',
'OC\\Preview\\MSOffice2007' => __DIR__ . '/../../..' . '/lib/private/Preview/MSOffice2007.php',
Expand Down
56 changes: 56 additions & 0 deletions lib/private/Preview/Bundled.php
Original file line number Diff line number Diff line change
@@ -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');
juliushaertl marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}

}
52 changes: 52 additions & 0 deletions lib/private/Preview/Krita.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
17 changes: 16 additions & 1 deletion lib/private/Preview/OpenDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
6 changes: 5 additions & 1 deletion lib/private/PreviewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,15 @@ protected function getEnabledDefaultProvider() {
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)) {
Expand Down Expand Up @@ -339,7 +341,9 @@ protected function registerCoreProviders() {
$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')) {
Expand Down