-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathRemoteService.php
More file actions
242 lines (203 loc) Β· 6.52 KB
/
RemoteService.php
File metadata and controls
242 lines (203 loc) Β· 6.52 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
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 function __construct(
private AppConfig $appConfig,
private IClientService $clientService,
private CapabilitiesService $capabilitiesService,
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;
}
/**
* @return resource|string
*/
public function convertFileTo(File $file, string $format, int $timeout = RemoteOptionsService::REMOTE_TIMEOUT_DEFAULT) {
$fileName = $file->getStorage()->getLocalFile($file->getInternalPath());
$stream = fopen($fileName, 'rb');
if ($stream === false) {
throw new Exception('Failed to open stream');
}
try {
return $this->convertTo($file->getName(), $stream, $format, [], $timeout);
} finally {
fclose($stream);
}
}
/**
* @param resource $stream
* @return resource|string
*/
public function convertTo(string $filename, $stream, string $format, ?array $conversionOptions = [], int $timeout = RemoteOptionsService::REMOTE_TIMEOUT_DEFAULT) {
$client = $this->clientService->newClient();
$options = RemoteOptionsService::getDefaultOptions($timeout);
// FIXME: can be removed once https://github.com/CollaboraOnline/online/issues/6983 is fixed upstream
$options['expect'] = false;
if ($this->appConfig->getDisableCertificateValidation()) {
$options['verify'] = false;
}
$options['multipart'] = [
array_merge([
'name' => $filename,
'filename' => $filename,
'contents' => $stream
], $conversionOptions),
];
try {
$response = $client->post($this->appConfig->getCollaboraUrlInternal() . '/cool/convert-to/' . $format, $options);
$body = $response->getBody();
if (is_null($body)) {
throw new \Exception('Empty response from Collabora server');
}
return $body;
} catch (\Exception $e) {
$this->logger->info('Failed to convert preview: ' . $e->getMessage(), ['exception' => $e]);
throw $e;
}
}
/**
* @param string $filename
* @param resource $stream
* @return array
*/
public function extractDocumentStructure(string $filename, $stream, string $filter): array {
if (!$this->capabilitiesService->hasFormFilling()) {
return [];
}
$collaboraUrl = $this->appConfig->getCollaboraUrlInternal();
$client = $this->clientService->newClient();
$options = RemoteOptionsService::getDefaultOptions();
$options['expect'] = false;
if ($this->appConfig->getDisableCertificateValidation()) {
$options['verify'] = false;
}
$options['query'] = ['filter' => $filter];
$options['multipart'] = [
[
'name' => 'data',
'filename' => $filename,
'contents' => $stream,
'headers' => [ 'Content-Type' => 'multipart/form-data' ],
],
];
try {
$response = $client->post(
$collaboraUrl . '/cool/extract-document-structure',
$options
);
return json_decode($response->getBody(), true)['DocStructure'] ?? [];
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
return [];
}
}
/**
* @param string $filename
* @param resource $stream
* @return string|resource
*/
public function transformDocumentStructure(string $filename, $stream, array $values, ?string $format = null) {
if (!$this->capabilitiesService->hasFormFilling()) {
throw new \RuntimeException('Form filling not supported by the Collabora server');
}
$collaboraUrl = $this->appConfig->getCollaboraUrlInternal();
$client = $this->clientService->newClient();
$options = RemoteOptionsService::getDefaultOptions();
$options['expect'] = false;
if ($this->appConfig->getDisableCertificateValidation()) {
$options['verify'] = false;
}
$data = [
'name' => 'data',
'filename' => $filename,
'contents' => $stream,
'headers' => [ 'Content-Type' => 'multipart/form-data' ],
];
$transform = [
'name' => 'transform',
'contents' => '{"Transforms": ' . json_encode($values) . '}',
'headers' => [ 'Content-Type' => 'application/json' ],
];
$options['multipart'] = [$data, $transform];
if ($format !== null) {
$options['multipart'][] = [
'name' => 'format',
'contents' => $format,
];
}
try {
$response = $client->post(
$collaboraUrl . '/cool/transform-document-structure',
$options
);
$body = $response->getBody();
if (is_null($body)) {
throw new \Exception('Empty response from Collabora server');
}
return $body;
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
throw $e;
}
}
private function getRequestOptionsForFile(File $file, ?string $target = null): array {
$localFile = $file->getStorage()->getLocalFile($file->getInternalPath());
if (!is_string($localFile)) {
throw new NotFoundException('Could not get local file');
}
$stream = fopen($localFile, 'rb');
$options = RemoteOptionsService::getDefaultOptions(25);
$options['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;
}
}