File.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Jakob Sack <mail@jakobsack.de>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Owen Winkler <a_github@midnightcircus.com>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. * @author Vincent Petry <pvince81@owncloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OCA\DAV\Connector\Sabre;
  34. use OC\Files\Filesystem;
  35. use OCA\DAV\Connector\Sabre\Exception\EntityTooLarge;
  36. use OCA\DAV\Connector\Sabre\Exception\FileLocked;
  37. use OCA\DAV\Connector\Sabre\Exception\Forbidden as DAVForbiddenException;
  38. use OCA\DAV\Connector\Sabre\Exception\UnsupportedMediaType;
  39. use OCP\Encryption\Exceptions\GenericEncryptionException;
  40. use OCP\Files\EntityTooLargeException;
  41. use OCP\Files\ForbiddenException;
  42. use OCP\Files\InvalidContentException;
  43. use OCP\Files\InvalidPathException;
  44. use OCP\Files\LockNotAcquiredException;
  45. use OCP\Files\NotPermittedException;
  46. use OCP\Files\StorageNotAvailableException;
  47. use OCP\Lock\ILockingProvider;
  48. use OCP\Lock\LockedException;
  49. use Sabre\DAV\Exception;
  50. use Sabre\DAV\Exception\BadRequest;
  51. use Sabre\DAV\Exception\Forbidden;
  52. use Sabre\DAV\Exception\NotImplemented;
  53. use Sabre\DAV\Exception\ServiceUnavailable;
  54. use Sabre\DAV\IFile;
  55. class File extends Node implements IFile {
  56. /**
  57. * Updates the data
  58. *
  59. * The data argument is a readable stream resource.
  60. *
  61. * After a successful put operation, you may choose to return an ETag. The
  62. * etag must always be surrounded by double-quotes. These quotes must
  63. * appear in the actual string you're returning.
  64. *
  65. * Clients may use the ETag from a PUT request to later on make sure that
  66. * when they update the file, the contents haven't changed in the mean
  67. * time.
  68. *
  69. * If you don't plan to store the file byte-by-byte, and you return a
  70. * different object on a subsequent GET you are strongly recommended to not
  71. * return an ETag, and just return null.
  72. *
  73. * @param resource $data
  74. *
  75. * @throws Forbidden
  76. * @throws UnsupportedMediaType
  77. * @throws BadRequest
  78. * @throws Exception
  79. * @throws EntityTooLarge
  80. * @throws ServiceUnavailable
  81. * @throws FileLocked
  82. * @return string|null
  83. */
  84. public function put($data) {
  85. try {
  86. $exists = $this->fileView->file_exists($this->path);
  87. if ($this->info && $exists && !$this->info->isUpdateable()) {
  88. throw new Forbidden();
  89. }
  90. } catch (StorageNotAvailableException $e) {
  91. throw new ServiceUnavailable("File is not updatable: " . $e->getMessage());
  92. }
  93. // verify path of the target
  94. $this->verifyPath();
  95. // chunked handling
  96. if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
  97. try {
  98. return $this->createFileChunked($data);
  99. } catch (\Exception $e) {
  100. $this->convertToSabreException($e);
  101. }
  102. }
  103. list($partStorage) = $this->fileView->resolvePath($this->path);
  104. $needsPartFile = $this->needsPartFile($partStorage) && (strlen($this->path) > 1);
  105. if ($needsPartFile) {
  106. // mark file as partial while uploading (ignored by the scanner)
  107. $partFilePath = $this->getPartFileBasePath($this->path) . '.ocTransferId' . rand() . '.part';
  108. } else {
  109. // upload file directly as the final path
  110. $partFilePath = $this->path;
  111. }
  112. // the part file and target file might be on a different storage in case of a single file storage (e.g. single file share)
  113. /** @var \OC\Files\Storage\Storage $partStorage */
  114. list($partStorage, $internalPartPath) = $this->fileView->resolvePath($partFilePath);
  115. /** @var \OC\Files\Storage\Storage $storage */
  116. list($storage, $internalPath) = $this->fileView->resolvePath($this->path);
  117. try {
  118. $target = $partStorage->fopen($internalPartPath, 'wb');
  119. if ($target === false) {
  120. \OCP\Util::writeLog('webdav', '\OC\Files\Filesystem::fopen() failed', \OCP\Util::ERROR);
  121. // because we have no clue about the cause we can only throw back a 500/Internal Server Error
  122. throw new Exception('Could not write file contents');
  123. }
  124. list($count, $result) = \OC_Helper::streamCopy($data, $target);
  125. fclose($target);
  126. if ($result === false) {
  127. $expected = -1;
  128. if (isset($_SERVER['CONTENT_LENGTH'])) {
  129. $expected = $_SERVER['CONTENT_LENGTH'];
  130. }
  131. throw new Exception('Error while copying file to target location (copied bytes: ' . $count . ', expected filesize: ' . $expected . ' )');
  132. }
  133. // if content length is sent by client:
  134. // double check if the file was fully received
  135. // compare expected and actual size
  136. if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['REQUEST_METHOD'] === 'PUT') {
  137. $expected = $_SERVER['CONTENT_LENGTH'];
  138. if ($count != $expected) {
  139. throw new BadRequest('expected filesize ' . $expected . ' got ' . $count);
  140. }
  141. }
  142. } catch (\Exception $e) {
  143. if ($needsPartFile) {
  144. $partStorage->unlink($internalPartPath);
  145. }
  146. $this->convertToSabreException($e);
  147. }
  148. try {
  149. $view = \OC\Files\Filesystem::getView();
  150. if ($view) {
  151. $run = $this->emitPreHooks($exists);
  152. } else {
  153. $run = true;
  154. }
  155. try {
  156. $this->changeLock(ILockingProvider::LOCK_EXCLUSIVE);
  157. } catch (LockedException $e) {
  158. if ($needsPartFile) {
  159. $partStorage->unlink($internalPartPath);
  160. }
  161. throw new FileLocked($e->getMessage(), $e->getCode(), $e);
  162. }
  163. if ($needsPartFile) {
  164. // rename to correct path
  165. try {
  166. if ($run) {
  167. $renameOkay = $storage->moveFromStorage($partStorage, $internalPartPath, $internalPath);
  168. $fileExists = $storage->file_exists($internalPath);
  169. }
  170. if (!$run || $renameOkay === false || $fileExists === false) {
  171. \OCP\Util::writeLog('webdav', 'renaming part file to final file failed', \OCP\Util::ERROR);
  172. throw new Exception('Could not rename part file to final file');
  173. }
  174. } catch (ForbiddenException $ex) {
  175. throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry());
  176. } catch (\Exception $e) {
  177. $partStorage->unlink($internalPartPath);
  178. $this->convertToSabreException($e);
  179. }
  180. }
  181. // since we skipped the view we need to scan and emit the hooks ourselves
  182. $storage->getUpdater()->update($internalPath);
  183. try {
  184. $this->changeLock(ILockingProvider::LOCK_SHARED);
  185. } catch (LockedException $e) {
  186. throw new FileLocked($e->getMessage(), $e->getCode(), $e);
  187. }
  188. // allow sync clients to send the mtime along in a header
  189. $request = \OC::$server->getRequest();
  190. if (isset($request->server['HTTP_X_OC_MTIME'])) {
  191. if ($this->fileView->touch($this->path, $request->server['HTTP_X_OC_MTIME'])) {
  192. header('X-OC-MTime: accepted');
  193. }
  194. }
  195. if ($view) {
  196. $this->emitPostHooks($exists);
  197. }
  198. $this->refreshInfo();
  199. if (isset($request->server['HTTP_OC_CHECKSUM'])) {
  200. $checksum = trim($request->server['HTTP_OC_CHECKSUM']);
  201. $this->fileView->putFileInfo($this->path, ['checksum' => $checksum]);
  202. $this->refreshInfo();
  203. } else if ($this->getChecksum() !== null && $this->getChecksum() !== '') {
  204. $this->fileView->putFileInfo($this->path, ['checksum' => '']);
  205. $this->refreshInfo();
  206. }
  207. } catch (StorageNotAvailableException $e) {
  208. throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage());
  209. }
  210. return '"' . $this->info->getEtag() . '"';
  211. }
  212. private function getPartFileBasePath($path) {
  213. $partFileInStorage = \OC::$server->getConfig()->getSystemValue('part_file_in_storage', true);
  214. if ($partFileInStorage) {
  215. return $path;
  216. } else {
  217. return md5($path); // will place it in the root of the view with a unique name
  218. }
  219. }
  220. /**
  221. * @param string $path
  222. */
  223. private function emitPreHooks($exists, $path = null) {
  224. if (is_null($path)) {
  225. $path = $this->path;
  226. }
  227. $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path));
  228. $run = true;
  229. if (!$exists) {
  230. \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_create, array(
  231. \OC\Files\Filesystem::signal_param_path => $hookPath,
  232. \OC\Files\Filesystem::signal_param_run => &$run,
  233. ));
  234. } else {
  235. \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_update, array(
  236. \OC\Files\Filesystem::signal_param_path => $hookPath,
  237. \OC\Files\Filesystem::signal_param_run => &$run,
  238. ));
  239. }
  240. \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_write, array(
  241. \OC\Files\Filesystem::signal_param_path => $hookPath,
  242. \OC\Files\Filesystem::signal_param_run => &$run,
  243. ));
  244. return $run;
  245. }
  246. /**
  247. * @param string $path
  248. */
  249. private function emitPostHooks($exists, $path = null) {
  250. if (is_null($path)) {
  251. $path = $this->path;
  252. }
  253. $hookPath = Filesystem::getView()->getRelativePath($this->fileView->getAbsolutePath($path));
  254. if (!$exists) {
  255. \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_create, array(
  256. \OC\Files\Filesystem::signal_param_path => $hookPath
  257. ));
  258. } else {
  259. \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_update, array(
  260. \OC\Files\Filesystem::signal_param_path => $hookPath
  261. ));
  262. }
  263. \OC_Hook::emit(\OC\Files\Filesystem::CLASSNAME, \OC\Files\Filesystem::signal_post_write, array(
  264. \OC\Files\Filesystem::signal_param_path => $hookPath
  265. ));
  266. }
  267. /**
  268. * Returns the data
  269. *
  270. * @return resource
  271. * @throws Forbidden
  272. * @throws ServiceUnavailable
  273. */
  274. public function get() {
  275. //throw exception if encryption is disabled but files are still encrypted
  276. try {
  277. $res = $this->fileView->fopen(ltrim($this->path, '/'), 'rb');
  278. if ($res === false) {
  279. throw new ServiceUnavailable("Could not open file");
  280. }
  281. return $res;
  282. } catch (GenericEncryptionException $e) {
  283. // returning 503 will allow retry of the operation at a later point in time
  284. throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage());
  285. } catch (StorageNotAvailableException $e) {
  286. throw new ServiceUnavailable("Failed to open file: " . $e->getMessage());
  287. } catch (ForbiddenException $ex) {
  288. throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry());
  289. } catch (LockedException $e) {
  290. throw new FileLocked($e->getMessage(), $e->getCode(), $e);
  291. }
  292. }
  293. /**
  294. * Delete the current file
  295. *
  296. * @throws Forbidden
  297. * @throws ServiceUnavailable
  298. */
  299. public function delete() {
  300. if (!$this->info->isDeletable()) {
  301. throw new Forbidden();
  302. }
  303. try {
  304. if (!$this->fileView->unlink($this->path)) {
  305. // assume it wasn't possible to delete due to permissions
  306. throw new Forbidden();
  307. }
  308. } catch (StorageNotAvailableException $e) {
  309. throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage());
  310. } catch (ForbiddenException $ex) {
  311. throw new DAVForbiddenException($ex->getMessage(), $ex->getRetry());
  312. } catch (LockedException $e) {
  313. throw new FileLocked($e->getMessage(), $e->getCode(), $e);
  314. }
  315. }
  316. /**
  317. * Returns the mime-type for a file
  318. *
  319. * If null is returned, we'll assume application/octet-stream
  320. *
  321. * @return string
  322. */
  323. public function getContentType() {
  324. $mimeType = $this->info->getMimetype();
  325. // PROPFIND needs to return the correct mime type, for consistency with the web UI
  326. if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PROPFIND') {
  327. return $mimeType;
  328. }
  329. return \OC::$server->getMimeTypeDetector()->getSecureMimeType($mimeType);
  330. }
  331. /**
  332. * @return array|false
  333. */
  334. public function getDirectDownload() {
  335. if (\OCP\App::isEnabled('encryption')) {
  336. return [];
  337. }
  338. /** @var \OCP\Files\Storage $storage */
  339. list($storage, $internalPath) = $this->fileView->resolvePath($this->path);
  340. if (is_null($storage)) {
  341. return [];
  342. }
  343. return $storage->getDirectDownload($internalPath);
  344. }
  345. /**
  346. * @param resource $data
  347. * @return null|string
  348. * @throws Exception
  349. * @throws BadRequest
  350. * @throws NotImplemented
  351. * @throws ServiceUnavailable
  352. */
  353. private function createFileChunked($data) {
  354. list($path, $name) = \Sabre\HTTP\URLUtil::splitPath($this->path);
  355. $info = \OC_FileChunking::decodeName($name);
  356. if (empty($info)) {
  357. throw new NotImplemented('Invalid chunk name');
  358. }
  359. $chunk_handler = new \OC_FileChunking($info);
  360. $bytesWritten = $chunk_handler->store($info['index'], $data);
  361. //detect aborted upload
  362. if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') {
  363. if (isset($_SERVER['CONTENT_LENGTH'])) {
  364. $expected = $_SERVER['CONTENT_LENGTH'];
  365. if ($bytesWritten != $expected) {
  366. $chunk_handler->remove($info['index']);
  367. throw new BadRequest(
  368. 'expected filesize ' . $expected . ' got ' . $bytesWritten);
  369. }
  370. }
  371. }
  372. if ($chunk_handler->isComplete()) {
  373. list($storage,) = $this->fileView->resolvePath($path);
  374. $needsPartFile = $this->needsPartFile($storage);
  375. $partFile = null;
  376. $targetPath = $path . '/' . $info['name'];
  377. /** @var \OC\Files\Storage\Storage $targetStorage */
  378. list($targetStorage, $targetInternalPath) = $this->fileView->resolvePath($targetPath);
  379. $exists = $this->fileView->file_exists($targetPath);
  380. try {
  381. $this->fileView->lockFile($targetPath, ILockingProvider::LOCK_SHARED);
  382. $this->emitPreHooks($exists, $targetPath);
  383. $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_EXCLUSIVE);
  384. /** @var \OC\Files\Storage\Storage $targetStorage */
  385. list($targetStorage, $targetInternalPath) = $this->fileView->resolvePath($targetPath);
  386. if ($needsPartFile) {
  387. // we first assembly the target file as a part file
  388. $partFile = $this->getPartFileBasePath($path . '/' . $info['name']) . '.ocTransferId' . $info['transferid'] . '.part';
  389. /** @var \OC\Files\Storage\Storage $targetStorage */
  390. list($partStorage, $partInternalPath) = $this->fileView->resolvePath($partFile);
  391. $chunk_handler->file_assemble($partStorage, $partInternalPath);
  392. // here is the final atomic rename
  393. $renameOkay = $targetStorage->moveFromStorage($partStorage, $partInternalPath, $targetInternalPath);
  394. $fileExists = $targetStorage->file_exists($targetInternalPath);
  395. if ($renameOkay === false || $fileExists === false) {
  396. \OCP\Util::writeLog('webdav', '\OC\Files\Filesystem::rename() failed', \OCP\Util::ERROR);
  397. // only delete if an error occurred and the target file was already created
  398. if ($fileExists) {
  399. // set to null to avoid double-deletion when handling exception
  400. // stray part file
  401. $partFile = null;
  402. $targetStorage->unlink($targetInternalPath);
  403. }
  404. $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED);
  405. throw new Exception('Could not rename part file assembled from chunks');
  406. }
  407. } else {
  408. // assemble directly into the final file
  409. $chunk_handler->file_assemble($targetStorage, $targetInternalPath);
  410. }
  411. // allow sync clients to send the mtime along in a header
  412. $request = \OC::$server->getRequest();
  413. if (isset($request->server['HTTP_X_OC_MTIME'])) {
  414. if ($targetStorage->touch($targetInternalPath, $request->server['HTTP_X_OC_MTIME'])) {
  415. header('X-OC-MTime: accepted');
  416. }
  417. }
  418. // since we skipped the view we need to scan and emit the hooks ourselves
  419. $targetStorage->getUpdater()->update($targetInternalPath);
  420. $this->fileView->changeLock($targetPath, ILockingProvider::LOCK_SHARED);
  421. $this->emitPostHooks($exists, $targetPath);
  422. // FIXME: should call refreshInfo but can't because $this->path is not the of the final file
  423. $info = $this->fileView->getFileInfo($targetPath);
  424. if (isset($request->server['HTTP_OC_CHECKSUM'])) {
  425. $checksum = trim($request->server['HTTP_OC_CHECKSUM']);
  426. $this->fileView->putFileInfo($targetPath, ['checksum' => $checksum]);
  427. } else if ($info->getChecksum() !== null && $info->getChecksum() !== '') {
  428. $this->fileView->putFileInfo($this->path, ['checksum' => '']);
  429. }
  430. $this->fileView->unlockFile($targetPath, ILockingProvider::LOCK_SHARED);
  431. return $info->getEtag();
  432. } catch (\Exception $e) {
  433. if ($partFile !== null) {
  434. $targetStorage->unlink($targetInternalPath);
  435. }
  436. $this->convertToSabreException($e);
  437. }
  438. }
  439. return null;
  440. }
  441. /**
  442. * Returns whether a part file is needed for the given storage
  443. * or whether the file can be assembled/uploaded directly on the
  444. * target storage.
  445. *
  446. * @param \OCP\Files\Storage $storage
  447. * @return bool true if the storage needs part file handling
  448. */
  449. private function needsPartFile($storage) {
  450. // TODO: in the future use ChunkHandler provided by storage
  451. // and/or add method on Storage called "needsPartFile()"
  452. return !$storage->instanceOfStorage('OCA\Files_Sharing\External\Storage') &&
  453. !$storage->instanceOfStorage('OC\Files\Storage\OwnCloud');
  454. }
  455. /**
  456. * Convert the given exception to a SabreException instance
  457. *
  458. * @param \Exception $e
  459. *
  460. * @throws \Sabre\DAV\Exception
  461. */
  462. private function convertToSabreException(\Exception $e) {
  463. if ($e instanceof \Sabre\DAV\Exception) {
  464. throw $e;
  465. }
  466. if ($e instanceof NotPermittedException) {
  467. // a more general case - due to whatever reason the content could not be written
  468. throw new Forbidden($e->getMessage(), 0, $e);
  469. }
  470. if ($e instanceof ForbiddenException) {
  471. // the path for the file was forbidden
  472. throw new DAVForbiddenException($e->getMessage(), $e->getRetry(), $e);
  473. }
  474. if ($e instanceof EntityTooLargeException) {
  475. // the file is too big to be stored
  476. throw new EntityTooLarge($e->getMessage(), 0, $e);
  477. }
  478. if ($e instanceof InvalidContentException) {
  479. // the file content is not permitted
  480. throw new UnsupportedMediaType($e->getMessage(), 0, $e);
  481. }
  482. if ($e instanceof InvalidPathException) {
  483. // the path for the file was not valid
  484. // TODO: find proper http status code for this case
  485. throw new Forbidden($e->getMessage(), 0, $e);
  486. }
  487. if ($e instanceof LockedException || $e instanceof LockNotAcquiredException) {
  488. // the file is currently being written to by another process
  489. throw new FileLocked($e->getMessage(), $e->getCode(), $e);
  490. }
  491. if ($e instanceof GenericEncryptionException) {
  492. // returning 503 will allow retry of the operation at a later point in time
  493. throw new ServiceUnavailable('Encryption not ready: ' . $e->getMessage(), 0, $e);
  494. }
  495. if ($e instanceof StorageNotAvailableException) {
  496. throw new ServiceUnavailable('Failed to write file contents: ' . $e->getMessage(), 0, $e);
  497. }
  498. throw new \Sabre\DAV\Exception($e->getMessage(), 0, $e);
  499. }
  500. /**
  501. * Get the checksum for this file
  502. *
  503. * @return string
  504. */
  505. public function getChecksum() {
  506. return $this->info->getChecksum();
  507. }
  508. }