Displaying assets outside of a public folder on the Play platform

We have a huge list of images that we need to store in an external path .. that is, outside the application folder for playback.

How can we make it playable as an asset so that it streams as a web server?

+3


source to share


4 answers


You may have seen the asset documentation . In addition to the standard Play Assets, you can define your own.

In conf/routes

you need to add a line for your external asset:

# Static resources (not managed by the Play framework)
GET     /my_external_assets/*file         controllers.ExtAssets.at(file)
# Play standard assets
GET     /assets/*file                     controllers.Assets.at(path = "/public", file)

      



And then you have to define your asset controller. A simple example of how this can be done:

public class ExtAssets extends Controller {

    public Result at(String filePath) {
        File file = new File(filePath);
        return ok(file, true);
    }
}

      

+4


source


In the interest of completeness, I have encountered this problem many times and there was not a sufficient answer for it.

Usually nginx faces the outside world and reverse proxy back to the application server . You don't want to serve files directly from the game, especially if you create paths yourself that could pose a security risk. Let the experts process static files, that is, nginx.

The Play Framework maintains a sbt-native-packager "dist" directory that will attach any files right inside the ZIP package distribution [1]. Here is the documentation for that:

dist    β†’ Arbitrary files to be included in your projects distribution

      



For cases like controlled uploads use nginx X-Accel , where through the response header you tell nginx which file to send back to the client.


TL; DR: use nginx for your own purposes, use Play for your purposes.

+2


source


A hacky solution would include in your sbt play shared folder a symbolic link to the location where you want to download the files. The symlink will be packaged into an assembly and will be functional at runtime.

cd ~/your/playframework/awesome/project/public
ln -s '/var/lib/funky-data-set-for-my-awesome-project/' funky-data

      

You can now use something like this in your routes file:

GET         /funky/assets/*file                controllers.Assets.at(path="/public/funky-data", file)

      

The downside to this is that your stage / dev / prod needs to have the content you want in the same place.

0


source


Play 2.5

file: routes

GET  /external_resources/*file controllers.ImagesController.getImage(path="/home/project_name/external/images", file: String)

      

file: ImagesController

package controllers;

import play.mvc.Controller;
import play.mvc.Result;

import java.io.File;

/**
 * Created by js on 6/1/17.
 */
public class ImagesController extends Controller {

public Result getImage(String path, String image) {
    System.out.println(path);
    System.out.println(image);
    image = image.replace("%20", " ");
    File file = new File(path + image);

    return ok(file);
  }
}

      

0


source







All Articles