Skip to content

Commit

Permalink
Added a system property to configure the directory of the webp binari…
Browse files Browse the repository at this point in the history
…es (#274)

* Added a system property to configure the directory of the webp binaries

* Update webp.md

* Added getPathFromProperty

* Update CWebpHandler.java

* Update DWebpHandler.java

* Update Gif2WebpHandler.java
  • Loading branch information
jdev-2020 authored Aug 11, 2023
1 parent 3bd7f51 commit 0f5abee
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

#### 4.0.38

* Added the system property "com.sksamuel.scrimage.webp.binary.dir" to configure the directory of the webp binaries.

#### 4.0.37

* Better error message when image loading fails, to indicate the file type.
Expand Down
10 changes: 8 additions & 2 deletions docs/webp.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ Scrimage provides support for webp through the `scrimage-webp` module. To use we
This module uses the `dwebp`, `cwebp` and `gif2webp` binaries, created by Google. The `scrimage-webp` module comes with the
linux_x64, window_x64, mac-10.15 binaries already included (see required [copyright notice](https://github.com/sksamuel/scrimage/blob/master/scrimage-webp/src/main/resources/dist_webp_binaries/LICENSE)).

If you don't wish to use the embedded binaries, then you can [download other versions](https://developers.google.com/speed/webp) and place them
on your classpath at `/webp_binaries/{osName}/dwebp` or `/webp_binaries/{osName}/cwebp` or `/webp_binaries/{osName}/gif2webp`.
If you don't wish to use the embedded binaries, then you can [download other versions](https://developers.google.com/speed/webp)
and use the system property "com.sksamuel.scrimage.webp.binary.dir" to configure the directory you placed the binaries:
```shell
java -Dcom.sksamuel.scrimage.webp.binary.dir="/tmp/webp" -jar myprogram.jar
```
Or you can place them on your classpath at `/webp_binaries/{osName}/dwebp` or `/webp_binaries/{osName}/cwebp` or
`/webp_binaries/{osName}/gif2webp`.
The directory set by the system property is always searched first.

`{osName}` must be one of `window`, `linux`, `mac`. ie `/webp_binaries/window/cwebp`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ public class CWebpHandler extends WebpHandler {

static {
try {
// write out binary to a location we can execute it from
binary = createPlaceholder("cwebp");
installCWebp();
// try to get the binary path from the system property
Path pathFromProperty = getPathFromProperty("cwebp");
if (pathFromProperty != null) {
binary = pathFromProperty;
} else {
// write out binary to a location we can execute it from
binary = createPlaceholder("cwebp");
installCWebp();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ public class DWebpHandler extends WebpHandler {

static {
try {
// write out binary to a location we can execute it from
binary = createPlaceholder("dwebp");
installDWebp();
// try to get the binary path from the system property
Path pathFromProperty = getPathFromProperty("dwebp");
if (pathFromProperty != null) {
binary = pathFromProperty;
} else {
// write out binary to a location we can execute it from
binary = createPlaceholder("dwebp");
installDWebp();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ public class Gif2WebpHandler extends WebpHandler {

static {
try {
// write out binary to a location we can execute it from
binary = createPlaceholder("gif2webp");
installGif2Webp();
// try to get the binary path from the system property
Path pathFromProperty = getPathFromProperty("gif2webp");
if (pathFromProperty != null) {
binary = pathFromProperty;
} else {
// write out binary to a location we can execute it from
binary = createPlaceholder("gif2webp");
installGif2Webp();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
Expand All @@ -16,6 +17,21 @@ abstract class WebpHandler {

private static final Logger logger = LoggerFactory.getLogger(WebpHandler.class);

protected static Path getPathFromProperty(String name) {
try {
String binaryDir = System.getProperty("com.sksamuel.scrimage.webp.binary.dir");
if (binaryDir != null && !binaryDir.isEmpty()) {
Path path = Paths.get(binaryDir, name);
if (Files.isExecutable(path)) {
return path;
}
}
return null;
} catch (Exception ignored) {
return null;
}
}

protected static Path createPlaceholder(String name) throws IOException {
return Files.createTempFile(name, "binary");
}
Expand Down

0 comments on commit 0f5abee

Please sign in to comment.