ideaboxer
ideaboxer

Reputation: 4091

Play! Framework: File not served after upload until play clean

If I do a file upload to the file system (to the public assets directory) in production mode, the file is not served afterwards. I need to run the clean command. I get a file not found error. How can I avoid this?

Upvotes: 3

Views: 1243

Answers (3)

biesior
biesior

Reputation: 55798

Although sending files as an action's Result is possible (what you proved) I'd suggest to store public uploads outside of the application and serving it with common HTTP server.

Reasons are simple:

  • if you will need to add/replace files fast, you can do it with common FTP client
  • you don't need to redeploy your app if you're adding files with 3rd part client
  • HTTP server serves static files just faster, it manages cache headers out of the box (cause it's its job) etc. Server doesn't need to rewrite file to the result.
  • using subdomain for this, you can create a'la CDN solution
  • app's code is just smaller

Upvotes: 3

ideaboxer
ideaboxer

Reputation: 4091

I added a custom Assets controller, now it seems to work properly:

package customcontrollers

import controllers.Assets
import play.api.mvc.Action
import play.api.mvc.AnyContent
import play.api.mvc.Results.Ok
import play.api.Play.current
import play.api._

object UploadedImageAssets {
  def at(file: String): Action[AnyContent] = Action {
    Ok.sendFile(Play.getFile("public/uploaded-images/"+file))
  } 
}

Upvotes: 0

Julien Lafont
Julien Lafont

Reputation: 7877

I don't like this answer, but there is no way to achieve this easily.

Because, "it's not a very good way to do things, it is unreliable, it doesn't scale, and there are security concerns". (I don't agree with this, but it's a fact).

The best solution is to store your file in a database, or to upload it to another service like S3.

Upvotes: 3

Related Questions