<commit_before>package controllers

import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import play.api.mvc._

import lila.api.GameApiV2
import lila.app._
import lila.common.config.MaxPerSecond
import lila.common.HTTPRequest
import lila.game.{ Game => GameModel }

final class Game(
    env: Env,
    apiC: => Api
) extends LilaController(env) {

  def delete(gameId: String) = Auth { implicit ctx => me =>
    OptionFuResult(env.game.gameRepo game gameId) { game =>
      if (game.pgnImport.flatMap(_.user) ?? (me.id ==)) {
        env.hub.bookmark ! lila.hub.actorApi.bookmark.Remove(game.id)
        (env.game.gameRepo remove game.id) >>
          (env.analyse.analysisRepo remove game.id) >>
          env.game.cached.clearNbImportedByCache(me.id) inject
          Redirect(routes.User.show(me.username))
      } else
        fuccess {
          Redirect(routes.Round.watcher(game.id, game.firstColor.name))
        }
    }
  }

  def exportOne(id: String) = Open { implicit ctx =>
    OptionFuResult(env.game.gameRepo game id) { game =>
      if (game.playable)
        BadRequest("Only bots can access their games in progress. See https://lichess.org/api#tag/Chess-Bot").fuccess
      else {
        val config = GameApiV2.OneConfig(
          format = if (HTTPRequest acceptsJson ctx.req) GameApiV2.Format.JSON else GameApiV2.Format.PGN,
          imported = getBool("imported"),
          flags = requestPgnFlags(ctx.req, extended = true)
        )
        lila.mon.export.pgn.game.increment()
        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"
            ) as gameContentType(config)
          }
        }<commit_msg>add API headers to /game/export/<commit_after>package controllers

import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import play.api.mvc._

import lila.api.GameApiV2
import lila.app._
import lila.common.config.MaxPerSecond
import lila.common.HTTPRequest
import lila.game.{ Game => GameModel }

final class Game(
    env: Env,
    apiC: => Api
) extends LilaController(env) {

  def delete(gameId: String) = Auth { implicit ctx => me =>
    OptionFuResult(env.game.gameRepo game gameId) { game =>
      if (game.pgnImport.flatMap(_.user) ?? (me.id ==)) {
        env.hub.bookmark ! lila.hub.actorApi.bookmark.Remove(game.id)
        (env.game.gameRepo remove game.id) >>
          (env.analyse.analysisRepo remove game.id) >>
          env.game.cached.clearNbImportedByCache(me.id) inject
          Redirect(routes.User.show(me.username))
      } else
        fuccess {
          Redirect(routes.Round.watcher(game.id, game.firstColor.name))
        }
    }
  }

  def exportOne(id: String) = Open { implicit ctx =>
    OptionFuResult(env.game.gameRepo game id) { game =>
      if (game.playable)
        BadRequest("Only bots can access their games in progress. See https://lichess.org/api#tag/Chess-Bot").fuccess
      else {
        val config = GameApiV2.OneConfig(
          format = if (HTTPRequest acceptsJson ctx.req) GameApiV2.Format.JSON else GameApiV2.Format.PGN,
          imported = getBool("imported"),
          flags = requestPgnFlags(ctx.req, extended = true)
        )
        lila.mon.export.pgn.game.increment()
        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"
            ).withHeaders(
              lila.app.http.ResponseHeaders.headersForApiOrApp(ctx.req): _*
            ) as gameContentType(config)
          }
        }
{'fraction_non_alphanumeric': 0.11892315734448511, 'fraction_numerical': 0.0031364349189754314, 'mean_word_length': 2.662200956937799, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4707951', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1213, 'n_words': 232}