Skip to content

Commit

Permalink
chore: Move to preview provider v2
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliushaertl committed Feb 21, 2024
1 parent 6623a4c commit 6da08e6
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 110 deletions.
42 changes: 7 additions & 35 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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'];
Expand Down
5 changes: 3 additions & 2 deletions lib/Preview/EMF.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 3 additions & 2 deletions lib/Preview/MSExcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 3 additions & 2 deletions lib/Preview/MSWord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
6 changes: 4 additions & 2 deletions lib/Preview/OOXML.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
37 changes: 16 additions & 21 deletions lib/Preview/Office.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -92,6 +87,6 @@ public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
$image->scaleDownToFit($maxX, $maxY);
return $image;
}
return false;
return null;
}
}
5 changes: 3 additions & 2 deletions lib/Preview/OpenDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 3 additions & 2 deletions lib/Preview/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
52 changes: 10 additions & 42 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<files psalm-version="5.21.1@8c473e2437be8b6a8fd8f630f0f11a16b114c494">
<file src="lib/AppConfig.php">
<InvalidArgument>
<code>[]</code>
Expand All @@ -8,16 +8,6 @@
<code><![CDATA[array_key_exists($key, self::APP_SETTING_TYPES) && self::APP_SETTING_TYPES[$key] === 'array']]></code>
</RedundantCondition>
</file>
<file src="lib/AppInfo/Application.php">
<MissingDependency>
<code>EMF</code>
<code>MSExcel</code>
<code>MSWord</code>
<code>OOXML</code>
<code>OpenDocument</code>
<code>Pdf</code>
</MissingDependency>
</file>
<file src="lib/Command/ActivateConfig.php">
<UndefinedClass>
<code>Command</code>
Expand All @@ -28,12 +18,12 @@
<code>Command</code>
</UndefinedClass>
</file>
<file src="lib/Command/UpdateEmptyTemplates.php">
<file src="lib/Command/InstallDefaultFonts.php">
<UndefinedClass>
<code>Command</code>
</UndefinedClass>
</file>
<file src="lib/Command/InstallDefaultFonts.php">
<file src="lib/Command/UpdateEmptyTemplates.php">
<UndefinedClass>
<code>Command</code>
</UndefinedClass>
Expand All @@ -53,7 +43,7 @@
<code><![CDATA[$node->getId()]]></code>
</InvalidScalarArgument>
<RedundantCondition>
<code><![CDATA[$app !== '']]></code>
<code>$app !== ''</code>
</RedundantCondition>
</file>
<file src="lib/Controller/SettingsController.php">
Expand All @@ -78,7 +68,7 @@
<code>null</code>
</NullArgument>
<TypeDoesNotContainType>
<code><![CDATA[$path === '']]></code>
<code>$path === ''</code>
</TypeDoesNotContainType>
<UndefinedInterfaceMethod>
<code>putContent</code>
Expand All @@ -95,35 +85,13 @@
<code><![CDATA[$share && method_exists($share, 'getAttributes')]]></code>
</RedundantCondition>
</file>
<file src="lib/Preview/EMF.php">
<MissingDependency>
<code>Office</code>
</MissingDependency>
</file>
<file src="lib/Preview/MSExcel.php">
<MissingDependency>
<code>Office</code>
</MissingDependency>
</file>
<file src="lib/Preview/MSWord.php">
<MissingDependency>
<code>Office</code>
</MissingDependency>
</file>
<file src="lib/Preview/OOXML.php">
<MissingDependency>
<code>Office</code>
</MissingDependency>
</file>
<file src="lib/Preview/OpenDocument.php">
<MissingDependency>
<code>Office</code>
</MissingDependency>
</file>
<file src="lib/Preview/Pdf.php">
<file src="lib/Preview/Office.php">
<MissingDependency>
<code>Office</code>
<code>Image</code>
</MissingDependency>
<UndefinedThisPropertyAssignment>
<code><![CDATA[$this->capabilitites]]></code>
</UndefinedThisPropertyAssignment>
</file>
<file src="lib/Service/ConnectivityService.php">
<UndefinedClass>
Expand Down

0 comments on commit 6da08e6

Please sign in to comment.