package org.deeplearning4j.ui.play.staticroutes;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nd4j.linalg.io.ClassPathResource;
import play.mvc.Result;
import play.mvc.Results;

import java.io.File;
import java.io.IOException;
import java.util.function.Function;

import static play.mvc.Results.ok;

/**
 * Simple function for serving assets. Assets are assumed to be in the specified root directory
 *
 * @author Alex Black
 */
@AllArgsConstructor @Slf4j
public class Assets implements Function<String,Result> {
    private final String assetsRootDirectory;

    @Override
    public Result apply(String s) {
        File f;
        try {
            f = new ClassPathResource(assetsRootDirectory + s).getFile();
        } catch (IOException e){
            log.debug("Error for asset request: {}",s,e);
            return Results.notFound("Not found: /assets/" + s);
        }
        return ok(f);
    }
}

@@ -39,24 +39,66 @@
 ticroutes;%0A%0A
+import com.google.common.net.HttpHeaders;%0A
 import lombo
@@ -168,22 +168,68 @@
 org.
-nd4j.linalg.io
+apache.commons.io.FilenameUtils;%0Aimport org.datavec.api.util
 .Cla
@@ -256,26 +256,34 @@
 rt play.
-mvc.Result
+api.libs.MimeTypes
 ;%0Aimport
@@ -298,17 +298,16 @@
 c.Result
-s
 ;%0A%0Aimpor
@@ -316,20 +316,27 @@
 java.io.
-File
+InputStream
 ;%0Aimport
@@ -345,60 +345,87 @@
 ava.
-io.IOExcep
+util.function.Func
 tion;%0A
+%0A
 import 
-java.util.function.Function;%0A
+static play.mvc.Http.Context.Implicit.response;
 %0Aimp
@@ -779,14 +779,83 @@
     
-File f
+String fullPath = assetsRootDirectory + s;%0A%0A        InputStream inputStream
 ;%0A  
@@ -863,17 +863,16 @@
      try
- 
 %7B%0A      
@@ -877,17 +877,27 @@
         
-f
+inputStream
  = new C
@@ -917,40 +917,32 @@
 rce(
-assetsRootDirectory + s).getFile
+fullPath).getInputStream
 ();%0A
@@ -954,18 +954,15 @@
    %7D
- 
 catch (
-IO
 Exce
@@ -991,112 +991,569 @@
 log.
-debug(%22Error for asset request: %7B%7D%22,s,e);%0A            return Results.notFound(%22Not found: /assets/%22 + s)
+info(%22Could not find asset: %7B%7D%22, s);    //TODO switch to debug later...%0A            return ok();%0A        %7D catch (Throwable t)%7B%0A            return ok();%0A        %7D%0A%0A        String fileName = FilenameUtils.getName(fullPath);%0A%0A        response().setHeader( HttpHeaders.CONTENT_DISPOSITION, %22attachment; filename=%5C%22%22 + fileName + %22%5C%22%22);%0A        scala.Option%3CString%3E contentType = MimeTypes.forFileName(fileName);%0A        String ct;%0A        if(contentType.isDefined())%7B%0A            ct = contentType.get();%0A        %7D else %7B%0A            ct = %22application/octet-stream%22
 ;%0A  
@@ -1560,16 +1560,17 @@
       %7D%0A
+%0A
         
@@ -1579,17 +1579,34 @@
 turn ok(
-f
+inputStream).as(ct
 );%0A    %7D

Fix UI asset routing (streams not files, for resources in JAR)