From 125f355df39e9929004bfe03f3a1b3c45640ff8b Mon Sep 17 00:00:00 2001 From: Muhammad Faizan Date: Sun, 6 Dec 2026 19:17:24 +0500 Subject: [PATCH] fix(preview): preserve multibucket preview mapping Signed-off-by: Muhammad Faizan Assisted-by: ChatGPT:gpt --- .../Storage/ObjectStorePreviewStorage.php | 3 +-- .../Storage/ObjectStorePreviewStorageTest.php | 22 +++++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/private/Preview/Storage/ObjectStorePreviewStorage.php b/lib/private/Preview/Storage/ObjectStorePreviewStorage.php index fee8143cafb8d..a5306d0b5d3dc 100644 --- a/lib/private/Preview/Storage/ObjectStorePreviewStorage.php +++ b/lib/private/Preview/Storage/ObjectStorePreviewStorage.php @@ -162,8 +162,7 @@ private function getConfigForNewPreview(Preview $preview, bool $migration = fals if ($config['arguments']['multibucket']) { if ($this->isMultibucketPreviewDistributionEnabled) { // Spread the previews on different buckets depending on their corresponding fileId - $oldLocationArray = str_split(substr(md5((string)$preview->getFileId()), 0, 2)); - $bucketNumber = hexdec('0x' . $oldLocationArray[0]) * 16 + hexdec('0x' . $oldLocationArray[0]); + $bucketNumber = hexdec(substr(md5((string)$preview->getFileId()), 0, 2)); $bucketName .= '-preview-' . $bucketNumber; } else { // Put all previews in the root (0) bucket diff --git a/tests/lib/Preview/Storage/ObjectStorePreviewStorageTest.php b/tests/lib/Preview/Storage/ObjectStorePreviewStorageTest.php index ae580619b03f6..daa24bdc03596 100644 --- a/tests/lib/Preview/Storage/ObjectStorePreviewStorageTest.php +++ b/tests/lib/Preview/Storage/ObjectStorePreviewStorageTest.php @@ -70,22 +70,26 @@ public function testMigratePreviewsLooksUpTheLocationOncePerBucket(): void { public function testMigratePreviewsWithMultibucketDistribution(): void { $storage = $this->createStorage(multibucket: true, distribution: true); - // md5('1') and md5('2') start with 'c', md5('3') with 'e' + // The legacy bucket suffix is the first two hex digits of the file ID hash. $previews = [self::createPreview(1), self::createPreview(2), self::createPreview(3)]; - $this->previewMapper->expects($this->exactly(2)) + $this->previewMapper->expects($this->exactly(3)) ->method('getLocationId') - ->willReturnMap([ - ['nc-preview-204', 'default', '1'], - ['nc-preview-238', 'default', '2'], - ]); + ->willReturnCallback(static fn (string $bucket, string $objectStore): string => match ([$bucket, $objectStore]) { + ['nc-preview-196', 'default'] => '1', + ['nc-preview-200', 'default'] => '2', + ['nc-preview-236', 'default'] => '3', + default => 'unexpected-location', + }); $storage->migratePreviews($previews); + $this->assertSame('nc-preview-196', $previews[0]->getBucketName()); + $this->assertSame('nc-preview-200', $previews[1]->getBucketName()); + $this->assertSame('nc-preview-236', $previews[2]->getBucketName()); $this->assertSame('1', $previews[0]->getLocationId()); - $this->assertSame('1', $previews[1]->getLocationId()); - $this->assertSame('2', $previews[2]->getLocationId()); - $this->assertSame('nc-preview-238', $previews[2]->getBucketName()); + $this->assertSame('2', $previews[1]->getLocationId()); + $this->assertSame('3', $previews[2]->getLocationId()); } /**