lila/app/controllers/Export.scala

88 lines
2.8 KiB
Scala
Raw Normal View History

package controllers
2016-08-13 08:25:09 -06:00
import scala.concurrent.duration._
import lila.app._
import lila.common.HTTPRequest
2017-05-22 05:00:25 -06:00
import lila.game.{ Game => GameModel, GameRepo, PgnDump }
object Export extends LilaController {
private def env = Env.game
def pgn(id: String) = Open { implicit ctx =>
lila.mon.export.pgn.game()
OptionFuResult(GameRepo game id) { game =>
gameToPgn(
game,
asImported = get("as") contains "imported",
asRaw = get("as").contains("raw")
) map { content =>
Ok(content).withHeaders(
CONTENT_TYPE -> pgnContentType,
CONTENT_DISPOSITION -> ("attachment; filename=" + (Env.api.pgnDump filename game))
)
} recover {
case err => NotFound(err.getMessage)
}
}
}
2016-09-04 05:40:37 -06:00
private def gameToPgn(from: GameModel, asImported: Boolean, asRaw: Boolean): Fu[String] = from match {
case game if game.playable => fufail("Can't export PGN of game in progress")
case game => (game.pgnImport.ifTrue(asImported) match {
case Some(i) => fuccess(i.pgn)
case None => for {
initialFen <- GameRepo initialFen game
pgn <- Env.api.pgnDump(game, initialFen, PgnDump.WithFlags(clocks = !asRaw))
2016-09-04 05:40:37 -06:00
analysis !asRaw ?? (Env.analyse.analyser get game.id)
} yield Env.analyse.annotator(pgn, analysis, game.opening, game.winnerColor, game.status, game.clock).toString
})
}
2017-02-15 17:53:15 -07:00
private val PngRateLimitGlobal = new lila.memo.RateLimit[String](
credits = 120,
2016-08-13 08:25:09 -06:00
duration = 1 minute,
2018-03-30 09:18:41 -06:00
name = "export PNG global",
key = "export.png.global"
)
2016-08-13 08:25:09 -06:00
def png(id: String) = Open { implicit ctx =>
OnlyHumansAndFacebookOrTwitter {
PngRateLimitGlobal("-", msg = s"${HTTPRequest.lastRemoteAddress(ctx.req).value} ${~HTTPRequest.userAgent(ctx.req)}") {
2016-08-13 08:25:09 -06:00
lila.mon.export.png.game()
OptionFuResult(GameRepo game id) { game =>
env.pngExport fromGame game map { stream =>
Ok.chunked(stream).withHeaders(
CONTENT_TYPE -> "image/png",
CACHE_CONTROL -> "max-age=7200"
)
}
2016-08-13 08:25:09 -06:00
}
}
}
}
def puzzlePng(id: Int) = Open { implicit ctx =>
OnlyHumansAndFacebookOrTwitter {
2017-02-15 17:53:15 -07:00
PngRateLimitGlobal("-", msg = HTTPRequest.lastRemoteAddress(ctx.req).value) {
2016-08-13 08:25:09 -06:00
lila.mon.export.png.puzzle()
OptionFuResult(Env.puzzle.api.puzzle find id) { puzzle =>
env.pngExport(
fen = chess.format.FEN(puzzle.fenAfterInitialMove | puzzle.fen),
2016-11-29 06:15:46 -07:00
lastMove = puzzle.initialMove.uci.some,
check = none,
2016-11-03 06:50:06 -06:00
orientation = puzzle.color.some,
logHint = s"puzzle $id"
) map { stream =>
Ok.chunked(stream).withHeaders(
CONTENT_TYPE -> "image/png",
CACHE_CONTROL -> "max-age=7200"
)
}
2016-08-13 08:25:09 -06:00
}
}
}
}
}