|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Piotr Mrowczynski <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2018, Piotr Mrowczynski. |
|
6
|
|
|
* @license AGPL-3.0 |
|
7
|
|
|
* |
|
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
10
|
|
|
* as published by the Free Software Foundation. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OCA\Files_PaperHive\Controller; |
|
23
|
|
|
|
|
24
|
|
|
use OCA\Files_PaperHive\PaperHiveMetadata; |
|
25
|
|
|
use OCP\Http\Client\IClient; |
|
26
|
|
|
use OC\Files\View; |
|
27
|
|
|
use OCP\AppFramework\Controller; |
|
28
|
|
|
use OCP\AppFramework\Http; |
|
29
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
30
|
|
|
use OCP\Files\ForbiddenException; |
|
31
|
|
|
use OCP\IL10N; |
|
32
|
|
|
use OCP\ILogger; |
|
33
|
|
|
use OCP\IRequest; |
|
34
|
|
|
use OCP\Lock\LockedException; |
|
35
|
|
|
|
|
36
|
|
|
class PaperHiveController extends Controller { |
|
37
|
|
|
|
|
38
|
|
|
/** @var IL10N */ |
|
39
|
|
|
private $l; |
|
40
|
|
|
|
|
41
|
|
|
/** @var View */ |
|
42
|
|
|
private $view; |
|
43
|
|
|
|
|
44
|
|
|
/** @var ILogger */ |
|
45
|
|
|
private $logger; |
|
46
|
|
|
|
|
47
|
|
|
/** @var \OCP\Http\Client\IClient */ |
|
48
|
|
|
private $client; |
|
49
|
|
|
|
|
50
|
|
|
/** @var PaperHiveMetadata */ |
|
51
|
|
|
private $paperHiveMetadata; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Paperhive base URL |
|
55
|
|
|
*/ |
|
56
|
|
|
private $paperhive_base_url = 'https://paperhive.org'; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Paperhive url for document API |
|
60
|
|
|
*/ |
|
61
|
|
|
private $paperhive_api_documents = '/api/document-items/'; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Paperhive url for discussions API |
|
65
|
|
|
*/ |
|
66
|
|
|
private $paperhive_api_discussions = '/api/discussions?documentItem='; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Paperhive url for document text in browser |
|
70
|
|
|
*/ |
|
71
|
|
|
private $paperhive_base_document_url = '/documents/items/'; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Paperhive file extension |
|
75
|
|
|
*/ |
|
76
|
|
|
private $paperhive_file_extension = '.paperhive'; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Paperhive BookID example |
|
80
|
|
|
*/ |
|
81
|
|
|
private $paperhive_bookid_example = 'ZYY0r21rJbqr'; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @NoAdminRequired |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $AppName |
|
87
|
|
|
* @param IRequest $request |
|
88
|
|
|
* @param IL10N $l10n |
|
89
|
|
|
* @param View $view |
|
90
|
|
|
* @param ILogger $logger |
|
91
|
|
|
* @param IClient $client |
|
92
|
|
|
* @param PaperHiveMetadata $paperHiveMetadata |
|
93
|
|
|
*/ |
|
94
|
|
|
public function __construct($AppName, |
|
95
|
|
|
IRequest $request, |
|
96
|
|
|
IL10N $l10n, |
|
97
|
|
|
View $view, |
|
98
|
|
|
ILogger $logger, |
|
99
|
|
|
IClient $client, |
|
100
|
|
|
PaperHiveMetadata $paperHiveMetadata) { |
|
101
|
|
|
parent::__construct($AppName, $request); |
|
102
|
|
|
$this->l = $l10n; |
|
103
|
|
|
$this->view = $view; |
|
104
|
|
|
$this->logger = $logger; |
|
105
|
|
|
$this->client = $client; |
|
106
|
|
|
$this->paperHiveMetadata = $paperHiveMetadata; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get URL to PaperHive book url |
|
111
|
|
|
* |
|
112
|
|
|
* @NoAdminRequired |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $dir |
|
115
|
|
|
* @param string $filename |
|
116
|
|
|
* @return DataResponse |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getPaperHiveBookURL($dir, $filename) { |
|
119
|
|
|
$bookId = $this->getBookIdforPath($dir, $filename); |
|
120
|
|
View Code Duplication |
if (!$bookId) { |
|
|
|
|
|
|
121
|
|
|
$message = (string)$this->l->t('No such document found in database.'); |
|
122
|
|
|
return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
|
123
|
|
|
} |
|
124
|
|
|
return new DataResponse($this->paperhive_base_url . $this->paperhive_base_document_url . $bookId, Http::STATUS_OK); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Get URL to PaperHive book discussion count |
|
129
|
|
|
* |
|
130
|
|
|
* @NoAdminRequired |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $dir |
|
133
|
|
|
* @param string $filename |
|
134
|
|
|
* @return DataResponse |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getPaperHiveBookDiscussionCount($dir, $filename) { |
|
137
|
|
|
$bookId = $this->getBookIdforPath($dir, $filename); |
|
138
|
|
|
$paperHiveString = $this->fetchDiscussions($bookId); |
|
139
|
|
|
$paperHiveDiscussions = json_decode($paperHiveString, true); |
|
140
|
|
|
$disscussionCount = -1; |
|
141
|
|
View Code Duplication |
if (json_last_error() === JSON_ERROR_NONE && isset($paperHiveDiscussions['discussions'])) { |
|
|
|
|
|
|
142
|
|
|
// Silently ignore discussions as this might indicate temporary unavailability |
|
143
|
|
|
$disscussionCount = count($paperHiveDiscussions['discussions']); |
|
144
|
|
|
} |
|
145
|
|
|
return new DataResponse($disscussionCount, Http::STATUS_OK); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
private function getBookIdforPath($dir, $filename){ |
|
149
|
|
|
if (!empty($filename)) { |
|
150
|
|
View Code Duplication |
if($dir == '/') { |
|
|
|
|
|
|
151
|
|
|
$path = $dir . $filename; |
|
152
|
|
|
} else { |
|
153
|
|
|
$path = $dir . '/' . $filename; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$fileInfo = $this->view->getFileInfo($path); |
|
157
|
|
|
if($fileInfo && $bookId = $this->paperHiveMetadata->getBookID($fileInfo['fileid'])) { |
|
158
|
|
|
return $bookId; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
return false; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Does the call to PaperHive Discussions API and returns disussions for specific it for specific BookID |
|
166
|
|
|
* |
|
167
|
|
|
* @NoAdminRequired |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $bookID |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
|
View Code Duplication |
private function fetchDiscussions($bookID) { |
|
|
|
|
|
|
173
|
|
|
$urlDiscussions = $this->paperhive_base_url . $this->paperhive_api_discussions . $bookID; |
|
174
|
|
|
try { |
|
175
|
|
|
$response = $this->client->get($urlDiscussions, []); |
|
176
|
|
|
} catch (\Exception $e) { |
|
177
|
|
|
return false; |
|
|
|
|
|
|
178
|
|
|
} |
|
179
|
|
|
return $response->getBody(); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Does the call to PaperHive Documents API and returns the JSON for a book for specific BookID |
|
184
|
|
|
* |
|
185
|
|
|
* @NoAdminRequired |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $bookID |
|
188
|
|
|
* @return string/boolean |
|
|
|
|
|
|
189
|
|
|
*/ |
|
190
|
|
View Code Duplication |
private function fetchDocument($bookID) { |
|
|
|
|
|
|
191
|
|
|
$urlDocument = $this->paperhive_base_url . $this->paperhive_api_documents . $bookID; |
|
192
|
|
|
try { |
|
193
|
|
|
$response = $this->client->get($urlDocument, []); |
|
194
|
|
|
} catch (\Exception $e) { |
|
195
|
|
|
return false; |
|
196
|
|
|
} |
|
197
|
|
|
return $response->getBody(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Gets the informations about the book for specific BookID and saves as a file |
|
202
|
|
|
* |
|
203
|
|
|
* @NoAdminRequired |
|
204
|
|
|
* |
|
205
|
|
|
* @param string $dir |
|
206
|
|
|
* @param string $bookID |
|
207
|
|
|
* @return DataResponse |
|
208
|
|
|
*/ |
|
209
|
|
|
public function generatePaperHiveDocument($dir, $bookID) { |
|
210
|
|
|
// Try to get the document |
|
211
|
|
|
$paperHiveObjectString = $this->fetchDocument($bookID); |
|
212
|
|
View Code Duplication |
if ($paperHiveObjectString === false) { |
|
|
|
|
|
|
213
|
|
|
$message = (string)$this->l->t('Problem connecting to PaperHive.'); |
|
214
|
|
|
return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
|
215
|
|
|
} |
|
216
|
|
|
$paperHiveObject = json_decode($paperHiveObjectString, true); |
|
217
|
|
|
|
|
218
|
|
|
// Check if correct response has been returned |
|
219
|
|
View Code Duplication |
if (json_last_error() != JSON_ERROR_NONE) { |
|
|
|
|
|
|
220
|
|
|
$message = (string)$this->l->t('Received wrong response from PaperHive.'); |
|
221
|
|
|
return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
// Check if document is found |
|
225
|
|
|
if (!(isset($paperHiveObject['metadata']) && isset($paperHiveObject['metadata']['title']))) { |
|
226
|
|
|
$message = (string)$this->l->t('Document with this BookID cannot be found'); |
|
227
|
|
|
return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
// Try fetching discussions |
|
231
|
|
|
$paperHiveDiscussionsString = $this->fetchDiscussions($bookID); |
|
232
|
|
|
$paperHiveDiscussions = json_decode($paperHiveDiscussionsString, true); |
|
233
|
|
View Code Duplication |
if ($paperHiveDiscussionsString === false || json_last_error() != JSON_ERROR_NONE) { |
|
|
|
|
|
|
234
|
|
|
$message = (string)$this->l->t('Problem connecting to PaperHive to fetch discussions.'); |
|
235
|
|
|
return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
|
236
|
|
|
} |
|
237
|
|
|
$discussionCount = -1; |
|
238
|
|
View Code Duplication |
if (json_last_error() === JSON_ERROR_NONE && isset($paperHiveDiscussions['discussions'])) { |
|
|
|
|
|
|
239
|
|
|
$discussionCount = count($paperHiveDiscussions['discussions']); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
// Save the file |
|
243
|
|
|
$title = $paperHiveObject['metadata']['title']; |
|
244
|
|
|
$filename = $title . $this->paperhive_file_extension; |
|
245
|
|
View Code Duplication |
if($dir == '/') { |
|
|
|
|
|
|
246
|
|
|
$path = $dir . $filename; |
|
247
|
|
|
} else { |
|
248
|
|
|
$path = $dir . '/' . $filename; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
$exists = $this->view->file_exists($path); |
|
252
|
|
View Code Duplication |
if ($exists) { |
|
|
|
|
|
|
253
|
|
|
$message = (string) $this->l->t('The file already exists.'); |
|
254
|
|
|
return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
try { |
|
258
|
|
|
$created = $this->view->touch($path); |
|
259
|
|
View Code Duplication |
if(!$created) { |
|
|
|
|
|
|
260
|
|
|
$message = (string) $this->l->t('Could not save document.'); |
|
261
|
|
|
return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
$fileInfo = $this->view->getFileInfo($path); |
|
265
|
|
|
$inserted = $this->paperHiveMetadata->insertBookID($fileInfo['fileid'], $bookID); |
|
266
|
|
View Code Duplication |
if(!$inserted) { |
|
|
|
|
|
|
267
|
|
|
$this->view->unlink($path); |
|
268
|
|
|
$message = (string) $this->l->t('Could not save document metadata.'); |
|
269
|
|
|
return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
|
270
|
|
|
} |
|
271
|
|
|
} catch (LockedException $e) { |
|
|
|
|
|
|
272
|
|
|
$message = (string) $this->l->t('The file is locked.'); |
|
273
|
|
|
return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
|
274
|
|
|
} catch (ForbiddenException $e) { |
|
|
|
|
|
|
275
|
|
|
return new DataResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST); |
|
276
|
|
|
} catch (\Exception $e) { |
|
277
|
|
|
$message = (string)$this->l->t('An internal server error occurred.'); |
|
278
|
|
|
return new DataResponse(['message' => $message], Http::STATUS_BAD_REQUEST); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
return new DataResponse(['path' => $path, 'filename' => $title, 'extension' => $this->paperhive_file_extension, 'discussionCount' => $discussionCount], Http::STATUS_OK); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Returns all required PaperHive setting |
|
286
|
|
|
* |
|
287
|
|
|
* @NoAdminRequired |
|
288
|
|
|
* |
|
289
|
|
|
* @return DataResponse |
|
290
|
|
|
*/ |
|
291
|
|
|
public function getPaperHiveDetails() { |
|
292
|
|
|
return new DataResponse([ |
|
293
|
|
|
'paperhive_bookid_example' => $this->paperhive_bookid_example, |
|
294
|
|
|
'paperhive_base_url' => $this->paperhive_base_url, |
|
295
|
|
|
'paperhive_base_document_url' => $this->paperhive_base_document_url, |
|
296
|
|
|
'paperhive_api_documents' => $this->paperhive_api_documents, |
|
297
|
|
|
'paperhive_api_discussions' => $this->paperhive_api_discussions, |
|
298
|
|
|
'paperhive_extension' => $this->paperhive_file_extension, |
|
299
|
|
|
], Http::STATUS_OK); |
|
300
|
|
|
} |
|
301
|
|
|
} |
|
302
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: