Skip to content

Instantly share code, notes, and snippets.

@kanterov
Created July 19, 2014 16:59
Show Gist options
  • Save kanterov/aa5c6ffe78c8cb697249 to your computer and use it in GitHub Desktop.
Save kanterov/aa5c6ffe78c8cb697249 to your computer and use it in GitHub Desktop.
object SourceMap extends Controller {
implicit val ec = ExecutionContext.global
def at(prefix: String, path: String) = Action {
val fileOpt = Play.getExistingFile(s"../$prefix/" + path)
fileOpt.map {
case _ if !Play.isDev => Forbidden
case file if file.isDirectory => NotFound
case file if !file.exists() => NotFound
case file =>
lazy val (length, resourceData) = {
val stream = new FileInputStream(file)
try {
(stream.available, Enumerator.fromStream(stream))
} catch {
case NonFatal(_) => (-1, Enumerator[Array[Byte]]())
}
}
if (length == -1) {
NotFound
} else {
Result(
ResponseHeader(OK, Map(
CONTENT_LENGTH -> length.toString,
CONTENT_TYPE -> MimeTypes.forFileName(path).getOrElse(BINARY))),
resourceData)
}
}.getOrElse(NotFound)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment