1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* For the full copyright and license information, please view |
5
|
|
|
* the LICENSE file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace ChampsLibres\WopiBundle\Controller; |
11
|
|
|
|
12
|
|
|
use ChampsLibres\WopiLib\Service\Contract\WopiInterface; |
13
|
|
|
use ChampsLibres\WopiLib\Service\Contract\WopiProofValidatorInterface; |
14
|
|
|
use Exception; |
15
|
|
|
use loophp\psr17\Psr17Interface; |
16
|
|
|
use Psr\Http\Message\RequestInterface; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Psr\Http\Message\UriInterface; |
19
|
|
|
use Throwable; |
20
|
|
|
|
21
|
|
|
use function array_key_exists; |
22
|
|
|
|
23
|
|
|
final class Files |
24
|
|
|
{ |
25
|
|
|
private Psr17Interface $psr17; |
26
|
|
|
|
27
|
|
|
private WopiInterface $wopi; |
28
|
|
|
|
29
|
|
|
private WopiProofValidatorInterface $wopiProofValidator; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
WopiInterface $wopi, |
33
|
|
|
WopiProofValidatorInterface $wopiProofValidator, |
34
|
|
|
Psr17Interface $psr17 |
35
|
|
|
) { |
36
|
|
|
$this->wopi = $wopi; |
37
|
|
|
$this->wopiProofValidator = $wopiProofValidator; |
38
|
|
|
$this->psr17 = $psr17; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function checkFileInfo(string $fileId, RequestInterface $request): ResponseInterface |
42
|
|
|
{ |
43
|
|
|
if (!$this->wopiProofValidator->isValid($request)) { |
44
|
|
|
return $this |
45
|
|
|
->psr17 |
46
|
|
|
->createResponse(500); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
try { |
50
|
|
|
$checkFileInfo = $this->wopi->checkFileInfo( |
51
|
|
|
$fileId, |
52
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
53
|
|
|
$request |
54
|
|
|
); |
55
|
|
|
} catch (Throwable $e) { |
56
|
|
|
throw $e; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $checkFileInfo; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function deleteFile(string $fileId, RequestInterface $request): ResponseInterface |
63
|
|
|
{ |
64
|
|
|
if (!$this->wopiProofValidator->isValid($request)) { |
65
|
|
|
return $this |
66
|
|
|
->psr17 |
67
|
|
|
->createResponse(500); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
$deleteFile = $this->wopi->deleteFile( |
72
|
|
|
$fileId, |
73
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
74
|
|
|
$request |
75
|
|
|
); |
76
|
|
|
} catch (Throwable $e) { |
77
|
|
|
throw $e; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $deleteFile; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function enumerateAncestors(string $fileId, RequestInterface $request): ResponseInterface |
84
|
|
|
{ |
85
|
|
|
if (!$this->wopiProofValidator->isValid($request)) { |
86
|
|
|
return $this |
87
|
|
|
->psr17 |
88
|
|
|
->createResponse(500); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
try { |
92
|
|
|
$enumerateAncestors = $this->wopi->enumerateAncestors( |
93
|
|
|
$fileId, |
94
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
95
|
|
|
$request |
96
|
|
|
); |
97
|
|
|
} catch (Throwable $e) { |
98
|
|
|
throw $e; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $enumerateAncestors; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getFile(string $fileId, RequestInterface $request): ResponseInterface |
105
|
|
|
{ |
106
|
|
|
if (!$this->wopiProofValidator->isValid($request)) { |
107
|
|
|
return $this |
108
|
|
|
->psr17 |
109
|
|
|
->createResponse(500); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
try { |
113
|
|
|
$getFile = $this->wopi->getFile( |
114
|
|
|
$fileId, |
115
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
116
|
|
|
$request |
117
|
|
|
); |
118
|
|
|
} catch (Throwable $e) { |
119
|
|
|
throw $e; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $getFile; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getLock(string $fileId, RequestInterface $request): ResponseInterface |
126
|
|
|
{ |
127
|
|
|
if (!$this->wopiProofValidator->isValid($request)) { |
128
|
|
|
return $this |
129
|
|
|
->psr17 |
130
|
|
|
->createResponse(500); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
try { |
134
|
|
|
$getLock = $this->wopi->getLock( |
135
|
|
|
$fileId, |
136
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
137
|
|
|
$request |
138
|
|
|
); |
139
|
|
|
} catch (Throwable $e) { |
140
|
|
|
throw $e; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $getLock; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getShareUrl(string $fileId, RequestInterface $request): ResponseInterface |
147
|
|
|
{ |
148
|
|
|
if (!$this->wopiProofValidator->isValid($request)) { |
149
|
|
|
return $this |
150
|
|
|
->psr17 |
151
|
|
|
->createResponse(500); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
try { |
155
|
|
|
$getShareUrl = $this->wopi->enumerateAncestors( |
156
|
|
|
$fileId, |
157
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
158
|
|
|
$request |
159
|
|
|
); |
160
|
|
|
} catch (Throwable $e) { |
161
|
|
|
throw $e; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $getShareUrl; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function lock(string $fileId, RequestInterface $request): ResponseInterface |
168
|
|
|
{ |
169
|
|
|
if (!$this->wopiProofValidator->isValid($request)) { |
170
|
|
|
return $this |
171
|
|
|
->psr17 |
172
|
|
|
->createResponse(500); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
try { |
176
|
|
|
$lock = $this->wopi->lock( |
177
|
|
|
$fileId, |
178
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
179
|
|
|
$request->getHeaderLine('X-WOPI-Lock'), |
180
|
|
|
$request |
181
|
|
|
); |
182
|
|
|
} catch (Throwable $e) { |
183
|
|
|
throw $e; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $lock; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function putFile(string $fileId, RequestInterface $request): ResponseInterface |
190
|
|
|
{ |
191
|
|
|
if (!$this->wopiProofValidator->isValid($request)) { |
192
|
|
|
return $this |
193
|
|
|
->psr17 |
194
|
|
|
->createResponse(500); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
try { |
198
|
|
|
$putFile = $this->wopi->putFile( |
199
|
|
|
$fileId, |
200
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
201
|
|
|
$request->getHeaderLine('X-WOPI-Lock'), |
202
|
|
|
$request->getHeaderLine('X-WOPI-Editors'), |
203
|
|
|
$request |
204
|
|
|
); |
205
|
|
|
} catch (Throwable $e) { |
206
|
|
|
throw $e; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $putFile; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function putRelativeFile(string $fileId, RequestInterface $request): ResponseInterface |
213
|
|
|
{ |
214
|
|
|
if (!$this->wopiProofValidator->isValid($request)) { |
215
|
|
|
return $this |
216
|
|
|
->psr17 |
217
|
|
|
->createResponse(500); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
try { |
221
|
|
|
$putRelativeFile = $this->wopi->putRelativeFile( |
222
|
|
|
$fileId, |
223
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
224
|
|
|
$request |
225
|
|
|
); |
226
|
|
|
} catch (Throwable $e) { |
227
|
|
|
throw $e; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $putRelativeFile; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function putUserInfo(string $fileId, RequestInterface $request): ResponseInterface |
234
|
|
|
{ |
235
|
|
|
if (!$this->wopiProofValidator->isValid($request)) { |
236
|
|
|
return $this |
237
|
|
|
->psr17 |
238
|
|
|
->createResponse(500); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
try { |
242
|
|
|
$putUserInfo = $this->wopi->putUserInfo( |
243
|
|
|
$fileId, |
244
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
245
|
|
|
$request |
246
|
|
|
); |
247
|
|
|
} catch (Throwable $e) { |
248
|
|
|
throw $e; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
return $putUserInfo; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function refreshLock(string $fileId, RequestInterface $request): ResponseInterface |
255
|
|
|
{ |
256
|
|
|
if (!$this->wopiProofValidator->isValid($request)) { |
257
|
|
|
return $this |
258
|
|
|
->psr17 |
259
|
|
|
->createResponse(500); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
try { |
263
|
|
|
$refreshLock = $this->wopi->refreshLock( |
264
|
|
|
$fileId, |
265
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
266
|
|
|
$request->getHeaderLine('X-WOPI-Lock'), |
267
|
|
|
$request |
268
|
|
|
); |
269
|
|
|
} catch (Throwable $e) { |
270
|
|
|
throw $e; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return $refreshLock; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function renameFile(string $fileId, RequestInterface $request): ResponseInterface |
277
|
|
|
{ |
278
|
|
|
if (!$this->wopiProofValidator->isValid($request)) { |
279
|
|
|
return $this |
280
|
|
|
->psr17 |
281
|
|
|
->createResponse(500); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
try { |
285
|
|
|
$renameFile = $this->wopi->renameFile( |
286
|
|
|
$fileId, |
287
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
288
|
|
|
$request->getHeaderLine('X-WOPI-Lock'), |
289
|
|
|
$request->getHeaderLine('X-WOPI-RequestedName'), |
290
|
|
|
$request |
291
|
|
|
); |
292
|
|
|
} catch (Throwable $e) { |
293
|
|
|
throw $e; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
return $renameFile; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function unlock(string $fileId, RequestInterface $request): ResponseInterface |
300
|
|
|
{ |
301
|
|
|
if (!$this->wopiProofValidator->isValid($request)) { |
302
|
|
|
return $this |
303
|
|
|
->psr17 |
304
|
|
|
->createResponse(500); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
try { |
308
|
|
|
$unlock = $this->wopi->unlock( |
309
|
|
|
$fileId, |
310
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
311
|
|
|
$request->getHeaderLine('X-WOPI-Lock'), |
312
|
|
|
$request |
313
|
|
|
); |
314
|
|
|
} catch (Throwable $e) { |
315
|
|
|
throw $e; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
return $unlock; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
public function unlockAndRelock(string $fileId, RequestInterface $request): ResponseInterface |
322
|
|
|
{ |
323
|
|
|
if (!$this->wopiProofValidator->isValid($request)) { |
324
|
|
|
return $this |
325
|
|
|
->psr17 |
326
|
|
|
->createResponse(500); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
try { |
330
|
|
|
$unlockAndRelock = $this->wopi->unlockAndRelock( |
331
|
|
|
$fileId, |
332
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
333
|
|
|
$request->getHeaderLine('X-WOPI-Lock'), |
334
|
|
|
$request->getHeaderLine('X-WOPI-OldLock'), |
335
|
|
|
$request |
336
|
|
|
); |
337
|
|
|
} catch (Throwable $e) { |
338
|
|
|
throw $e; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
return $unlockAndRelock; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
private function getParam(UriInterface $uri, string $param): string |
345
|
|
|
{ |
346
|
|
|
$output = []; |
347
|
|
|
|
348
|
|
|
parse_str($uri->getQuery(), $output); |
349
|
|
|
|
350
|
|
|
if (!array_key_exists($param, $output)) { |
351
|
|
|
// TODO |
352
|
|
|
throw new Exception('No param found.'); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
return $output[$param]; |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|