diff --git a/build.gradle b/build.gradle index 953199d..e31239d 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ plugins { } group 'com.docutools' -version = '3.1.0' +version = '3.1.1' java { toolchain { diff --git a/src/main/java/com/docutools/jocument/annotations/Image.java b/src/main/java/com/docutools/jocument/annotations/Image.java index 2412064..b6628d6 100644 --- a/src/main/java/com/docutools/jocument/annotations/Image.java +++ b/src/main/java/com/docutools/jocument/annotations/Image.java @@ -21,4 +21,11 @@ */ int maxWidth() default -1; + /** + * Whether the resolved image should be deleted from the file system after insertion. + * + * @return whether the image should be deleted after insertion + */ + boolean deleteAfterInsertion() default true; + } diff --git a/src/main/java/com/docutools/jocument/impl/ReflectionResolver.java b/src/main/java/com/docutools/jocument/impl/ReflectionResolver.java index c4c8e97..d2d1c08 100644 --- a/src/main/java/com/docutools/jocument/impl/ReflectionResolver.java +++ b/src/main/java/com/docutools/jocument/impl/ReflectionResolver.java @@ -470,7 +470,7 @@ private Optional resolveSimplePlaceholder(Object property, Stri return formatTemporal(placeholderName, temporal, locale); } else if (property instanceof Path path && isFieldAnnotatedWith(bean.getClass(), placeholderName, Image.class)) { return ReflectionUtils.findFieldAnnotation(bean.getClass(), placeholderName, Image.class) - .map(image -> new ImagePlaceholderData(path).withMaxWidth(image.maxWidth())); + .map(image -> new ImagePlaceholderData(path).withMaxWidth(image.maxWidth()).withFileDeletionAfterInsertion(image.deleteAfterInsertion())); } else if (property instanceof UUID uuid) { return Optional.of(new ScalarPlaceholderData<>(uuid.toString())); } else { diff --git a/src/main/java/com/docutools/jocument/impl/word/WordImageUtils.java b/src/main/java/com/docutools/jocument/impl/word/WordImageUtils.java index 5d0d641..9926b26 100644 --- a/src/main/java/com/docutools/jocument/impl/word/WordImageUtils.java +++ b/src/main/java/com/docutools/jocument/impl/word/WordImageUtils.java @@ -59,21 +59,14 @@ public static XWPFPicture insertImage(XWPFParagraph paragraph, Path path, ImageS .orElse(DEFAULT_DIM); var contentType = probeImageType(path, imageStrategy); - XWPFPicture xwpfPicture; try (var in = Files.newInputStream(path, StandardOpenOption.READ)) { logger.debug("Adding picture from path {} with content type {} and dimensions {} {}", path, contentType, dim.width, dim.height); - xwpfPicture = paragraph.createRun() + return paragraph.createRun() .addPicture(in, contentType, path.getFileName().toString(), dim.width, dim.height); } catch (InvalidFormatException | IOException e) { logger.error("Could not insert image from given Path %s.".formatted(path), e); throw new IllegalArgumentException("Could not insert image from given %s".formatted(path), e); } - try { - Files.delete(path); - } catch (IOException e) { - logger.warn("Could not delete image from %s".formatted(path), e); - } - return xwpfPicture; } diff --git a/src/main/java/com/docutools/jocument/impl/word/placeholders/ImagePlaceholderData.java b/src/main/java/com/docutools/jocument/impl/word/placeholders/ImagePlaceholderData.java index c7fb62a..6d81ad9 100644 --- a/src/main/java/com/docutools/jocument/impl/word/placeholders/ImagePlaceholderData.java +++ b/src/main/java/com/docutools/jocument/impl/word/placeholders/ImagePlaceholderData.java @@ -21,6 +21,7 @@ public class ImagePlaceholderData extends CustomWordPlaceholderData { // Options private int maxWidth; + private boolean deleteAfterInsertion; public ImagePlaceholderData(Path imagePath) { this.imagePath = imagePath; @@ -31,6 +32,11 @@ public ImagePlaceholderData withMaxWidth(int maxWidth) { return this; } + public ImagePlaceholderData withFileDeletionAfterInsertion(boolean deleteAfterInsertion) { + this.deleteAfterInsertion = deleteAfterInsertion; + return this; + } + @Override protected void transform(IBodyElement placeholder, IBody part, Locale locale, GenerationOptions options) { Path path = applyOptions(options); @@ -47,7 +53,14 @@ protected void transform(IBodyElement placeholder, IBody part, Locale locale, Ge try { Files.deleteIfExists(path); } catch (IOException e) { - logger.error(e); + logger.warn(e); + } + } + if (deleteAfterInsertion) { + try { + Files.deleteIfExists(imagePath); + } catch (IOException e) { + logger.warn(e); } } }