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);
    }
}

package org.deeplearning4j.ui.play.staticroutes;

import com.google.common.net.HttpHeaders;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.datavec.api.util.ClassPathResource;
import play.api.libs.MimeTypes;
import play.mvc.Result;

import java.io.InputStream;
import java.util.function.Function;

import static play.mvc.Http.Context.Implicit.response;
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) {
        String fullPath = assetsRootDirectory + s;

        InputStream inputStream;
        try{
            inputStream = new ClassPathResource(fullPath).getInputStream();
        }catch (Exception e){
            log.info("Could not find asset: {}", s);    //TODO switch to debug later...
            return ok();
        } catch (Throwable t){
            return ok();
        }

        String fileName = FilenameUtils.getName(fullPath);

        response().setHeader( HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"");
        scala.Option<String> contentType = MimeTypes.forFileName(fileName);
        String ct;
        if(contentType.isDefined()){
            ct = contentType.get();
        } else {
            ct = "application/octet-stream";
        }

        return ok(inputStream).as(ct);
    }
}

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


Former-commit-id: 501af0ee4f927b3ebabd521ecc9a65d2e2144083