Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
Fixes #10.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Nov 19, 2023
1 parent a4627f9 commit 461dbd4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ targetCompatibility = 17

description = 'Utilities to import diagrams and documentation into a Structurizr workspace'
group = 'com.structurizr'
version = '1.6.0'
version = '1.7.0'

test {
useJUnitPlatform()
Expand Down
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.7.0 (unreleased)

- Adds support for SVG images (https://github.com/structurizr/import/issues/10)

## 1.6.0 (26th October 2023)

- Fixes https://github.com/structurizr/import/issues/13 (Mermaid diagrams not working).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Base64;

/**
* This implementation scans a given directory and automatically imports all Markdown or AsciiDoc
Expand Down Expand Up @@ -57,7 +59,7 @@ private void importImages(Documentable documentable, String root, File path) thr
importImages(documentable, root + "/" + file.getName(), file);
}
} else {
if (name.endsWith(".png") || name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".gif")) {
if (name.endsWith(".png") || name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".gif") || name.endsWith(".svg")) {
importImage(documentable, root, file);
}
}
Expand All @@ -67,7 +69,7 @@ private void importImages(Documentable documentable, String root, File path) thr

private void importImage(Documentable documentable, String path, File file) throws IOException {
String contentType = ImageUtils.getContentType(file);
String base64Content = ImageUtils.getImageAsBase64(file);
String base64Content;

String name;
if (StringUtils.isNullOrEmpty(path)) {
Expand All @@ -76,6 +78,13 @@ private void importImage(Documentable documentable, String path, File file) thro
name = path + "/" + file.getName();
}

if (ImageUtils.CONTENT_TYPE_IMAGE_SVG.equalsIgnoreCase(contentType)) {
base64Content = Base64.getEncoder().encodeToString(Files.readAllBytes(file.toPath()));
} else {
contentType = ImageUtils.getContentType(file);
base64Content = ImageUtils.getImageAsBase64(file);
}

documentable.getDocumentation().addImage(new Image(name, contentType, base64Content));
}

Expand Down
3 changes: 3 additions & 0 deletions src/test/docs/images/image.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void test_importDocumentation_AddsAllImages_Recursively() {
assertTrue(documentation.getImages().isEmpty());

imageImporter.importDocumentation(workspace, new File("./src/test/docs/images"));
assertEquals(8, documentation.getImages().size());
assertEquals(9, documentation.getImages().size());

Image pngInDirectory = documentation.getImages().stream().filter(i -> i.getName().equals("image.png")).findFirst().get();
assertEquals("image/png", pngInDirectory.getType());
Expand All @@ -127,6 +127,10 @@ public void test_importDocumentation_AddsAllImages_Recursively() {
assertEquals("image/gif", gifInDirectory.getType());
assertEquals("R0lGODlhIAAaAPcAAAAAAAACCwAFHAAGFAAGIwAIFgAKHAAKJgAMKgAOMwAPPAARHwAUOQ0UHQIVMgMVJQMVLAoVKwwVJBEVHgAYOAQYJwkYORAYJQIZKwoZJQMaMgoaKwobMxQcKQsjRwMnVxcoPBsoOAAtWx8vQAAwXwIzaQ9HehZLhEVMWRRNjB5Ng0VNYUtQWkFRXhZShBVTjRRVkxlVjhtVlBtWmQpXoxFXmxZYmRFZpBhZjxpZlRValRlamAdcrgxcqg1cpCFdmw1esRReqhleqx9eoyhenhNgrAxitBlirxNktCZknw5luxllsyJlrBJmuhhmuCNnsCVnpBRptRRpvBxpryNprhVqwhtrvBxrtSJrtRxtwCVuuyFytCZ0xid0uit0wCV4xi97xDh9xDGAyzuAy0KBy0SCw0iDw0aG0EuGyjuI2EuM1EiN2EOO2EaP1USR26SipZ+kqKSmsqamrGGq76SquG+w9Gy0/Im05nK183m1+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afiRzwlttMDAgxAR4pHPpkQQ31RtJNwHvWXgXFKfrTABg4gABYCaKYZFpoJJFAmBAQgoFVxCGnlnAMlDIEFe1b02ScWgGJhRRZ9RjEFFCLE1dFHCLywpxRN8MBFGmyw0UYbamS6hhtfCOhECgRIYF5PGSx2RRRR9DCHI5FR5scfgUy/pgcWS8hAgEFNKqZDFFIA0QUehfyRIGWWjUjGZhH6RxwEANYgxYBa5LHHIcOKmBQhjRSCRg0/3GrBik/+FJQRv1ZGGYhMDbJIIWUUYUNnCBwmGHpASSGFqo/04SGIhazmiCB77nDARFrBJQEBIi2BBBI0YBFGGRBHTAYZY2jRwxJMqCBABysIAAKTwYlgwgsvwADDDtmZXF12271wgggeGMBCJG+gcJGdGGyQwc4ZNIAXzxlIMEEDO9OFAhySBAQAOw==", gifInDirectory.getContent());

Image svgInDirectory = documentation.getImages().stream().filter(i -> i.getName().equals("image.svg")).findFirst().get();
assertEquals("image/svg+xml", svgInDirectory.getType());
assertEquals("PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMDAiIGhlaWdodD0iMTAwIj4KICA8Y2lyY2xlIGN4PSI1MCIgY3k9IjUwIiByPSI0MCIgc3Ryb2tlPSJibGFjayIgc3Ryb2tlLXdpZHRoPSIzIiBmaWxsPSJyZWQiIC8+Cjwvc3ZnPiA=", svgInDirectory.getContent());

Image pngInSubDirectory = documentation.getImages().stream().filter(i -> i.getName().equals("images/image.png")).findFirst().get();
assertEquals("image/png", pngInSubDirectory.getType());
assertTrue(pngInSubDirectory.getContent().startsWith("iVBORw0KGgoAAAANSUhEUgAAACAAAAAaCAYAAADWm14/AAAD"));
Expand Down

0 comments on commit 461dbd4

Please sign in to comment.