Skip to content

Commit 1e7df28

Browse files
committed
Set export service to set correct svg image mimetype
For BookStackApp#1538
1 parent 629b7a6 commit 1e7df28

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

app/Uploads/ImageService.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,12 @@ public function imageUriToBase64(string $uri)
442442
return null;
443443
}
444444

445-
return 'data:image/' . pathinfo($uri, PATHINFO_EXTENSION) . ';base64,' . base64_encode($imageData);
445+
$extension = pathinfo($uri, PATHINFO_EXTENSION);
446+
if ($extension === 'svg') {
447+
$extension = 'svg+xml';
448+
}
449+
450+
return 'data:image/' . $extension . ';base64,' . base64_encode($imageData);
446451
}
447452

448453
/**

tests/Entity/ExportTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use BookStack\Entities\Chapter;
55
use BookStack\Entities\Page;
6+
use BookStack\Uploads\HttpFetcher;
67

78
class ExportTest extends TestCase
89
{
@@ -148,4 +149,17 @@ public function test_page_html_export_use_absolute_dates()
148149
$resp->assertDontSee($page->updated_at->diffForHumans());
149150
}
150151

152+
public function test_page_export_sets_right_data_type_for_svg_embeds()
153+
{
154+
$page = Page::first();
155+
$page->html = '<img src="http://example.com/image.svg">';
156+
$page->save();
157+
158+
$this->asEditor();
159+
$this->mockHttpFetch('<svg></svg>');
160+
$resp = $this->get($page->getUrl('/export/html'));
161+
$resp->assertStatus(200);
162+
$resp->assertSee('<img src="data:image/svg+xml;base64');
163+
}
164+
151165
}

tests/SharedTestHelpers.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use BookStack\Auth\Permissions\PermissionService;
1212
use BookStack\Entities\Repos\PageRepo;
1313
use BookStack\Settings\SettingService;
14+
use BookStack\Uploads\HttpFetcher;
1415

1516
trait SharedTestHelpers
1617
{
@@ -189,4 +190,18 @@ protected function createNewRole($permissions = [])
189190
return $permissionRepo->saveNewRole($roleData);
190191
}
191192

193+
/**
194+
* Mock the HttpFetcher service and return the given data on fetch.
195+
* @param $returnData
196+
* @param int $times
197+
*/
198+
protected function mockHttpFetch($returnData, int $times = 1)
199+
{
200+
$mockHttp = \Mockery::mock(HttpFetcher::class);
201+
$this->app[HttpFetcher::class] = $mockHttp;
202+
$mockHttp->shouldReceive('fetch')
203+
->times($times)
204+
->andReturn($returnData);
205+
}
206+
192207
}

0 commit comments

Comments
 (0)