AME
AME

Reputation: 2302

Play! upload images and make them available in production at /assets/images

From the docs:

When you package your application, all assets for the application, including all sub projects, are aggregated into a single jar, in target/my-first-app-1.0.0-assets.jar. This jar is included in the distribution so that your Play application can serve them. This jar can also be used to deploy the assets to a CDN or reverse proxy.

I got a few image uploads in my application, everything works fine in development but in production it gives me errors.

The thing is, I need those files in the public/images folder in order to display them on the page but it does not work in production.

How can I let the user upload images and also make them available at /public/images ?

Do I have to create another Assets path or something like that ?

note: Didn't post code because it's just a standard upload method from the docs.

Upvotes: 0

Views: 256

Answers (1)

AME
AME

Reputation: 2302

What I did:

I'm aware of the fact that this might be just a stupid workaround.

GET         /images/:name                               controllers.ImageController.show(name: String)

I created a simple action that serves the images like:

def show(name: String) = Action { implicit r =>
    val rootPath = Play.application.path
    val path = FileSystems.getDefault().getPath("/" + rootPath + "/public/images/");
    Logger.info("serving image: " + path.toString  + "/" + name )
    Ok.sendFile(new java.io.File(path.toString + "/" + name))
  }

Basically this whole operation will fail in development but it works in production.

After deployment, I created the folders manually e.g

 your_root/target/universal/stage/public/images

Now all the images are available at:

 /images/name_of_the_image

note: Play version = 2.4.x

Upvotes: 1

Related Questions