print game to PDF, stream it as chunked HTTP and cache it in CDN

pull/132/head
Thibault Duplessis 2014-10-31 17:17:52 +01:00
parent 458e389887
commit 8717264ae0
8 changed files with 46 additions and 5 deletions

3
.gitmodules vendored
View File

@ -10,3 +10,6 @@
[submodule "public/staunton"]
path = public/staunton
url = https://github.com/clarkerubber/Staunton-Pieces
[submodule "submodules/pdfexporter"]
path = submodules/pdfexporter
url = https://github.com/clarkerubber/lichessPDFExporter

View File

@ -6,6 +6,7 @@ import akka.pattern.ask
import play.api.http.ContentTypes
import play.api.mvc._
import play.twirl.api.Html
import play.api.libs.iteratee.{ Iteratee, Enumerator }
import lila.analyse.{ Analysis, TimeChart, AdvantageChart }
import lila.api.Context
@ -92,6 +93,13 @@ object Analyse extends LilaController {
}
}
def pdf(id: String) = Open { implicit ctx =>
OptionResult(GameRepo game id) { game =>
Ok.chunked(Enumerator.outputStream(Env.game.pdfExport(game.id))).withHeaders(
CONTENT_TYPE -> ContentTypes.withCharset("application/pdf"))
}
}
private def gameOpening(game: GameModel) =
if (game.fromPosition || game.variant.exotic) none
else chess.OpeningExplorer openingOf game.pgnMoves

View File

@ -138,6 +138,8 @@ POST /$gameId<\w{8}>/better-analysis/$color<white|black> controllers.Analyse.be
POST /$gameId<\w{8}>/post-analysis controllers.Analyse.postAnalysis(gameId: String)
GET /$gameId<\w{8}>/pgn controllers.Analyse.pgn(gameId: String)
GET /game/pdf/$gameId<\w{8}> controllers.Analyse.pdf(gameId: String)
# Pref
POST /pref/:name controllers.Pref.set(name: String)
GET /account/preferences controllers.Pref.form

View File

@ -28,6 +28,7 @@ final class Env(
val ActorName = config getString "actor.name"
val UciMemoTtl = config duration "uci_memo.ttl"
val netBaseUrl = config getString "net.base_url"
val PdfExecPath = config getString "pdf.exec_path"
}
import settings._
@ -35,6 +36,8 @@ final class Env(
private[game] lazy val gameColl = db(CollectionGame)
lazy val pdfExport = PdfExport(PdfExecPath) _
lazy val cached = new Cached(ttl = CachedNbTtl)
lazy val paginator = new PaginatorBuilder(

View File

@ -0,0 +1,14 @@
package lila.game
import java.io.{ File, OutputStream }
import scala.sys.process._
object PdfExport {
private val logger = ProcessLogger(_ => (), _ => ())
def apply(execPath: String)(id: String)(out: OutputStream) {
val exec = Process(Seq("php", "main.php", id), new File(execPath))
exec #> out ! logger
}
}

View File

@ -22,6 +22,7 @@ final class Env(
val CollectionTranslation = config getString "collection.translation"
val ContextGitUrl = config getString "context.git.url"
val ContextGitFile = config getString "context.git.file"
val CdnDomain = config getString "cdn_domain"
}
import settings._
@ -40,7 +41,10 @@ final class Env(
lazy val keys = new I18nKeys(translator)
lazy val requestHandler = new I18nRequestHandler(pool, RequestHandlerProtocol)
lazy val requestHandler = new I18nRequestHandler(
pool,
RequestHandlerProtocol,
CdnDomain)
lazy val jsDump = new JsDump(
path = appPath + "/" + WebPathRelative,

View File

@ -6,12 +6,18 @@ import play.api.mvc.{ Action, RequestHeader, Handler }
import lila.common.HTTPRequest
final class I18nRequestHandler(pool: I18nPool, protocol: String) {
final class I18nRequestHandler(
pool: I18nPool,
protocol: String,
cdnDomain: String) {
def apply(req: RequestHeader): Option[Handler] =
(HTTPRequest.isRedirectable(req) && !pool.domainLang(req).isDefined) option Action {
Redirect(redirectUrl(req))
}
(HTTPRequest.isRedirectable(req) &&
!pool.domainLang(req).isDefined &&
req.host != cdnDomain
) option Action {
Redirect(redirectUrl(req))
}
private def redirectUrl(req: RequestHeader) =
protocol +

@ -0,0 +1 @@
Subproject commit 8d6c9550432ce462e4e977a770751354da097cf4