Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SVG support and refactor code using nio and try-with-resource #9

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The output is in the ```target/site/``` directory by default. You can open ```ta

# Documentation (GitHub Pages)

Documentation is available at [https://bloomreach-forge.github.io/content-export-import/](https://bloomreach-forge.github.io/content-export-import/).
Documentation is available at [https://bloomreach-forge.github.io/gallery-magick/index.html](https://bloomreach-forge.github.io/gallery-magick/index.html).

You can generate the GitHub pages only from ```master``` branch by this command:

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.onehippo.forge.gallerymagick.cms.plugins.gallery.processor;

public class MagickRuntimeException extends RuntimeException {

public MagickRuntimeException(final String message, final Throwable cause) {
super(message, cause);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -33,14 +36,14 @@
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Abstract *Magick Command.
*/
abstract public class AbstractMagickCommand {
public abstract class AbstractMagickCommand {

private static final Logger log = LoggerFactory.getLogger(AbstractMagickCommand.class);

Expand All @@ -62,7 +65,7 @@ abstract public class AbstractMagickCommand {
/**
* Working directory of a command execution.
*/
private File workingDirectory;
private Path workingDirectory;

/**
* Command executable. e.g, <code>gm</code>, <code>/usr/bin/gm</code> or <code>/usr/local/bin/gm</code>.
Expand Down Expand Up @@ -93,15 +96,15 @@ public AbstractMagickCommand(final String executable, final String subCommand) {
* Returns working directory.
* @return working directory
*/
public File getWorkingDirectory() {
public Path getWorkingDirectory() {
return workingDirectory;
}

/**
* Sets working directory
* @param workingDirectory working directory
*/
public void setWorkingDirectory(File workingDirectory) {
public void setWorkingDirectory(Path workingDirectory) {
this.workingDirectory = workingDirectory;
}

Expand Down Expand Up @@ -175,14 +178,18 @@ public void execute() throws IOException {
*/
public void execute(final OutputStream stdOut) throws IOException {
CommandLine cmdLine = createCommandLine();
ByteArrayOutputStream errStream = null;
int exitValue = 0;
DefaultExecuteResultHandler resultHandler = null;

try {
errStream = new ByteArrayOutputStream(512);
try (ByteArrayOutputStream errStream = new ByteArrayOutputStream(512)) {

DefaultExecutor executor;
if (getWorkingDirectory() != null) {
executor = DefaultExecutor.builder().setWorkingDirectory(getWorkingDirectory().toFile()).get();
} else {
executor = DefaultExecutor.builder().get();
}

final DefaultExecutor executor = new DefaultExecutor();
ExecuteStreamHandler streamHandler;

if (stdOut != null) {
Expand All @@ -193,45 +200,39 @@ public void execute(final OutputStream stdOut) throws IOException {

executor.setStreamHandler(streamHandler);

if (getWorkingDirectory() != null) {
executor.setWorkingDirectory(getWorkingDirectory());
}

long timeout = NumberUtils.toLong(System.getProperty(PROP_TIMEOUT), DEFAULT_COMMAND_TIMEOUT);

if (timeout > 0) {
ExecuteWatchdog watchdog = new ExecuteWatchdog(DEFAULT_COMMAND_TIMEOUT);
executor.setWatchdog(watchdog);
resultHandler = new DefaultExecuteResultHandler();
executor.execute(cmdLine, resultHandler);
log.debug("Executed with watchdog: {}", cmdLine);
resultHandler.waitFor();
} else {
exitValue = executor.execute(cmdLine);
log.debug("Executed without watchdog: {}", cmdLine);
}
} catch (ExecuteException | InterruptedException e) {
if (resultHandler != null) {
exitValue = resultHandler.getExitValue();
}
if (e.getCause() == null) {
throw new MagickExecuteException(getExecutionErrorMessage(cmdLine, errStream, e), exitValue);
} else {
throw new MagickExecuteException(getExecutionErrorMessage(cmdLine, errStream, e), exitValue, e.getCause());
try {
if (timeout > 0) {
ExecuteWatchdog watchdog = ExecuteWatchdog.builder().setTimeout(Duration.ofMillis(DEFAULT_COMMAND_TIMEOUT)).get();
executor.setWatchdog(watchdog);
resultHandler = new DefaultExecuteResultHandler();
executor.execute(cmdLine, resultHandler);
log.debug("Executed with watchdog: {}", cmdLine);
resultHandler.waitFor();
} else {
exitValue = executor.execute(cmdLine);
log.debug("Executed without watchdog: {}", cmdLine);
}
} catch (ExecuteException e) {
if (resultHandler != null) {
exitValue = resultHandler.getExitValue();
}
if (e.getCause() == null) {
throw new MagickExecuteException(getExecutionErrorMessage(cmdLine, errStream, e), exitValue);
} else {
throw new MagickExecuteException(getExecutionErrorMessage(cmdLine, errStream, e), exitValue, e.getCause());
}
} catch (InterruptedException e) {
log.error("InterruptedException: ", e);
Thread.currentThread().interrupt();
}
} finally {
IOUtils.closeQuietly(errStream);
}
}

private String getExecutionErrorMessage(final CommandLine cmdLine, final ByteArrayOutputStream errStream,
final Exception e) throws UnsupportedEncodingException {
StringBuilder sbMsg = new StringBuilder(256);
sbMsg.append(StringUtils.trim(errStream.toString("UTF-8")));
sbMsg.append(' ').append(cmdLine.toString());
sbMsg.append(". ").append(e.getMessage());
return sbMsg.toString();
}
private String getExecutionErrorMessage(final CommandLine cmdLine, final ByteArrayOutputStream errStream, final Exception e) {
return org.apache.commons.lang3.StringUtils.trim(errStream.toString(StandardCharsets.UTF_8)) + ' ' + cmdLine.toString() + ". " + e.getMessage();
}

/**
* Create a {@link CommandLine} from executable and arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
package org.onehippo.forge.gallerymagick.core.command;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand All @@ -40,21 +42,21 @@ private GraphicsMagickCommandUtils() {
* @throws MagickExecuteException if execution exception occurs
* @throws IOException if IO exception occurs
*/
public static String identifyAllMetadata(File sourceFile) throws MagickExecuteException, IOException {
public static String identifyAllMetadata(Path sourceFile) throws MagickExecuteException, IOException {
GraphicsMagickCommand cmd = new GraphicsMagickCommand(null, "identify");

final File tempFolder = getTempFolder();
final Path tempFolder = getTempFolder();

if (tempFolder != null) {
cmd.setWorkingDirectory(tempFolder);
}

cmd.addArgument("-verbose");
cmd.addArgument(sourceFile.getCanonicalPath());
cmd.addArgument(sourceFile.toRealPath().toString());

ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
cmd.execute(baos);
return StringUtils.trim(baos.toString("UTF-8"));
return StringUtils.trim(baos.toString(StandardCharsets.UTF_8));
}

/**
Expand All @@ -64,22 +66,22 @@ public static String identifyAllMetadata(File sourceFile) throws MagickExecuteEx
* @throws MagickExecuteException if execution exception occurs
* @throws IOException if IO exception occurs
*/
public static ImageDimension identifyDimension(File sourceFile) throws MagickExecuteException, IOException {
public static ImageDimension identifyDimension(Path sourceFile) throws MagickExecuteException, IOException {
GraphicsMagickCommand cmd = new GraphicsMagickCommand(null, "identify");

final File tempFolder = getTempFolder();
final Path tempFolder = getTempFolder();

if (tempFolder != null) {
cmd.setWorkingDirectory(tempFolder);
}

cmd.addArgument("-format");
cmd.addArgument("%wx%h");
cmd.addArgument(sourceFile.getCanonicalPath());
cmd.addArgument(sourceFile.toRealPath().toString());

ByteArrayOutputStream baos = new ByteArrayOutputStream(40);
cmd.execute(baos);
String output = StringUtils.trim(baos.toString("UTF-8"));
String output = StringUtils.trim(baos.toString(StandardCharsets.UTF_8));

return ImageDimension.from(output);
}
Expand All @@ -93,7 +95,7 @@ public static ImageDimension identifyDimension(File sourceFile) throws MagickExe
* @throws MagickExecuteException if execution exception occurs
* @throws IOException if IO exception occurs
*/
public static void resizeImage(File sourceFile, File targetFile, ImageDimension dimension)
public static void resizeImage(Path sourceFile, Path targetFile, ImageDimension dimension)
throws MagickExecuteException, IOException {
resizeImage(sourceFile, targetFile, dimension, (String []) null);
}
Expand All @@ -108,21 +110,21 @@ public static void resizeImage(File sourceFile, File targetFile, ImageDimension
* @throws MagickExecuteException if execution exception occurs
* @throws IOException if IO exception occurs
*/
public static void resizeImage(File sourceFile, File targetFile, ImageDimension dimension, String ... extraOptions)
public static void resizeImage(Path sourceFile, Path targetFile, ImageDimension dimension, String ... extraOptions)
throws MagickExecuteException, IOException {
if (dimension == null) {
throw new IllegalArgumentException("Invalid dimension: " + dimension);
}

GraphicsMagickCommand cmd = new GraphicsMagickCommand(null, "convert");

final File tempFolder = getTempFolder();
final Path tempFolder = getTempFolder();

if (tempFolder != null) {
cmd.setWorkingDirectory(tempFolder);
}

cmd.addArgument(sourceFile.getCanonicalPath());
cmd.addArgument(sourceFile.toRealPath().toString());
final String dimensionArg = dimension.toCommandArgument();
cmd.addArgument("-resize");
cmd.addArgument(dimensionArg);
Expand All @@ -145,7 +147,7 @@ public static void resizeImage(File sourceFile, File targetFile, ImageDimension
cmd.addArgument("*");
}

cmd.addArgument(targetFile.getCanonicalPath());
cmd.addArgument(targetFile.toRealPath().toString());

cmd.execute();
}
Expand All @@ -154,12 +156,12 @@ public static void resizeImage(File sourceFile, File targetFile, ImageDimension
* Returns the temporary folder file.
* @return the temporary foler file
*/
private static File getTempFolder() {
File tempFolder = null;
private static Path getTempFolder() {
Path tempFolder = null;
final String tmpDirPath = System.getProperty("java.io.tmpdir");

if (StringUtils.isNotBlank(tmpDirPath)) {
tempFolder = new File(tmpDirPath);
tempFolder = Paths.get(tmpDirPath);
}

return tempFolder;
Expand Down
Loading