package com.sksamuel.scrimage.webp;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class DWebpHandler extends WebpHandler {

   private static final Path binary;

   static {
      try {
         // write out binary to a location we can execute it from
         binary = createPlaceholder("dwebp");
         installDWebp();
      } catch (IOException e) {
         throw new RuntimeException(e);
      }
   }

   /**
    * Scrimage includes the latest dwebp binary. Alternatively, you can provide another version by
    * placing it on the classpath at `webp_binaries`, eg, /webp_binaries/dwebp.
    * <p>
    * Binaries can be downloaded here:
    * https://storage.googleapis.com/downloads.webmproject.org/releases/webp/index.html
    */
   private static void installDWebp() throws IOException {
      installBinary(binary, getBinaryPath("dwebp"));
   }

   public byte[] convert(byte[] bytes) throws IOException {
      Path path = Files.createTempFile("input", "webp").toAbsolutePath();
      Files.write(path, bytes, StandardOpenOption.CREATE);
      try {
         return convert(path);
      } finally {
         path.toFile().delete();
      }
   }

   public byte[] convert(Path source) throws IOException {
      Path target = Files.createTempFile("from_webp", "png").toAbsolutePath();
      try {
         convert(source, target);
         return Files.readAllBytes(target);
      } finally {
         target.toFile().delete();
      }
   }

   private void convert(Path input, Path target) throws IOException {

      Path stdout = Files.createTempFile("stdout", "webp");
      ProcessBuilder builder = new ProcessBuilder(
         binary.toAbsolutePath().toString(),
         input.toAbsolutePath().toString(),
         "-o",
         target.toAbsolutePath().toString()
      );
      builder.redirectErrorStream(true);
      builder.redirectOutput(stdout.toFile());

      Process process = builder.start();
      try {
         process.waitFor(5, TimeUnit.MINUTES);
      } catch (InterruptedException e) {
         throw new IOException(e);
      }
      int exitStatus = process.exitValue();
      if (exitStatus != 0) {
         List<String> error = Files.readAllLines(stdout);
         throw new IOException(error.toString());
      }
   }
}

@@ -2129,89 +2129,8 @@
 %0A   
-   %7D catch (InterruptedException e) %7B%0A         throw new IOException(e);%0A      %7D%0A
     
@@ -2175,16 +2175,19 @@
 ;%0A      
+   
 if (exit
@@ -2210,16 +2210,19 @@
         
+  
+ 
 List%3CStr
@@ -2263,32 +2263,35 @@
 dout);%0A         
+   
 throw new IOExce
@@ -2315,23 +2315,26 @@
 ing());%0A
+   
       %7D%0A
    %7D%0A%7D%0A
@@ -2322,19 +2322,171 @@
 %0A         %7D%0A
+      %7D catch (InterruptedException %7C IOException e) %7B%0A         throw new IOException(e);%0A      %7D finally %7B%0A         stdout.toFile().delete();%0A      %7D%0A%0A
    %7D%0A%7D%0A

put everything in try so finally can cleanup tmp file (#240)