File.php 20 KB

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