2014-10-31 11:04:57 -06:00
|
|
|
package controllers
|
|
|
|
|
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
|
2017-05-22 05:00:25 -06:00
|
|
|
import lila.game.{ Game => GameModel, GameRepo, PgnDump }
|
2014-10-31 11:04:57 -06:00
|
|
|
|
|
|
|
object Export extends LilaController {
|
|
|
|
|
|
|
|
private def env = Env.game
|
|
|
|
|
|
|
|
def pgn(id: String) = Open { implicit ctx =>
|
2016-10-24 05:23:26 -06:00
|
|
|
lila.mon.export.pgn.game()
|
|
|
|
OptionFuResult(GameRepo game id) { game =>
|
|
|
|
gameToPgn(
|
|
|
|
game,
|
|
|
|
asImported = get("as") contains "imported",
|
2017-02-14 08:34:07 -07:00
|
|
|
asRaw = get("as").contains("raw")
|
|
|
|
) map { content =>
|
2016-10-24 05:23:26 -06:00
|
|
|
Ok(content).withHeaders(
|
2017-09-09 09:01:35 -06:00
|
|
|
CONTENT_TYPE -> pgnContentType,
|
2017-02-14 08:34:07 -07:00
|
|
|
CONTENT_DISPOSITION -> ("attachment; filename=" + (Env.api.pgnDump filename game))
|
2017-09-09 09:01:35 -06:00
|
|
|
)
|
2016-10-24 05:23:26 -06:00
|
|
|
} recover {
|
|
|
|
case err => NotFound(err.getMessage)
|
|
|
|
}
|
2014-10-31 11:04:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2017-05-22 05:00:25 -06:00
|
|
|
pgn <- Env.api.pgnDump(game, initialFen, PgnDump.WithFlags(clocks = true))
|
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](
|
2016-08-13 08:25:09 -06:00
|
|
|
credits = 60,
|
|
|
|
duration = 1 minute,
|
2016-09-01 15:54:43 -06:00
|
|
|
name = "export PGN global",
|
2017-02-14 08:34:07 -07:00
|
|
|
key = "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 =>
|
2017-02-05 05:25:05 -07:00
|
|
|
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.game()
|
2016-11-02 10:06:55 -06:00
|
|
|
OptionFuResult(GameRepo game id) { game =>
|
2016-11-02 10:22:44 -06:00
|
|
|
env.pngExport fromGame game map { stream =>
|
2016-11-02 10:06:55 -06:00
|
|
|
Ok.chunked(stream).withHeaders(
|
2017-09-09 09:01:35 -06:00
|
|
|
CONTENT_TYPE -> "image/png",
|
2017-02-14 08:34:07 -07:00
|
|
|
CACHE_CONTROL -> "max-age=7200"
|
2017-09-09 09:01:35 -06:00
|
|
|
)
|
2016-11-02 10:06:55 -06:00
|
|
|
}
|
2016-08-13 08:25:09 -06:00
|
|
|
}
|
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 =>
|
2017-02-05 05:25:05 -07:00
|
|
|
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()
|
2016-11-02 10:22:44 -06:00
|
|
|
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,
|
2016-11-02 10:22:44 -06:00
|
|
|
check = none,
|
2016-11-03 06:50:06 -06:00
|
|
|
orientation = puzzle.color.some,
|
|
|
|
logHint = s"puzzle $id"
|
2016-11-02 10:22:44 -06:00
|
|
|
) map { stream =>
|
|
|
|
Ok.chunked(stream).withHeaders(
|
2017-09-09 09:01:35 -06:00
|
|
|
CONTENT_TYPE -> "image/png",
|
2017-02-14 08:34:07 -07:00
|
|
|
CACHE_CONTROL -> "max-age=7200"
|
2017-09-09 09:01:35 -06:00
|
|
|
)
|
2016-11-02 10:22:44 -06:00
|
|
|
}
|
2016-08-13 08:25:09 -06:00
|
|
|
}
|
2015-03-17 16:32:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-31 11:04:57 -06:00
|
|
|
}
|