Skip to content

Commit

Permalink
Delete images after insertion
Browse files Browse the repository at this point in the history
To save storage space, pictures are deleted after insertion
  • Loading branch information
AntonOellerer committed Jun 20, 2024
1 parent 29dfb56 commit a2d29ed
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 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.0.2'
version = '3.1.0'

java {
toolchain {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,21 @@ 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);
return paragraph.createRun()
xwpfPicture = 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 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

0 comments on commit a2d29ed

Please sign in to comment.