From 3101cf0e4279c3bf9c8e72d17bdb0921ec547397 Mon Sep 17 00:00:00 2001 From: George Kalampokis Date: Thu, 17 Mar 2022 17:08:45 +0200 Subject: [PATCH] Optimize image export --- .../utilities/documents/word/WordBuilder.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/word/WordBuilder.java b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/word/WordBuilder.java index ca365b8cb..3d467497c 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/word/WordBuilder.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/documents/word/WordBuilder.java @@ -40,9 +40,22 @@ import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.*; import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.apache.poi.xwpf.usermodel.Document.*; public class WordBuilder { private static final Logger logger = LoggerFactory.getLogger(WordBuilder.class); + private static final Map IMAGE_TYPE_MAP = Stream.of(new Object[][] { + {".jpeg", PICTURE_TYPE_JPEG}, + {".jpg", PICTURE_TYPE_JPEG}, + {".png", PICTURE_TYPE_PNG}, + {".gif", PICTURE_TYPE_GIF}, + {".tiff", PICTURE_TYPE_TIFF}, + {".bmp", PICTURE_TYPE_BMP}, + {".wmf", PICTURE_TYPE_WMF} + } + ).collect(Collectors.toMap(objects -> (String)objects[0], o -> (Integer)o[1])); private Map> options = new HashMap<>(); private CTAbstractNum cTAbstractNum; @@ -148,16 +161,9 @@ public class WordBuilder { XWPFRun run = paragraph.createRun(); String imageId = ((Map)item).get("id"); String fileName = ((Map)item).get("name"); - int format = 0; - if (fileName.endsWith(".jpeg") || fileName.endsWith(".jpg")) { - format = org.apache.poi.xwpf.usermodel.Document.PICTURE_TYPE_JPEG; - } else if (fileName.endsWith(".png")) { - format = org.apache.poi.xwpf.usermodel.Document.PICTURE_TYPE_PNG; - } else if (fileName.endsWith(".gif")) { - format = org.apache.poi.xwpf.usermodel.Document.PICTURE_TYPE_GIF; - } else if (fileName.endsWith(".tiff")) { - format = org.apache.poi.xwpf.usermodel.Document.PICTURE_TYPE_TIFF; - } + int format; + String fileExtension = fileName.substring(fileName.lastIndexOf(".")); + format = IMAGE_TYPE_MAP.getOrDefault(fileExtension, 0); try { FileInputStream image = new FileInputStream(environment.getProperty("file.storage") + imageId); ImageInputStream iis = ImageIO.createImageInputStream(new File(environment.getProperty("file.storage") + imageId));