Skip to content

Commit

Permalink
Allow controlling image deletion via @Image annotation
Browse files Browse the repository at this point in the history
As simply removing all images after insertion does not allow for sufficient
control, deciding removal is now done via a parameter `deleteAfterInsertion` in the
`@Image` annotation, which is true by default
  • Loading branch information
AntonOellerer committed Jun 24, 2024
1 parent a2d29ed commit 2c81ac6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.docutools'
version = '3.1.0'
version = '3.1.1'

java {
toolchain {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/docutools/jocument/annotations/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ private Optional<PlaceholderData> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class ImagePlaceholderData extends CustomWordPlaceholderData {

// Options
private int maxWidth;
private boolean deleteAfterInsertion;

public ImagePlaceholderData(Path imagePath) {
this.imagePath = imagePath;
Expand All @@ -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);
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit 2c81ac6

Please sign in to comment.