Skip to content

Commit 3e8e8be

Browse files
kesselbzak39
authored andcommitted
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. nextcloud#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 <mail@danielkesselberg.de>
1 parent 8cb3d4b commit 3e8e8be

2 files changed

Lines changed: 43 additions & 37 deletions

File tree

lib/private/Preview/Office.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
use OCP\IImage;
3434
use OCP\ITempManager;
3535
use OCP\Server;
36-
use Psr\Log\LoggerInterface;
3736

3837
abstract class Office extends ProviderV2 {
3938
/**
@@ -93,30 +92,18 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
9392
return null;
9493
}
9594

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-
}
95+
$preview = $outdir . pathinfo($absPath, PATHINFO_FILENAME) . '.png';
10996

11097
$image = new \OCP\Image();
111-
$image->loadFromData((string) $png);
98+
$image->loadFromFile($preview);
11299

113100
$this->cleanTmpFiles();
114101

115102
if ($image->valid()) {
116103
$image->scaleDownToFit($maxX, $maxY);
117-
118104
return $image;
119105
}
106+
120107
return null;
121108
}
122109
}

lib/private/PreviewManager.php

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ protected function registerCoreProviders() {
366366
$this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/');
367367
$this->registerCoreProvider(Preview\Imaginary::class, Preview\Imaginary::supportedMimeTypes());
368368

369-
// SVG, Office and Bitmap require imagick
369+
// SVG and Bitmap require imagick
370370
if ($this->imagickSupport->hasExtension()) {
371371
$imagickProviders = [
372372
'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class],
@@ -391,28 +391,10 @@ protected function registerCoreProviders() {
391391
$this->registerCoreProvider($class, $provider['mimetype']);
392392
}
393393
}
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-
}
414394
}
415395

396+
$this->registerCoreProvidersOffice();
397+
416398
// Video requires avconv or ffmpeg
417399
if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) {
418400
$movieBinary = $this->config->getSystemValue('preview_ffmpeg_path', null);
@@ -430,6 +412,43 @@ protected function registerCoreProviders() {
430412
}
431413
}
432414

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

0 commit comments

Comments
 (0)