Set export service to set correct svg image mimetype

For #1538
This commit is contained in:
Dan Brown 2019-07-17 22:36:49 +01:00
parent 629b7a674e
commit 1e7df28238
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
3 changed files with 35 additions and 1 deletions

View File

@ -442,7 +442,12 @@ class ImageService extends UploadService
return null;
}
return 'data:image/' . pathinfo($uri, PATHINFO_EXTENSION) . ';base64,' . base64_encode($imageData);
$extension = pathinfo($uri, PATHINFO_EXTENSION);
if ($extension === 'svg') {
$extension = 'svg+xml';
}
return 'data:image/' . $extension . ';base64,' . base64_encode($imageData);
}
/**

View File

@ -3,6 +3,7 @@
use BookStack\Entities\Chapter;
use BookStack\Entities\Page;
use BookStack\Uploads\HttpFetcher;
class ExportTest extends TestCase
{
@ -148,4 +149,17 @@ class ExportTest extends TestCase
$resp->assertDontSee($page->updated_at->diffForHumans());
}
public function test_page_export_sets_right_data_type_for_svg_embeds()
{
$page = Page::first();
$page->html = '<img src="http://example.com/image.svg">';
$page->save();
$this->asEditor();
$this->mockHttpFetch('<svg></svg>');
$resp = $this->get($page->getUrl('/export/html'));
$resp->assertStatus(200);
$resp->assertSee('<img src="data:image/svg+xml;base64');
}
}

View File

@ -11,6 +11,7 @@ use BookStack\Auth\Role;
use BookStack\Auth\Permissions\PermissionService;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Settings\SettingService;
use BookStack\Uploads\HttpFetcher;
trait SharedTestHelpers
{
@ -189,4 +190,18 @@ trait SharedTestHelpers
return $permissionRepo->saveNewRole($roleData);
}
/**
* Mock the HttpFetcher service and return the given data on fetch.
* @param $returnData
* @param int $times
*/
protected function mockHttpFetch($returnData, int $times = 1)
{
$mockHttp = \Mockery::mock(HttpFetcher::class);
$this->app[HttpFetcher::class] = $mockHttp;
$mockHttp->shouldReceive('fetch')
->times($times)
->andReturn($returnData);
}
}