Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package controllers
- import play.api.libs.iteratee.{Input, Iteratee, Enumerator}
- import play.api.libs.concurrent.Execution.Implicits.defaultContext
- import play.api.mvc.{Request, SimpleResult, AnyContent, Action}
- import play.api.Play
- import play.api.Play.current
- import com.mosaic.core.config.{Development, EnvironmentAware}
- class AssetsController extends AssetsBuilder
- with EnvironmentAware {
- private lazy val cdnUrl = Play.configuration.getString("cdn.url." + currentEnvironment.name.toLowerCase).getOrElse("")
- private lazy val cdnUrlToken = Play.configuration.getString("build.config.cdnUrlToken").getOrElse("")
- private lazy val defaultCharSet = Play.configuration.getString("default.charset").getOrElse("utf-8")
- private val rStylesheet = """.css$""".r
- /**
- *
- */
- override def at(path: String, file: String): Action[AnyContent] = Action.async { request: Request[AnyContent] =>
- // Call the standard Play Assets.at method to get the result
- val futureResult = super.at(path, file)(request)
- (rStylesheet findFirstIn file, currentEnvironment != Development) match {
- // This logic should only apply to stylesheets and in non-development environments
- case (Some(stylesheet), true) => futureResult.flatMap { result: SimpleResult =>
- // Get result body as String
- result.body |>>> Iteratee.consume[Array[Byte]]().map { bytes: Array[Byte] =>
- // Convert string bytes into string
- val bodyContentAsString = new String(bytes, defaultCharSet)
- // Replace the CDN URL token with the actual CDN URL
- val bodyContentReplaced = bodyContentAsString.replaceAll(cdnUrlToken, cdnUrl)
- // Create a new response.body for the new content
- val newResponseBody = Enumerator(bodyContentReplaced.getBytes) >>> Enumerator.enumInput(Input.EOF)
- // Return the result with modified body content
- result.copy(body = newResponseBody)
- }
- }
- case _ => futureResult
- }
- }
- }
- object AssetsController extends AssetsController
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement