Reading files from a shared folder in a gaming environment

My play app is in 2.4.2. In developer mode, I used to read files from a shared folder on external controllers using Source.fromFile ("./public/files/abc.json")

When I converted the application to production mode, I get file exceptions not found. I found that the shared folder is packed in an asset bank in production mode. What can I do to make it work both in development mode and in production?

+8
source share
6 answers

Have you tried this Play Documentation https://www.playframework.com/documentation/2.5.x/AssetsOverview ? This is normal with Play 2.4 and even with 2.3 as well, I tried.

There you can find it, you can just do it in your conf / routes file.

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

file β†’ refers to the file name. Example: myFile.json

To make this work in production mode, you need to work a little. As explained in this answer, add these lines to the /build.sbt file.

 import com.typesafe.sbt.packager.MappingsHelper._ mappings in Universal ++= directory(baseDirectory.value / "public") 

This will include your β€œpublic” directory inside the dist file (you can include any directory this way). Then your application will work in a working environment.

+4
source

You can use this method:

 Play.application().getFile("/public/foobar.baz"); 

Doc Method :

Get the file relative to the root path of the application.

+2
source

Play comes with an integrated controller for serving public assets.

The controller is available in the Play JAR by default as controllers. Disputes and determines a single in action with two parameters:

Assets.at (path: String, file: String) The path parameter must be fixed and defines the directory controlled by the action. The file parameter is usually dynamically extracted from the request path.

Here is a typical display of the Assets controller in the conf / routes file:

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

See here for more details .

0
source

There are two ways to solve this problem: 1) Direct - use what Supun Wijerathne suggested in the comment above. 2) Secondly, you should use the following approach if you do not want to add an additional shared folder to the distribution version of the application: a) Copy all the files from the shared folder to the conf folder. b) Get the resource as a stream using the Play classloader. c) Convert this input stream to a buffer source using the Source.fromInputStream method. Now this file can be used by the controller for processing.

0
source

Solution found: it is necessary to call these resources using assets /

0
source

Use Source.fromResource instead of Source.fromFile if you are using scala 2.12.

Example:

If you put the file in a shared folder, do not use the initial slash.

 Source.fromResource("public/files/abc.txt") 

If you put the file in the conf folder, do not include conf in the path.

 Source.fromResource("files/abc.txt") // conf/files/abc.txt 

If you use scala <2.12, you can return to Java. There are 2 ways.

Relative path (no slash)

 scala.io.Source.fromInputStream(getClass.getClassLoader.getResourceAsStream("public/test.txt")) 

Absolute path (requires an initial slash)

 scala.io.Source.fromInputStream(getClass.getResourceAsStream("/public/test.txt")) 

I think you should also exclude "conf" in the path if you put the file in the conf folder.

Finally, you can find out how loading Java resources on this blog works.

0
source

All Articles