refactor attachment download actions

pull/8888/head
Thibault Duplessis 2021-05-06 13:18:15 +02:00
parent 25b3605cbd
commit 6957c0fc16
7 changed files with 24 additions and 41 deletions

View File

@ -3,6 +3,7 @@ package controllers
import play.api.libs.json._
import play.api.mvc._
import scala.annotation.nowarn
import scala.util.chaining._
import views.html
import lila.api.AnnounceStore
@ -450,10 +451,7 @@ final class Account(
env.api.personalDataExport(user)
) { source =>
Ok.chunked(source.map(_ + "\n"))
.withHeaders(
noProxyBufferHeader,
CONTENT_DISPOSITION -> s"attachment; filename=lichess_${user.username}.txt"
)
.pipe(asAttachmentStream(s"lichess_${user.username}.txt"))
}
else Ok(html.account.bits.data(user))
}

View File

@ -226,10 +226,7 @@ final class Api(
) { source =>
val filename = env.api.gameApiV2.filename(tour, config.format)
Ok.chunked(source)
.withHeaders(
noProxyBufferHeader,
CONTENT_DISPOSITION -> s"attachment; filename=$filename"
)
.pipe(asAttachmentStream(env.api.gameApiV2.filename(tour, config.format)))
.as(gameC gameContentType config)
}.fuccess
}
@ -297,10 +294,7 @@ final class Api(
) { source =>
val filename = env.api.gameApiV2.filename(swiss, config.format)
Ok.chunked(source)
.withHeaders(
noProxyBufferHeader,
CONTENT_DISPOSITION -> s"attachment; filename=$filename"
)
.pipe(asAttachmentStream(filename))
.as(gameC gameContentType config)
}.fuccess
}

View File

@ -3,6 +3,7 @@ package controllers
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import play.api.mvc._
import scala.util.chaining._
import lila.api.GameApiV2
import lila.app._
@ -52,9 +53,7 @@ final class Game(
env.api.gameApiV2.exportOne(game, config) flatMap { content =>
env.api.gameApiV2.filename(game, config.format) map { filename =>
Ok(content)
.withHeaders(
CONTENT_DISPOSITION -> s"attachment; filename=$filename"
)
.pipe(asAttachment(filename))
.withHeaders(
lila.app.http.ResponseHeaders.headersForApiOrApp(req): _*
) as gameContentType(config)
@ -104,10 +103,7 @@ final class Game(
.GlobalConcurrencyLimitPerIpAndUserOption(req, me)(env.api.gameApiV2.exportByUser(config)) {
source =>
Ok.chunked(source)
.withHeaders(
noProxyBufferHeader,
CONTENT_DISPOSITION -> s"attachment; filename=lichess_${user.username}_$date.${format.toString.toLowerCase}"
)
.pipe(asAttachmentStream(s"lichess_${user.username}_$date.${format.toString.toLowerCase}"))
.as(gameContentType(config))
}
.fuccess

View File

@ -3,13 +3,14 @@ package controllers
import play.api.data._
import play.api.data.Forms._
import scala.concurrent.duration._
import scala.util.chaining._
import lila.api.Context
import lila.api.GameApiV2
import lila.app._
import lila.common.config
import lila.common.HTTPRequest
import lila.db.dsl._
import lila.api.GameApiV2
import lila.common.config
import lila.user.Holder
final class GameMod(env: Env)(implicit mat: akka.stream.Materializer) extends LilaController(env) {
@ -93,10 +94,8 @@ final class GameMod(env: Env)(implicit mat: akka.stream.Materializer) extends Li
playerFile = none
)
)
}.withHeaders(
noProxyBufferHeader,
CONTENT_DISPOSITION -> s"attachment; filename=lichess_mod_${user.username}_${gameIds.size}_games.pgn"
).as(pgnContentType)
}.pipe(asAttachmentStream(s"lichess_mod_${user.username}_${gameIds.size}_games.pgn"))
.as(pgnContentType)
private def guessSwisses(user: lila.user.User): Fu[Seq[lila.swiss.Swiss]] = fuccess(Nil)
}

View File

@ -676,7 +676,11 @@ abstract private[controllers] class LilaController(val env: Env)
protected val noProxyBufferHeader = "X-Accel-Buffering" -> "no"
protected val noProxyBuffer = (res: Result) => res.withHeaders(noProxyBufferHeader)
protected def asAttachment(name: String) = (res: Result) =>
res.withHeaders(CONTENT_DISPOSITION -> s"attachment; filename=$name")
protected def asAttachmentStream(name: String) = (res: Result) => noProxyBuffer(asAttachment(name)(res))
protected val pgnContentType = "application/x-chess-pgn"
protected val ndJsonContentType = "application/x-ndjson"
protected val csvContentType = "text/csv"
}

View File

@ -3,6 +3,7 @@ package controllers
import play.api.libs.json._
import play.api.mvc._
import scala.concurrent.duration._
import scala.util.chaining._
import lila.api.Context
import lila.app._
@ -430,10 +431,7 @@ final class Study(
CanViewResult(study) {
lila.mon.export.pgn.study.increment()
Ok.chunked(env.study.pgnDump(study, requestPgnFlags(ctx.req)))
.withHeaders(
noProxyBufferHeader,
CONTENT_DISPOSITION -> s"attachment; filename=${env.study.pgnDump filename study}.pgn"
)
.pipe(asAttachmentStream(s"${env.study.pgnDump filename study}.pgn"))
.as(pgnContentType)
.fuccess
}
@ -448,9 +446,7 @@ final class Study(
CanViewResult(study) {
lila.mon.export.pgn.studyChapter.increment()
Ok(env.study.pgnDump.ofChapter(study, requestPgnFlags(ctx.req))(chapter).toString)
.withHeaders(
CONTENT_DISPOSITION -> s"attachment; filename=${env.study.pgnDump.filename(study, chapter)}.pgn"
)
.pipe(asAttachment(s"${env.study.pgnDump.filename(study, chapter)}.pgn"))
.as(pgnContentType)
.fuccess
}
@ -483,10 +479,7 @@ final class Study(
.throttle(30, 1 second)
} { source =>
Ok.chunked(source)
.withHeaders(
noProxyBufferHeader,
CONTENT_DISPOSITION -> s"attachment; filename=${username}-${if (isMe) "all" else "public"}-studies.pgn"
)
.pipe(asAttachmentStream(s"${username}-${if (isMe) "all" else "public"}-studies.pgn"))
.as(pgnContentType)
}
.fuccess
@ -506,10 +499,8 @@ final class Study(
CanViewResult(study) {
env.study.gifExport.ofChapter(chapter) map { stream =>
Ok.chunked(stream)
.withHeaders(
noProxyBufferHeader,
CONTENT_DISPOSITION -> s"attachment; filename=${env.study.pgnDump.filename(study, chapter)}.gif"
) as "image/gif"
.pipe(asAttachmentStream(s"${env.study.pgnDump.filename(study, chapter)}.gif"))
.as("image/gif")
}
}
}

View File

@ -3,6 +3,7 @@ package controllers
import play.api.libs.json.Json
import play.api.mvc._
import scala.concurrent.duration._
import scala.util.chaining._
import views._
import lila.api.Context
@ -259,7 +260,7 @@ final class Swiss(
case None => NotFound("Tournament not found")
case Some(swiss) =>
Ok.chunked(env.swiss.trf(swiss, sorted = true) intersperse "\n")
.withHeaders(CONTENT_DISPOSITION -> s"attachment; filename=lichess_swiss_$id.trf")
.pipe(asAttachmentStream(env.api.gameApiV2.filename(swiss, "trf")))
}
}