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\WopiInterface; |
13
|
|
|
use Exception; |
14
|
|
|
use Psr\Http\Message\RequestInterface; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
use Psr\Http\Message\UriInterface; |
17
|
|
|
use Throwable; |
18
|
|
|
use function array_key_exists; |
19
|
|
|
|
20
|
|
|
final class Files |
21
|
|
|
{ |
22
|
|
|
private WopiInterface $wopi; |
23
|
|
|
|
24
|
|
|
public function __construct(WopiInterface $wopi) |
25
|
|
|
{ |
26
|
|
|
$this->wopi = $wopi; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function checkFileInfo(string $fileId, RequestInterface $request): ResponseInterface |
30
|
|
|
{ |
31
|
|
|
try { |
32
|
|
|
$checkFileInfo = $this->wopi->checkFileInfo( |
33
|
|
|
$fileId, |
34
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
35
|
|
|
$request |
36
|
|
|
); |
37
|
|
|
} catch (Throwable $e) { |
38
|
|
|
throw $e; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $checkFileInfo; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function deleteFile(string $fileId, RequestInterface $request): ResponseInterface |
45
|
|
|
{ |
46
|
|
|
try { |
47
|
|
|
$deleteFile = $this->wopi->deleteFile( |
48
|
|
|
$fileId, |
49
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
50
|
|
|
$request |
51
|
|
|
); |
52
|
|
|
} catch (Throwable $e) { |
53
|
|
|
throw $e; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $deleteFile; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function enumerateAncestors(string $fileId, RequestInterface $request): ResponseInterface |
60
|
|
|
{ |
61
|
|
|
try { |
62
|
|
|
$enumerateAncestors = $this->wopi->enumerateAncestors( |
63
|
|
|
$fileId, |
64
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
65
|
|
|
$request |
66
|
|
|
); |
67
|
|
|
} catch (Throwable $e) { |
68
|
|
|
throw $e; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $enumerateAncestors; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getFile(string $fileId, RequestInterface $request): ResponseInterface |
75
|
|
|
{ |
76
|
|
|
try { |
77
|
|
|
$getFile = $this->wopi->getFile( |
78
|
|
|
$fileId, |
79
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
80
|
|
|
$request |
81
|
|
|
); |
82
|
|
|
} catch (Throwable $e) { |
83
|
|
|
throw $e; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $getFile; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getLock(string $fileId, RequestInterface $request): ResponseInterface |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
|
|
$getLock = $this->wopi->getLock( |
93
|
|
|
$fileId, |
94
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
95
|
|
|
$request |
96
|
|
|
); |
97
|
|
|
} catch (Throwable $e) { |
98
|
|
|
throw $e; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $getLock; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getShareUrl(string $fileId, RequestInterface $request): ResponseInterface |
105
|
|
|
{ |
106
|
|
|
try { |
107
|
|
|
$getShareUrl = $this->wopi->enumerateAncestors( |
108
|
|
|
$fileId, |
109
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
110
|
|
|
$request |
111
|
|
|
); |
112
|
|
|
} catch (Throwable $e) { |
113
|
|
|
throw $e; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $getShareUrl; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function lock(string $fileId, RequestInterface $request): ResponseInterface |
120
|
|
|
{ |
121
|
|
|
try { |
122
|
|
|
$lock = $this->wopi->lock( |
123
|
|
|
$fileId, |
124
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
125
|
|
|
$request->getHeaderLine('X-WOPI-Lock'), |
126
|
|
|
$request |
127
|
|
|
); |
128
|
|
|
} catch (Throwable $e) { |
129
|
|
|
throw $e; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $lock; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function putFile(string $fileId, RequestInterface $request): ResponseInterface |
136
|
|
|
{ |
137
|
|
|
try { |
138
|
|
|
$putFile = $this->wopi->putFile( |
139
|
|
|
$fileId, |
140
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
141
|
|
|
$request->getHeaderLine('X-WOPI-Lock'), |
142
|
|
|
$request->getHeaderLine('X-WOPI-Editors'), |
143
|
|
|
$request |
144
|
|
|
); |
145
|
|
|
} catch (Throwable $e) { |
146
|
|
|
throw $e; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $putFile; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function putRelativeFile(string $fileId, RequestInterface $request): ResponseInterface |
153
|
|
|
{ |
154
|
|
|
try { |
155
|
|
|
$putRelativeFile = $this->wopi->putRelativeFile( |
156
|
|
|
$fileId, |
157
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
158
|
|
|
$request |
159
|
|
|
); |
160
|
|
|
} catch (Throwable $e) { |
161
|
|
|
throw $e; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $putRelativeFile; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function putUserInfo(string $fileId, RequestInterface $request): ResponseInterface |
168
|
|
|
{ |
169
|
|
|
try { |
170
|
|
|
$putUserInfo = $this->wopi->putUserInfo( |
171
|
|
|
$fileId, |
172
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
173
|
|
|
$request |
174
|
|
|
); |
175
|
|
|
} catch (Throwable $e) { |
176
|
|
|
throw $e; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $putUserInfo; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function refreshLock(string $fileId, RequestInterface $request): ResponseInterface |
183
|
|
|
{ |
184
|
|
|
try { |
185
|
|
|
$refreshLock = $this->wopi->refreshLock( |
186
|
|
|
$fileId, |
187
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
188
|
|
|
$request->getHeaderLine('X-WOPI-Lock'), |
189
|
|
|
$request |
190
|
|
|
); |
191
|
|
|
} catch (Throwable $e) { |
192
|
|
|
throw $e; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return $refreshLock; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function renameFile(string $fileId, RequestInterface $request): ResponseInterface |
199
|
|
|
{ |
200
|
|
|
try { |
201
|
|
|
$renameFile = $this->wopi->renameFile( |
202
|
|
|
$fileId, |
203
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
204
|
|
|
$request->getHeaderLine('X-WOPI-Lock'), |
205
|
|
|
$request->getHeaderLine('X-WOPI-RequestedName'), |
206
|
|
|
$request |
207
|
|
|
); |
208
|
|
|
} catch (Throwable $e) { |
209
|
|
|
throw $e; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $renameFile; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function unlock(string $fileId, RequestInterface $request): ResponseInterface |
216
|
|
|
{ |
217
|
|
|
try { |
218
|
|
|
$unlock = $this->wopi->unlock( |
219
|
|
|
$fileId, |
220
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
221
|
|
|
$request->getHeaderLine('X-WOPI-Lock'), |
222
|
|
|
$request |
223
|
|
|
); |
224
|
|
|
} catch (Throwable $e) { |
225
|
|
|
throw $e; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $unlock; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function unlockAndRelock(string $fileId, RequestInterface $request): ResponseInterface |
232
|
|
|
{ |
233
|
|
|
try { |
234
|
|
|
$unlockAndRelock = $this->wopi->unlockAndRelock( |
235
|
|
|
$fileId, |
236
|
|
|
$this->getParam($request->getUri(), 'access_token'), |
237
|
|
|
$request->getHeaderLine('X-WOPI-Lock'), |
238
|
|
|
$request->getHeaderLine('X-WOPI-OldLock'), |
239
|
|
|
$request |
240
|
|
|
); |
241
|
|
|
} catch (Throwable $e) { |
242
|
|
|
throw $e; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return $unlockAndRelock; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
private function getParam(UriInterface $uri, string $param): string |
249
|
|
|
{ |
250
|
|
|
$output = []; |
251
|
|
|
|
252
|
|
|
parse_str($uri->getQuery(), $output); |
253
|
|
|
|
254
|
|
|
if (!array_key_exists($param, $output)) { |
255
|
|
|
// TODO |
256
|
|
|
throw new Exception('No param found.'); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
return $output[$param]; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|