File.php 20 KB

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