Advertisement
Guest User

Untitled

a guest
Sep 11th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.04 KB | None | 0 0
  1. package controllers
  2.  
  3. import play.api.libs.iteratee.{Input, Iteratee, Enumerator}
  4. import play.api.libs.concurrent.Execution.Implicits.defaultContext
  5. import play.api.mvc.{Request, SimpleResult, AnyContent, Action}
  6. import play.api.Play
  7. import play.api.Play.current
  8. import com.mosaic.core.config.{Development, EnvironmentAware}
  9.  
  10. class AssetsController extends AssetsBuilder
  11.   with EnvironmentAware {
  12.   private lazy val cdnUrl = Play.configuration.getString("cdn.url." + currentEnvironment.name.toLowerCase).getOrElse("")
  13.   private lazy val cdnUrlToken = Play.configuration.getString("build.config.cdnUrlToken").getOrElse("")
  14.   private lazy val defaultCharSet = Play.configuration.getString("default.charset").getOrElse("utf-8")
  15.  
  16.   private val rStylesheet = """.css$""".r
  17.  
  18.   /**
  19.    *
  20.    */
  21.   override def at(path: String, file: String): Action[AnyContent] = Action.async { request: Request[AnyContent] =>
  22.     // Call the standard Play Assets.at method to get the result
  23.     val futureResult = super.at(path, file)(request)
  24.  
  25.     (rStylesheet findFirstIn file, currentEnvironment != Development) match {
  26.       // This logic should only apply to stylesheets and in non-development environments
  27.       case (Some(stylesheet), true) => futureResult.flatMap { result: SimpleResult =>
  28.         // Get result body as String
  29.         result.body |>>> Iteratee.consume[Array[Byte]]().map { bytes: Array[Byte] =>
  30.           // Convert string bytes into string
  31.           val bodyContentAsString = new String(bytes, defaultCharSet)
  32.           // Replace the CDN URL token with the actual CDN URL
  33.           val bodyContentReplaced = bodyContentAsString.replaceAll(cdnUrlToken, cdnUrl)
  34.           // Create a new response.body for the new content
  35.           val newResponseBody = Enumerator(bodyContentReplaced.getBytes) >>> Enumerator.enumInput(Input.EOF)
  36.  
  37.           // Return the result with modified body content
  38.           result.copy(body = newResponseBody)
  39.         }
  40.       }
  41.       case _ => futureResult
  42.     }
  43.   }
  44. }
  45.  
  46. object AssetsController extends AssetsController
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement