-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathBulkUploadPlugin.php
More file actions
131 lines (116 loc) · 3.98 KB
/
BulkUploadPlugin.php
File metadata and controls
131 lines (116 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
* @copyright Copyright (c) 2021, Louis Chemineau <louis@chmn.me>
*
* @author Louis Chemineau <louis@chmn.me>
* @author Côme Chilliet <come.chilliet@nextcloud.com>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\DAV\BulkUpload;
use OC\Files\SymlinkManager;
use OCA\DAV\Connector\Sabre\MtimeSanitizer;
use OCP\AppFramework\Http;
use OCP\Files\DavUtil;
use OCP\Files\Folder;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
class BulkUploadPlugin extends ServerPlugin {
private Folder $userFolder;
private LoggerInterface $logger;
/**
* @var SymlinkManager
*/
private $symlinkManager;
public function __construct(
Folder $userFolder,
LoggerInterface $logger
) {
$this->userFolder = $userFolder;
$this->logger = $logger;
$this->symlinkManager = new SymlinkManager();
}
/**
* Register listener on POST requests with the httpPost method.
*/
public function initialize(Server $server): void {
$server->on('method:POST', [$this, 'httpPost'], 10);
}
/**
* Handle POST requests on /dav/bulk
* - parsing is done with a MultipartContentsParser object
* - writing is done with the userFolder service
*
* Will respond with an object containing an ETag for every written files.
*/
public function httpPost(RequestInterface $request, ResponseInterface $response): bool {
// Limit bulk upload to the /dav/bulk endpoint
if ($request->getPath() !== "bulk") {
return true;
}
$multiPartParser = new MultipartRequestParser($request, $this->logger);
$writtenFiles = [];
while (!$multiPartParser->isAtLastBoundary()) {
try {
[$headers, $content] = $multiPartParser->parseNextPart();
} catch (\Exception $e) {
// Return early if an error occurs during parsing.
$this->logger->error($e->getMessage());
$response->setStatus(Http::STATUS_BAD_REQUEST);
$response->setBody(json_encode($writtenFiles, JSON_THROW_ON_ERROR));
return false;
}
try {
// TODO: Remove 'x-file-mtime' when the desktop client no longer use it.
if (isset($headers['x-file-mtime'])) {
$mtime = MtimeSanitizer::sanitizeMtime($headers['x-file-mtime']);
} elseif (isset($headers['x-oc-mtime'])) {
$mtime = MtimeSanitizer::sanitizeMtime($headers['x-oc-mtime']);
} else {
$mtime = null;
}
$node = $this->userFolder->newFile($headers['x-file-path'], $content);
$node->touch($mtime);
$node = $this->userFolder->getById($node->getId())[0];
if (isset($headers['oc-file-type']) && $headers['oc-file-type'] == 1) {
$this->symlinkManager->storeSymlink($node);
} elseif ($this->symlinkManager->isSymlink($node)) {
// uploaded file is not a symlink, but there was a symlink
// at the same location before
$this->symlinkManager->deleteSymlink($node);
}
$writtenFiles[$headers['x-file-path']] = [
"error" => false,
"etag" => $node->getETag(),
"fileid" => DavUtil::getDavFileId($node->getId()),
"permissions" => DavUtil::getDavPermissions($node),
];
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['path' => $headers['x-file-path']]);
$writtenFiles[$headers['x-file-path']] = [
"error" => true,
"message" => $e->getMessage(),
];
}
}
$response->setStatus(Http::STATUS_OK);
$response->setBody(json_encode($writtenFiles, JSON_THROW_ON_ERROR));
return false;
}
}