Skip to content

Commit

Permalink
print game to PDF, stream it as chunked HTTP and cache it in CDN
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Oct 31, 2014
1 parent 458e389 commit 8717264
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions app/controllers/Analyse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions modules/game/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ 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._

val MandatorySecondsToMove = config getInt "mandatory.seconds_to_move"

private[game] lazy val gameColl = db(CollectionGame)

lazy val pdfExport = PdfExport(PdfExecPath) _

lazy val cached = new Cached(ttl = CachedNbTtl)

lazy val paginator = new PaginatorBuilder(
Expand Down
14 changes: 14 additions & 0 deletions modules/game/src/main/PdfExport.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
6 changes: 5 additions & 1 deletion modules/i18n/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._

Expand All @@ -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,
Expand Down
14 changes: 10 additions & 4 deletions modules/i18n/src/main/I18nRequestHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
Expand Down
1 change: 1 addition & 0 deletions submodules/pdfexporter
Submodule pdfexporter added at 8d6c95

0 comments on commit 8717264

Please sign in to comment.