From 10b6d92319df345716b69e13dfabf2841945ba06 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 13 Sep 2023 16:36:25 +0200 Subject: [PATCH] fix: fix mimetype not being updated when changing file extention on object store Signed-off-by: Robin Appelman --- lib/private/Files/Cache/Updater.php | 22 ++++++++++------- tests/lib/Files/Cache/UpdaterTest.php | 35 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/lib/private/Files/Cache/Updater.php b/lib/private/Files/Cache/Updater.php index 457dd207e9d8a..f56f7f044649c 100644 --- a/lib/private/Files/Cache/Updater.php +++ b/lib/private/Files/Cache/Updater.php @@ -194,6 +194,10 @@ public function renameFromStorage(IStorage $sourceStorage, $source, $target) { $sourceInfo = $sourceCache->get($source); + $sourceExtension = pathinfo($source, PATHINFO_EXTENSION); + $targetExtension = pathinfo($target, PATHINFO_EXTENSION); + $targetIsTrash = preg_match("/^d\d+$/", $targetExtension); + if ($sourceInfo !== false) { if ($this->cache->inCache($target)) { $this->cache->remove($target); @@ -205,16 +209,16 @@ public function renameFromStorage(IStorage $sourceStorage, $source, $target) { $this->cache->moveFromCache($sourceCache, $source, $target); } - $sourceExtension = pathinfo($source, PATHINFO_EXTENSION); - $targetExtension = pathinfo($target, PATHINFO_EXTENSION); - $targetIsTrash = preg_match("/d\d+/", $targetExtension); + $isDir = $sourceInfo->getMimeType() === FileInfo::MIMETYPE_FOLDER; + } else { + $isDir = $this->storage->is_dir($target); + } - if ($sourceExtension !== $targetExtension && $sourceInfo->getMimeType() !== FileInfo::MIMETYPE_FOLDER && !$targetIsTrash) { - // handle mime type change - $mimeType = $this->storage->getMimeType($target); - $fileId = $this->cache->getId($target); - $this->cache->update($fileId, ['mimetype' => $mimeType]); - } + if ($sourceExtension !== $targetExtension && !$isDir && !$targetIsTrash) { + // handle mime type change + $mimeType = $this->storage->getMimeType($target); + $fileId = $this->cache->getId($target); + $this->cache->update($fileId, ['mimetype' => $mimeType]); } if ($sourceCache instanceof Cache) { diff --git a/tests/lib/Files/Cache/UpdaterTest.php b/tests/lib/Files/Cache/UpdaterTest.php index 7e0f686679335..3a6a37ae9f7b4 100644 --- a/tests/lib/Files/Cache/UpdaterTest.php +++ b/tests/lib/Files/Cache/UpdaterTest.php @@ -9,7 +9,10 @@ namespace Test\Files\Cache; use OC\Files\Filesystem; +use OC\Files\ObjectStore\ObjectStoreStorage; +use OC\Files\ObjectStore\StorageObjectStore; use OC\Files\Storage\Temporary; +use OCP\Files\Storage\IStorage; /** * Class UpdaterTest @@ -302,4 +305,36 @@ public function testMoveFolderCrossStorage() { $this->assertEquals($old['mimetype'], $new['mimetype']); } } + + public function changeExtensionProvider(): array { + return [ + [new Temporary()], + [new ObjectStoreStorage(['objectstore' => new StorageObjectStore(new Temporary())])] + ]; + } + + /** + * @dataProvider changeExtensionProvider + */ + public function testChangeExtension(IStorage $storage) { + $updater = $storage->getUpdater(); + $cache = $storage->getCache(); + $storage->file_put_contents('foo', 'qwerty'); + $updater->update('foo'); + + $bareCached = $cache->get('foo'); + $this->assertEquals('application/octet-stream', $bareCached->getMimeType()); + + $storage->rename('foo', 'foo.txt'); + $updater->renameFromStorage($storage, 'foo', 'foo.txt'); + + $cached = $cache->get('foo.txt'); + $this->assertEquals('text/plain', $cached->getMimeType()); + + $storage->rename('foo.txt', 'foo.md'); + $updater->renameFromStorage($storage, 'foo.txt', 'foo.md'); + + $cachedTarget = $cache->get('foo.md'); + $this->assertEquals('text/markdown', $cachedTarget->getMimeType()); + } }