BulkUploadPlugin.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. private Folder $userFolder;
  18. private LoggerInterface $logger;
  19. public function __construct(
  20. Folder $userFolder,
  21. LoggerInterface $logger
  22. ) {
  23. $this->userFolder = $userFolder;
  24. $this->logger = $logger;
  25. }
  26. /**
  27. * Register listener on POST requests with the httpPost method.
  28. */
  29. public function initialize(Server $server): void {
  30. $server->on('method:POST', [$this, 'httpPost'], 10);
  31. }
  32. /**
  33. * Handle POST requests on /dav/bulk
  34. * - parsing is done with a MultipartContentsParser object
  35. * - writing is done with the userFolder service
  36. *
  37. * Will respond with an object containing an ETag for every written files.
  38. */
  39. public function httpPost(RequestInterface $request, ResponseInterface $response): bool {
  40. // Limit bulk upload to the /dav/bulk endpoint
  41. if ($request->getPath() !== "bulk") {
  42. return true;
  43. }
  44. $multiPartParser = new MultipartRequestParser($request, $this->logger);
  45. $writtenFiles = [];
  46. while (!$multiPartParser->isAtLastBoundary()) {
  47. try {
  48. [$headers, $content] = $multiPartParser->parseNextPart();
  49. } catch (\Exception $e) {
  50. // Return early if an error occurs during parsing.
  51. $this->logger->error($e->getMessage());
  52. $response->setStatus(Http::STATUS_BAD_REQUEST);
  53. $response->setBody(json_encode($writtenFiles, JSON_THROW_ON_ERROR));
  54. return false;
  55. }
  56. try {
  57. // TODO: Remove 'x-file-mtime' when the desktop client no longer use it.
  58. if (isset($headers['x-file-mtime'])) {
  59. $mtime = MtimeSanitizer::sanitizeMtime($headers['x-file-mtime']);
  60. } elseif (isset($headers['x-oc-mtime'])) {
  61. $mtime = MtimeSanitizer::sanitizeMtime($headers['x-oc-mtime']);
  62. } else {
  63. $mtime = null;
  64. }
  65. $node = $this->userFolder->newFile($headers['x-file-path'], $content);
  66. $node->touch($mtime);
  67. $node = $this->userFolder->getFirstNodeById($node->getId());
  68. $writtenFiles[$headers['x-file-path']] = [
  69. "error" => false,
  70. "etag" => $node->getETag(),
  71. "fileid" => DavUtil::getDavFileId($node->getId()),
  72. "permissions" => DavUtil::getDavPermissions($node),
  73. ];
  74. } catch (\Exception $e) {
  75. $this->logger->error($e->getMessage(), ['path' => $headers['x-file-path']]);
  76. $writtenFiles[$headers['x-file-path']] = [
  77. "error" => true,
  78. "message" => $e->getMessage(),
  79. ];
  80. }
  81. }
  82. $response->setStatus(Http::STATUS_OK);
  83. $response->setBody(json_encode($writtenFiles, JSON_THROW_ON_ERROR));
  84. return false;
  85. }
  86. }