aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2023-09-13 16:36:25 +0200
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>2024-12-11 09:00:37 +0000
commitf8ad3d6cc9b049bcefdfeb36c676c4be8b1bb39d (patch)
treec8d74c833c46fb120898315e9737c51a9317e95c
parentc1451b2ab162ee0ce9b5694367ff361d6d9b42eb (diff)
downloadnextcloud-server-f8ad3d6cc9b049bcefdfeb36c676c4be8b1bb39d.tar.gz
nextcloud-server-f8ad3d6cc9b049bcefdfeb36c676c4be8b1bb39d.zip
fix: fix mimetype not being updated when changing file extention on object storebackport/40394/stable29
Signed-off-by: Robin Appelman <robin@icewind.nl>
-rw-r--r--lib/private/Files/Cache/Updater.php22
-rw-r--r--tests/lib/Files/Cache/UpdaterTest.php35
2 files changed, 48 insertions, 9 deletions
diff --git a/lib/private/Files/Cache/Updater.php b/lib/private/Files/Cache/Updater.php
index a6f2f3375a4..d84ce0967a0 100644
--- a/lib/private/Files/Cache/Updater.php
+++ b/lib/private/Files/Cache/Updater.php
@@ -196,6 +196,10 @@ class Updater implements IUpdater {
$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);
@@ -207,16 +211,16 @@ class Updater implements IUpdater {
$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 7e0f6866793..3a6a37ae9f7 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 @@ class UpdaterTest extends \Test\TestCase {
$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());
+ }
}