Skip to content

Commit 43f2858

Browse files
committed
Quota checking
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 95bd8e2 commit 43f2858

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

apps/dav/lib/Upload/ChunkingV2Plugin.php

+30-6
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,27 @@ public function beforePut(RequestInterface $request, ResponseInterface $response
137137
}
138138

139139
$targetFile = $this->getTargetFile($targetPath);
140+
$tempTargetFile = $this->uploadFolder->getChild(self::TEMP_TARGET);
141+
$cacheEntry = $storage->getCache()->get($tempTargetFile->getInternalPath());
142+
143+
[$destinationDir, $destinationName] = Uri\split($targetPath);
144+
/** @var Directory $destinationParent */
145+
$destinationParent = $this->server->tree->getNodeForPath($destinationDir);
146+
$free = $storage->free_space($destinationParent->getInternalPath());
147+
$additionalSize = (int)$request->getHeader('Content-Length');
148+
$newSize = $cacheEntry->getSize() + $additionalSize;
149+
if ($free >= 0 && ($cacheEntry->getSize() > $free || $newSize > $free)) {
150+
// FIXME: check if needed but might be in responsibility of the clients
151+
// $this->uploadFolder->delete();
152+
throw new InsufficientStorage("Insufficient space in $targetPath");
153+
}
154+
140155
$stream = $request->getBodyAsStream();
141-
$storage->putChunkedFilePart($targetFile->getInternalPath(), $uploadId, (string)$partId, $stream, (int)$request->getHeader('Content-Length'));
156+
$storage->putChunkedFilePart($targetFile->getInternalPath(), $uploadId, (string)$partId, $stream, $additionalSize);
157+
// FIXME add return value to putChunkedFilePart to validate against size
158+
159+
$storage->getCache()->update($cacheEntry->getId(), ['size' => $cacheEntry->getSize() + $additionalSize]);
160+
$storage->getPropagator()->propagateChange($tempTargetFile->getInternalPath(), time(), $additionalSize);
142161

143162
$response->setStatus(201);
144163
return false;
@@ -155,6 +174,8 @@ public function beforeMove($sourcePath, $destination): bool {
155174
$properties = $this->server->getProperties(dirname($sourcePath) . '/', [ self::OBJECT_UPLOAD_CHUNKTOKEN, self::OBJECT_UPLOAD_TARGET ]);
156175
$targetPath = $properties[self::OBJECT_UPLOAD_TARGET];
157176
$uploadId = $properties[self::OBJECT_UPLOAD_CHUNKTOKEN];
177+
178+
// FIXME: check if $destination === TARGET
158179
if (empty($targetPath) || empty($uploadId)) {
159180
throw new PreconditionFailed('Missing metadata for chunked upload');
160181
}
@@ -172,11 +193,14 @@ public function beforeMove($sourcePath, $destination): bool {
172193

173194
$rootView = new View();
174195
if ($storage->instanceOfStorage(ObjectStoreStorage::class)) {
175-
$storage->processingCallback('writeChunkedFile', function() {
176-
sleep(1);
177-
\OC_Util::obEnd();
178-
echo ' ';
179-
flush();
196+
$lastTick = time();
197+
$storage->processingCallback('writeChunkedFile', function() use ($lastTick){
198+
if ($lastTick < time()) {
199+
\OC_Util::obEnd();
200+
echo ' ';
201+
flush();
202+
}
203+
$lastTick = time();
180204
});
181205
}
182206

lib/private/Files/ObjectStore/ObjectStoreStorage.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -674,9 +674,11 @@ public function writeChunkedFile(string $targetPath, string $writeToken): int {
674674
$urn = $this->getURN($cacheEntry->getId());
675675
$uploadId = $this->uploadCache->get($this->getUploadCacheKey($urn, $writeToken, 'uploadId'));
676676
$parts = $this->uploadCache->get($this->getUploadCacheKey($urn, $uploadId, 'parts'));
677+
$sortedParts = array_values($parts);
678+
sort($sortedParts);
677679
try {
678680
if ($this->objectStore instanceof S3) {
679-
$size = $this->objectStore->completeMultipartUpload($urn, $uploadId, array_values($parts), function () {
681+
$size = $this->objectStore->completeMultipartUpload($urn, $uploadId, $sortedParts, function () {
680682
foreach ($this->processingCallbacks['writeChunkedFile'] as $callback) {
681683
$callback();
682684
}

0 commit comments

Comments
 (0)