<commit_before>package skinny.controller

import skinny._, assets._, LoanPattern._
import scala.io.Source
import java.io.File

/**
 * Assets controller.
 */
class AssetsController extends SkinnyController {

  protected val isEnabledInProduction: Boolean = false

  protected def isEnabled: Boolean = if (isEnabledInProduction) true else !SkinnyEnv.isProduction()

  protected val coffeeScriptCompiler = CoffeeScriptCompiler()

  protected val lessCompiler = LessCompiler

  protected val basePath = "/WEB-INF/assets"

  def jsOrCoffee() = if (isEnabled) {
    multiParams("splat").headOption.flatMap {
      _.split("\\.") match {
        case Array(path, "js") => Some(path)
        case _ => None
      }
    }.map { path =>

      // try to load from class path resources
      val jsResource = ClassPathResourceLoader.getResourceAsStream(s"${basePath}/js/${path}.js")
      val coffeeResource = ClassPathResourceLoader.getResourceAsStream(s"${basePath}/coffee/${path}.coffee")
      if (jsResource.isDefined) {
        jsResource.map { resource =>
          using(Source.fromInputStream(resource))(_.mkString)
        } getOrElse (halt(404))
      } else if (coffeeResource.isDefined) {
        coffeeResource.map { resource =>
          coffeeScriptCompiler.compile(using(Source.fromInputStream(resource))(_.mkString))
        }.getOrElse(halt(404))

      } else {
        // load content from real files
        val jsFile = new File(servletContext.getRealPath(s"${basePath}/js/${path}.js"))
        val coffeeFile = new File(servletContext.getRealPath(s"${basePath}/coffee/${path}.coffee"))
        if (jsFile.exists()) {
          using(Source.fromFile(jsFile))(js => js.mkString)
        } else if (coffeeFile.exists()) {
          using(Source.fromFile(coffeeFile))(coffee => coffeeScriptCompiler.compile(coffee.mkString))
        } else {
          pass()
        }

      }
    } getOrElse {
      pass()
    }
  } else {
    pass()
  }

  def cssOrLess() = if (isEnabled) {
    multiParams("splat").headOption.flatMap {
      _.split("\\.") match {
        case Array(path, "css") => Some(path)
        case _ => None
      }
    }.map { path =>

      // try to load from class path resources
      val cssResource = ClassPathResourceLoader.getResourceAsStream(s"${basePath}/css/${path}.css")
      val lessResource = ClassPathResourceLoader.getResourceAsStream(s"${basePath}/less/${path}.less")
      if (cssResource.isDefined) {
        cssResource.map { resource =>
          using(Source.fromInputStream(resource))(_.mkString)
        } getOrElse (halt(404))
      } else if (lessResource.isDefined) {
        lessResource.map { resource =>
          lessCompiler.compile(using(Source.fromInputStream(resource))(_.mkString))
        }.getOrElse(halt(404))

      } else {
        // load content from real files
        val cssFile = new File(servletContext.getRealPath(s"${basePath}/css/${path}.css"))
        val lessFile = new File(servletContext.getRealPath(s"${basePath}/less/${path}.less"))
        if (cssFile.exists()) {
          using(Source.fromFile(cssFile))(js => js.mkString)
        } else if (lessFile.exists()) {
          using(Source.fromFile(lessFile))(coffee => lessCompiler.compile(coffee.mkString))
        } else {
          pass()
        }

      }
    } getOrElse {
      pass()
    }
  } else {
    pass()
  }

}

object AssetsController extends AssetsController with Routes {

  override protected val isEnabledInProduction = false

