Skip to content

Commit 0627efe

Browse files
committed
Updated base64 image extraction to use url instead of path
To ensure it works with all storage types and follows the format of manually uploaded image content
1 parent af7d627 commit 0627efe

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

app/Entities/Tools/PageContent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function extractBase64Images(Page $page, string $htmlText): string
9999
$imageName = 'embedded-image-' . Str::random(8) . '.' . $extension;
100100
try {
101101
$image = $imageRepo->saveNewFromData($imageName, base64_decode($base64ImageData), 'gallery', $page->id);
102-
$imageNode->setAttribute('src', $image->path);
102+
$imageNode->setAttribute('src', $image->url);
103103
} catch (ImageUploadException $exception) {
104104
$imageNode->setAttribute('src', '');
105105
}

app/Uploads/Image.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
use BookStack\Model;
55
use BookStack\Traits\HasCreatorAndUpdater;
66

7+
/**
8+
* @property int $id
9+
* @property string $name
10+
* @property string $url
11+
* @property string $path
12+
* @property string $type
13+
* @property int $uploaded_to
14+
* @property int $created_by
15+
* @property int $updated_by
16+
*/
717
class Image extends Model
818
{
919
use HasCreatorAndUpdater;

tests/Entity/PageContentTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,10 @@ public function test_base64_images_get_extracted_from_page_content()
495495
]);
496496

497497
$page->refresh();
498-
$this->assertStringMatchesFormat('%A<p%A>test<img src="/uploads/images/gallery/%A.jpeg">%A</p>%A', $page->html);
498+
$this->assertStringMatchesFormat('%A<p%A>test<img src="http://localhost/uploads/images/gallery/%A.jpeg">%A</p>%A', $page->html);
499499

500500
$matches = [];
501-
preg_match('/src="(.*?)"/', $page->html, $matches);
501+
preg_match('/src="http:\/\/localhost(.*?)"/', $page->html, $matches);
502502
$imagePath = $matches[1];
503503
$imageFile = public_path($imagePath);
504504
$this->assertEquals(base64_decode($this->base64Jpeg), file_get_contents($imageFile));
@@ -519,10 +519,10 @@ public function test_base64_images_get_extracted_when_containing_whitespace()
519519
]);
520520

521521
$page->refresh();
522-
$this->assertStringMatchesFormat('%A<p%A>test<img src="/uploads/images/gallery/%A.png">%A</p>%A', $page->html);
522+
$this->assertStringMatchesFormat('%A<p%A>test<img src="http://localhost/uploads/images/gallery/%A.png">%A</p>%A', $page->html);
523523

524524
$matches = [];
525-
preg_match('/src="(.*?)"/', $page->html, $matches);
525+
preg_match('/src="http:\/\/localhost(.*?)"/', $page->html, $matches);
526526
$imagePath = $matches[1];
527527
$imageFile = public_path($imagePath);
528528
$this->assertEquals(base64_decode($base64PngWithoutWhitespace), file_get_contents($imageFile));

0 commit comments

Comments
 (0)