Skip to content

Commit dc2040f

Browse files
committed
feat: make preview conversion timeout and max file size configurable
Signed-off-by: Julius Knorr <jus@bitgrid.net>
1 parent 20780fb commit dc2040f

4 files changed

Lines changed: 42 additions & 3 deletions

File tree

docs/app_settings.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ By default Nextcloud will generate previews of Office files using the Collabora
4141

4242
occ config:app:set richdocuments preview_generation --type boolean --lazy --value false
4343

44+
The timeout for preview conversion requests (in seconds) can be configured. The default is 5 seconds:
45+
46+
occ config:app:set richdocuments preview_conversion_timeout --type integer --value 10
47+
48+
Files larger than the configured maximum file size will be skipped and no preview will be generated. The default limit is 100 MB (104857600 bytes):
49+
50+
occ config:app:set richdocuments preview_conversion_max_filesize --type integer --value 52428800
51+
4452
### Electronic signature
4553
From a shell running in the Nextcloud root directory, run the following `occ`
4654
command to configure a non-default base URL for eID Easy. For example:

lib/AppConfig.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ class AppConfig {
3232
// Default: 'no', set to 'yes' to enable
3333
public const USE_SECURE_VIEW_ADDITIONAL_MIMES = 'use_secure_view_additional_mimes';
3434

35+
public const PREVIEW_CONVERSION_TIMEOUT = 'preview_conversion_timeout';
36+
public const PREVIEW_CONVERSION_MAX_FILESIZE = 'preview_conversion_max_filesize';
37+
3538
private array $defaults = [
3639
'wopi_url' => '',
3740
'timeout' => 15,
41+
'preview_conversion_timeout' => 5,
42+
'preview_conversion_max_filesize' => 104857600, // 100 MB
3843
'watermark_text' => '{userId}',
3944
'watermark_allGroupsList' => [],
4045
'watermark_allTagsList' => [],
@@ -248,6 +253,21 @@ public function isPreviewGenerationEnabled(): bool {
248253
return $this->appConfig->getAppValueBool('preview_generation', true);
249254
}
250255

256+
/**
257+
* Returns the timeout in seconds for preview conversion requests to Collabora.
258+
*/
259+
public function getPreviewConversionTimeout(): int {
260+
return (int)$this->getAppValue(self::PREVIEW_CONVERSION_TIMEOUT);
261+
}
262+
263+
/**
264+
* Returns the maximum file size in bytes for which preview conversion is attempted.
265+
* Files larger than this limit will be skipped and return no preview.
266+
*/
267+
public function getPreviewConversionMaxFileSize(): int {
268+
return (int)$this->getAppValue(self::PREVIEW_CONVERSION_MAX_FILESIZE);
269+
}
270+
251271
private function getGSDomains(): array {
252272
if (!$this->globalScaleConfig->isGlobalScaleEnabled()) {
253273
return [];

lib/Preview/Office.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
4242
return null;
4343
}
4444

45+
$fileSize = $file->getSize();
46+
$maxFileSize = $this->appConfig->getPreviewConversionMaxFileSize();
47+
if ($fileSize > $maxFileSize) {
48+
$this->logger->debug('Skipping preview conversion: file size {size} exceeds limit {limit}', [
49+
'size' => $fileSize,
50+
'limit' => $maxFileSize,
51+
]);
52+
return null;
53+
}
54+
4555
try {
4656
$response = $this->remoteService->convertFileTo($file, 'png');
4757
$image = new Image();

lib/Service/RemoteService.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,17 @@ public function convertFileTo(File $file, string $format) {
6666
if ($stream === false) {
6767
throw new Exception('Failed to open stream');
6868
}
69-
return $this->convertTo($file->getName(), $stream, $format);
69+
$timeout = $this->appConfig->getPreviewConversionTimeout();
70+
return $this->convertTo($file->getName(), $stream, $format, [], $timeout);
7071
}
7172

7273
/**
7374
* @param resource $stream
7475
* @return resource|string
7576
*/
76-
public function convertTo(string $filename, $stream, string $format, ?array $conversionOptions = []) {
77+
public function convertTo(string $filename, $stream, string $format, ?array $conversionOptions = [], int $timeout = RemoteOptionsService::REMOTE_TIMEOUT_DEFAULT) {
7778
$client = $this->clientService->newClient();
78-
$options = RemoteOptionsService::getDefaultOptions();
79+
$options = RemoteOptionsService::getDefaultOptions($timeout);
7980
// FIXME: can be removed once https://github.com/CollaboraOnline/online/issues/6983 is fixed upstream
8081
$options['expect'] = false;
8182

0 commit comments

Comments
 (0)