Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions apps/dav/lib/Upload/ChunkingV2Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ public function beforePut(RequestInterface $request, ResponseInterface $response
// Refresh the session metadata TTL on every successful chunk
$this->storeUploadSession();

$storage->getCache()->update($uploadFile->getId(), ['size' => $uploadFile->getSize() + $additionalSize]);
if ($tempTargetFile) {
$storage->getCache()->update($uploadFile->getId(), ['size' => $uploadFile->getSize() + $additionalSize]);
$storage->getPropagator()->propagateChange($tempTargetFile->getInternalPath(), time(), $additionalSize);
}

Expand Down Expand Up @@ -276,6 +276,8 @@ public function beforeMove($sourcePath, $destination): bool {

$rootView = new View();
$rootView->putFileInfo($destinationInView, $updateFileInfo);
// drop cached nodes of the destination folder so later lookups read the updated etag and size
$this->server->tree->markDirty($destinationDir);

$sourceNode = $this->server->tree->getNodeForPath($sourcePath);
if ($sourceNode instanceof FutureFile) {
Expand Down Expand Up @@ -392,7 +394,9 @@ private function completeChunkedWrite(string $targetAbsolutePath): void {
$this->emitPreHooks($targetAbsolutePath, $exists);
try {
$uploadFile->changeLock(ILockingProvider::LOCK_EXCLUSIVE);
$storage->completeChunkedWrite($storagePath, $this->uploadId);
$previousSize = $uploadFile->getSize();
$size = $storage->completeChunkedWrite($storagePath, $this->uploadId);
$storage->getUpdater()->update($storagePath, null, $size - $previousSize);
$uploadFile->changeLock(ILockingProvider::LOCK_SHARED);
} catch (Exception $e) {
$uploadFile->unlock(ILockingProvider::LOCK_EXCLUSIVE);
Expand Down
133 changes: 133 additions & 0 deletions apps/dav/tests/unit/Upload/ChunkingV2PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
use OCA\DAV\Upload\FutureFile;
use OCA\DAV\Upload\UploadFile;
use OCA\DAV\Upload\UploadFolder;
use OCP\Files\Cache\IPropagator;
use OCP\Files\FileInfo;
use OCP\Files\Folder;
use OCP\Files\Storage\IChunkedFileWrite;
use OCP\ICache;
use OCP\ICacheFactory;
Expand Down Expand Up @@ -192,4 +195,134 @@ public function testAfterMkcolStartsChunkedWriteOnlyWithoutEncryption(bool $encr

$this->assertTrue($plugin->afterMkcol($this->request, $this->response));
}

public function testBeforePutOnExistingTargetKeepsItsSize(): void {
$storage = $this->createChunkedWriteStorage();
$storage->expects($this->once())
->method('putChunkedWritePart')
->with('files/dir/f.bin', 'upload-token', '1', $this->anything(), 5242880);
$storage->expects($this->never())->method('getCache');
$storage->expects($this->never())->method('getPropagator');

$uploadFolder = $this->createUploadFolder($storage);
$uploadFolder->method('childExists')->with('.target')->willReturn(false);
$this->mockTree($uploadFolder, $this->createExistingTarget($storage));
$this->mockRequest('uploads/admin/upload-id/1', ['Content-Length' => '5242880']);

$this->response->expects($this->once())->method('setStatus')->with(201);

$this->assertFalse($this->createPlugin()->beforePut($this->request, $this->response));
}

public function testBeforePutOnTempTargetUpdatesSizeAndPropagates(): void {
$cache = $this->createMock(\OCP\Files\Cache\ICache::class);
$cache->expects($this->once())
->method('update')
->with(43, ['size' => 5242880 + 1048576]);
$propagator = $this->createMock(IPropagator::class);
$propagator->expects($this->once())
->method('propagateChange')
->with('uploads/upload-id/.target', $this->anything(), 1048576);

$storage = $this->createChunkedWriteStorage();
$storage->expects($this->once())
->method('putChunkedWritePart')
->with('uploads/upload-id/.target', 'upload-token', '2', $this->anything(), 1048576);
$storage->method('getCache')->willReturn($cache);
$storage->method('getPropagator')->willReturn($propagator);

$tempTargetFile = $this->createMock(File::class);
$tempTargetFile->method('getId')->willReturn(43);
$tempTargetFile->method('getSize')->willReturn(5242880);
$tempTargetFile->method('getInternalPath')->willReturn('uploads/upload-id/.target');
$tempTarget = $this->createMock(UploadFile::class);
$tempTarget->method('getFile')->willReturn($tempTargetFile);
$tempTarget->method('getSize')->willReturn(5242880);
$tempTarget->method('getInternalPath')->willReturn('uploads/upload-id/.target');

$uploadFolder = $this->createUploadFolder($storage);
$uploadFolder->method('childExists')->with('.target')->willReturn(true);
$uploadFolder->method('getChild')->with('.target')->willReturn($tempTarget);
$this->mockTree($uploadFolder, null, $this->createDestinationParent());
$this->mockRequest('uploads/admin/upload-id/2', ['Content-Length' => '1048576']);

$this->response->expects($this->once())->method('setStatus')->with(201);

$this->assertFalse($this->createPlugin()->beforePut($this->request, $this->response));
}

private function createPlugin(): ChunkingV2Plugin {
$config = $this->createMock(IConfig::class);
$config->method('getSystemValue')
->willReturnCallback(static fn (string $key, mixed $default = '') => $key === 'memcache.distributed' ? Redis::class : $default);
$this->overwriteService(IConfig::class, $config);

$cache = $this->createMock(Redis::class);
$cache->method('get')->with('upload-id')->willReturn([
ChunkingV2Plugin::UPLOAD_ID => 'upload-token',
ChunkingV2Plugin::UPLOAD_TARGET_PATH => 'files/admin/dir/f.bin',
ChunkingV2Plugin::UPLOAD_TARGET_ID => 42,
]);
$cacheFactory = $this->createMock(ICacheFactory::class);
$cacheFactory->method('createDistributed')->willReturn($cache);

$plugin = new ChunkingV2Plugin($cacheFactory);
$plugin->initialize($this->server);
return $plugin;
}

private function createChunkedWriteStorage(): ObjectStoreStorage&MockObject {
$storage = $this->createMock(ObjectStoreStorage::class);
$storage->method('getId')->willReturn('object::user:admin');
$storage->method('instanceOfStorage')
->willReturnCallback(static fn (string $class): bool => in_array($class, [IChunkedFileWrite::class, ObjectStoreStorage::class], true));
$storage->method('getObjectStore')->willReturn($this->createMock(S3::class));
return $storage;
}

private function createUploadFolder(ObjectStoreStorage $storage): UploadFolder&MockObject {
$uploadFolder = $this->createMock(UploadFolder::class);
$uploadFolder->method('getName')->willReturn('upload-id');
$uploadFolder->method('getStorage')->willReturn($storage);
return $uploadFolder;
}

private function createExistingTarget(ObjectStoreStorage $storage): File&MockObject {
$node = $this->createMock(\OCP\Files\File::class);
$node->method('isUpdateable')->willReturn(true);
$node->method('getStorage')->willReturn($storage);

$target = $this->createMock(File::class);
$target->method('getNode')->willReturn($node);
$target->method('getInternalPath')->willReturn('files/dir/f.bin');
return $target;
}

private function createDestinationParent(): Directory&MockObject {
$folder = $this->createMock(Folder::class);
$folder->method('getFreeSpace')->willReturn(FileInfo::SPACE_UNLIMITED);

$parent = $this->createMock(Directory::class);
$parent->method('getNode')->willReturn($folder);
return $parent;
}

private function mockTree(UploadFolder $uploadFolder, ?File $target, ?Directory $destinationParent = null): void {
$this->tree->method('getNodeForPath')
->willReturnCallback(static fn (string $path) => match ($path) {
'uploads/admin/upload-id' => $uploadFolder,
'files/admin/dir/f.bin' => $target ?? throw new NotFound(),
'files/admin/dir' => $destinationParent ?? throw new NotFound(),
default => throw new NotFound(),
});
}

private function mockRequest(string $path, array $headers): void {
$headers['Destination'] = 'http://localhost/remote.php/dav/files/admin/dir/f.bin';
$this->request->method('getPath')->willReturn($path);
$this->request->method('getHeader')
->willReturnCallback(static fn (string $name): ?string => $headers[$name] ?? null);
$this->request->method('getBodyAsStream')->willReturn(fopen('php://memory', 'r'));
$this->server->method('calculateUri')->willReturn('files/admin/dir/f.bin');
}
}
2 changes: 2 additions & 0 deletions lib/private/Files/ObjectStore/ObjectStoreStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,8 @@ public function completeChunkedWrite(string $targetPath, string $writeToken): in
$stat['size'] = $size;
$stat['mtime'] = $mtime;
$stat['mimetype'] = $this->getMimeType($targetPath);
$stat['etag'] = $this->getETag($targetPath);
$stat['checksum'] = '';
$this->getCache()->update($stat['fileid'], $stat);
}
} catch (S3MultipartUploadException|S3Exception $e) {
Expand Down
Loading