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

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