Skip to content

Commit 5e29170

Browse files
authored
Merge pull request #49788 from nextcloud/backport/40394/stable27
[stable27] fix mimetype not being updated when changing file extention on objectstore
2 parents b49a8a5 + 3b655e6 commit 5e29170

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

lib/private/Files/Cache/Updater.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ public function renameFromStorage(IStorage $sourceStorage, $source, $target) {
196196

197197
$sourceInfo = $sourceCache->get($source);
198198

199+
$sourceExtension = pathinfo($source, PATHINFO_EXTENSION);
200+
$targetExtension = pathinfo($target, PATHINFO_EXTENSION);
201+
$targetIsTrash = preg_match("/^d\d+$/", $targetExtension);
202+
199203
if ($sourceInfo !== false) {
200204
if ($this->cache->inCache($target)) {
201205
$this->cache->remove($target);
@@ -207,16 +211,16 @@ public function renameFromStorage(IStorage $sourceStorage, $source, $target) {
207211
$this->cache->moveFromCache($sourceCache, $source, $target);
208212
}
209213

210-
$sourceExtension = pathinfo($source, PATHINFO_EXTENSION);
211-
$targetExtension = pathinfo($target, PATHINFO_EXTENSION);
212-
$targetIsTrash = preg_match("/d\d+/", $targetExtension);
214+
$isDir = $sourceInfo->getMimeType() === FileInfo::MIMETYPE_FOLDER;
215+
} else {
216+
$isDir = $this->storage->is_dir($target);
217+
}
213218

214-
if ($sourceExtension !== $targetExtension && $sourceInfo->getMimeType() !== FileInfo::MIMETYPE_FOLDER && !$targetIsTrash) {
215-
// handle mime type change
216-
$mimeType = $this->storage->getMimeType($target);
217-
$fileId = $this->cache->getId($target);
218-
$this->cache->update($fileId, ['mimetype' => $mimeType]);
219-
}
219+
if ($sourceExtension !== $targetExtension && !$isDir && !$targetIsTrash) {
220+
// handle mime type change
221+
$mimeType = $this->storage->getMimeType($target);
222+
$fileId = $this->cache->getId($target);
223+
$this->cache->update($fileId, ['mimetype' => $mimeType]);
220224
}
221225

222226
if ($sourceCache instanceof Cache) {

tests/lib/Files/Cache/UpdaterTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
namespace Test\Files\Cache;
1010

1111
use OC\Files\Filesystem;
12+
use OC\Files\ObjectStore\ObjectStoreStorage;
13+
use OC\Files\ObjectStore\StorageObjectStore;
1214
use OC\Files\Storage\Temporary;
15+
use OCP\Files\Storage\IStorage;
1316

1417
/**
1518
* Class UpdaterTest
@@ -302,4 +305,36 @@ public function testMoveFolderCrossStorage() {
302305
$this->assertEquals($old['mimetype'], $new['mimetype']);
303306
}
304307
}
308+
309+
public function changeExtensionProvider(): array {
310+
return [
311+
[new Temporary()],
312+
[new ObjectStoreStorage(['objectstore' => new StorageObjectStore(new Temporary())])]
313+
];
314+
}
315+
316+
/**
317+
* @dataProvider changeExtensionProvider
318+
*/
319+
public function testChangeExtension(IStorage $storage) {
320+
$updater = $storage->getUpdater();
321+
$cache = $storage->getCache();
322+
$storage->file_put_contents('foo', 'qwerty');
323+
$updater->update('foo');
324+
325+
$bareCached = $cache->get('foo');
326+
$this->assertEquals('application/octet-stream', $bareCached->getMimeType());
327+
328+
$storage->rename('foo', 'foo.txt');
329+
$updater->renameFromStorage($storage, 'foo', 'foo.txt');
330+
331+
$cached = $cache->get('foo.txt');
332+
$this->assertEquals('text/plain', $cached->getMimeType());
333+
334+
$storage->rename('foo.txt', 'foo.md');
335+
$updater->renameFromStorage($storage, 'foo.txt', 'foo.md');
336+
337+
$cachedTarget = $cache->get('foo.md');
338+
$this->assertEquals('text/markdown', $cachedTarget->getMimeType());
339+
}
305340
}

0 commit comments

Comments
 (0)