Skip to content

Commit 884664b

Browse files
committed
Ensured base64 images are read from image upload folder
Also removed unused storage systems and updated testing.
1 parent 8911e3f commit 884664b

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

app/Config/filesystems.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@
4242
'root' => storage_path(),
4343
],
4444

45-
'ftp' => [
46-
'driver' => 'ftp',
47-
'host' => 'ftp.example.com',
48-
'username' => 'your-username',
49-
'password' => 'your-password',
50-
],
51-
5245
's3' => [
5346
'driver' => 's3',
5447
'key' => env('STORAGE_S3_KEY', 'your-key'),
@@ -59,16 +52,6 @@
5952
'use_path_style_endpoint' => env('STORAGE_S3_ENDPOINT', null) !== null,
6053
],
6154

62-
'rackspace' => [
63-
'driver' => 'rackspace',
64-
'username' => 'your-username',
65-
'key' => 'your-key',
66-
'container' => 'your-container',
67-
'endpoint' => 'https://identity.api.rackspacecloud.com/v2.0/',
68-
'region' => 'IAD',
69-
'url_type' => 'publicURL',
70-
],
71-
7255
],
7356

7457
];

app/Uploads/ImageService.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,28 +450,32 @@ public function imageUriToBase64(string $uri): ?string
450450

451451
/**
452452
* Get a storage path for the given image URL.
453+
* Ensures the path will start with "uploads/images".
453454
* Returns null if the url cannot be resolved to a local URL.
454455
*/
455456
private function imageUrlToStoragePath(string $url): ?string
456457
{
457-
$url = trim($url);
458+
$url = ltrim(trim($url), '/');
458459

459460
// Handle potential relative paths
460461
$isRelative = strpos($url, 'http') !== 0;
461462
if ($isRelative) {
462-
return trim($url, '/');
463+
if (strpos(strtolower($url), 'uploads/images') === 0) {
464+
return trim($url, '/');
465+
}
466+
return null;
463467
}
464468

465469
// Handle local images based on paths on the same domain
466470
$potentialHostPaths = [
467-
url('/'),
468-
$this->getPublicUrl('/'),
471+
url('uploads/images/'),
472+
$this->getPublicUrl('/uploads/images/'),
469473
];
470474

471475
foreach ($potentialHostPaths as $potentialBasePath) {
472476
$potentialBasePath = strtolower($potentialBasePath);
473477
if (strpos(strtolower($url), $potentialBasePath) === 0) {
474-
return trim(substr($url, strlen($potentialBasePath)), '/');
478+
return 'uploads/images/' . trim(substr($url, strlen($potentialBasePath)), '/');
475479
}
476480
}
477481

tests/Entity/ExportTest.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php namespace Tests\Entity;
22

3-
43
use BookStack\Entities\Chapter;
54
use BookStack\Entities\Page;
6-
use BookStack\Uploads\HttpFetcher;
5+
use Illuminate\Support\Facades\Storage;
76
use Illuminate\Support\Str;
87
use Tests\TestCase;
98

@@ -154,14 +153,39 @@ public function test_page_html_export_use_absolute_dates()
154153
public function test_page_export_sets_right_data_type_for_svg_embeds()
155154
{
156155
$page = Page::first();
157-
$page->html = '<img src="http://example.com/image.svg">';
156+
Storage::disk('local')->makeDirectory('uploads/images/gallery');
157+
Storage::disk('local')->put('uploads/images/gallery/svg_test.svg', '<svg></svg>');
158+
$page->html = '<img src="http://localhost/uploads/images/gallery/svg_test.svg">';
158159
$page->save();
159160

160161
$this->asEditor();
161-
$this->mockHttpFetch('<svg></svg>');
162162
$resp = $this->get($page->getUrl('/export/html'));
163+
Storage::disk('local')->delete('uploads/images/gallery/svg_test.svg');
164+
163165
$resp->assertStatus(200);
164166
$resp->assertSee('<img src="data:image/svg+xml;base64');
165167
}
166168

169+
public function test_page_export_contained_html_image_fetches_only_run_when_url_points_to_image_upload_folder()
170+
{
171+
$page = Page::first();
172+
$page->html = '<img src="http://localhost/uploads/images/gallery/svg_test.svg"/>'
173+
."\n".'<img src="http://localhost/uploads/svg_test.svg"/>'
174+
."\n".'<img src="/uploads/svg_test.svg"/>';
175+
$storageDisk = Storage::disk('local');
176+
$storageDisk->makeDirectory('uploads/images/gallery');
177+
$storageDisk->put('uploads/images/gallery/svg_test.svg', '<svg>good</svg>');
178+
$storageDisk->put('uploads/svg_test.svg', '<svg>bad</svg>');
179+
$page->save();
180+
181+
$resp = $this->asEditor()->get($page->getUrl('/export/html'));
182+
183+
$storageDisk->delete('uploads/images/gallery/svg_test.svg');
184+
$storageDisk->delete('uploads/svg_test.svg');
185+
186+
$resp->assertDontSee('http://localhost/uploads/images/gallery/svg_test.svg');
187+
$resp->assertSee('http://localhost/uploads/svg_test.svg');
188+
$resp->assertSee('src="/uploads/svg_test.svg"');
189+
}
190+
167191
}

0 commit comments

Comments
 (0)