From 6da08e6eb55ff1d4aef2e4bf1637cbe951fbd9e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Wed, 21 Feb 2024 13:24:10 +0100 Subject: [PATCH] chore: Move to preview provider v2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/AppInfo/Application.php | 42 +++++------------------------ lib/Preview/EMF.php | 5 ++-- lib/Preview/MSExcel.php | 5 ++-- lib/Preview/MSWord.php | 5 ++-- lib/Preview/OOXML.php | 6 +++-- lib/Preview/Office.php | 37 +++++++++++-------------- lib/Preview/OpenDocument.php | 5 ++-- lib/Preview/Pdf.php | 5 ++-- tests/psalm-baseline.xml | 52 +++++++----------------------------- 9 files changed, 52 insertions(+), 110 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index c4ce0361d8..831e56b78c 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -61,7 +61,6 @@ use OCP\Files\Template\TemplateFileCreator; use OCP\IConfig; use OCP\IL10N; -use OCP\IPreview; use OCP\Preview\BeforePreviewFetchedEvent; use OCP\Security\CSP\AddContentSecurityPolicyEvent; use OCP\Security\FeaturePolicy\AddFeaturePolicyEvent; @@ -92,6 +91,13 @@ public function register(IRegistrationContext $context): void { 'getPathForToken', 'getWopiForToken', ]); + + $context->registerPreviewProvider(EMF::class, EMF::MIMETYPE_REGEX); + $context->registerPreviewProvider(MSExcel::class, MSExcel::MIMETYPE_REGEX); + $context->registerPreviewProvider(MSWord::class, MSWord::MIMETYPE_REGEX); + $context->registerPreviewProvider(OOXML::class, OOXML::MIMETYPE_REGEX); + $context->registerPreviewProvider(OpenDocument::class, OpenDocument::MIMETYPE_REGEX); + $context->registerPreviewProvider(Pdf::class, Pdf::MIMETYPE_REGEX); } public function boot(IBootContext $context): void { @@ -153,43 +159,9 @@ public function boot(IBootContext $context): void { }); }); - $this->registerProvider(); $this->checkAndEnableCODEServer(); } - public function registerProvider() { - $container = $this->getContainer(); - - /** @var IPreview $previewManager */ - $previewManager = $container->get(IPreview::class); - - $previewManager->registerProvider('/application\/vnd.ms-excel/', function () use ($container) { - return $container->get(MSExcel::class); - }); - - $previewManager->registerProvider('/application\/msword/', function () use ($container) { - return $container->get(MSWord::class); - }); - - $previewManager->registerProvider('/application\/vnd.openxmlformats-officedocument.*/', function () use ($container) { - return $container->get(OOXML::class); - }); - - $previewManager->registerProvider('/application\/vnd.oasis.opendocument.*/', function () use ($container) { - return $container->get(OpenDocument::class); - }); - - $previewManager->registerProvider('/image\/emf/', function () use ($container) { - return $container->get(EMF::class); - }); - - if (!$previewManager->isMimeSupported('application/pdf')) { - $previewManager->registerProvider('/application\/pdf/', function () use ($container) { - return $container->get(Pdf::class); - }); - } - } - public function checkAndEnableCODEServer() { // Supported only on Linux OS, and x86_64 & ARM64 platforms $supportedArchs = ['x86_64', 'aarch64']; diff --git a/lib/Preview/EMF.php b/lib/Preview/EMF.php index 2ecb0a12f2..ded3db5ca1 100644 --- a/lib/Preview/EMF.php +++ b/lib/Preview/EMF.php @@ -26,10 +26,11 @@ namespace OCA\Richdocuments\Preview; class EMF extends Office { + public const MIMETYPE_REGEX = '/image\/emf/'; /** * {@inheritDoc} */ - public function getMimeType() { - return '/image\/emf/'; + public function getMimeType(): string { + return self::MIMETYPE_REGEX; } } diff --git a/lib/Preview/MSExcel.php b/lib/Preview/MSExcel.php index 1d0a080195..4e13be501e 100644 --- a/lib/Preview/MSExcel.php +++ b/lib/Preview/MSExcel.php @@ -22,10 +22,11 @@ namespace OCA\Richdocuments\Preview; class MSExcel extends Office { + public const MIMETYPE_REGEX = '/application\/vnd.ms-excel/'; /** * {@inheritDoc} */ - public function getMimeType() { - return '/application\/vnd.ms-excel/'; + public function getMimeType(): string { + return self::MIMETYPE_REGEX; } } diff --git a/lib/Preview/MSWord.php b/lib/Preview/MSWord.php index f56ee18b47..a3f701854b 100644 --- a/lib/Preview/MSWord.php +++ b/lib/Preview/MSWord.php @@ -22,10 +22,11 @@ namespace OCA\Richdocuments\Preview; class MSWord extends Office { + public const MIMETYPE_REGEX = '/application\/msword/'; /** * {@inheritDoc} */ - public function getMimeType() { - return '/application\/msword/'; + public function getMimeType(): string { + return self::MIMETYPE_REGEX; } } diff --git a/lib/Preview/OOXML.php b/lib/Preview/OOXML.php index 3052c3f82c..3e1ec2c430 100644 --- a/lib/Preview/OOXML.php +++ b/lib/Preview/OOXML.php @@ -22,10 +22,12 @@ namespace OCA\Richdocuments\Preview; class OOXML extends Office { + public const MIMETYPE_REGEX = '/application\/vnd.openxmlformats-officedocument.*/'; + /** * {@inheritDoc} */ - public function getMimeType() { - return '/application\/vnd.openxmlformats-officedocument.*/'; + public function getMimeType(): string { + return self::MIMETYPE_REGEX; } } diff --git a/lib/Preview/Office.php b/lib/Preview/Office.php index e87c04795b..daaa30328d 100644 --- a/lib/Preview/Office.php +++ b/lib/Preview/Office.php @@ -21,27 +21,23 @@ */ namespace OCA\Richdocuments\Preview; -use OC\Preview\Provider; use OCA\Richdocuments\AppConfig; use OCA\Richdocuments\Capabilities; +use OCP\Files\File; use OCP\Http\Client\IClientService; -use OCP\IConfig; -use OCP\ILogger; +use OCP\IImage; use OCP\Image; +use OCP\Preview\IProviderV2; use Psr\Log\LoggerInterface; -abstract class Office extends Provider { +abstract class Office implements IProviderV2 { + private array $capabilities = []; + public function __construct(private IClientService $clientService, private AppConfig $config, Capabilities $capabilities, private LoggerInterface $logger) { - parent::__construct(); - $this->capabilitites = $capabilities->getCapabilities()['richdocuments'] ?? []; } - private function getWopiURL() { - return $this->config->getAppValue('richdocuments', 'wopi_url'); - } - - public function isAvailable(\OCP\Files\FileInfo $file) { + public function isAvailable(\OCP\Files\FileInfo $file): bool { if (isset($this->capabilitites['collabora']['convert-to']['available'])) { return (bool)$this->capabilitites['collabora']['convert-to']['available']; } @@ -51,18 +47,17 @@ public function isAvailable(\OCP\Files\FileInfo $file) { /** * {@inheritDoc} */ - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { - $fileInfo = $fileview->getFileInfo($path); - if (!$fileInfo || $fileInfo->getSize() === 0) { - return false; + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { + if ($file->getSize() === 0) { + return null; } - $useTempFile = $fileInfo->isEncrypted() || !$fileInfo->getStorage()->isLocal(); + $useTempFile = $file->isEncrypted() || !$file->getStorage()->isLocal(); if ($useTempFile) { - $fileName = $fileview->toTmpFile($path); + $fileName = $file->getStorage()->getLocalFile($file->getInternalPath()); $stream = fopen($fileName, 'r'); } else { - $stream = $fileview->fopen($path, 'r'); + $stream = $file->fopen('r'); } $client = $this->clientService->newClient(); @@ -76,13 +71,13 @@ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { $options['verify'] = false; } - $options['multipart'] = [['name' => $path, 'contents' => $stream]]; + $options['multipart'] = [['name' => $file->getName(), 'contents' => $stream]]; try { $response = $client->post($this->config->getCollaboraUrlInternal() . '/cool/convert-to/png', $options); } catch (\Exception $e) { $this->logger->info('Failed to convert preview: ' . $e->getMessage(), ['exception' => $e]); - return false; + return null; } $image = new Image(); @@ -92,6 +87,6 @@ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { $image->scaleDownToFit($maxX, $maxY); return $image; } - return false; + return null; } } diff --git a/lib/Preview/OpenDocument.php b/lib/Preview/OpenDocument.php index fc7dab453e..3b8743e24d 100644 --- a/lib/Preview/OpenDocument.php +++ b/lib/Preview/OpenDocument.php @@ -23,10 +23,11 @@ //.odt, .ott, .oth, .odm, .odg, .otg, .odp, .otp, .ods, .ots, .odc, .odf, .odb, .odi, .oxt class OpenDocument extends Office { + public const MIMETYPE_REGEX = '/application\/vnd.oasis.opendocument.*/'; /** * {@inheritDoc} */ - public function getMimeType() { - return '/application\/vnd.oasis.opendocument.*/'; + public function getMimeType(): string { + return self::MIMETYPE_REGEX; } } diff --git a/lib/Preview/Pdf.php b/lib/Preview/Pdf.php index 4fd2265145..2e86909b33 100644 --- a/lib/Preview/Pdf.php +++ b/lib/Preview/Pdf.php @@ -23,10 +23,11 @@ namespace OCA\Richdocuments\Preview; class Pdf extends Office { + public const MIMETYPE_REGEX = '/application\/pdf/'; /** * {@inheritDoc} */ - public function getMimeType() { - return '/application\/pdf/'; + public function getMimeType(): string { + return self::MIMETYPE_REGEX; } } diff --git a/tests/psalm-baseline.xml b/tests/psalm-baseline.xml index 7ed92ff27d..8a02919953 100644 --- a/tests/psalm-baseline.xml +++ b/tests/psalm-baseline.xml @@ -1,5 +1,5 @@ - + [] @@ -8,16 +8,6 @@ - - - EMF - MSExcel - MSWord - OOXML - OpenDocument - Pdf - - Command @@ -28,12 +18,12 @@ Command - + Command - + Command @@ -53,7 +43,7 @@ getId()]]> - + $app !== '' @@ -78,7 +68,7 @@ null - + $path === '' putContent @@ -95,35 +85,13 @@ - - - Office - - - - - Office - - - - - Office - - - - - Office - - - - - Office - - - + - Office + Image + + capabilitites]]> +