2014-10-31 11:04:57 -06:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import play.api.mvc.Action
|
2016-08-13 08:25:09 -06:00
|
|
|
import scala.concurrent.duration._
|
2014-10-31 11:04:57 -06:00
|
|
|
|
|
|
|
import lila.app._
|
2014-11-18 14:52:29 -07:00
|
|
|
import lila.common.HTTPRequest
|
2014-10-31 11:04:57 -06:00
|
|
|
import lila.game.{ Game => GameModel, GameRepo }
|
|
|
|
import play.api.http.ContentTypes
|
|
|
|
import play.api.libs.iteratee.{ Iteratee, Enumerator }
|
2014-11-18 14:52:29 -07:00
|
|
|
import play.api.mvc.Result
|
2014-10-31 11:04:57 -06:00
|
|
|
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 {
|
2014-11-18 14:52:29 -07:00
|
|
|
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)
|
2016-03-12 05:56:44 -07:00
|
|
|
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
|
2014-11-18 14:52:29 -07:00
|
|
|
}) 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)))
|
2014-11-18 14:52:29 -07:00
|
|
|
}
|
2014-10-31 11:04:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-13 08:25:09 -06:00
|
|
|
private val PdfRateLimitGlobal = new lila.memo.RateLimit(
|
|
|
|
credits = 20,
|
|
|
|
duration = 1 minute,
|
|
|
|
name = "export PDF global")
|
|
|
|
|
2014-10-31 11:04:57 -06:00
|
|
|
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")
|
|
|
|
}
|
2014-11-18 14:52:29 -07:00
|
|
|
}
|
2014-10-31 11:04:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-10-31 11:04:57 -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")
|
|
|
|
}
|
2014-11-18 14:52:29 -07:00
|
|
|
}
|
2014-10-31 11:04:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 16:32:45 -06:00
|
|
|
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")
|
|
|
|
}
|
2015-03-17 16:32:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-31 11:04:57 -06:00
|
|
|
}
|