File.php 20 KB

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