Merge pull request #41395 from nextcloud/hello-emf

feat: add preview provider for emf files based on office
pull/41555/head
Christoph Wurst 7 months ago committed by GitHub
commit c9dc377ebc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1237,14 +1237,6 @@ $CONFIG = [
* Defaults to ``''`` (empty string)
*/
'preview_libreoffice_path' => '/usr/bin/libreoffice',
/**
* Use this if LibreOffice/OpenOffice requires additional arguments.
*
* Defaults to ``''`` (empty string)
*/
'preview_office_cl_parameters' =>
' --headless --nologo --nofirststartwizard --invisible --norestore '.
'--convert-to png --outdir ',
/**
* custom path for ffmpeg binary
@ -1285,6 +1277,7 @@ $CONFIG = [
* - ``OC\Preview\StarOffice``
* - ``OC\Preview\SVG``
* - ``OC\Preview\TIFF``
* - ``OC\Preview\EMF``
*
*
* Defaults to the following providers:
@ -1945,7 +1938,7 @@ $CONFIG = [
*
* Example for windows systems: ``array('?', '<', '>', ':', '*', '|', '"', chr(0), "\n", "\r")``
* see https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits
*
*
* Defaults to ``array()``
*/
'forbidden_chars' => [],

@ -113,7 +113,8 @@ OC.MimeTypeList={
"application/vnd.xmind.workbook": "mindmap",
"image/targa": "image/tga",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.oform": "x-office/form",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.docxf": "x-office/form-template"
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.docxf": "x-office/form-template",
"image/x-emf": "image/emf"
},
files: [
"application",

@ -1540,6 +1540,7 @@ return array(
'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\\EMF' => $baseDir . '/lib/private/Preview/EMF.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',

@ -1573,6 +1573,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'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\\EMF' => __DIR__ . '/../../..' . '/lib/private/Preview/EMF.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',

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
/**
* @copyright 2023 Daniel Kesselberg <mail@danielkesselberg.de>
*
* @author Daniel Kesselberg <mail@danielkesselberg.de>
*
* @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;
class EMF extends Office {
public function getMimeType(): string {
return '/image\/emf/';
}
}

@ -31,7 +31,8 @@ namespace OC\Preview;
use OCP\Files\File;
use OCP\Files\FileInfo;
use OCP\IImage;
use Psr\Log\LoggerInterface;
use OCP\ITempManager;
use OCP\Server;
abstract class Office extends ProviderV2 {
/**
@ -49,51 +50,60 @@ abstract class Office extends ProviderV2 {
return null;
}
$absPath = $this->getLocalFile($file);
$tmpDir = \OC::$server->getTempManager()->getTempBaseDir();
$tempManager = Server::get(ITempManager::class);
$defaultParameters = ' -env:UserInstallation=file://' . escapeshellarg($tmpDir . '/owncloud-' . \OC_Util::getInstanceId() . '/') . ' --headless --nologo --nofirststartwizard --invisible --norestore --convert-to png --outdir ';
$clParameters = \OC::$server->getConfig()->getSystemValue('preview_office_cl_parameters', $defaultParameters);
// The file to generate the preview for.
$absPath = $this->getLocalFile($file);
$cmd = $this->options['officeBinary'] . $clParameters . escapeshellarg($tmpDir) . ' ' . escapeshellarg($absPath);
// The destination for the LibreOffice user profile.
// LibreOffice can rune once per user profile and therefore instance id and file id are included.
$profile = $tempManager->getTemporaryFolder(
'nextcloud-office-profile-' . \OC_Util::getInstanceId() . '-' . $file->getId()
);
exec($cmd, $output, $returnCode);
// The destination for the LibreOffice convert result.
$outdir = $tempManager->getTemporaryFolder(
'nextcloud-office-preview-' . \OC_Util::getInstanceId() . '-' . $file->getId()
);
if ($returnCode !== 0) {
if ($profile === false || $outdir === false) {
$this->cleanTmpFiles();
return null;
}
//create imagick object from png
$pngPreview = null;
try {
[$dirname, , , $filename] = array_values(pathinfo($absPath));
$pngPreview = $tmpDir . '/' . $filename . '.png';
$parameters = [
$this->options['officeBinary'],
'-env:UserInstallation=file://' . escapeshellarg($profile),
'--headless',
'--nologo',
'--nofirststartwizard',
'--invisible',
'--norestore',
'--convert-to png',
'--outdir ' . escapeshellarg($outdir),
escapeshellarg($absPath),
];
$png = new \Imagick($pngPreview . '[0]');
$png->setImageFormat('jpg');
} catch (\Exception $e) {
$cmd = implode(' ', $parameters);
exec($cmd, $output, $returnCode);
if ($returnCode !== 0) {
$this->cleanTmpFiles();
unlink($pngPreview);
\OC::$server->get(LoggerInterface::class)->error($e->getMessage(), [
'exception' => $e,
'app' => 'core',
]);
return null;
}
$preview = $outdir . pathinfo($absPath, PATHINFO_FILENAME) . '.png';
$image = new \OCP\Image();
$image->loadFromData((string) $png);
$image->loadFromFile($preview);
$this->cleanTmpFiles();
unlink($pngPreview);
if ($image->valid()) {
$image->scaleDownToFit($maxX, $maxY);
return $image;
}
return null;
}
}

@ -366,7 +366,7 @@ class PreviewManager implements IPreview {
$this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/');
$this->registerCoreProvider(Preview\Imaginary::class, Preview\Imaginary::supportedMimeTypes());
// SVG, Office and Bitmap require imagick
// SVG and Bitmap require imagick
if ($this->imagickSupport->hasExtension()) {
$imagickProviders = [
'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class],
@ -391,27 +391,10 @@ class PreviewManager implements IPreview {
$this->registerCoreProvider($class, $provider['mimetype']);
}
}
if ($this->imagickSupport->supportsFormat('PDF')) {
// Office requires openoffice or libreoffice
$officeBinary = $this->config->getSystemValue('preview_libreoffice_path', null);
if (!is_string($officeBinary)) {
$officeBinary = $this->binaryFinder->findBinaryPath('libreoffice');
}
if (!is_string($officeBinary)) {
$officeBinary = $this->binaryFinder->findBinaryPath('openoffice');
}
if (is_string($officeBinary)) {
$this->registerCoreProvider(Preview\MSOfficeDoc::class, '/application\/msword/', ["officeBinary" => $officeBinary]);
$this->registerCoreProvider(Preview\MSOffice2003::class, '/application\/vnd.ms-.*/', ["officeBinary" => $officeBinary]);
$this->registerCoreProvider(Preview\MSOffice2007::class, '/application\/vnd.openxmlformats-officedocument.*/', ["officeBinary" => $officeBinary]);
$this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/', ["officeBinary" => $officeBinary]);
$this->registerCoreProvider(Preview\StarOffice::class, '/application\/vnd.sun.xml.*/', ["officeBinary" => $officeBinary]);
}
}
}
$this->registerCoreProvidersOffice();
// Video requires avconv or ffmpeg
if (in_array(Preview\Movie::class, $this->getEnabledDefaultProvider())) {
$movieBinary = $this->config->getSystemValue('preview_ffmpeg_path', null);
@ -429,6 +412,43 @@ class PreviewManager implements IPreview {
}
}
private function registerCoreProvidersOffice(): void {
$officeProviders = [
['mimetype' => '/application\/msword/', 'class' => Preview\MSOfficeDoc::class],
['mimetype' => '/application\/vnd.ms-.*/', 'class' => Preview\MSOffice2003::class],
['mimetype' => '/application\/vnd.openxmlformats-officedocument.*/', 'class' => Preview\MSOffice2007::class],
['mimetype' => '/application\/vnd.oasis.opendocument.*/', 'class' => Preview\OpenDocument::class],
['mimetype' => '/application\/vnd.sun.xml.*/', 'class' => Preview\StarOffice::class],
['mimetype' => '/image\/emf/', 'class' => Preview\EMF::class],
];
$findBinary = true;
$officeBinary = false;
foreach ($officeProviders as $provider) {
$class = $provider['class'];
if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) {
continue;
}
if ($findBinary) {
// Office requires openoffice or libreoffice
$officeBinary = $this->config->getSystemValue('preview_libreoffice_path', false);
if ($officeBinary === false) {
$officeBinary = $this->binaryFinder->findBinaryPath('libreoffice');
}
if ($officeBinary === false) {
$officeBinary = $this->binaryFinder->findBinaryPath('openoffice');
}
$findBinary = false;
}
if ($officeBinary) {
$this->registerCoreProvider($class, $provider['mimetype'], ['officeBinary' => $officeBinary]);
}
}
}
private function registerBootstrapProviders(): void {
$context = $this->bootstrapCoordinator->getRegistrationContext();

@ -229,6 +229,13 @@ class RepairMimeTypes implements IRepairStep {
return $this->updateMimetypes($updatedMimetypes);
}
private function introduceEnhancedMetafileFormatType() {
$updatedMimetypes = [
'emf' => 'image/emf',
];
return $this->updateMimetypes($updatedMimetypes);
}
/**
* Fix mime types
@ -286,5 +293,9 @@ class RepairMimeTypes implements IRepairStep {
if (version_compare($ocVersionFromBeforeUpdate, '26.0.0.1', '<') && $this->introduceAsciidocType()) {
$out->info('Fixed AsciiDoc mime types');
}
if (version_compare($ocVersionFromBeforeUpdate, '28.0.0.5', '<') && $this->introduceEnhancedMetafileFormatType()) {
$out->info('Fixed Enhanced Metafile Format mime types');
}
}
}

@ -114,5 +114,6 @@
"application/vnd.xmind.workbook": "mindmap",
"image/targa": "image/tga",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.oform": "x-office/form",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.docxf": "x-office/form-template"
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.docxf": "x-office/form-template",
"image/x-emf": "image/emf"
}

@ -51,6 +51,7 @@
"dot": ["application/msword"],
"dotx": ["application/vnd.openxmlformats-officedocument.wordprocessingml.template"],
"dv": ["video/dv"],
"emf": ["image/emf"],
"eot": ["application/vnd.ms-fontobject"],
"eps": ["application/postscript"],
"epub": ["application/epub+zip"],

@ -115,6 +115,7 @@ OC.MimeTypeList={
"image/targa": "image/tga",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.oform": "x-office/form",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.docxf": "x-office/form-template",
"image/x-emf": "image/emf",
"my-custom/mimetype": "custom"
},
files: [

@ -1,7 +1,7 @@
{
"hashes": {
"core\/js\/mimetypelist.js": "2a6ab9af5353d464c0dd3bd651cfc13ba35009463949bc45bd8f7073828fa34fbdac7a81ba2b188e7e0c97fc63c2167825b069f61341e507ca33213b1e6e4593"
"core\/js\/mimetypelist.js": "e9e515c5eeb08b02971e5943640bef89b6d1c2d746d5883bf974c6ac753f810685c7a20bf0ed4d32d8c046cd2cef6c045682c7d89c6a3b7b6c33122f5fd2088f"
},
"signature": "A5JmjvGiWC9iDaDL0\/wzIBx1ovV6xW5pL134ZtzcFh6dCRoYVZFZvInb98zz4Js3Y4R+uAv4XYKFgclG9BXjoa+q7SmN4qV95o870OsX3MEAaPRHEJv7V6P19hinwC0\/c6XUtMUlTn2IqdoacmELV3v+vmXiU0bYpNfqkYDjt1mCfl3EWB+uMCn+W849k1hoRc\/nI3JuDzl3VtP4G6zzJ3NexsXJOHOb6\/GyGVnkOltN3Ep5wBqXtr28LLLWWYbgxUEQ5ZNBd98PCpRm3r\/3eGreREccUzh+Kfo1XK+Rbnf8U6z3DXOwZK4cP\/CJBAhtlUDyw+TY58jcOSLxF2I61jqY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