Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

make rss fast again :^)

Cette révision appartient à :
Jeff 2017-05-09 11:07:42 -04:00
Parent 6a49afab5f
révision 18d6ad3992
4 fichiers modifiés avec 32 ajouts et 6 suppressions

Voir le fichier

@ -3,6 +3,7 @@ package main
import ( import (
"bufio" "bufio"
"flag" "flag"
"github.com/nicksnyder/go-i18n/i18n" "github.com/nicksnyder/go-i18n/i18n"
"github.com/ewhal/nyaa/config" "github.com/ewhal/nyaa/config"

Voir le fichier

@ -13,7 +13,7 @@ import (
func RssHandler(w http.ResponseWriter, r *http.Request) { func RssHandler(w http.ResponseWriter, r *http.Request) {
_, torrents, _, err := search.SearchByQuery(r, 1) _, torrents, err := search.SearchByQueryNoCount(r, 1)
if err != nil { if err != nil {
util.SendError(w, err, 400) util.SendError(w, err, 400)
return return

Voir le fichier

@ -67,7 +67,17 @@ func GetTorrentById(id string) (model.Torrents, error) {
return torrent, nil return torrent, nil
} }
func GetTorrentsOrderByNoCount(parameters *WhereParams, orderBy string, limit int, offset int) (torrents []model.Torrents, err error) {
torrents, _, err = getTorrentsOrderBy(parameters, orderBy, limit, offset, false)
return
}
func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int) (torrents []model.Torrents, count int, err error) { func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int) (torrents []model.Torrents, count int, err error) {
torrents, count, err = getTorrentsOrderBy(parameters, orderBy, limit, offset, true)
return
}
func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int, countAll bool) (torrents []model.Torrents, count int, err error) {
var conditionArray []string var conditionArray []string
if strings.HasPrefix(orderBy, "filesize") { if strings.HasPrefix(orderBy, "filesize") {
// torrents w/ NULL filesize fuck up the sorting on postgres // torrents w/ NULL filesize fuck up the sorting on postgres
@ -81,11 +91,12 @@ func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs
params = parameters.Params params = parameters.Params
} }
conditions := strings.Join(conditionArray, " AND ") conditions := strings.Join(conditionArray, " AND ")
if countAll {
err = db.ORM.Model(&torrents).Where(conditions, params...).Count(&count).Error err = db.ORM.Model(&torrents).Where(conditions, params...).Count(&count).Error
if err != nil { if err != nil {
return return
} }
}
// TODO: Vulnerable to injections. Use query builder. // TODO: Vulnerable to injections. Use query builder.
// build custom db query for performance reasons // build custom db query for performance reasons

Voir le fichier

@ -57,6 +57,16 @@ type SearchParam struct {
} }
func SearchByQuery(r *http.Request, pagenum int) (search SearchParam, tor []model.Torrents, count int, err error) { func SearchByQuery(r *http.Request, pagenum int) (search SearchParam, tor []model.Torrents, count int, err error) {
search, tor, count, err = searchByQuery(r, pagenum, true)
return
}
func SearchByQueryNoCount(r *http.Request, pagenum int) (search SearchParam, tor []model.Torrents, err error) {
search, tor, _, err = searchByQuery(r, pagenum, false)
return
}
func searchByQuery(r *http.Request, pagenum int, countAll bool) (search SearchParam, tor []model.Torrents, count int, err error) {
max, err := strconv.ParseUint(r.URL.Query().Get("max"), 10, 32) max, err := strconv.ParseUint(r.URL.Query().Get("max"), 10, 32)
if err != nil { if err != nil {
err = nil err = nil
@ -180,6 +190,10 @@ func SearchByQuery(r *http.Request, pagenum int) (search SearchParam, tor []mode
parameters.Conditions = strings.Join(conditions[:], " AND ") parameters.Conditions = strings.Join(conditions[:], " AND ")
log.Infof("SQL query is :: %s\n", parameters.Conditions) log.Infof("SQL query is :: %s\n", parameters.Conditions)
if countAll {
tor, count, err = torrentService.GetTorrentsOrderBy(&parameters, order_by, int(search.Max), int(search.Max)*(pagenum-1)) tor, count, err = torrentService.GetTorrentsOrderBy(&parameters, order_by, int(search.Max), int(search.Max)*(pagenum-1))
} else {
tor, err = torrentService.GetTorrentsOrderByNoCount(&parameters, order_by, int(search.Max), int(search.Max)*(pagenum-1))
}
return return
} }