Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mimetype not being updated when changing file extention on objectstore #40394

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions lib/private/Files/Cache/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fragile, shouldn’t this test if target is in files_trashbin instead?
Or at least have a regex with ^ or $, not something that can match anything in the middle.
https://www.file-extension.info/format/d3d This format for instance would match the regex while not being trash.


if ($sourceInfo !== false) {
if ($this->cache->inCache($target)) {
$this->cache->remove($target);
Expand All @@ -209,12 +213,16 @@ public function renameFromStorage(IStorage $sourceStorage, $source, $target) {
$targetExtension = pathinfo($target, PATHINFO_EXTENSION);
$targetIsTrash = preg_match("/d\d+/", $targetExtension);
Comment on lines 213 to 214
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now done twice?


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]);
}
$isDir = $sourceInfo->getMimeType() === FileInfo::MIMETYPE_FOLDER;
} else {
$isDir = $this->storage->is_dir($target);
}

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 @@ -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
Expand Down Expand Up @@ -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());
}
}
Loading