You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FilesPlugin.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Michael Jobst <mjobst+github@tecratech.de>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Robin McCorkell <robin@mccorkell.me.uk>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. * @author Tobias Kaminsky <tobias@kaminsky.me>
  17. * @author Vincent Petry <vincent@nextcloud.com>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. namespace OCA\DAV\Connector\Sabre;
  35. use OC\AppFramework\Http\Request;
  36. use OC\Metadata\IMetadataManager;
  37. use OCP\Constants;
  38. use OCP\Files\ForbiddenException;
  39. use OCP\Files\StorageNotAvailableException;
  40. use OCP\IConfig;
  41. use OCP\IPreview;
  42. use OCP\IRequest;
  43. use OCP\IUserSession;
  44. use Psr\Log\LoggerInterface;
  45. use Sabre\DAV\Exception\Forbidden;
  46. use Sabre\DAV\Exception\NotFound;
  47. use Sabre\DAV\IFile;
  48. use Sabre\DAV\PropFind;
  49. use Sabre\DAV\PropPatch;
  50. use Sabre\DAV\ServerPlugin;
  51. use Sabre\DAV\Tree;
  52. use Sabre\HTTP\RequestInterface;
  53. use Sabre\HTTP\ResponseInterface;
  54. use Sabre\Uri;
  55. class FilesPlugin extends ServerPlugin {
  56. // namespace
  57. public const NS_OWNCLOUD = 'http://owncloud.org/ns';
  58. public const NS_NEXTCLOUD = 'http://nextcloud.org/ns';
  59. public const FILEID_PROPERTYNAME = '{http://owncloud.org/ns}id';
  60. public const INTERNAL_FILEID_PROPERTYNAME = '{http://owncloud.org/ns}fileid';
  61. public const PERMISSIONS_PROPERTYNAME = '{http://owncloud.org/ns}permissions';
  62. public const SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-collaboration-services.org/ns}share-permissions';
  63. public const OCM_SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-cloud-mesh.org/ns}share-permissions';
  64. public const DOWNLOADURL_PROPERTYNAME = '{http://owncloud.org/ns}downloadURL';
  65. public const SIZE_PROPERTYNAME = '{http://owncloud.org/ns}size';
  66. public const GETETAG_PROPERTYNAME = '{DAV:}getetag';
  67. public const LASTMODIFIED_PROPERTYNAME = '{DAV:}lastmodified';
  68. public const CREATIONDATE_PROPERTYNAME = '{DAV:}creationdate';
  69. public const OWNER_ID_PROPERTYNAME = '{http://owncloud.org/ns}owner-id';
  70. public const OWNER_DISPLAY_NAME_PROPERTYNAME = '{http://owncloud.org/ns}owner-display-name';
  71. public const CHECKSUMS_PROPERTYNAME = '{http://owncloud.org/ns}checksums';
  72. public const DATA_FINGERPRINT_PROPERTYNAME = '{http://owncloud.org/ns}data-fingerprint';
  73. public const HAS_PREVIEW_PROPERTYNAME = '{http://nextcloud.org/ns}has-preview';
  74. public const MOUNT_TYPE_PROPERTYNAME = '{http://nextcloud.org/ns}mount-type';
  75. public const IS_ENCRYPTED_PROPERTYNAME = '{http://nextcloud.org/ns}is-encrypted';
  76. public const METADATA_ETAG_PROPERTYNAME = '{http://nextcloud.org/ns}metadata_etag';
  77. public const UPLOAD_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}upload_time';
  78. public const CREATION_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}creation_time';
  79. public const SHARE_NOTE = '{http://nextcloud.org/ns}note';
  80. public const SUBFOLDER_COUNT_PROPERTYNAME = '{http://nextcloud.org/ns}contained-folder-count';
  81. public const SUBFILE_COUNT_PROPERTYNAME = '{http://nextcloud.org/ns}contained-file-count';
  82. public const FILE_METADATA_SIZE = '{http://nextcloud.org/ns}file-metadata-size';
  83. /**
  84. * Reference to main server object
  85. *
  86. * @var \Sabre\DAV\Server
  87. */
  88. private $server;
  89. /**
  90. * @var Tree
  91. */
  92. private $tree;
  93. /**
  94. * @var IUserSession
  95. */
  96. private $userSession;
  97. /**
  98. * Whether this is public webdav.
  99. * If true, some returned information will be stripped off.
  100. *
  101. * @var bool
  102. */
  103. private $isPublic;
  104. /**
  105. * @var bool
  106. */
  107. private $downloadAttachment;
  108. /**
  109. * @var IConfig
  110. */
  111. private $config;
  112. /**
  113. * @var IRequest
  114. */
  115. private $request;
  116. /**
  117. * @var IPreview
  118. */
  119. private $previewManager;
  120. /**
  121. * @param Tree $tree
  122. * @param IConfig $config
  123. * @param IRequest $request
  124. * @param IPreview $previewManager
  125. * @param bool $isPublic
  126. * @param bool $downloadAttachment
  127. */
  128. public function __construct(Tree $tree,
  129. IConfig $config,
  130. IRequest $request,
  131. IPreview $previewManager,
  132. IUserSession $userSession,
  133. $isPublic = false,
  134. $downloadAttachment = true) {
  135. $this->tree = $tree;
  136. $this->config = $config;
  137. $this->request = $request;
  138. $this->userSession = $userSession;
  139. $this->isPublic = $isPublic;
  140. $this->downloadAttachment = $downloadAttachment;
  141. $this->previewManager = $previewManager;
  142. }
  143. /**
  144. * This initializes the plugin.
  145. *
  146. * This function is called by \Sabre\DAV\Server, after
  147. * addPlugin is called.
  148. *
  149. * This method should set up the required event subscriptions.
  150. *
  151. * @param \Sabre\DAV\Server $server
  152. * @return void
  153. */
  154. public function initialize(\Sabre\DAV\Server $server) {
  155. $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
  156. $server->xml->namespaceMap[self::NS_NEXTCLOUD] = 'nc';
  157. $server->protectedProperties[] = self::FILEID_PROPERTYNAME;
  158. $server->protectedProperties[] = self::INTERNAL_FILEID_PROPERTYNAME;
  159. $server->protectedProperties[] = self::PERMISSIONS_PROPERTYNAME;
  160. $server->protectedProperties[] = self::SHARE_PERMISSIONS_PROPERTYNAME;
  161. $server->protectedProperties[] = self::OCM_SHARE_PERMISSIONS_PROPERTYNAME;
  162. $server->protectedProperties[] = self::SIZE_PROPERTYNAME;
  163. $server->protectedProperties[] = self::DOWNLOADURL_PROPERTYNAME;
  164. $server->protectedProperties[] = self::OWNER_ID_PROPERTYNAME;
  165. $server->protectedProperties[] = self::OWNER_DISPLAY_NAME_PROPERTYNAME;
  166. $server->protectedProperties[] = self::CHECKSUMS_PROPERTYNAME;
  167. $server->protectedProperties[] = self::DATA_FINGERPRINT_PROPERTYNAME;
  168. $server->protectedProperties[] = self::HAS_PREVIEW_PROPERTYNAME;
  169. $server->protectedProperties[] = self::MOUNT_TYPE_PROPERTYNAME;
  170. $server->protectedProperties[] = self::IS_ENCRYPTED_PROPERTYNAME;
  171. $server->protectedProperties[] = self::SHARE_NOTE;
  172. // normally these cannot be changed (RFC4918), but we want them modifiable through PROPPATCH
  173. $allowedProperties = ['{DAV:}getetag'];
  174. $server->protectedProperties = array_diff($server->protectedProperties, $allowedProperties);
  175. $this->server = $server;
  176. $this->server->on('propFind', [$this, 'handleGetProperties']);
  177. $this->server->on('propPatch', [$this, 'handleUpdateProperties']);
  178. $this->server->on('afterBind', [$this, 'sendFileIdHeader']);
  179. $this->server->on('afterWriteContent', [$this, 'sendFileIdHeader']);
  180. $this->server->on('afterMethod:GET', [$this,'httpGet']);
  181. $this->server->on('afterMethod:GET', [$this, 'handleDownloadToken']);
  182. $this->server->on('afterResponse', function ($request, ResponseInterface $response) {
  183. $body = $response->getBody();
  184. if (is_resource($body)) {
  185. fclose($body);
  186. }
  187. });
  188. $this->server->on('beforeMove', [$this, 'checkMove']);
  189. }
  190. /**
  191. * Plugin that checks if a move can actually be performed.
  192. *
  193. * @param string $source source path
  194. * @param string $destination destination path
  195. * @throws Forbidden
  196. * @throws NotFound
  197. */
  198. public function checkMove($source, $destination) {
  199. $sourceNode = $this->tree->getNodeForPath($source);
  200. if (!$sourceNode instanceof Node) {
  201. return;
  202. }
  203. [$sourceDir,] = \Sabre\Uri\split($source);
  204. [$destinationDir,] = \Sabre\Uri\split($destination);
  205. if ($sourceDir !== $destinationDir) {
  206. $sourceNodeFileInfo = $sourceNode->getFileInfo();
  207. if ($sourceNodeFileInfo === null) {
  208. throw new NotFound($source . ' does not exist');
  209. }
  210. if (!$sourceNodeFileInfo->isDeletable()) {
  211. throw new Forbidden($source . " cannot be deleted");
  212. }
  213. }
  214. }
  215. /**
  216. * This sets a cookie to be able to recognize the start of the download
  217. * the content must not be longer than 32 characters and must only contain
  218. * alphanumeric characters
  219. *
  220. * @param RequestInterface $request
  221. * @param ResponseInterface $response
  222. */
  223. public function handleDownloadToken(RequestInterface $request, ResponseInterface $response) {
  224. $queryParams = $request->getQueryParameters();
  225. /**
  226. * this sets a cookie to be able to recognize the start of the download
  227. * the content must not be longer than 32 characters and must only contain
  228. * alphanumeric characters
  229. */
  230. if (isset($queryParams['downloadStartSecret'])) {
  231. $token = $queryParams['downloadStartSecret'];
  232. if (!isset($token[32])
  233. && preg_match('!^[a-zA-Z0-9]+$!', $token) === 1) {
  234. // FIXME: use $response->setHeader() instead
  235. setcookie('ocDownloadStarted', $token, time() + 20, '/');
  236. }
  237. }
  238. }
  239. /**
  240. * Add headers to file download
  241. *
  242. * @param RequestInterface $request
  243. * @param ResponseInterface $response
  244. */
  245. public function httpGet(RequestInterface $request, ResponseInterface $response) {
  246. // Only handle valid files
  247. $node = $this->tree->getNodeForPath($request->getPath());
  248. if (!($node instanceof IFile)) {
  249. return;
  250. }
  251. // adds a 'Content-Disposition: attachment' header in case no disposition
  252. // header has been set before
  253. if ($this->downloadAttachment &&
  254. $response->getHeader('Content-Disposition') === null) {
  255. $filename = $node->getName();
  256. if ($this->request->isUserAgent(
  257. [
  258. Request::USER_AGENT_IE,
  259. Request::USER_AGENT_ANDROID_MOBILE_CHROME,
  260. Request::USER_AGENT_FREEBOX,
  261. ])) {
  262. $response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"');
  263. } else {
  264. $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename)
  265. . '; filename="' . rawurlencode($filename) . '"');
  266. }
  267. }
  268. if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
  269. //Add OC-Checksum header
  270. $checksum = $node->getChecksum();
  271. if ($checksum !== null && $checksum !== '') {
  272. $response->addHeader('OC-Checksum', $checksum);
  273. }
  274. }
  275. $response->addHeader('X-Accel-Buffering', 'no');
  276. }
  277. /**
  278. * Adds all ownCloud-specific properties
  279. *
  280. * @param PropFind $propFind
  281. * @param \Sabre\DAV\INode $node
  282. * @return void
  283. */
  284. public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) {
  285. $httpRequest = $this->server->httpRequest;
  286. if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
  287. /**
  288. * This was disabled, because it made dir listing throw an exception,
  289. * so users were unable to navigate into folders where one subitem
  290. * is blocked by the files_accesscontrol app, see:
  291. * https://github.com/nextcloud/files_accesscontrol/issues/65
  292. * if (!$node->getFileInfo()->isReadable()) {
  293. * // avoid detecting files through this means
  294. * throw new NotFound();
  295. * }
  296. */
  297. $propFind->handle(self::FILEID_PROPERTYNAME, function () use ($node) {
  298. return $node->getFileId();
  299. });
  300. $propFind->handle(self::INTERNAL_FILEID_PROPERTYNAME, function () use ($node) {
  301. return $node->getInternalFileId();
  302. });
  303. $propFind->handle(self::PERMISSIONS_PROPERTYNAME, function () use ($node) {
  304. $perms = $node->getDavPermissions();
  305. if ($this->isPublic) {
  306. // remove mount information
  307. $perms = str_replace(['S', 'M'], '', $perms);
  308. }
  309. return $perms;
  310. });
  311. $propFind->handle(self::SHARE_PERMISSIONS_PROPERTYNAME, function () use ($node, $httpRequest) {
  312. $user = $this->userSession->getUser();
  313. if ($user === null) {
  314. return null;
  315. }
  316. return $node->getSharePermissions(
  317. $user->getUID()
  318. );
  319. });
  320. $propFind->handle(self::OCM_SHARE_PERMISSIONS_PROPERTYNAME, function () use ($node, $httpRequest) {
  321. $user = $this->userSession->getUser();
  322. if ($user === null) {
  323. return null;
  324. }
  325. $ncPermissions = $node->getSharePermissions(
  326. $user->getUID()
  327. );
  328. $ocmPermissions = $this->ncPermissions2ocmPermissions($ncPermissions);
  329. return json_encode($ocmPermissions);
  330. });
  331. $propFind->handle(self::GETETAG_PROPERTYNAME, function () use ($node) {
  332. return $node->getETag();
  333. });
  334. $propFind->handle(self::OWNER_ID_PROPERTYNAME, function () use ($node) {
  335. $owner = $node->getOwner();
  336. if (!$owner) {
  337. return null;
  338. } else {
  339. return $owner->getUID();
  340. }
  341. });
  342. $propFind->handle(self::OWNER_DISPLAY_NAME_PROPERTYNAME, function () use ($node) {
  343. $owner = $node->getOwner();
  344. if (!$owner) {
  345. return null;
  346. } else {
  347. return $owner->getDisplayName();
  348. }
  349. });
  350. $propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function () use ($node) {
  351. return json_encode($this->previewManager->isAvailable($node->getFileInfo()));
  352. });
  353. $propFind->handle(self::SIZE_PROPERTYNAME, function () use ($node) {
  354. return $node->getSize();
  355. });
  356. $propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function () use ($node) {
  357. return $node->getFileInfo()->getMountPoint()->getMountType();
  358. });
  359. $propFind->handle(self::SHARE_NOTE, function () use ($node, $httpRequest) {
  360. $user = $this->userSession->getUser();
  361. if ($user === null) {
  362. return null;
  363. }
  364. return $node->getNoteFromShare(
  365. $user->getUID()
  366. );
  367. });
  368. $propFind->handle(self::DATA_FINGERPRINT_PROPERTYNAME, function () use ($node) {
  369. return $this->config->getSystemValue('data-fingerprint', '');
  370. });
  371. $propFind->handle(self::CREATIONDATE_PROPERTYNAME, function () use ($node) {
  372. return (new \DateTimeImmutable())
  373. ->setTimestamp($node->getFileInfo()->getCreationTime())
  374. ->format(\DateTimeInterface::ATOM);
  375. });
  376. $propFind->handle(self::CREATION_TIME_PROPERTYNAME, function () use ($node) {
  377. return $node->getFileInfo()->getCreationTime();
  378. });
  379. }
  380. if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
  381. $propFind->handle(self::DOWNLOADURL_PROPERTYNAME, function () use ($node) {
  382. try {
  383. $directDownloadUrl = $node->getDirectDownload();
  384. if (isset($directDownloadUrl['url'])) {
  385. return $directDownloadUrl['url'];
  386. }
  387. } catch (StorageNotAvailableException $e) {
  388. return false;
  389. } catch (ForbiddenException $e) {
  390. return false;
  391. }
  392. return false;
  393. });
  394. $propFind->handle(self::CHECKSUMS_PROPERTYNAME, function () use ($node) {
  395. $checksum = $node->getChecksum();
  396. if ($checksum === null || $checksum === '') {
  397. return null;
  398. }
  399. return new ChecksumList($checksum);
  400. });
  401. $propFind->handle(self::UPLOAD_TIME_PROPERTYNAME, function () use ($node) {
  402. return $node->getFileInfo()->getUploadTime();
  403. });
  404. if ($this->config->getSystemValueBool('enable_file_metadata', true)) {
  405. $propFind->handle(self::FILE_METADATA_SIZE, function () use ($node) {
  406. if (!str_starts_with($node->getFileInfo()->getMimetype(), 'image')) {
  407. return json_encode([]);
  408. }
  409. if ($node->hasMetadata('size')) {
  410. $sizeMetadata = $node->getMetadata('size');
  411. } else {
  412. // This code path should not be called since we try to preload
  413. // the metadata when loading the folder or the search results
  414. // in one go
  415. $metadataManager = \OC::$server->get(IMetadataManager::class);
  416. $sizeMetadata = $metadataManager->fetchMetadataFor('size', [$node->getId()])[$node->getId()];
  417. // TODO would be nice to display this in the profiler...
  418. \OC::$server->get(LoggerInterface::class)->warning('Inefficient fetching of metadata');
  419. }
  420. return json_encode($sizeMetadata->getMetadata());
  421. });
  422. }
  423. }
  424. if ($node instanceof Directory) {
  425. $propFind->handle(self::SIZE_PROPERTYNAME, function () use ($node) {
  426. return $node->getSize();
  427. });
  428. $propFind->handle(self::IS_ENCRYPTED_PROPERTYNAME, function () use ($node) {
  429. return $node->getFileInfo()->isEncrypted() ? '1' : '0';
  430. });
  431. $requestProperties = $propFind->getRequestedProperties();
  432. // TODO detect dynamically which metadata groups are requested and
  433. // preload all of them and not just size
  434. if ($this->config->getSystemValueBool('enable_file_metadata', true)
  435. && in_array(self::FILE_METADATA_SIZE, $requestProperties, true)) {
  436. // Preloading of the metadata
  437. $fileIds = [];
  438. foreach ($node->getChildren() as $child) {
  439. /** @var \OCP\Files\Node|Node $child */
  440. if (str_starts_with($child->getFileInfo()->getMimeType(), 'image/')) {
  441. /** @var File $child */
  442. $fileIds[] = $child->getFileInfo()->getId();
  443. }
  444. }
  445. /** @var IMetaDataManager $metadataManager */
  446. $metadataManager = \OC::$server->get(IMetadataManager::class);
  447. $preloadedMetadata = $metadataManager->fetchMetadataFor('size', $fileIds);
  448. foreach ($node->getChildren() as $child) {
  449. /** @var \OCP\Files\Node|Node $child */
  450. if (str_starts_with($child->getFileInfo()->getMimeType(), 'image')) {
  451. /** @var File $child */
  452. $child->setMetadata('size', $preloadedMetadata[$child->getFileInfo()->getId()]);
  453. }
  454. }
  455. }
  456. if (in_array(self::SUBFILE_COUNT_PROPERTYNAME, $requestProperties, true)
  457. || in_array(self::SUBFOLDER_COUNT_PROPERTYNAME, $requestProperties, true)) {
  458. $nbFiles = 0;
  459. $nbFolders = 0;
  460. foreach ($node->getChildren() as $child) {
  461. if ($child instanceof File) {
  462. $nbFiles++;
  463. } elseif ($child instanceof Directory) {
  464. $nbFolders++;
  465. }
  466. }
  467. $propFind->handle(self::SUBFILE_COUNT_PROPERTYNAME, $nbFiles);
  468. $propFind->handle(self::SUBFOLDER_COUNT_PROPERTYNAME, $nbFolders);
  469. }
  470. }
  471. }
  472. /**
  473. * translate Nextcloud permissions to OCM Permissions
  474. *
  475. * @param $ncPermissions
  476. * @return array
  477. */
  478. protected function ncPermissions2ocmPermissions($ncPermissions) {
  479. $ocmPermissions = [];
  480. if ($ncPermissions & Constants::PERMISSION_SHARE) {
  481. $ocmPermissions[] = 'share';
  482. }
  483. if ($ncPermissions & Constants::PERMISSION_READ) {
  484. $ocmPermissions[] = 'read';
  485. }
  486. if (($ncPermissions & Constants::PERMISSION_CREATE) ||
  487. ($ncPermissions & Constants::PERMISSION_UPDATE)) {
  488. $ocmPermissions[] = 'write';
  489. }
  490. return $ocmPermissions;
  491. }
  492. /**
  493. * Update ownCloud-specific properties
  494. *
  495. * @param string $path
  496. * @param PropPatch $propPatch
  497. *
  498. * @return void
  499. */
  500. public function handleUpdateProperties($path, PropPatch $propPatch) {
  501. $node = $this->tree->getNodeForPath($path);
  502. if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) {
  503. return;
  504. }
  505. $propPatch->handle(self::LASTMODIFIED_PROPERTYNAME, function ($time) use ($node) {
  506. if (empty($time)) {
  507. return false;
  508. }
  509. $node->touch($time);
  510. return true;
  511. });
  512. $propPatch->handle(self::GETETAG_PROPERTYNAME, function ($etag) use ($node) {
  513. if (empty($etag)) {
  514. return false;
  515. }
  516. if ($node->setEtag($etag) !== -1) {
  517. return true;
  518. }
  519. return false;
  520. });
  521. $propPatch->handle(self::CREATIONDATE_PROPERTYNAME, function ($time) use ($node) {
  522. if (empty($time)) {
  523. return false;
  524. }
  525. $dateTime = new \DateTimeImmutable($time);
  526. $node->setCreationTime($dateTime->getTimestamp());
  527. return true;
  528. });
  529. $propPatch->handle(self::CREATION_TIME_PROPERTYNAME, function ($time) use ($node) {
  530. if (empty($time)) {
  531. return false;
  532. }
  533. $node->setCreationTime((int) $time);
  534. return true;
  535. });
  536. }
  537. /**
  538. * @param string $filePath
  539. * @param \Sabre\DAV\INode $node
  540. * @throws \Sabre\DAV\Exception\BadRequest
  541. */
  542. public function sendFileIdHeader($filePath, \Sabre\DAV\INode $node = null) {
  543. // chunked upload handling
  544. if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
  545. [$path, $name] = \Sabre\Uri\split($filePath);
  546. $info = \OC_FileChunking::decodeName($name);
  547. if (!empty($info)) {
  548. $filePath = $path . '/' . $info['name'];
  549. }
  550. }
  551. // we get the node for the given $filePath here because in case of afterCreateFile $node is the parent folder
  552. if (!$this->server->tree->nodeExists($filePath)) {
  553. return;
  554. }
  555. $node = $this->server->tree->getNodeForPath($filePath);
  556. if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
  557. $fileId = $node->getFileId();
  558. if (!is_null($fileId)) {
  559. $this->server->httpResponse->setHeader('OC-FileId', $fileId);
  560. }
  561. }
  562. }
  563. }