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
|
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Text\Service;
use Exception;
use OC\Files\Node\File;
use OCA\Text\DocumentHasUnsavedChangesException;
use OCA\Text\DocumentSaveConflictException;
use OCA\Text\VersionMismatchException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\Constants;
use OCP\Files\NotFoundException;
use OCP\ILogger;
use OCP\IRequest;
use OCP\Lock\LockedException;
class ApiService {
protected $request;
protected $sessionService;
protected $documentService;
protected $logger;
private $imageService;
public function __construct(IRequest $request,
SessionService $sessionService,
DocumentService $documentService,
ImageService $imageService,
ILogger $logger) {
$this->request = $request;
$this->sessionService = $sessionService;
$this->documentService = $documentService;
$this->logger = $logger;
$this->imageService = $imageService;
}
public function create($fileId = null, $filePath = null, $token = null, $guestName = null, bool $forceRecreate = false): DataResponse {
try {
$readOnly = true;
/** @var File $file */
if ($token) {
$file = $this->documentService->getFileByShareToken($token, $this->request->getParam('filePath'));
/*
* Check if we have proper read access (files drop)
* If not then well 404 it is.
*/
try {
$this->documentService->checkSharePermissions($token, Constants::PERMISSION_READ);
} catch (NotFoundException $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}
try {
$this->documentService->checkSharePermissions($token, Constants::PERMISSION_UPDATE);
$readOnly = false;
} catch (NotFoundException $e) {
}
} elseif ($fileId) {
$file = $this->documentService->getFileById($fileId);
$readOnly = !$file->isUpdateable();
} else {
return new DataResponse('No valid file argument provided', 500);
}
$this->sessionService->removeInactiveSessions($file->getId());
$activeSessions = $this->sessionService->getActiveSessions($file->getId());
if ($forceRecreate || count($activeSessions) === 0) {
try {
$this->documentService->resetDocument($file->getId(), $forceRecreate);
} catch (DocumentHasUnsavedChangesException $e) {
}
}
$document = $this->documentService->createDocument($file);
} catch (Exception $e) {
$this->logger->logException($e);
return new DataResponse('Failed to create the document session', 500);
}
$session = $this->sessionService->initSession($document->getId(), $guestName);
try {
$baseFile = $this->documentService->getBaseFile($document->getId());
$content = $baseFile->getContent();
} catch (NotFoundException $e) {
$this->logger->logException($e, ['level' => ILogger::INFO]);
$content = null;
}
return new DataResponse([
'document' => $document,
'session' => $session,
'readOnly' => $readOnly,
'content' => $content
]);
}
public function fetch($documentId, $sessionId, $sessionToken) {
if ($this->sessionService->isValidSession($documentId, $sessionId, $sessionToken)) {
$this->sessionService->removeInactiveSessions($documentId);
try {
$file = $this->documentService->getBaseFile($documentId);
} catch (NotFoundException $e) {
return new NotFoundResponse();
}
return new FileDisplayResponse($file, 200, ['Content-Type' => 'text/plain']);
}
return new NotFoundResponse();
}
public function close($documentId, $sessionId, $sessionToken): DataResponse {
$this->sessionService->closeSession($documentId, $sessionId, $sessionToken);
$this->sessionService->removeInactiveSessions($documentId);
$activeSessions = $this->sessionService->getActiveSessions($documentId);
if (count($activeSessions) === 0) {
try {
$this->documentService->resetDocument($documentId);
$this->imageService->cleanupAttachments($documentId);
} catch (DocumentHasUnsavedChangesException $e) {
}
}
return new DataResponse([]);
}
/**
* @throws NotFoundException
* @throws \OCP\AppFramework\Db\DoesNotExistException
*/
public function push($documentId, $sessionId, $sessionToken, $version, $steps, $token = null): DataResponse {
$session = $this->sessionService->getSession($documentId, $sessionId, $sessionToken);
$file = $this->documentService->getFileForSession($session, $token);
if ($this->sessionService->isValidSession($documentId, $sessionId, $sessionToken) && !$this->documentService->isReadOnly($file, $token)) {
try {
$steps = $this->documentService->addStep($documentId, $sessionId, $steps, $version);
} catch (VersionMismatchException $e) {
return new DataResponse($e->getMessage(), $e->getStatus());
}
return new DataResponse($steps);
}
return new DataResponse([], 403);
}
public function sync($documentId, $sessionId, $sessionToken, $version = 0, $autosaveContent = null, bool $force = false, bool $manualSave = false, $token = null): DataResponse {
if (!$this->sessionService->isValidSession($documentId, $sessionId, $sessionToken)) {
return new DataResponse([], 403);
}
try {
$result = [
'steps' => $this->documentService->getSteps($documentId, $version),
'sessions' => $this->sessionService->getAllSessions($documentId),
'document' => $this->documentService->get($documentId)
];
$session = $this->sessionService->getSession($documentId, $sessionId, $sessionToken);
$file = $this->documentService->getFileForSession($session, $token);
} catch (NotFoundException $e) {
$this->logger->logException($e, ['level' => ILogger::INFO]);
return new DataResponse([
'message' => 'File not found'
], 404);
} catch (DoesNotExistException $e) {
$this->logger->logException($e, ['level' => ILogger::INFO]);
return new DataResponse([
'message' => 'Document no longer exists'
], 404);
}
try {
$result['document'] = $this->documentService->autosave($file, $documentId, $version, $autosaveContent, $force, $manualSave, $token, $this->request->getParam('filePath'));
} catch (DocumentSaveConflictException $e) {
try {
$result['outsideChange'] = $file->getContent();
} catch (LockedException $e) {
// Ignore locked exception since it might happen due to an autosave action happening at the same time
}
} catch (NotFoundException $e) {
return new DataResponse([], 404);
} catch (Exception $e) {
$this->logger->logException($e);
return new DataResponse([
'message' => 'Failed to autosave document'
], 500);
}
return new DataResponse($result, isset($result['outsideChange']) ? 409 : 200);
}
/**
* @throws \OCP\AppFramework\Db\DoesNotExistException
*/
public function updateSession(int $documentId, int $sessionId, string $sessionToken, string $guestName): DataResponse {
if (!$this->sessionService->isValidSession($documentId, $sessionId, $sessionToken)) {
return new DataResponse([], 500);
}
if ($guestName === '') {
return new DataResponse([ 'message' => 'A guest name needs to be provided'], 500);
}
return new DataResponse($this->sessionService->updateSession($documentId, $sessionId, $sessionToken, $guestName));
}
}
|