Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asset mapping outside public folder on Play framework

We have huge list of images that we need to store in an external path.. i.e outside of play application folder.

How can we make it available to play as an asset so it streams it as a web server?

like image 677
Nishanth Chakrapani Avatar asked Jul 13 '15 23:07

Nishanth Chakrapani


2 Answers

You've probably seen Play's documentation about Assets. Additionally to Play's standard 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's standard assets
GET     /assets/*file                     controllers.Assets.at(path = "/public", file)

And then you have to define your assets controller. A simple example how it could be done:

public class ExtAssets extends Controller {

    public Result at(String filePath) {
        File file = new File(filePath);
        return ok(file, true);
    }
}
like image 67
Kris Avatar answered Jan 03 '23 14:01

Kris


In the interests of completeness, I've experienced this problem many times and there has been no sufficient answer for it.

Typically nginx would face the outside world and reverse-proxy back into the application server. You do not want to serve files straight from Play especially if you're building paths yourself which may present security risks. Let the experts handle static files, ie nginx.

Play Framework supports sbt-native-packager's "dist" directory which will attach any files in there right inside the distribution ZIP package [1]. Here is the documentation for that:

dist    → Arbitrary files to be included in your projects distribution

For use cases such as controlled downloads use nginx's X-Accel where via a response header you tell nginx what file to send back to the client.


TLDR: use nginx for what it's made for, use Play for what it's made for.

like image 38
ScalaWilliam Avatar answered Jan 03 '23 14:01

ScalaWilliam