Skip to content

Commit 3d4db58

Browse files
committed
fix: use png as preview right away
The initial office preview implementation converted an office document with LibreOffice to PDF, used ImageMagick to extract the first page as JPEG, and passed it OC_Image. #10198 changed the implementation to use PNG rather than PDF. OC_Image can use a PNG as a preview right away, so the ImageMagick step is unnecessary. The registration code was updated to not ask ImageMagick if PDF is supported, as PDFs are no longer used to create office document previews. Signed-off-by: Daniel Kesselberg <[email protected]>
1 parent b5241d5 commit 3d4db58

File tree

2 files changed

+49
-36
lines changed

2 files changed

+49
-36
lines changed

lib/private/Preview/Office.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,30 +93,18 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
9393
return null;
9494
}
9595

96-
try {
97-
$filename = $outdir . pathinfo($absPath, PATHINFO_FILENAME) . '.png';
98-
99-
$png = new \Imagick($filename . '[0]');
100-
$png->setImageFormat('jpg');
101-
} catch (\Exception $e) {
102-
$this->cleanTmpFiles();
103-
\OC::$server->get(LoggerInterface::class)->error($e->getMessage(), [
104-
'exception' => $e,
105-
'app' => 'core',
106-
]);
107-
return null;
108-
}
96+
$preview = $outdir . pathinfo($absPath, PATHINFO_FILENAME) . '.png';
10997

11098
$image = new \OCP\Image();
111-
$image->loadFromData((string) $png);
99+
$image->loadFromFile($preview);
112100

113101
$this->cleanTmpFiles();
114102

115103
if ($image->valid()) {
116104
$image->scaleDownToFit($maxX, $maxY);
117-
118105
return $image;
119106
}
107+
120108
return null;
121109
}
122110
}

lib/private/PreviewManager.php

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,15 @@
3131
namespace OC;
3232

3333
use OC\AppFramework\Bootstrap\Coordinator;
34+
use OC\Preview\EMF;
3435
use OC\Preview\Generator;
3536
use OC\Preview\GeneratorHelper;
3637
use OC\Preview\IMagickSupport;
38+
use OC\Preview\MSOffice2003;
39+
use OC\Preview\MSOffice2007;
40+
use OC\Preview\MSOfficeDoc;
41+
use OC\Preview\OpenDocument;
42+
use OC\Preview\StarOffice;
3743
use OCP\AppFramework\QueryException;
3844
use OCP\EventDispatcher\IEventDispatcher;
3945
use OCP\Files\File;
@@ -366,7 +372,7 @@ protected function registerCoreProviders() {
366372
$this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/');
367373
$this->registerCoreProvider(Preview\Imaginary::class, Preview\Imaginary::supportedMimeTypes());
368374

369-
// SVG, Office and Bitmap require imagick
375+
// SVG and Bitmap require imagick
370376
if ($this->imagickSupport->hasExtension()) {
371377
$imagickProviders = [
372378
'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class],
@@ -391,28 +397,10 @@ protected function registerCoreProviders() {
391397
$this->registerCoreProvider($class, $provider['mimetype']);
392398
}
393399
}
394-
395-
if ($this->imagickSupport->supportsFormat('PDF')) {
396-
// Office requires openoffice or libreoffice
397-
$officeBinary = $this->config->getSystemValue('preview_libreoffice_path', null);
398-
if (!is_string($officeBinary)) {
399-
$officeBinary = $this->binaryFinder->findBinaryPath('libreoffice');
400-
}
401-
if (!is_string($officeBinary)) {
402-
$officeBinary = $this->binaryFinder->findBinaryPath('openoffice');
403-
}
404-
405-
if (is_string($officeBinary)) {
406-
$this->registerCoreProvider(Preview\MSOfficeDoc::class, '/application\/msword/', ["officeBinary" => $officeBinary]);
407-
$this->registerCoreProvider(Preview\MSOffice2003::class, '/application\/vnd.ms-.*/', ["officeBinary" => $officeBinary]);
408-
$this->registerCoreProvider(Preview\MSOffice2007::class, '/application\/vnd.openxmlformats-officedocument.*/', ["officeBinary" => $officeBinary]);
409-
$this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/', ["officeBinary" => $officeBinary]);
410-
$this->registerCoreProvider(Preview\StarOffice::class, '/application\/vnd.sun.xml.*/', ["officeBinary" => $officeBinary]);
411-
$this->registerCoreProvider(Preview\EMF::class, '/image\/emf/', ['officeBinary' => $officeBinary]);
412-
}
413-
}
414400
}
415401

402+
$this->registerCoreProvidersOffice();
403+
416404
// Video requires avconv or ffmpeg
417405
if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) {
418406
$movieBinary = $this->config->getSystemValue('preview_ffmpeg_path', null);
@@ -430,6 +418,43 @@ protected function registerCoreProviders() {
430418
}
431419
}
432420

421+
private function registerCoreProvidersOffice(): void {
422+
$officeProviders = [
423+
['mimetype' => '/application\/msword/', 'class' => Preview\MSOfficeDoc::class],
424+
['mimetype' => '/application\/vnd.ms-.*/', 'class' => Preview\MSOffice2003::class],
425+
['mimetype' => '/application\/vnd.openxmlformats-officedocument.*/', 'class' => Preview\MSOffice2007::class],
426+
['mimetype' => '/application\/vnd.oasis.opendocument.*/', 'class' => Preview\OpenDocument::class],
427+
['mimetype' => '/application\/vnd.sun.xml.*/', 'class' => Preview\StarOffice::class],
428+
['mimetype' => '/image\/emf/', 'class' => Preview\EMF::class],
429+
];
430+
431+
$findBinary = true;
432+
$officeBinary = false;
433+
434+
foreach ($officeProviders as $provider) {
435+
$class = $provider['class'];
436+
if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
437+
continue;
438+
}
439+
440+
if ($findBinary) {
441+
// Office requires openoffice or libreoffice
442+
$officeBinary = $this->config->getSystemValue('preview_libreoffice_path', false);
443+
if ($officeBinary === false) {
444+
$officeBinary = $this->binaryFinder->findBinaryPath('libreoffice');
445+
}
446+
if ($officeBinary === false) {
447+
$officeBinary = $this->binaryFinder->findBinaryPath('openoffice');
448+
}
449+
$findBinary = false;
450+
}
451+
452+
if ($officeBinary) {
453+
$this->registerCoreProvider($class, $provider['mimetype'], ['officeBinary' => $officeBinary]);
454+
}
455+
}
456+
}
457+
433458
private function registerBootstrapProviders(): void {
434459
$context = $this->bootstrapCoordinator->getRegistrationContext();
435460

0 commit comments

Comments
 (0)