Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play: additional public / assets directory

In order to have a clean directory structure, I would like to make an additional assets folder public. I've created a directory 'assets' in my project folder, wrote an 'PictureAssets' controller which is nearly identical to 'controller.Assets', added 'assets' to the playAssetsDirectories in the build.sbt and tried following some route entries without success.

PictureAssets:

package controllers

import play.api.mvc.Action
import play.api.mvc.AnyContent

object PictureAssets extends AssetsBuilder {
   def create : Action[AnyContent]  =  Action {
      Ok(views.html.fileUploadForm())
   }
}

build.sbt

playAssetsDirectories <+= baseDirectory / "assets"

routes

# GET     /file                 controllers.PictureAssets.at(path="/assets", file="MM1.png")
GET     /datei/*file            controllers.PictureAssets.at(path="/assets", file)
# GET       /datei/*file            controllers.Assets.at(path="/assets", file)

If I try to access the URL, either nothing is displayed, or the error 'The image http:9000//localhost/datei/MM1.png cannot be displayed because it contains errors' is displayed or the css references to handled by the 'controller.Assets' don't work any more.

What am I missing?

like image 298
Stefan K. Avatar asked Mar 03 '14 12:03

Stefan K.


People also ask

What is the difference between the public/assets and optional app/assets directories?

According to this document, the public/ directory should contain static assets that can be served as they are, while the optional app/assets directory should contain any asset that needs to be processed, for example, by the minify compiler during the build phase.

How do I use play asset delivery in Unity?

To use Play Asset Delivery, you need to set up your project to build Android App Bundles and split the application binary. To configure Unity to build Android App Bundles: Open Build Settings (menu: File > Build Settings ). In Platform, select Android. If Export Project is enabled, enable Export for App Bundle.

How to list all the files in the assets folder?

assets: If the developer wants to list all the files in the assets folder, he/she has used the list () function and provide the folder name or ” “ on the root folder as given below.

How to create an asset folder in Android Studio?

Step 1: To create an asset folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder. Step 3: Android Studio will open a dialog box.


1 Answers

I think the issue comes from the fact that the at method used is the default one used previously by Assets.

I ran into the same issue at some point last year, where I wanted to serve images that would be stored in a external folder, a folder that is somewhere on disk, and here is how I coded this:

I created a simple controller called Photos, that contained one Action:

object Photos extends Controller {

  val AbsolutePath = """^(/|[a-zA-Z]:\\).*""".r

  /**
   * Generates an `Action` that serves a static resource from an external folder
   *
   * @param absoluteRootPath the root folder for searching the static resource files.
   * @param file the file part extracted from the URL
   */
  def at(rootPath: String, file: String): Action[AnyContent] = Action { request =>
    val fileToServe = rootPath match {
      case AbsolutePath(_) => new File(rootPath, file)
      case _ => new File(Play.application.getFile(rootPath), file)
    }

    if (fileToServe.exists) {
      Ok.sendFile(fileToServe, inline = true)
    } else {
      Logger.error("Photos controller failed to serve photo: " + file)
      NotFound
    }
  }

}

Then, in my routes, I defined the following:

GET /photos/*file   controllers.Photos.at(path="/absolute/path/to/photos",file)

This worked just fine for me. Hope this helps.

PS: This was in addition to the normal Assets controller that helped serving js and css files.

like image 171
Peter Avatar answered Oct 21 '22 00:10

Peter