-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathOffice.php
More file actions
71 lines (61 loc) · 1.89 KB
/
Office.php
File metadata and controls
71 lines (61 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Richdocuments\Preview;
use OCA\Richdocuments\AppConfig;
use OCA\Richdocuments\Capabilities;
use OCA\Richdocuments\Service\RemoteService;
use OCP\Files\File;
use OCP\Files\FileInfo;
use OCP\IImage;
use OCP\Image;
use OCP\Preview\IProviderV2;
use Psr\Log\LoggerInterface;
abstract class Office implements IProviderV2 {
private array $capabilities;
public function __construct(
private RemoteService $remoteService,
private LoggerInterface $logger,
private AppConfig $appConfig,
Capabilities $capabilities,
) {
$this->capabilities = $capabilities->getCapabilities()['richdocuments'] ?? [];
}
#[\Override]
public function isAvailable(FileInfo $file): bool {
if (isset($this->capabilities['collabora']['convert-to']['available'])) {
return (bool)$this->capabilities['collabora']['convert-to']['available'] && $this->appConfig->isPreviewGenerationEnabled();
}
return false;
}
#[\Override]
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
$fileSize = $file->getSize();
if ($fileSize === 0) {
return null;
}
$maxFileSize = $this->appConfig->getPreviewConversionMaxFileSize();
if ($fileSize > $maxFileSize) {
$this->logger->debug('Skipping preview conversion: file size {size} exceeds limit {limit}', [
'size' => $fileSize,
'limit' => $maxFileSize,
]);
return null;
}
try {
$response = $this->remoteService->convertFileTo($file, 'png', $this->appConfig->getPreviewConversionTimeout());
$image = new Image();
$image->loadFromData($response);
if ($image->valid()) {
$image->scaleDownToFit($maxX, $maxY);
return $image;
}
} catch (\Exception $e) {
$this->logger->info('Failed to convert preview: ' . $e->getMessage(), ['exception' => $e]);
return null;
}
return null;
}
}