lila/app/controllers/Export.scala

88 lines
2.8 KiB
Scala
Raw Normal View History

package controllers
import play.api.mvc.Action
2016-08-13 08:25:09 -06:00
import scala.concurrent.duration._
import lila.app._
import lila.common.HTTPRequest
import lila.game.{ Game => GameModel, GameRepo }
import play.api.http.ContentTypes
import play.api.libs.iteratee.{ Iteratee, Enumerator }
import play.api.mvc.Result
import views._
object Export extends LilaController {
private def env = Env.game
def pgn(id: String) = Open { implicit ctx =>
2014-11-19 02:56:15 -07:00
OnlyHumans {
2016-08-18 03:16:54 -06:00
lila.mon.export.pgn.game()
2016-07-26 15:58:23 -06:00
OptionFuResult(GameRepo game id) {
case game if game.playable => NotFound("Can't export PGN of game in progress").fuccess
case game => (game.pgnImport.ifTrue(get("as") contains "imported") match {
case Some(i) => fuccess(i.pgn)
case None => for {
initialFen <- GameRepo initialFen game
2015-06-28 14:36:36 -06:00
pgn = Env.api.pgnDump(game, initialFen)
analysis !get("as").contains("raw") ?? (Env.analyse.analyser get game.id)
2016-02-25 03:03:09 -07:00
} yield Env.analyse.annotator(pgn, analysis, game.opening, game.winnerColor, game.status, game.clock).toString
}) map { content =>
Ok(content).withHeaders(
CONTENT_TYPE -> ContentTypes.TEXT,
2015-06-28 14:36:36 -06:00
CONTENT_DISPOSITION -> ("attachment; filename=" + (Env.api.pgnDump filename game)))
}
}
}
}
2016-08-13 08:25:09 -06:00
private val PdfRateLimitGlobal = new lila.memo.RateLimit(
credits = 20,
duration = 1 minute,
name = "export PDF global")
def pdf(id: String) = Open { implicit ctx =>
2014-11-19 02:56:15 -07:00
OnlyHumans {
2016-08-13 08:25:09 -06:00
PdfRateLimitGlobal("-", msg = HTTPRequest lastRemoteAddress ctx.req) {
lila.mon.export.pdf()
OptionResult(GameRepo game id) { game =>
Ok.chunked(Enumerator.outputStream(env.pdfExport(game.id))).withHeaders(
CONTENT_TYPE -> "application/pdf",
CACHE_CONTROL -> "max-age=7200")
}
}
}
}
2016-08-13 08:25:09 -06:00
private val PngRateLimitGlobal = new lila.memo.RateLimit(
credits = 60,
duration = 1 minute,
2016-09-01 01:03:19 -06:00
name = "export PGN global")
2016-08-13 08:25:09 -06:00
def png(id: String) = Open { implicit ctx =>
2014-11-19 02:56:15 -07:00
OnlyHumansAndFacebook {
2016-08-13 08:25:09 -06:00
PngRateLimitGlobal("-", msg = HTTPRequest lastRemoteAddress ctx.req) {
lila.mon.export.png.game()
OptionResult(GameRepo game id) { game =>
Ok.chunked(Enumerator.outputStream(env.pngExport(game))).withHeaders(
CONTENT_TYPE -> "image/png",
CACHE_CONTROL -> "max-age=7200")
}
}
}
}
def puzzlePng(id: Int) = Open { implicit ctx =>
OnlyHumansAndFacebook {
2016-08-13 08:25:09 -06:00
PngRateLimitGlobal("-", msg = HTTPRequest lastRemoteAddress ctx.req) {
lila.mon.export.png.puzzle()
OptionResult(Env.puzzle.api.puzzle find id) { puzzle =>
Ok.chunked(Enumerator.outputStream(Env.puzzle.pngExport(puzzle))).withHeaders(
CONTENT_TYPE -> "image/png",
CACHE_CONTROL -> "max-age=7200")
}
}
}
}
}