forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search.scala
54 lines (48 loc) · 1.64 KB
/
Search.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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 => data.nonEmptyQuery ?? { 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 => data.nonEmptyQuery ?? { 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
}