aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/PreviewManager.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/PreviewManager.php')
-rw-r--r--lib/private/PreviewManager.php202
1 files changed, 103 insertions, 99 deletions
diff --git a/lib/private/PreviewManager.php b/lib/private/PreviewManager.php
index 367f0c1c057..0bb0280406c 100644
--- a/lib/private/PreviewManager.php
+++ b/lib/private/PreviewManager.php
@@ -1,38 +1,16 @@
<?php
+
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author John Molakvoæ <skjnldsv@protonmail.com>
- * @author Julius Härtl <jus@bitgrid.net>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Olivier Paroz <github@oparoz.com>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Sebastian Steinmetz <462714+steiny2k@users.noreply.github.com>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * 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, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OC;
use OC\AppFramework\Bootstrap\Coordinator;
use OC\Preview\Generator;
use OC\Preview\GeneratorHelper;
+use OC\Preview\IMagickSupport;
use OCP\AppFramework\QueryException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\File;
@@ -43,9 +21,10 @@ use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IBinaryFinder;
use OCP\IConfig;
use OCP\IPreview;
-use OCP\IServerContainer;
use OCP\Preview\IProviderV2;
-use Symfony\Component\EventDispatcher\EventDispatcherInterface;
+use Psr\Container\ContainerInterface;
+use Psr\Log\LoggerInterface;
+
use function array_key_exists;
class PreviewManager implements IPreview {
@@ -53,7 +32,6 @@ class PreviewManager implements IPreview {
protected IRootFolder $rootFolder;
protected IAppData $appData;
protected IEventDispatcher $eventDispatcher;
- protected EventDispatcherInterface $legacyEventDispatcher;
private ?Generator $generator = null;
private GeneratorHelper $helper;
protected bool $providerListDirty = false;
@@ -71,31 +49,34 @@ class PreviewManager implements IPreview {
* @psalm-var array<string, null>
*/
private array $loadedBootstrapProviders = [];
- private IServerContainer $container;
+ private ContainerInterface $container;
private IBinaryFinder $binaryFinder;
+ private IMagickSupport $imagickSupport;
+ private bool $enablePreviews;
public function __construct(
- IConfig $config,
- IRootFolder $rootFolder,
- IAppData $appData,
- IEventDispatcher $eventDispatcher,
- EventDispatcherInterface $legacyEventDispatcher,
- GeneratorHelper $helper,
- ?string $userId,
- Coordinator $bootstrapCoordinator,
- IServerContainer $container,
- IBinaryFinder $binaryFinder
+ IConfig $config,
+ IRootFolder $rootFolder,
+ IAppData $appData,
+ IEventDispatcher $eventDispatcher,
+ GeneratorHelper $helper,
+ ?string $userId,
+ Coordinator $bootstrapCoordinator,
+ ContainerInterface $container,
+ IBinaryFinder $binaryFinder,
+ IMagickSupport $imagickSupport,
) {
$this->config = $config;
$this->rootFolder = $rootFolder;
$this->appData = $appData;
$this->eventDispatcher = $eventDispatcher;
- $this->legacyEventDispatcher = $legacyEventDispatcher;
$this->helper = $helper;
$this->userId = $userId;
$this->bootstrapCoordinator = $bootstrapCoordinator;
$this->container = $container;
$this->binaryFinder = $binaryFinder;
+ $this->imagickSupport = $imagickSupport;
+ $this->enablePreviews = $config->getSystemValueBool('enable_previews', true);
}
/**
@@ -109,7 +90,7 @@ class PreviewManager implements IPreview {
* @return void
*/
public function registerProvider($mimeTypeRegex, \Closure $callable): void {
- if (!$this->config->getSystemValue('enable_previews', true)) {
+ if (!$this->enablePreviews) {
return;
}
@@ -124,7 +105,7 @@ class PreviewManager implements IPreview {
* Get all providers
*/
public function getProviders(): array {
- if (!$this->config->getSystemValue('enable_previews', true)) {
+ if (!$this->enablePreviews) {
return [];
}
@@ -157,35 +138,27 @@ class PreviewManager implements IPreview {
$this->rootFolder,
$this->config
),
- $this->legacyEventDispatcher,
- $this->eventDispatcher
+ $this->eventDispatcher,
+ $this->container->get(LoggerInterface::class),
);
}
return $this->generator;
}
- /**
- * Returns a preview of a file
- *
- * The cache is searched first and if nothing usable was found then a preview is
- * generated by one of the providers
- *
- * @param File $file
- * @param int $width
- * @param int $height
- * @param bool $crop
- * @param string $mode
- * @param string $mimeType
- * @return ISimpleFile
- * @throws NotFoundException
- * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid)
- * @since 11.0.0 - \InvalidArgumentException was added in 12.0.0
- */
- public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) {
+ public function getPreview(
+ File $file,
+ $width = -1,
+ $height = -1,
+ $crop = false,
+ $mode = IPreview::MODE_FILL,
+ $mimeType = null,
+ bool $cacheResult = true,
+ ): ISimpleFile {
+ $this->throwIfPreviewsDisabled($file, $mimeType);
$previewConcurrency = $this->getGenerator()->getNumConcurrentPreviews('preview_concurrency_all');
$sem = Generator::guardWithSemaphore(Generator::SEMAPHORE_ID_ALL, $previewConcurrency);
try {
- $preview = $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType);
+ $preview = $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType, $cacheResult);
} finally {
Generator::unguardWithSemaphore($sem);
}
@@ -205,6 +178,7 @@ class PreviewManager implements IPreview {
* @since 19.0.0
*/
public function generatePreviews(File $file, array $specifications, $mimeType = null) {
+ $this->throwIfPreviewsDisabled($file, $mimeType);
return $this->getGenerator()->generatePreviews($file, $specifications, $mimeType);
}
@@ -215,7 +189,7 @@ class PreviewManager implements IPreview {
* @return boolean
*/
public function isMimeSupported($mimeType = '*') {
- if (!$this->config->getSystemValue('enable_previews', true)) {
+ if (!$this->enablePreviews) {
return false;
}
@@ -239,13 +213,15 @@ class PreviewManager implements IPreview {
/**
* Check if a preview can be generated for a file
*/
- public function isAvailable(\OCP\Files\FileInfo $file): bool {
- if (!$this->config->getSystemValue('enable_previews', true)) {
+ public function isAvailable(\OCP\Files\FileInfo $file, ?string $mimeType = null): bool {
+ if (!$this->enablePreviews) {
return false;
}
+ $fileMimeType = $mimeType ?? $file->getMimeType();
+
$this->registerCoreProviders();
- if (!$this->isMimeSupported($file->getMimetype())) {
+ if (!$this->isMimeSupported($fileMimeType)) {
return false;
}
@@ -255,7 +231,7 @@ class PreviewManager implements IPreview {
}
foreach ($this->providers as $supportedMimeType => $providers) {
- if (preg_match($supportedMimeType, $file->getMimetype())) {
+ if (preg_match($supportedMimeType, $fileMimeType)) {
foreach ($providers as $providerClosure) {
$provider = $this->helper->getProvider($providerClosure);
if (!($provider instanceof IProviderV2)) {
@@ -363,14 +339,13 @@ class PreviewManager implements IPreview {
$this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/');
$this->registerCoreProvider(Preview\WebP::class, '/image\/webp/');
$this->registerCoreProvider(Preview\Krita::class, '/application\/x-krita/');
- $this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg/');
+ $this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg$/');
$this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/');
$this->registerCoreProvider(Preview\Imaginary::class, Preview\Imaginary::supportedMimeTypes());
+ $this->registerCoreProvider(Preview\ImaginaryPDF::class, Preview\ImaginaryPDF::supportedMimeTypes());
- // SVG, Office and Bitmap require imagick
- if (extension_loaded('imagick')) {
- $checkImagick = new \Imagick();
-
+ // SVG and Bitmap require imagick
+ if ($this->imagickSupport->hasExtension()) {
$imagickProviders = [
'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class],
'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => Preview\TIFF::class],
@@ -379,9 +354,9 @@ class PreviewManager implements IPreview {
'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => Preview\Photoshop::class],
'EPS' => ['mimetype' => '/application\/postscript/', 'class' => Preview\Postscript::class],
'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => Preview\Font::class],
- 'HEIC' => ['mimetype' => '/image\/hei(f|c)/', 'class' => Preview\HEIC::class],
- 'TGA' => ['mimetype' => '/image\/t(ar)?ga/', 'class' => Preview\TGA::class],
- 'SGI' => ['mimetype' => '/image\/sgi/', 'class' => Preview\SGI::class],
+ 'HEIC' => ['mimetype' => '/image\/(x-)?hei(f|c)/', 'class' => Preview\HEIC::class],
+ 'TGA' => ['mimetype' => '/image\/(x-)?t(ar)?ga/', 'class' => Preview\TGA::class],
+ 'SGI' => ['mimetype' => '/image\/(x-)?sgi/', 'class' => Preview\SGI::class],
];
foreach ($imagickProviders as $queryFormat => $provider) {
@@ -390,31 +365,14 @@ class PreviewManager implements IPreview {
continue;
}
- if (count($checkImagick->queryFormats($queryFormat)) === 1) {
+ if ($this->imagickSupport->supportsFormat($queryFormat)) {
$this->registerCoreProvider($class, $provider['mimetype']);
}
}
-
- if (count($checkImagick->queryFormats('PDF')) === 1) {
- // 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);
@@ -427,7 +385,44 @@ class PreviewManager implements IPreview {
if (is_string($movieBinary)) {
- $this->registerCoreProvider(Preview\Movie::class, '/video\/.*/', ["movieBinary" => $movieBinary]);
+ $this->registerCoreProvider(Preview\Movie::class, '/video\/.*/', ['movieBinary' => $movieBinary]);
+ }
+ }
+ }
+
+ 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]);
}
}
}
@@ -458,4 +453,13 @@ class PreviewManager implements IPreview {
});
}
}
+
+ /**
+ * @throws NotFoundException if preview generation is disabled
+ */
+ private function throwIfPreviewsDisabled(File $file, ?string $mimeType = null): void {
+ if (!$this->isAvailable($file, $mimeType)) {
+ throw new NotFoundException('Previews disabled');
+ }
+ }
}