1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-08-07 23:03:00 +03:00

ZIP Imports: Added image type validation/handling

Images were missing their extension after import since it was
(potentially) not part of the import data.
This adds validation via mime sniffing (to match normal image upload
checks) and also uses the same logic to sniff out a correct extension.

Added tests to cover.
Also fixed some existing tests around zip functionality.
This commit is contained in:
Dan Brown
2024-11-18 17:42:49 +00:00
parent e2f6e50df4
commit 59cfc087e1
9 changed files with 92 additions and 10 deletions

View File

@@ -107,12 +107,10 @@ class ZipExportTest extends TestCase
[
'name' => 'Exporty',
'value' => 'Content',
'order' => 1,
],
[
'name' => 'Another',
'value' => '',
'order' => 2,
]
], $pageData['tags']);
}
@@ -162,7 +160,6 @@ class ZipExportTest extends TestCase
$attachmentData = $pageData['attachments'][0];
$this->assertEquals('PageAttachmentExport.txt', $attachmentData['name']);
$this->assertEquals($attachment->id, $attachmentData['id']);
$this->assertEquals(1, $attachmentData['order']);
$this->assertArrayNotHasKey('link', $attachmentData);
$this->assertNotEmpty($attachmentData['file']);
@@ -193,7 +190,6 @@ class ZipExportTest extends TestCase
$attachmentData = $pageData['attachments'][0];
$this->assertEquals('My link attachment for export', $attachmentData['name']);
$this->assertEquals($attachment->id, $attachmentData['id']);
$this->assertEquals(1, $attachmentData['order']);
$this->assertEquals('https://example.com/cats', $attachmentData['link']);
$this->assertArrayNotHasKey('file', $attachmentData);
}

View File

@@ -11,7 +11,7 @@ use BookStack\Exports\ZipExports\ZipImportRunner;
use BookStack\Uploads\Image;
use Tests\TestCase;
class ZipExportValidatorTests extends TestCase
class ZipExportValidatorTest extends TestCase
{
protected array $filesToRemove = [];
@@ -71,4 +71,23 @@ class ZipExportValidatorTests extends TestCase
$this->assertEquals($expectedMessage, $results['book.pages.1.id']);
$this->assertEquals($expectedMessage, $results['book.chapters.1.id']);
}
public function test_image_files_need_to_be_a_valid_detected_image_file()
{
$validator = $this->getValidatorForData([
'page' => [
'id' => 4,
'name' => 'My page',
'markdown' => 'hello',
'images' => [
['id' => 4, 'name' => 'Image A', 'type' => 'gallery', 'file' => 'cat'],
],
]
], ['cat' => $this->files->testFilePath('test-file.txt')]);
$results = $validator->validate();
$this->assertCount(1, $results);
$this->assertEquals('The file needs to reference a file of type image/png,image/jpeg,image/gif,image/webp, found text/plain.', $results['page.images.0.file']);
}
}

View File

@@ -358,4 +358,39 @@ class ZipImportRunnerTest extends TestCase
ZipTestHelper::deleteZipForImport($import);
}
public function test_imported_images_have_their_detected_extension_added()
{
$testImagePath = $this->files->testFilePath('test-image.png');
$parent = $this->entities->chapter();
$import = ZipTestHelper::importFromData([], [
'page' => [
'name' => 'Page A',
'html' => '<p>hello</p>',
'images' => [
[
'id' => 2,
'name' => 'Cat',
'type' => 'gallery',
'file' => 'cat_image'
]
],
],
], [
'cat_image' => $testImagePath,
]);
$this->asAdmin();
/** @var Page $page */
$page = $this->runner->run($import, $parent);
$pageImages = Image::where('uploaded_to', '=', $page->id)->whereIn('type', ['gallery', 'drawio'])->get();
$this->assertCount(1, $pageImages);
$this->assertStringEndsWith('.png', $pageImages[0]->url);
$this->assertStringEndsWith('.png', $pageImages[0]->path);
ZipTestHelper::deleteZipForImport($import);
}
}