1
0

BulkUploadPlugin.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-only
  5. */
  6. namespace OCA\DAV\BulkUpload;
  7. use OCA\DAV\Connector\Sabre\MtimeSanitizer;
  8. use OCP\AppFramework\Http;
  9. use OCP\Files\DavUtil;
  10. use OCP\Files\Folder;
  11. use Psr\Log\LoggerInterface;
  12. use Sabre\DAV\Server;
  13. use Sabre\DAV\ServerPlugin;
  14. use Sabre\HTTP\RequestInterface;
  15. use Sabre\HTTP\ResponseInterface;
  16. class BulkUploadPlugin extends ServerPlugin {
  17. public function __construct(
  18. private Folder $userFolder,
  19. private LoggerInterface $logger,
  20. ) {
  21. }
  22. /**
  23. * Register listener on POST requests with the httpPost method.
  24. */
  25. public function initialize(Server $server): void {
  26. $server->on('method:POST', [$this, 'httpPost'], 10);
  27. }
  28. /**
  29. * Handle POST requests on /dav/bulk
  30. * - parsing is done with a MultipartContentsParser object
  31. * - writing is done with the userFolder service
  32. *
  33. * Will respond with an object containing an ETag for every written files.
  34. */
  35. public function httpPost(RequestInterface $request, ResponseInterface $response): bool {
  36. // Limit bulk upload to the /dav/bulk endpoint
  37. if ($request->getPath() !== 'bulk') {
  38. return true;
  39. }
  40. $multiPartParser = new MultipartRequestParser($request, $this->logger);
  41. $writtenFiles = [];
  42. while (!$multiPartParser->isAtLastBoundary()) {
  43. try {
  44. [$headers, $content] = $multiPartParser->parseNextPart();
  45. } catch (\Exception $e) {
  46. // Return early if an error occurs during parsing.
  47. $this->logger->error($e->getMessage());
  48. $response->setStatus(Http::STATUS_BAD_REQUEST);
  49. $response->setBody(json_encode($writtenFiles, JSON_THROW_ON_ERROR));
  50. return false;
  51. }
  52. try {
  53. // TODO: Remove 'x-file-mtime' when the desktop client no longer use it.
  54. if (isset($headers['x-file-mtime'])) {
  55. $mtime = MtimeSanitizer::sanitizeMtime($headers['x-file-mtime']);
  56. } elseif (isset($headers['x-oc-mtime'])) {
  57. $mtime = MtimeSanitizer::sanitizeMtime($headers['x-oc-mtime']);
  58. } else {
  59. $mtime = null;
  60. }
  61. $node = $this->userFolder->newFile($headers['x-file-path'], $content);
  62. $node->touch($mtime);
  63. $node = $this->userFolder->getFirstNodeById($node->getId());
  64. $writtenFiles[$headers['x-file-path']] = [
  65. 'error' => false,
  66. 'etag' => $node->getETag(),
  67. 'fileid' => DavUtil::getDavFileId($node->getId()),
  68. 'permissions' => DavUtil::getDavPermissions($node),
  69. ];
  70. } catch (\Exception $e) {
  71. $this->logger->error($e->getMessage(), ['path' => $headers['x-file-path']]);
  72. $writtenFiles[$headers['x-file-path']] = [
  73. 'error' => true,
  74. 'message' => $e->getMessage(),
  75. ];
  76. }
  77. }
  78. $response->setStatus(Http::STATUS_OK);
  79. $response->setBody(json_encode($writtenFiles, JSON_THROW_ON_ERROR));
  80. return false;
  81. }
  82. }