Skip to content

Commit

Permalink
Merge pull request #40394 from nextcloud/updater-change-mimetype-obje…
Browse files Browse the repository at this point in the history
…ctstore
  • Loading branch information
skjnldsv authored Dec 11, 2024
2 parents ed54713 + 3719977 commit 3baa91d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
22 changes: 13 additions & 9 deletions lib/private/Files/Cache/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,25 @@ private function copyOrRenameFromStorage(IStorage $sourceStorage, string $source

$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->storage->instanceOfStorage(ObjectStoreStorage::class)) {
$operation($sourceCache, $sourceInfo);
}

$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) {
Expand Down
35 changes: 35 additions & 0 deletions tests/lib/Files/Cache/UpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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
Expand Down Expand Up @@ -301,4 +304,36 @@ public function testMoveFolderCrossStorage(): void {
$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());
}
}

0 comments on commit 3baa91d

Please sign in to comment.