2014-10-31 11:04:57 -06:00
|
|
|
package controllers
|
|
|
|
|
2019-12-04 16:39:16 -07:00
|
|
|
import akka.stream.scaladsl._
|
|
|
|
import akka.util.ByteString
|
2020-08-16 10:20:56 -06:00
|
|
|
import chess.Color
|
2020-02-26 11:19:19 -07:00
|
|
|
import play.api.mvc.Result
|
2020-08-16 10:20:56 -06:00
|
|
|
import scala.concurrent.duration._
|
2014-10-31 11:04:57 -06:00
|
|
|
|
|
|
|
import lila.app._
|
2020-03-04 02:40:27 -07:00
|
|
|
import lila.game.Pov
|
2020-11-11 02:08:19 -07:00
|
|
|
import lila.puzzle.Puzzle.Id
|
2014-10-31 11:04:57 -06:00
|
|
|
|
2019-12-04 16:39:16 -07:00
|
|
|
final class Export(env: Env) extends LilaController(env) {
|
2014-10-31 11:04:57 -06:00
|
|
|
|
2020-04-04 13:37:19 -06:00
|
|
|
private val ExportImageRateLimitGlobal = new lila.memo.RateLimit[String](
|
|
|
|
credits = 600,
|
2020-04-29 08:58:36 -06:00
|
|
|
duration = 1.minute,
|
2020-02-26 11:19:19 -07:00
|
|
|
key = "export.image.global"
|
2017-02-14 08:34:07 -07:00
|
|
|
)
|
2020-04-04 13:37:19 -06:00
|
|
|
private val ExportGifRateLimitGlobal = new lila.memo.RateLimit[String](
|
|
|
|
credits = 240,
|
2020-04-29 08:58:36 -06:00
|
|
|
duration = 1.minute,
|
2020-04-04 13:37:19 -06:00
|
|
|
key = "export.gif.global"
|
|
|
|
)
|
2016-08-13 08:25:09 -06:00
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def gif(id: String, color: String) =
|
|
|
|
Open { implicit ctx =>
|
|
|
|
OnlyHumansAndFacebookOrTwitter {
|
2021-09-07 11:43:17 -06:00
|
|
|
ExportGifRateLimitGlobal("-", msg = ctx.ip.value) {
|
2020-09-21 01:28:28 -06:00
|
|
|
OptionFuResult(env.game.gameRepo gameWithInitialFen id) { case (game, initialFen) =>
|
2020-09-21 03:31:16 -06:00
|
|
|
val pov = Pov(game, Color.fromName(color) | Color.white)
|
2020-09-21 01:28:28 -06:00
|
|
|
env.game.gifExport.fromPov(pov, initialFen) map
|
|
|
|
stream("image/gif") map
|
|
|
|
gameImageCacheSeconds(game)
|
2020-05-05 22:11:15 -06:00
|
|
|
}
|
2020-05-14 14:23:11 -06:00
|
|
|
}(rateLimitedFu)
|
2014-11-18 14:52:29 -07:00
|
|
|
}
|
2014-10-31 11:04:57 -06:00
|
|
|
}
|
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def legacyGameThumbnail(id: String) =
|
|
|
|
Action {
|
|
|
|
MovedPermanently(routes.Export.gameThumbnail(id).url)
|
|
|
|
}
|
2020-02-26 11:19:19 -07:00
|
|
|
|
2020-05-05 22:11:15 -06:00
|
|
|
def gameThumbnail(id: String) =
|
|
|
|
Open { implicit ctx =>
|
2021-09-07 11:43:17 -06:00
|
|
|
ExportImageRateLimitGlobal("-", msg = ctx.ip.value) {
|
2020-05-05 22:11:15 -06:00
|
|
|
OptionFuResult(env.game.gameRepo game id) { game =>
|
|
|
|
env.game.gifExport.gameThumbnail(game) map
|
|
|
|
stream("image/gif") map
|
|
|
|
gameImageCacheSeconds(game)
|
|
|
|
}
|
2020-05-14 14:23:11 -06:00
|
|
|
}(rateLimitedFu)
|
2020-03-05 11:40:33 -07:00
|
|
|
}
|
|
|
|
|
2020-11-11 02:08:19 -07:00
|
|
|
def puzzleThumbnail(id: String) =
|
2020-05-05 22:11:15 -06:00
|
|
|
Open { implicit ctx =>
|
2021-09-07 11:43:17 -06:00
|
|
|
ExportImageRateLimitGlobal("-", msg = ctx.ip.value) {
|
2020-11-11 02:08:19 -07:00
|
|
|
OptionFuResult(env.puzzle.api.puzzle find Id(id)) { puzzle =>
|
2020-05-05 22:11:15 -06:00
|
|
|
env.game.gifExport.thumbnail(
|
2020-11-11 02:08:19 -07:00
|
|
|
fen = puzzle.fenAfterInitialMove,
|
|
|
|
lastMove = puzzle.line.head.uci.some,
|
2020-05-05 22:11:15 -06:00
|
|
|
orientation = puzzle.color
|
|
|
|
) map stream("image/gif") map { res =>
|
2021-05-14 01:46:27 -06:00
|
|
|
res.withHeaders(CACHE_CONTROL -> "max-age=1209600")
|
2020-05-05 22:11:15 -06:00
|
|
|
}
|
2016-08-13 08:25:09 -06:00
|
|
|
}
|
2020-05-14 14:23:11 -06:00
|
|
|
}(rateLimitedFu)
|
2015-03-17 16:32:45 -06:00
|
|
|
}
|
2019-01-21 20:16:32 -07:00
|
|
|
|
2020-03-05 11:40:33 -07:00
|
|
|
private def gameImageCacheSeconds(game: lila.game.Game)(res: Result): Result = {
|
|
|
|
val cacheSeconds =
|
|
|
|
if (game.finishedOrAborted) 3600 * 24
|
|
|
|
else 10
|
|
|
|
res.withHeaders(CACHE_CONTROL -> s"max-age=$cacheSeconds")
|
|
|
|
}
|
|
|
|
|
2020-02-25 14:25:09 -07:00
|
|
|
private def stream(contentType: String)(stream: Source[ByteString, _]) =
|
2020-03-05 11:40:33 -07:00
|
|
|
Ok.chunked(stream).withHeaders(noProxyBufferHeader) as contentType
|
2014-10-31 11:04:57 -06:00
|
|
|
}
|