Merge pull request #6089 from owncloud/files-mimetyperefreshafterrename

Redetect mime type whenever extension is renamed
This commit is contained in:
Frank Karlitschek 2013-11-28 03:46:42 -08:00
commit dab3629ff5
5 changed files with 89 additions and 28 deletions

View file

@ -422,12 +422,27 @@ var FileList={
}
tr.find('.fileactions').effect('highlight', {}, 5000);
tr.effect('highlight', {}, 5000);
// remove loading mark and recover old image
td.css('background-image', oldBackgroundImage);
}
else {
var fileInfo = result.data;
tr.attr('data-mime', fileInfo.mime);
tr.attr('data-etag', fileInfo.etag);
if (fileInfo.isPreviewAvailable) {
Files.lazyLoadPreview(fileInfo.directory + '/' + fileInfo.name, result.data.mime, function(previewpath) {
tr.find('td.filename').attr('style','background-image:url('+previewpath+')');
}, null, null, result.data.etag);
}
else {
tr.find('td.filename').removeClass('preview').attr('style','background-image:url('+fileInfo.icon+')');
}
}
// reinsert row
tr.detach();
FileList.insertElement( tr.attr('data-file'), tr.attr('data-type'),tr );
// remove loading mark and recover old image
td.css('background-image', oldBackgroundImage);
// update file actions in case the extension changed
FileActions.display( tr.find('td.filename'), true);
}
});
}

View file

@ -76,12 +76,19 @@ class App {
$this->view->rename($dir . '/' . $oldname, $dir . '/' . $newname)
) {
// successful rename
$result['success'] = true;
$result['data'] = array(
'dir' => $dir,
'file' => $oldname,
'newname' => $newname
$meta = $this->view->getFileInfo($dir . '/' . $newname);
$fileinfo = array(
'id' => $meta['fileid'],
'mime' => $meta['mimetype'],
'size' => $meta['size'],
'etag' => $meta['etag'],
'directory' => $dir,
'name' => $newname,
'isPreviewAvailable' => \OC::$server->getPreviewManager()->isMimeSupported($meta['mimetype']),
'icon' => \OCA\Files\Helper::determineIcon($meta)
);
$result['success'] = true;
$result['data'] = $fileinfo;
} else {
// rename failed
$result['data'] = array(

View file

@ -38,13 +38,14 @@ class Test_OC_Files_App_Rename extends \PHPUnit_Framework_TestCase {
$l10nMock->expects($this->any())
->method('t')
->will($this->returnArgument(0));
$viewMock = $this->getMock('\OC\Files\View', array('rename', 'normalizePath'), array(), '', false);
$viewMock = $this->getMock('\OC\Files\View', array('rename', 'normalizePath', 'getFileInfo'), array(), '', false);
$viewMock->expects($this->any())
->method('normalizePath')
->will($this->returnArgument(0));
$viewMock->expects($this->any())
->method('rename')
->will($this->returnValue(true));
$this->viewMock = $viewMock;
$this->files = new \OCA\Files\App($viewMock, $l10nMock);
}
@ -79,17 +80,28 @@ class Test_OC_Files_App_Rename extends \PHPUnit_Framework_TestCase {
$oldname = 'Shared';
$newname = 'new_name';
$result = $this->files->rename($dir, $oldname, $newname);
$expected = array(
'success' => true,
'data' => array(
'dir' => $dir,
'file' => $oldname,
'newname' => $newname
)
);
$this->viewMock->expects($this->any())
->method('getFileInfo')
->will($this->returnValue(array(
'fileid' => 123,
'type' => 'dir',
'mimetype' => 'httpd/unix-directory',
'size' => 18,
'etag' => 'abcdef',
'directory' => '/',
'name' => 'new_name',
)));
$this->assertEquals($expected, $result);
$result = $this->files->rename($dir, $oldname, $newname);
$this->assertTrue($result['success']);
$this->assertEquals(123, $result['data']['id']);
$this->assertEquals('new_name', $result['data']['name']);
$this->assertEquals('/test', $result['data']['directory']);
$this->assertEquals(18, $result['data']['size']);
$this->assertEquals('httpd/unix-directory', $result['data']['mime']);
$this->assertEquals(\OC_Helper::mimetypeIcon('dir'), $result['data']['icon']);
$this->assertFalse($result['data']['isPreviewAvailable']);
}
/**
@ -117,16 +129,29 @@ class Test_OC_Files_App_Rename extends \PHPUnit_Framework_TestCase {
$oldname = 'oldname';
$newname = 'newname';
$result = $this->files->rename($dir, $oldname, $newname);
$expected = array(
'success' => true,
'data' => array(
'dir' => $dir,
'file' => $oldname,
'newname' => $newname
)
);
$this->viewMock->expects($this->any())
->method('getFileInfo')
->will($this->returnValue(array(
'fileid' => 123,
'type' => 'dir',
'mimetype' => 'httpd/unix-directory',
'size' => 18,
'etag' => 'abcdef',
'directory' => '/',
'name' => 'new_name',
)));
$this->assertEquals($expected, $result);
$result = $this->files->rename($dir, $oldname, $newname);
$this->assertTrue($result['success']);
$this->assertEquals(123, $result['data']['id']);
$this->assertEquals('newname', $result['data']['name']);
$this->assertEquals('/', $result['data']['directory']);
$this->assertEquals(18, $result['data']['size']);
$this->assertEquals('httpd/unix-directory', $result['data']['mime']);
$this->assertEquals('abcdef', $result['data']['etag']);
$this->assertEquals(\OC_Helper::mimetypeIcon('dir'), $result['data']['icon']);
$this->assertFalse($result['data']['isPreviewAvailable']);
}
}

View file

@ -86,6 +86,12 @@ class Updater {
if ($storageFrom === $storageTo) {
$cache = $storageFrom->getCache($internalFrom);
$cache->move($internalFrom, $internalTo);
if (pathinfo($internalFrom, PATHINFO_EXTENSION) !== pathinfo($internalTo, PATHINFO_EXTENSION)) {
// redetect mime type change
$mimeType = $storageTo->getMimeType($internalTo);
$fileId = $storageTo->getCache()->getId($internalTo);
$storageTo->getCache()->update($fileId, array('mimetype' => $mimeType));
}
$cache->correctFolderSize($internalFrom);
$cache->correctFolderSize($internalTo);
self::correctFolder($from, time());

View file

@ -202,6 +202,14 @@ class Updater extends \PHPUnit_Framework_TestCase {
$this->assertNotEquals($rootCachedData['etag'], $cachedData['etag']);
}
public function testRenameExtension() {
$fooCachedData = $this->cache->get('foo.txt');
$this->assertEquals('text/plain', $fooCachedData['mimetype']);
Filesystem::rename('foo.txt', 'foo.abcd');
$fooCachedData = $this->cache->get('foo.abcd');
$this->assertEquals('application/octet-stream', $fooCachedData['mimetype']);
}
public function testRenameWithMountPoints() {
$storage2 = new \OC\Files\Storage\Temporary(array());
$cache2 = $storage2->getCache();