Search code examples
javascalanginxplayframeworkakka-http

play! Uploaded images not visible in production mode


Hi guys I'm new to play framework,

Qun 1:

what I wanted in my server is Users can upload images, and see uploaded images in web page, when I run the server in Dev mode everything is works fine (I can upload and see the images in web page), but I try do this same thing in production mode, uploading works fine(images saved in public directory) but images are not visible in web page...!

my twirl html template:

<img src="@routes.Assets.versioned(public/file1.png)" width=25% height=auto alt="file1.png">

routes:

GET        /versionedAssets/*file        controllers.Assets.versioned(path="/public", file: Asset)

browser's Elements page (F12):

<img src="/versionedAssets/file1.png" width="25%" height="auto" alt="file1.png">

I can even show the image using NGINX (passing image location as an url) successfully, but why i can't see the image in play's production mode?, I don't even have clue about what part I did wrong.! please help me if you know anything about it.!

Qun 2:

Is that possible to serve our app without frontend server like NGINX, etc. because play already running in AkkaHttpServer.


Solution

  • After reading docs
    I've found Ok.SendFile() method used to serving files, then I've created a method in controller

        def getImageFromPath()= Action{ implicit request: Request[AnyContent] =>
    Ok.sendFile(new java.io.File("/path/to/file")) }   
    

    then added route in routes file

      GET     /getimage        controllers.ImageController.getImageFromPath()
    

    reverse routing in Twirl template

      <img src="@routes.ImageController.getImageFromPath()" width=25% height=auto alt="file1.png">
    

    helped me to solve this issue..!