55 lines
1.7 KiB
Scala
55 lines
1.7 KiB
Scala
package controllers
|
|
|
|
import lila.app._
|
|
import lila.api.Context
|
|
import lila.common.HTTPRequest
|
|
import lila.game.{ Game => GameModel, GameRepo }
|
|
import play.api.http.ContentTypes
|
|
import views._
|
|
|
|
object Search extends LilaController {
|
|
|
|
private def paginator = Env.game.paginator
|
|
private def env = Env.gameSearch
|
|
def searchForm = env.forms.search
|
|
|
|
def index(page: Int) = OpenBody { implicit ctx =>
|
|
NoBot {
|
|
Reasonable(page, 100) {
|
|
implicit def req = ctx.body
|
|
searchForm.bindFromRequest.fold(
|
|
failure => Ok(html.search.index(failure)).fuccess,
|
|
data => env.nonEmptyQuery(data) ?? { query =>
|
|
env.paginator(query, page) map (_.some)
|
|
} map { pager =>
|
|
Ok(html.search.index(searchForm fill data, pager))
|
|
}
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
def export = OpenBody { implicit ctx =>
|
|
NoBot {
|
|
implicit def req = ctx.body
|
|
searchForm.bindFromRequest.fold(
|
|
failure => Ok(html.search.index(failure)).fuccess,
|
|
data => env.nonEmptyQuery(data) ?? { query =>
|
|
env.paginator.ids(query, 5000) map { ids =>
|
|
import org.joda.time.DateTime
|
|
import org.joda.time.format.DateTimeFormat
|
|
val date = (DateTimeFormat forPattern "yyyy-MM-dd") print new DateTime
|
|
Ok.chunked(Env.api.pgnDump exportGamesFromIds ids).withHeaders(
|
|
CONTENT_TYPE -> ContentTypes.TEXT,
|
|
CONTENT_DISPOSITION -> ("attachment; filename=" + s"lichess_search_$date.pgn"))
|
|
}
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
private def NoBot(res: => Fu[play.api.mvc.Result])(implicit ctx: Context) =
|
|
if (HTTPRequest.isBot(ctx.req)) notFound
|
|
else res
|
|
}
|