|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Bart Visscher <[email protected]> |
|
6
|
|
|
* @author Björn Schießle <[email protected]> |
|
7
|
|
|
* @author Christoph Wurst <[email protected]> |
|
8
|
|
|
* @author Daniel Calviño Sánchez <[email protected]> |
|
9
|
|
|
* @author Jakob Sack <[email protected]> |
|
10
|
|
|
* @author Jan-Philipp Litza <[email protected]> |
|
11
|
|
|
* @author Joas Schilling <[email protected]> |
|
12
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
|
13
|
|
|
* @author Julius Härtl <[email protected]> |
|
14
|
|
|
* @author Lukas Reschke <[email protected]> |
|
15
|
|
|
* @author Morris Jobke <[email protected]> |
|
16
|
|
|
* @author Owen Winkler <[email protected]> |
|
17
|
|
|
* @author Robin Appelman <[email protected]> |
|
18
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
19
|
|
|
* @author Semih Serhat Karakaya <[email protected]> |
|
20
|
|
|
* @author Stefan Schneider <[email protected]> |
|
21
|
|
|
* @author Thomas Müller <[email protected]> |
|
22
|
|
|
* @author Vincent Petry <[email protected]> |
|
23
|
|
|
* |
|
24
|
|
|
* @license AGPL-3.0 |
|
25
|
|
|
* |
|
26
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
27
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
28
|
|
|
* as published by the Free Software Foundation. |
|
29
|
|
|
* |
|
30
|
|
|
* This program is distributed in the hope that it will be useful, |
|
31
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
32
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
33
|
|
|
* GNU Affero General Public License for more details. |
|
34
|
|
|
* |
|
35
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
36
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
37
|
|
|
* |
|
38
|
|
|
*/ |
|
39
|
|
|
namespace OCA\DAV\Connector\Sabre; |
|
40
|
|
|
|
|
41
|
|
|
use Icewind\Streams\CallbackWrapper; |
|
42
|
|
|
use OC\AppFramework\Http\Request; |
|
43
|
|
|
use OC\Files\Filesystem; |
|
44
|
|
|
use OC\Files\Stream\HashWrapper; |
|
45
|
|
|
use OC\Files\View; |
|
46
|
|
|
use OC\Metadata\FileMetadata; |
|
47
|
|
|
use OCA\DAV\AppInfo\Application; |
|
48
|
|
|
use OCA\DAV\Connector\Sabre\Exception\BadGateway; |
|
49
|
|
|
use OCA\DAV\Connector\Sabre\Exception\EntityTooLarge; |
|
50
|
|
|
use OCA\DAV\Connector\Sabre\Exception\FileLocked; |
|
51
|
|
|
use OCA\DAV\Connector\Sabre\Exception\Forbidden as DAVForbiddenException; |
|
52
|
|
|
use OCA\DAV\Connector\Sabre\Exception\UnsupportedMediaType; |
|
53
|
|
|
use OCP\Encryption\Exceptions\GenericEncryptionException; |
|
54
|
|
|
use OCP\Files\EntityTooLargeException; |
|
55
|
|
|
use OCP\Files\FileInfo; |
|
56
|
|
|
use OCP\Files\ForbiddenException; |
|
57
|
|
|
use OCP\Files\GenericFileException; |
|
58
|
|
|
use OCP\Files\InvalidContentException; |
|
59
|
|
|
use OCP\Files\InvalidPathException; |
|
60
|
|
|
use OCP\Files\LockNotAcquiredException; |
|
61
|
|
|
use OCP\Files\NotFoundException; |
|
62
|
|
|
use OCP\Files\NotPermittedException; |
|
63
|
|
|
use OCP\Files\Storage; |
|
64
|
|
|
use OCP\Files\StorageNotAvailableException; |
|
65
|
|
|
use OCP\IL10N; |
|
66
|
|
|
use OCP\L10N\IFactory as IL10NFactory; |
|
67
|
|
|
use OCP\Lock\ILockingProvider; |
|
68
|
|
|
use OCP\Lock\LockedException; |
|
69
|
|
|
use OCP\Share\IManager; |
|
70
|
|
|
use Psr\Log\LoggerInterface; |
|
71
|
|
|
use Sabre\DAV\Exception; |
|
72
|
|
|
use Sabre\DAV\Exception\BadRequest; |
|
73
|
|
|
use Sabre\DAV\Exception\Forbidden; |
|
74
|
|
|
use Sabre\DAV\Exception\NotFound; |
|
75
|
|
|
use Sabre\DAV\Exception\NotImplemented; |
|
76
|
|
|
use Sabre\DAV\Exception\ServiceUnavailable; |
|
77
|
|
|
use Sabre\DAV\IFile; |
|
78
|
|
|
|
|
79
|
|
|
class File extends Node implements IFile { |
|
80
|
|
|
protected $request; |
|
81
|
|
|
|
|
82
|
|
|
protected IL10N $l10n; |
|
83
|
|
|
|
|
84
|
|
|
/** @var array<string, FileMetadata> */ |
|
85
|
|
|
private array $metadata = []; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Sets up the node, expects a full path name |
|
89
|
|
|
* |
|
90
|
|
|
* @param \OC\Files\View $view |
|
91
|
|
|
* @param \OCP\Files\FileInfo $info |
|
92
|
|
|
* @param \OCP\Share\IManager $shareManager |
|
93
|
|
|
* @param \OC\AppFramework\Http\Request $request |
|
94
|
|
|
*/ |
|
95
|
|
|
public function __construct(View $view, FileInfo $info, IManager $shareManager = null, Request $request = null) { |
|
96
|
|
|
parent::__construct($view, $info, $shareManager); |
|
97
|
|
|
|
|
98
|
|
|
// Querying IL10N directly results in a dependency loop |
|
99
|
|
|
/** @var IL10NFactory $l10nFactory */ |
|
100
|
|
|
$l10nFactory = \OC::$server->get(IL10NFactory::class); |
|
101
|
|
|
$this->l10n = $l10nFactory->get(Application::APP_ID); |
|
102
|
|
|
|
|
103
|
|
|
if (isset($request)) { |
|
104
|
|
|
$this->request = $request; |
|
105
|
|
|
} else { |
|
106
|
|
|
$this->request = \OC::$server->getRequest(); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Updates the data |
|
112
|
|
|
* |
|
113
|
|
|
* The data argument is a readable stream resource. |
|
114
|
|
|
* |
|
115
|
|
|
* After a successful put operation, you may choose to return an ETag. The |
|
116
|
|
|
* etag must always be surrounded by double-quotes. These quotes must |
|
117
|
|
|
* appear in the actual string you're returning. |
|
118
|
|
|
* |
|
119
|
|
|
* Clients may use the ETag from a PUT request to later on make sure that |
|
120
|
|
|
* when they update the file, the contents haven't changed in the mean |
|
121
|
|
|
* time. |
|
122
|
|
|
* |
|
123
|
|
|
* If you don't plan to store the file byte-by-byte, and you return a |
|
124
|
|
|
* different object on a subsequent GET you are strongly recommended to not |
|
125
|
|
|
* return an ETag, and just return null. |
|
126
|
|
|
* |
|
127
|
|
|
* @param resource $data |
|
128
|
|
|
* |
|
129
|
|
|
* @throws Forbidden |
|
130
|
|
|
* @throws UnsupportedMediaType |
|
131
|
|
|
* @throws BadRequest |
|
132
|
|
|
* @throws Exception |
|
133
|
|
|
* @throws EntityTooLarge |
|
134
|
|
|
* @throws ServiceUnavailable |
|
135
|
|
|
* @throws FileLocked |
|
136
|
|
|
* @return string|null |
|
137
|
|
|
*/ |
|
138
|
|
|
public function put($data) { |
|
139
|
|
|
try { |
|
140
|
|
|
$exists = $this->fileView->file_exists($this->path); |
|
141
|
|
|
if ($exists && !$this->info->isUpdateable()) { |
|
142
|
|
|
throw new Forbidden(); |
|
143
|
|
|
} |
|
144
|
|
|
} catch (StorageNotAvailableException $e) { |
|
145
|
|
|
throw new ServiceUnavailable($this->l10n->t('File is not updatable: %1$s', [$e->getMessage()])); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
// verify path of the target |
|
149
|
|
|
$this->verifyPath(); |
|
150
|
|
|
|
|
151
|
|
|
/** @var Storage $partStorage */ |
|
152
|
|
|
[$partStorage] = $this->fileView->resolvePath($this->path); |
|
153
|
|
|
$needsPartFile = $partStorage->needsPartFile() && (strlen($this->path) > 1); |
|
154
|
|
|
|
|
155
|
|
|
$view = \OC\Files\Filesystem::getView(); |
|
156
|
|
|
|
|
157
|
|
|
if ($needsPartFile) { |
|
158
|
|
|
// mark file as partial while uploading (ignored by the scanner) |
|
159
|
|
|
$partFilePath = $this->getPartFileBasePath($this->path) . '.ocTransferId' . rand() . '.part'; |
|
160
|
|
|
|
|
161
|
|
|
if (!$view->isCreatable($partFilePath) && $view->isUpdatable($this->path)) { |
|
|
|
|
|
|
162
|
|
|
$needsPartFile = false; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
if (!$needsPartFile) { |
|
166
|
|
|
// upload file directly as the final path |
|
167
|
|
|
$partFilePath = $this->path; |
|
168
|
|
|
|
|
169
|
|
|
if ($view && !$this->emitPreHooks($exists)) { |
|
170
|
|
|
throw new Exception($this->l10n->t('Could not write to final file, canceled by hook')); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
// the part file and target file might be on a different storage in case of a single file storage (e.g. single file share) |
|
175
|
|
|
/** @var \OC\Files\Storage\Storage $partStorage */ |
|
176
|
|
|
[$partStorage, $internalPartPath] = $this->fileView->resolvePath($partFilePath); |
|
|
|
|
|
|
177
|
|
|
/** @var \OC\Files\Storage\Storage $storage */ |
|
178
|
|
|
[$storage, $internalPath] = $this->fileView->resolvePath($this->path); |
|
179
|
|
|
try { |
|
180
|
|
|
if (!$needsPartFile) { |
|
181
|
|
|
try { |
|
182
|
|
|
$this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
183
|
|
|
} catch (LockedException $e) { |
|
184
|
|
|
// during very large uploads, the shared lock we got at the start might have been expired |
|
185
|
|
|
// meaning that the above lock can fail not just only because somebody else got a shared lock |
|
186
|
|
|
// or because there is no existing shared lock to make exclusive |
|
187
|
|
|
// |
|
188
|
|
|
// Thus we try to get a new exclusive lock, if the original lock failed because of a different shared |
|
189
|
|
|
// lock this will still fail, if our original shared lock expired the new lock will be successful and |
|
190
|
|
|
// the entire operation will be safe |
|
191
|
|
|
|
|
192
|
|
|
try { |
|
193
|
|
|
$this->acquireLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
194
|
|
|
} catch (LockedException $ex) { |
|
195
|
|
|
throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
if (!is_resource($data)) { |
|
201
|
|
|
$tmpData = fopen('php://temp', 'r+'); |
|
202
|
|
|
if ($data !== null) { |
|
203
|
|
|
fwrite($tmpData, $data); |
|
204
|
|
|
rewind($tmpData); |
|
205
|
|
|
} |
|
206
|
|
|
$data = $tmpData; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
if ($this->request->getHeader('X-HASH') !== '') { |
|
210
|
|
|
$hash = $this->request->getHeader('X-HASH'); |
|
211
|
|
|
if ($hash === 'all' || $hash === 'md5') { |
|
212
|
|
|
$data = HashWrapper::wrap($data, 'md5', function ($hash) { |
|
213
|
|
|
$this->header('X-Hash-MD5: ' . $hash); |
|
214
|
|
|
}); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
if ($hash === 'all' || $hash === 'sha1') { |
|
218
|
|
|
$data = HashWrapper::wrap($data, 'sha1', function ($hash) { |
|
219
|
|
|
$this->header('X-Hash-SHA1: ' . $hash); |
|
220
|
|
|
}); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
if ($hash === 'all' || $hash === 'sha256') { |
|
224
|
|
|
$data = HashWrapper::wrap($data, 'sha256', function ($hash) { |
|
225
|
|
|
$this->header('X-Hash-SHA256: ' . $hash); |
|
226
|
|
|
}); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
if ($partStorage->instanceOfStorage(Storage\IWriteStreamStorage::class)) { |
|
231
|
|
|
$isEOF = false; |
|
232
|
|
|
$wrappedData = CallbackWrapper::wrap($data, null, null, null, null, function ($stream) use (&$isEOF) { |
|
233
|
|
|
$isEOF = feof($stream); |
|
234
|
|
|
}); |
|
235
|
|
|
|
|
236
|
|
|
$result = true; |
|
237
|
|
|
$count = -1; |
|
|
|
|
|
|
238
|
|
|
try { |
|
239
|
|
|
$count = $partStorage->writeStream($internalPartPath, $wrappedData); |
|
|
|
|
|
|
240
|
|
|
} catch (GenericFileException $e) { |
|
241
|
|
|
$result = false; |
|
242
|
|
|
} catch (BadGateway $e) { |
|
243
|
|
|
throw $e; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
if ($result === false) { |
|
248
|
|
|
$result = $isEOF; |
|
249
|
|
|
if (is_resource($wrappedData)) { |
|
250
|
|
|
$result = feof($wrappedData); |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
} else { |
|
254
|
|
|
$target = $partStorage->fopen($internalPartPath, 'wb'); |
|
255
|
|
|
if ($target === false) { |
|
256
|
|
|
\OC::$server->get(LoggerInterface::class)->error('\OC\Files\Filesystem::fopen() failed', ['app' => 'webdav']); |
|
257
|
|
|
// because we have no clue about the cause we can only throw back a 500/Internal Server Error |
|
258
|
|
|
throw new Exception($this->l10n->t('Could not write file contents')); |
|
259
|
|
|
} |
|
260
|
|
|
[$count, $result] = \OC_Helper::streamCopy($data, $target); |
|
261
|
|
|
fclose($target); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
if ($result === false) { |
|
265
|
|
|
$expected = -1; |
|
266
|
|
|
if (isset($_SERVER['CONTENT_LENGTH'])) { |
|
267
|
|
|
$expected = (int)$_SERVER['CONTENT_LENGTH']; |
|
268
|
|
|
} |
|
269
|
|
|
if ($expected !== 0) { |
|
270
|
|
|
throw new Exception( |
|
271
|
|
|
$this->l10n->t( |
|
272
|
|
|
'Error while copying file to target location (copied: %1$s, expected filesize: %2$s)', |
|
273
|
|
|
[ |
|
274
|
|
|
$this->l10n->n('%n byte', '%n bytes', $count), |
|
275
|
|
|
$this->l10n->n('%n byte', '%n bytes', $expected), |
|
276
|
|
|
], |
|
277
|
|
|
) |
|
278
|
|
|
); |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
// if content length is sent by client: |
|
283
|
|
|
// double check if the file was fully received |
|
284
|
|
|
// compare expected and actual size |
|
285
|
|
|
if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { |
|
286
|
|
|
$expected = (int)$_SERVER['CONTENT_LENGTH']; |
|
287
|
|
|
if ($count !== $expected) { |
|
288
|
|
|
throw new BadRequest( |
|
289
|
|
|
$this->l10n->t( |
|
290
|
|
|
'Expected filesize of %1$s but read (from Nextcloud client) and wrote (to Nextcloud storage) %2$s. Could either be a network problem on the sending side or a problem writing to the storage on the server side.', |
|
291
|
|
|
[ |
|
292
|
|
|
$this->l10n->n('%n byte', '%n bytes', $expected), |
|
293
|
|
|
$this->l10n->n('%n byte', '%n bytes', $count), |
|
294
|
|
|
], |
|
295
|
|
|
) |
|
296
|
|
|
); |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
} catch (\Exception $e) { |
|
300
|
|
|
if ($e instanceof LockedException) { |
|
301
|
|
|
\OC::$server->get(LoggerInterface::class)->debug($e->getMessage(), ['exception' => $e]); |
|
302
|
|
|
} else { |
|
303
|
|
|
\OC::$server->get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
if ($needsPartFile) { |
|
307
|
|
|
$partStorage->unlink($internalPartPath); |
|
308
|
|
|
} |
|
309
|
|
|
$this->convertToSabreException($e); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
try { |
|
313
|
|
|
if ($needsPartFile) { |
|
314
|
|
|
if ($view && !$this->emitPreHooks($exists)) { |
|
315
|
|
|
$partStorage->unlink($internalPartPath); |
|
316
|
|
|
throw new Exception($this->l10n->t('Could not rename part file to final file, canceled by hook')); |
|
317
|
|
|
} |
|
318
|
|
|
try { |
|
319
|
|
|
$this->changeLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
320
|
|
|
} catch (LockedException $e) { |
|
321
|
|
|
// during very large uploads, the shared lock we got at the start might have been expired |
|
322
|
|
|
// meaning that the above lock can fail not just only because somebody else got a shared lock |
|
323
|
|
|
// or because there is no existing shared lock to make exclusive |
|
324
|
|
|
// |
|
325
|
|
|
// Thus we try to get a new exclusive lock, if the original lock failed because of a different shared |
|
326
|
|
|
// lock this will still fail, if our original shared lock expired the new lock will be successful and |
|
327
|
|
|
// the entire operation will be safe |
|
328
|
|
|
|
|
329
|
|
|
try { |
|
330
|
|
|
$this->acquireLock(ILockingProvider::LOCK_EXCLUSIVE); |
|
331
|
|
|
} catch (LockedException $ex) { |
|
332
|
|
|
if ($needsPartFile) { |
|
|
|
|
|
|
333
|
|
|
$partStorage->unlink($internalPartPath); |
|
334
|
|
|
} |
|
335
|
|
|
throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
// rename to correct path |
|
340
|
|
|
try { |
|
341
|
|
|
$renameOkay = $storage->moveFromStorage($partStorage, $internalPartPath, $internalPath); |
|
342
|
|
|
$fileExists = $storage->file_exists($internalPath); |
|
343
|
|
|
if ($renameOkay === false || $fileExists === false) { |
|
344
|
|
|
\OC::$server->get(LoggerInterface::class)->error('renaming part file to final file failed $renameOkay: ' . ($renameOkay ? 'true' : 'false') . ', $fileExists: ' . ($fileExists ? 'true' : 'false') . ')', ['app' => 'webdav']); |
|
345
|
|
|
throw new Exception($this->l10n->t('Could not rename part file to final file')); |
|
346
|
|
|
} |
|
347
|
|
|
} catch (ForbiddenException $ex) { |
|
348
|
|
|
if (!$ex->getRetry()) { |
|
349
|
|
|
$partStorage->unlink($internalPartPath); |
|
350
|
|
|
} |
|
351
|
|
|
throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
352
|
|
|
} catch (\Exception $e) { |
|
353
|
|
|
$partStorage->unlink($internalPartPath); |
|
354
|
|
|
$this->convertToSabreException($e); |
|
355
|
|
|
} |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
// since we skipped the view we need to scan and emit the hooks ourselves |
|
359
|
|
|
$storage->getUpdater()->update($internalPath); |
|
360
|
|
|
|
|
361
|
|
|
try { |
|
362
|
|
|
$this->changeLock(ILockingProvider::LOCK_SHARED); |
|
363
|
|
|
} catch (LockedException $e) { |
|
364
|
|
|
throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
// allow sync clients to send the mtime along in a header |
|
368
|
|
|
if (isset($this->request->server['HTTP_X_OC_MTIME'])) { |
|
369
|
|
|
$mtime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_MTIME']); |
|
370
|
|
|
if ($this->fileView->touch($this->path, $mtime)) { |
|
371
|
|
|
$this->header('X-OC-MTime: accepted'); |
|
372
|
|
|
} |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
$fileInfoUpdate = [ |
|
376
|
|
|
'upload_time' => time() |
|
377
|
|
|
]; |
|
378
|
|
|
|
|
379
|
|
|
// allow sync clients to send the creation time along in a header |
|
380
|
|
|
if (isset($this->request->server['HTTP_X_OC_CTIME'])) { |
|
381
|
|
|
$ctime = $this->sanitizeMtime($this->request->server['HTTP_X_OC_CTIME']); |
|
382
|
|
|
$fileInfoUpdate['creation_time'] = $ctime; |
|
383
|
|
|
$this->header('X-OC-CTime: accepted'); |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
$this->fileView->putFileInfo($this->path, $fileInfoUpdate); |
|
387
|
|
|
|
|
388
|
|
|
if ($view) { |
|
|
|
|
|
|
389
|
|
|
$this->emitPostHooks($exists); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
$this->refreshInfo(); |
|
393
|
|
|
|
|
394
|
|
|
if (isset($this->request->server['HTTP_OC_CHECKSUM'])) { |
|
395
|
|
|
$checksum = trim($this->request->server['HTTP_OC_CHECKSUM']); |
|
396
|
|
|
$this->setChecksum($checksum); |
|
397
|
|
|
} elseif ($this->getChecksum() !== null && $this->getChecksum() !== '') { |
|
398
|
|
|
$this->setChecksum(''); |
|
399
|
|
|
} |
|
400
|
|
|
} catch (StorageNotAvailableException $e) { |
|
401
|
|
|
throw new ServiceUnavailable($this->l10n->t('Failed to check file size: %1$s', [$e->getMessage()]), 0, $e); |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
return '"' . $this->info->getEtag() . '"'; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
private function getPartFileBasePath($path) { |
|
408
|
|
|
$partFileInStorage = \OC::$server->getConfig()->getSystemValue('part_file_in_storage', true); |
|
409
|
|
|
if ($partFileInStorage) { |
|
410
|
|
|
return $path; |
|
411
|
|
|
} else { |
|
412
|
|
|
return md5($path); // will place it in the root of the view with a unique name |
|
413
|
|
|
} |
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
|
|
/** |
|
417
|
|
|
* @param string $path |
|
418
|
|
|
*/ |
|
419
|
|
|
private function emitPreHooks($exists, $path = null) { |
|
420
|
|
|
if (is_null($path)) { |
|
421
|
|
|
$path = $this->path; |
|
422
|
|
|
} |
|
423
|
|
|
$hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
424
|
|
|
$run = true; |
|
425
|
|
|
|
|
426
|
|
|
if (!$exists) { |
|
427
|
|
|
\OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_create, [ |
|
428
|
|
|
\OC\Files\Filesystem::signal_param_path => $hookPath, |
|
429
|
|
|
\OC\Files\Filesystem::signal_param_run => &$run, |
|
430
|
|
|
]); |
|
431
|
|
|
} else { |
|
432
|
|
|
\OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_update, [ |
|
433
|
|
|
\OC\Files\Filesystem::signal_param_path => $hookPath, |
|
434
|
|
|
\OC\Files\Filesystem::signal_param_run => &$run, |
|
435
|
|
|
]); |
|
436
|
|
|
} |
|
437
|
|
|
\OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_write, [ |
|
438
|
|
|
\OC\Files\Filesystem::signal_param_path => $hookPath, |
|
439
|
|
|
\OC\Files\Filesystem::signal_param_run => &$run, |
|
440
|
|
|
]); |
|
441
|
|
|
return $run; |
|
442
|
|
|
} |
|
443
|
|
|
|
|
444
|
|
|
/** |
|
445
|
|
|
* @param string $path |
|
446
|
|
|
*/ |
|
447
|
|
|
private function emitPostHooks($exists, $path = null) { |
|
448
|
|
|
if (is_null($path)) { |
|
449
|
|
|
$path = $this->path; |
|
450
|
|
|
} |
|
451
|
|
|
$hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path)); |
|
452
|
|
|
if (!$exists) { |
|
453
|
|
|
\OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_create, [ |
|
454
|
|
|
\OC\Files\Filesystem::signal_param_path => $hookPath |
|
455
|
|
|
]); |
|
456
|
|
|
} else { |
|
457
|
|
|
\OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_update, [ |
|
458
|
|
|
\OC\Files\Filesystem::signal_param_path => $hookPath |
|
459
|
|
|
]); |
|
460
|
|
|
} |
|
461
|
|
|
\OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_write, [ |
|
462
|
|
|
\OC\Files\Filesystem::signal_param_path => $hookPath |
|
463
|
|
|
]); |
|
464
|
|
|
} |
|
465
|
|
|
|
|
466
|
|
|
/** |
|
467
|
|
|
* Returns the data |
|
468
|
|
|
* |
|
469
|
|
|
* @return resource |
|
470
|
|
|
* @throws Forbidden |
|
471
|
|
|
* @throws ServiceUnavailable |
|
472
|
|
|
*/ |
|
473
|
|
|
public function get() { |
|
474
|
|
|
//throw exception if encryption is disabled but files are still encrypted |
|
475
|
|
|
try { |
|
476
|
|
|
if (!$this->info->isReadable()) { |
|
477
|
|
|
// do a if the file did not exist |
|
478
|
|
|
throw new NotFound(); |
|
479
|
|
|
} |
|
480
|
|
|
try { |
|
481
|
|
|
$res = $this->fileView->fopen(ltrim($this->path, '/'), 'rb'); |
|
482
|
|
|
} catch (\Exception $e) { |
|
483
|
|
|
$this->convertToSabreException($e); |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
|
|
if ($res === false) { |
|
|
|
|
|
|
487
|
|
|
throw new ServiceUnavailable($this->l10n->t('Could not open file')); |
|
488
|
|
|
} |
|
489
|
|
|
|
|
490
|
|
|
// comparing current file size with the one in DB |
|
491
|
|
|
// if different, fix DB and refresh cache. |
|
492
|
|
|
if ($this->getSize() !== $this->fileView->filesize($this->getPath())) { |
|
493
|
|
|
$logger = \OC::$server->get(LoggerInterface::class); |
|
494
|
|
|
$logger->warning('fixing cached size of file id=' . $this->getId()); |
|
495
|
|
|
|
|
496
|
|
|
$this->getFileInfo()->getStorage()->getUpdater()->update($this->getFileInfo()->getInternalPath()); |
|
497
|
|
|
$this->refreshInfo(); |
|
498
|
|
|
} |
|
499
|
|
|
|
|
500
|
|
|
return $res; |
|
501
|
|
|
} catch (GenericEncryptionException $e) { |
|
502
|
|
|
// returning 503 will allow retry of the operation at a later point in time |
|
503
|
|
|
throw new ServiceUnavailable($this->l10n->t('Encryption not ready: %1$s', [$e->getMessage()])); |
|
504
|
|
|
} catch (StorageNotAvailableException $e) { |
|
505
|
|
|
throw new ServiceUnavailable($this->l10n->t('Failed to open file: %1$s', [$e->getMessage()])); |
|
506
|
|
|
} catch (ForbiddenException $ex) { |
|
507
|
|
|
throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
508
|
|
|
} catch (LockedException $e) { |
|
509
|
|
|
throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
510
|
|
|
} |
|
511
|
|
|
} |
|
512
|
|
|
|
|
513
|
|
|
/** |
|
514
|
|
|
* Delete the current file |
|
515
|
|
|
* |
|
516
|
|
|
* @throws Forbidden |
|
517
|
|
|
* @throws ServiceUnavailable |
|
518
|
|
|
*/ |
|
519
|
|
|
public function delete() { |
|
520
|
|
|
if (!$this->info->isDeletable()) { |
|
521
|
|
|
throw new Forbidden(); |
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
|
|
try { |
|
525
|
|
|
if (!$this->fileView->unlink($this->path)) { |
|
|
|
|
|
|
526
|
|
|
// assume it wasn't possible to delete due to permissions |
|
527
|
|
|
throw new Forbidden(); |
|
528
|
|
|
} |
|
529
|
|
|
} catch (StorageNotAvailableException $e) { |
|
530
|
|
|
throw new ServiceUnavailable($this->l10n->t('Failed to unlink: %1$s', [$e->getMessage()])); |
|
531
|
|
|
} catch (ForbiddenException $ex) { |
|
532
|
|
|
throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry()); |
|
533
|
|
|
} catch (LockedException $e) { |
|
534
|
|
|
throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
535
|
|
|
} |
|
536
|
|
|
} |
|
537
|
|
|
|
|
538
|
|
|
/** |
|
539
|
|
|
* Returns the mime-type for a file |
|
540
|
|
|
* |
|
541
|
|
|
* If null is returned, we'll assume application/octet-stream |
|
542
|
|
|
* |
|
543
|
|
|
* @return string |
|
544
|
|
|
*/ |
|
545
|
|
|
public function getContentType() { |
|
546
|
|
|
$mimeType = $this->info->getMimetype(); |
|
547
|
|
|
|
|
548
|
|
|
// PROPFIND needs to return the correct mime type, for consistency with the web UI |
|
549
|
|
|
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') { |
|
550
|
|
|
return $mimeType; |
|
551
|
|
|
} |
|
552
|
|
|
return \OC::$server->getMimeTypeDetector()->getSecureMimeType($mimeType); |
|
553
|
|
|
} |
|
554
|
|
|
|
|
555
|
|
|
/** |
|
556
|
|
|
* @return array|bool |
|
557
|
|
|
*/ |
|
558
|
|
|
public function getDirectDownload() { |
|
559
|
|
|
if (\OCP\Server::get(\OCP\App\IAppManager::class)->isEnabledForUser('encryption')) { |
|
560
|
|
|
return []; |
|
561
|
|
|
} |
|
562
|
|
|
/** @var \OCP\Files\Storage $storage */ |
|
563
|
|
|
[$storage, $internalPath] = $this->fileView->resolvePath($this->path); |
|
564
|
|
|
if (is_null($storage)) { |
|
565
|
|
|
return []; |
|
566
|
|
|
} |
|
567
|
|
|
|
|
568
|
|
|
return $storage->getDirectDownload($internalPath); |
|
569
|
|
|
} |
|
570
|
|
|
|
|
571
|
|
|
/** |
|
572
|
|
|
* Convert the given exception to a SabreException instance |
|
573
|
|
|
* |
|
574
|
|
|
* @param \Exception $e |
|
575
|
|
|
* |
|
576
|
|
|
* @throws \Sabre\DAV\Exception |
|
577
|
|
|
*/ |
|
578
|
|
|
private function convertToSabreException(\Exception $e) { |
|
579
|
|
|
if ($e instanceof \Sabre\DAV\Exception) { |
|
580
|
|
|
throw $e; |
|
581
|
|
|
} |
|
582
|
|
|
if ($e instanceof NotPermittedException) { |
|
583
|
|
|
// a more general case - due to whatever reason the content could not be written |
|
584
|
|
|
throw new Forbidden($e->getMessage(), 0, $e); |
|
585
|
|
|
} |
|
586
|
|
|
if ($e instanceof ForbiddenException) { |
|
587
|
|
|
// the path for the file was forbidden |
|
588
|
|
|
throw new DAVForbiddenException($e->getMessage(), $e->getRetry(), $e); |
|
589
|
|
|
} |
|
590
|
|
|
if ($e instanceof EntityTooLargeException) { |
|
591
|
|
|
// the file is too big to be stored |
|
592
|
|
|
throw new EntityTooLarge($e->getMessage(), 0, $e); |
|
593
|
|
|
} |
|
594
|
|
|
if ($e instanceof InvalidContentException) { |
|
595
|
|
|
// the file content is not permitted |
|
596
|
|
|
throw new UnsupportedMediaType($e->getMessage(), 0, $e); |
|
597
|
|
|
} |
|
598
|
|
|
if ($e instanceof InvalidPathException) { |
|
599
|
|
|
// the path for the file was not valid |
|
600
|
|
|
// TODO: find proper http status code for this case |
|
601
|
|
|
throw new Forbidden($e->getMessage(), 0, $e); |
|
602
|
|
|
} |
|
603
|
|
|
if ($e instanceof LockedException || $e instanceof LockNotAcquiredException) { |
|
604
|
|
|
// the file is currently being written to by another process |
|
605
|
|
|
throw new FileLocked($e->getMessage(), $e->getCode(), $e); |
|
606
|
|
|
} |
|
607
|
|
|
if ($e instanceof GenericEncryptionException) { |
|
608
|
|
|
// returning 503 will allow retry of the operation at a later point in time |
|
609
|
|
|
throw new ServiceUnavailable($this->l10n->t('Encryption not ready: %1$s', [$e->getMessage()]), 0, $e); |
|
610
|
|
|
} |
|
611
|
|
|
if ($e instanceof StorageNotAvailableException) { |
|
612
|
|
|
throw new ServiceUnavailable($this->l10n->t('Failed to write file contents: %1$s', [$e->getMessage()]), 0, $e); |
|
613
|
|
|
} |
|
614
|
|
|
if ($e instanceof NotFoundException) { |
|
615
|
|
|
throw new NotFound($this->l10n->t('File not found: %1$s', [$e->getMessage()]), 0, $e); |
|
616
|
|
|
} |
|
617
|
|
|
|
|
618
|
|
|
throw new \Sabre\DAV\Exception($e->getMessage(), 0, $e); |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
/** |
|
622
|
|
|
* Get the checksum for this file |
|
623
|
|
|
* |
|
624
|
|
|
* @return string|null |
|
625
|
|
|
*/ |
|
626
|
|
|
public function getChecksum() { |
|
627
|
|
|
return $this->info->getChecksum(); |
|
628
|
|
|
} |
|
629
|
|
|
|
|
630
|
|
|
public function setChecksum(string $checksum) { |
|
631
|
|
|
$this->fileView->putFileInfo($this->path, ['checksum' => $checksum]); |
|
632
|
|
|
$this->refreshInfo(); |
|
633
|
|
|
} |
|
634
|
|
|
|
|
635
|
|
|
protected function header($string) { |
|
636
|
|
|
if (!\OC::$CLI) { |
|
637
|
|
|
\header($string); |
|
638
|
|
|
} |
|
639
|
|
|
} |
|
640
|
|
|
|
|
641
|
|
|
public function hash(string $type) { |
|
642
|
|
|
return $this->fileView->hash($type, $this->path); |
|
643
|
|
|
} |
|
644
|
|
|
|
|
645
|
|
|
public function getNode(): \OCP\Files\File { |
|
646
|
|
|
return $this->node; |
|
|
|
|
|
|
647
|
|
|
} |
|
648
|
|
|
|
|
649
|
|
|
public function getMetadata(string $group): FileMetadata { |
|
650
|
|
|
return $this->metadata[$group]; |
|
651
|
|
|
} |
|
652
|
|
|
|
|
653
|
|
|
public function setMetadata(string $group, FileMetadata $metadata): void { |
|
654
|
|
|
$this->metadata[$group] = $metadata; |
|
655
|
|
|
} |
|
656
|
|
|
|
|
657
|
|
|
public function hasMetadata(string $group) { |
|
658
|
|
|
return array_key_exists($group, $this->metadata); |
|
659
|
|
|
} |
|
660
|
|
|
} |
|
661
|
|
|
|
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.