  // Unfortunately, *.* seems not to work.
  get("/assets/js/*")(jsOrCoffee).as('jsOrCoffee)
  get("/assets/css/*")(cssOrLess).as('cssOrLess)
}<commit_msg>Added configurations to AssetsController<commit_after>package skinny.controller

import skinny._, assets._, LoanPattern._
import scala.io.Source
import java.io.File

/**
 * Assets controller.
 */
class AssetsController extends SkinnyController {

  /**
   * Returns assets root path.
   */
  protected def assetsRootPath = "/assets"

  /**
   * Returns assets/js root path.
   */
  protected def jsRootPath = s"${assetsRootPath}/js"

  /**
   * Returns assets/css root path.
   */
  protected def cssRootPath = s"${assetsRootPath}/css"

  /**
   * Predicates this controller in staging env.
   */
  protected def isDisabledInStaging: Boolean = true

  /**
   * Predicates this controller in production env.
   */
  protected def isDisabledInProduction: Boolean = true

  /**
   * Predicates this controller is enabled in the current env.
   */
  protected def isEnabled: Boolean = {
    if (SkinnyEnv.isProduction()) !isDisabledInProduction
    if (SkinnyEnv.isStaging()) !isDisabledInStaging
    else true
  }

  /**
   * CoffeeScript compiler.
   */
  protected val coffeeScriptCompiler = CoffeeScriptCompiler()

  /**
   * LESS compiler.
   */
  protected val lessCompiler = LessCompiler

  /**
   * Base path for assets files.
   */
  protected val basePath = "/WEB-INF/assets"

  /**
   * Returns js or coffee assets.
   */
  def jsOrCoffee() = if (isEnabled) {
    multiParams("splat").headOption.flatMap {
      _.split("\\.") match {
        case Array(path, "js") => Some(path)
        case _ => None
      }
    }.map { path =>

      // try to load from class path resources
      val jsResource = ClassPathResourceLoader.getResourceAsStream(s"${basePath}/js/${path}.js")
      val coffeeResource = ClassPathResourceLoader.getResourceAsStream(s"${basePath}/coffee/${path}.coffee")
      if (jsResource.isDefined) {
        jsResource.map { resource =>
          using(Source.fromInputStream(resource))(_.mkString)
        } getOrElse (halt(404))
      } else if (coffeeResource.isDefined) {
        coffeeResource.map { resource =>
          coffeeScriptCompiler.compile(using(Source.fromInputStream(resource))(_.mkString))
        }.getOrElse(halt(404))

      } else {
        // load content from real files
        val jsFile = new File(servletContext.getRealPath(s"${basePath}/js/${path}.js"))
        val coffeeFile = new File(servletContext.getRealPath(s"${basePath}/coffee/${path}.coffee"))
        if (jsFile.exists()) {
          using(Source.fromFile(jsFile))(js => js.mkString)
        } else if (coffeeFile.exists()) {
          using(Source.fromFile(coffeeFile))(coffee => coffeeScriptCompiler.compile(coffee.mkString))
        } else {
          pass()
        }

      }
    } getOrElse {
      pass()
    }
  } else {
    pass()
  }

  /**
   * Returns css or less assets.
   */
  def cssOrLess() = if (isEnabled) {
    multiParams("splat").headOption.flatMap {
      _.split("\\.") match {
        case Array(path, "css") => Some(path)
        case _ => None
      }
    }.map { path =>

      // try to load from class path resources
      val cssResource = ClassPathResourceLoader.getResourceAsStream(s"${basePath}/css/${path}.css")
      val lessResource = ClassPathResourceLoader.getResourceAsStream(s"${basePath}/less/${path}.less")
      if (cssResource.isDefined) {
        cssResource.map { resource =>
          using(Source.fromInputStream(resource))(_.mkString)
        } getOrElse (halt(404))
      } else if (lessResource.isDefined) {
        lessResource.map { resource =>
          lessCompiler.compile(using(Source.fromInputStream(resource))(_.mkString))
        }.getOrElse(halt(404))

      } else {
        // load content from real files
        val cssFile = new File(servletContext.getRealPath(s"${basePath}/css/${path}.css"))
        val lessFile = new File(servletContext.getRealPath(s"${basePath}/less/${path}.less"))
        if (cssFile.exists()) {
          using(Source.fromFile(cssFile))(js => js.mkString)
        } else if (lessFile.exists()) {
          using(Source.fromFile(lessFile))(coffee => lessCompiler.compile(coffee.mkString))
        } else {
          pass()
        }

      }
    } getOrElse {
      pass()
    }
  } else {
    pass()
  }

}

/**
 * AssetsController with default configurations.
 */
object AssetsController extends AssetsController with Routes {

  // Unfortunately, *.* seems not to work.
  get(s"${jsRootPath}/*")(jsOrCoffee).as('jsOrCoffee)
  get(s"${cssRootPath}/*")(cssOrLess).as('cssOrLess)

}

{'fraction_non_alphanumeric': 0.1361958661417323, 'fraction_numerical': 0.002952755905511811, 'mean_word_length': 2.8110642287857477, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5043970', 'n_tokens_mistral': 2510, 'n_tokens_neox': 2410, 'n_words': 478}