Git Product home page Git Product logo

play-res's Introduction

Resource plugin for Play Framework 2

use it like an amazon s3 bucket. It will only store one version of a file since it computes the sha1 hash of the file and uses that as a pathname.

Add plugin to dependencies

val appDependencies = Seq(
	"se.digiplant" %% "play-res" % "1.0"
)

val root = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
  // To simplify the reverse routing we can import the digiPlant namespace
  templatesImport ++= Seq(
    "se.digiplant._"
  )
)

Add to conf/application.conf

# Resource plugin save directory
# is relative to app, but can be absolute to filesystem also
res.default=res/default

Add to conf/routes

GET    /res/:file      se.digiplant.res.ResAssets.at(file)

# If you wan't to separate your resources into separate sources, you can add multiple routes

GET    /images/:file      se.digiplant.res.ResAssets.at(file, "images")

Usage

Reverse routing

The play-res plugin generates a unique sha1 hash of the file and stores the file on disk in an optimal 4char/4char/sha1hash.ext directory structure. By only having to save the fileuid (5564ac5e3968e77b4022f55a23d36630bdeb0274.jpg) to the db it's easy to retrieve the image or other document.

Here are a few examples on how to get files

<img src="@res.routes.ResAssets.at("5564ac5e3968e77b4022f55a23d36630bdeb0274.jpg")" alt="" />

Or if you wan't to get the route to an file stored in a different source

<img src="@res.routes.ResAssets.at("5564ac5e3968e77b4022f55a23d36630bdeb0274.jpg", "images")" alt="" />

Advanced Usage

You can specify multiple sources if you would like to split you resources into categories, such as profile images / uploads. By then adding another source in conf/application.conf

res.profile=res/profile

you will also have to add another route

GET    /profile/:file      se.digiplant.res.ResAssets.at(file, "profile")

play-res's People

Contributors

ggrossetie avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.