forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExport.scala
48 lines (40 loc) · 1.54 KB
/
Export.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package controllers
import play.api.mvc.Action
import lila.app._
import lila.game.{ Game => GameModel, GameRepo }
import play.api.http.ContentTypes
import play.api.libs.iteratee.{ Iteratee, Enumerator }
import views._
object Export extends LilaController {
private def env = Env.game
def pgn(id: String) = Open { implicit ctx =>
OptionFuResult(GameRepo game id) { game =>
(game.pgnImport.ifTrue(~get("as") == "imported") match {
case Some(i) => fuccess(i.pgn)
case None => for {
pgn ← Env.game.pgnDump(game)
analysis ← (~get("as") != "raw") ?? (Env.analyse.analyser getDone game.id)
} yield Env.analyse.annotator(pgn, analysis, gameOpening(game), game.winnerColor, game.status, game.clock).toString
}) map { content =>
Ok(content).withHeaders(
CONTENT_TYPE -> ContentTypes.TEXT,
CONTENT_DISPOSITION -> ("attachment; filename=" + (Env.game.pgnDump filename game)))
}
}
}
def pdf(id: String) = Open { implicit ctx =>
OptionResult(GameRepo game id) { game =>
Ok.chunked(Enumerator.outputStream(env.pdfExport(game.id))).withHeaders(
CONTENT_TYPE -> "application/pdf")
}
}
def png(id: String) = Open { implicit ctx =>
OptionResult(GameRepo game id) { game =>
Ok.chunked(Enumerator.outputStream(env.pngExport(game))).withHeaders(
CONTENT_TYPE -> "image/png")
}
}
private def gameOpening(game: GameModel) =
if (game.fromPosition || game.variant.exotic) none
else chess.OpeningExplorer openingOf game.pgnMoves
}