play! scala file resource handling and image resizing

  • 2,292 views
Uploaded on

file resource and image resize plugin for play! 2 framework in scala. storing files like amazon s3 bucket and resize images on the fly.

file resource and image resize plugin for play! 2 framework in scala. storing files like amazon s3 bucket and resize images on the fly.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,292
On Slideshare
0
From Embeds
0
Number of Embeds
13

Actions

Shares
Downloads
5
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. file resource handling & image resizing in play! (scala) by david pichsenmeister
  • 2. play-res & play-scalr for play! framework 2 developed by @LeonRadley
  • 3. play-res like amazon s3 bucket computes sha1 hash of file and uses it as pathname
  • 4. dependencies val appDependencies = Seq( "se.digiplant" %% "play-res" % "1.0" ) val main = play.Project(appName, appVersion, appDependencies).settings( // to simplify the reverse routing templatesImport ++= Seq("se.digiplant._") )
  • 5. conf/application.conf # Resource plugin save directory # is relative to app, but can be absolute to filesystem also res.default=res res.images=res/images conf/routes GET /res/:file se.digiplant.res.ResAssets.at(file) GET /images/:file se.digiplant.res.ResAssets.at(file,“images”)
  • 6. upload def upload = SecuredAction(true) { implicit request => val file = for { body <- request.body.asMultipartFormData uploaded <- body.file("file") } yield uploaded file match { case Some(f) => { val id: String = Res.put(f.ref.file) Ok(Json.obj("url" -> id)) } case _ => BadRequest } }
  • 7. reverse routing <img src="@res.routes.ResAssets.at ("5564ac5e3968e77b4022f55a23d36630bdeb0274.jpg")" alt="" /> <img src="@res.routes.ResAssets.at ("5564ac5e3968e77b4022f55a23d36630bdeb0274.jpg", "images")" alt="" /> direct http://www.sharedspaces. at/res/cf342662c630a41f0dcf19314a35f82e5abbc9c4.jpg
  • 8. play-scalr image resizing “on-the-fly” http://sharedspaces.at/{{width}}/{{height}}/ {{mode}}/file.jpg different modes: ● ● ● ● ● automatic fit_exact fit_to_width fit_to_height crop
  • 9. dependencies val appDependencies = Seq( "se.digiplant" %% "play-scalr" % "1.0.1" ) conf/application.conf # define cache, so they only need to be resized once res.scalrcache=res/scalr # Tell scalr to use scalrcache source we just created scalr.cache=scalrcache conf/routes GET /res/:width/:height/:mode/:file se.digiplant. scalr.ScalrResAssets.at(width: Int, height: Int, mode: String, file)
  • 10. reverse routing <img src="@scalr.routes.ScalrResAssets.at ("5564ac5e3968e77b4022f55a23d36630bdeb0274.jpg", 150, 100, "crop")" alt="" /> direct http://www.sharedspaces. at/res/150/100/crop/cf342662c630a41f0dcf19314a35f82e5abbc9c 4.jpg
  • 11. resources http://leon.radley.se/project/ https://github.com/digiPlant/play-res https://github.com/digiPlant/play-scalr
  • 12. thanks! feel free to add me on: .../3x14159265