MDL-72443 files: support svg preview as with other image types.

This commit is contained in:
Paul Holden
2021-11-12 15:07:09 +00:00
parent 5d7fa92613
commit 0efdee5e6e
3 changed files with 50 additions and 14 deletions
+13 -3
View File
@@ -224,7 +224,7 @@ class file_storage {
/**
* Returns an image file that represent the given stored file as a preview
*
* At the moment, only GIF, JPEG and PNG files are supported to have previews. In the
* At the moment, only GIF, JPEG, PNG and SVG files are supported to have previews. In the
* future, the support for other mimetypes can be added, too (eg. generate an image
* preview of PDF, text documents etc).
*
@@ -410,7 +410,9 @@ class file_storage {
if ($mimetype === 'image/gif' or $mimetype === 'image/jpeg' or $mimetype === 'image/png') {
// make a preview of the image
$data = $this->create_imagefile_preview($file, $mode);
} else if ($mimetype === 'image/svg+xml') {
// If we have an SVG image, then return the original (scalable) file.
return $file;
} else {
// unable to create the preview of this mimetype yet
return false;
@@ -2229,7 +2231,15 @@ class file_storage {
if (file_exists($fullpath)) {
// The type is unknown. Attempt to look up the file type now.
$finfo = new finfo(FILEINFO_MIME_TYPE);
return mimeinfo_from_type('type', $finfo->file($fullpath));
// See https://bugs.php.net/bug.php?id=79045 - finfo isn't consistent with returned type, normalize into value
// that is used internally by the {@see core_filetypes} class and the {@see mimeinfo_from_type} call below.
$mimetype = $finfo->file($fullpath);
if ($mimetype === 'image/svg') {
$mimetype = 'image/svg+xml';
}
return mimeinfo_from_type('type', $mimetype);
}
return 'document/unknown';
+31 -11
View File
@@ -2115,26 +2115,42 @@ class core_files_file_storage_testcase extends advanced_testcase {
}
/**
* Test that mimetype_from_file returns appropriate output for a known
* file.
* Data provider to return fixture files and their expected mimetype
*
* @return array[]
*/
public function filepath_mimetype_provider(): array {
return [
[__DIR__ . '/fixtures/testimage.jpg', 'image/jpeg'],
[__DIR__ . '/fixtures/testimage.svg', 'image/svg+xml'],
[__DIR__ . '/fixtures/testimage_basic.svg', 'image/svg+xml'],
];
}
/**
* Test that mimetype returns appropriate output for a known file.
*
* Note: this is not intended to check that functions outside of this
* file works. It is intended to validate the codepath contains no
* errors and behaves as expected.
*
* @covers ::mimetype
*
* @param string $filepath
* @param string $expectedmimetype
*
* @dataProvider filepath_mimetype_provider
*/
public function test_mimetype_known() {
$filepath = __DIR__ . '/fixtures/testimage.jpg';
$mimetype = file_storage::mimetype_from_file($filepath);
$this->assertEquals('image/jpeg', $mimetype);
public function test_mimetype_known(string $filepath, string $expectedmimetype): void {
$mimetype = file_storage::mimetype($filepath);
$this->assertEquals($expectedmimetype, $mimetype);
}
/**
* Test that mimetype_from_file returns appropriate output when the
* file could not be found.
*
* @covers ::mimetype
* @covers ::mimetype_from_file
*/
public function test_mimetype_from_file_not_found() {
$mimetype = file_storage::mimetype_from_file('/path/to/nonexistent/file');
@@ -2149,12 +2165,16 @@ class core_files_file_storage_testcase extends advanced_testcase {
* file works. It is intended to validate the codepath contains no
* errors and behaves as expected.
*
* @covers ::mimetype
* @covers ::mimetype_from_file
*
* @param string $filepath
* @param string $expectedmimetype
*
* @dataProvider filepath_mimetype_provider
*/
public function test_mimetype_from_file_known() {
$filepath = __DIR__ . '/fixtures/testimage.jpg';
public function test_mimetype_from_file_known(string $filepath, string $expectedmimetype): void {
$mimetype = file_storage::mimetype_from_file($filepath);
$this->assertEquals('image/jpeg', $mimetype);
$this->assertEquals($expectedmimetype, $mimetype);
}
}
+6
View File
@@ -0,0 +1,6 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" stroke="#000">
<path d="M8,80s-5,8,5,9l78,0s9,0,5-9l-40-71s-4-6-8,0z" stroke-width="2" fill="#fff" fill-rule="evenodd" />
<path d="M52,10 L10,85 L93,85z" stroke-width="2" stroke-linejoin="round" fill="#fc0" fill-rule="evenodd"/>
<path d="M52,32l0,26" stroke-width="9" stroke-linecap="round" fill-rule="evenodd"/>
<circle r="6" cx="52" cy="73"/>
</svg>

After

Width:  |  Height:  |  Size: 422 B