-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathRemoteService.php
88 lines (75 loc) · 2.45 KB
/
RemoteService.php
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
namespace OCA\Richdocuments\Service;
use Exception;
use OCA\Richdocuments\AppConfig;
use OCP\Files\File;
use OCP\Files\NotFoundException;
use OCP\Http\Client\IClientService;
use Psr\Log\LoggerInterface;
class RemoteService {
public const REMOTE_TIMEOUT_DEFAULT = 25;
public function __construct(
private AppConfig $appConfig,
private IClientService $clientService,
private LoggerInterface $logger,
) {
}
public function fetchTargets($file): array {
$client = $this->clientService->newClient();
try {
$response = $client->put(
$this->appConfig->getCollaboraUrlInternal(). '/cool/extract-link-targets',
$this->getRequestOptionsForFile($file)
);
} catch (Exception $e) {
$this->logger->warning('Failed to fetch extract-link-targets', ['exception' => $e]);
return [];
}
$json = trim($response->getBody());
$json = str_replace(['", }', "\r\n", "\t"], ['" }', '\r\n', '\t'], $json);
try {
$result = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
$this->logger->warning('Failed to parse extract-link-targets response', ['exception' => $e]);
return [];
}
return $result;
}
public function fetchTargetThumbnail(File $file, string $target): ?string {
$client = $this->clientService->newClient();
try {
$response = $client->put($this->appConfig->getCollaboraUrlInternal(). '/cool/get-thumbnail', $this->getRequestOptionsForFile($file, $target));
return (string)$response->getBody();
} catch (Exception $e) {
$this->logger->info('Failed to fetch target thumbnail', ['exception' => $e]);
}
return null;
}
private function getRequestOptionsForFile(File $file, ?string $target = null): array {
$useTempFile = $file->isEncrypted() || !$file->getStorage()->isLocal();
if ($useTempFile) {
$localFile = $file->getStorage()->getLocalFile($file->getInternalPath());
if (!is_string($localFile)) {
throw new NotFoundException('Could not get local file');
}
$stream = fopen($localFile, 'rb');
} else {
$stream = $file->fopen('rb');
}
$options = [
'timeout' => self::REMOTE_TIMEOUT_DEFAULT,
'multipart' => [
['name' => $file->getName(), 'contents' => $stream],
['name' => 'target', 'contents' => $target]
]
];
if ($this->appConfig->getDisableCertificateValidation()) {
$options['verify'] = false;
}
$options['headers'] = [
'User-Agent' => 'Nextcloud Server / richdocuments',
'Accept' => 'application/json',
];
return $options;
}
}