]> BookStack Code Mirror - bookstack/commitdiff
Set export service to set correct svg image mimetype
authorDan Brown <redacted>
Wed, 17 Jul 2019 21:36:49 +0000 (22:36 +0100)
committerDan Brown <redacted>
Wed, 17 Jul 2019 21:37:19 +0000 (22:37 +0100)
For #1538

app/Uploads/ImageService.php
tests/Entity/ExportTest.php
tests/SharedTestHelpers.php

index 71fd2cd4eab31f5c0f4545cc9c671d95090443f6..ae1c6a254786c1c00fd010e181a0970bcc38d238 100644 (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);
     }
 
     /**
index 683f23674d66ed6997c947c2d2fe409b67994f86..e3a74f64d1d2c306df662071b369beadfd9bfbac 100644 (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');
+    }
+
 }
\ No newline at end of file
index 8e903be11a3089ed25a3c1ed77c7890924b25657..1d87e942aaf2167e10dbaed48f0a20f887511b94 100644 (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);
+    }
+
 }
\ No newline at end of file