révision
d71489ae02
5 fichiers modifiés avec 45 ajouts et 21 suppressions
|
@ -1,17 +1,17 @@
|
||||||
package router
|
package router
|
||||||
|
|
||||||
import(
|
import (
|
||||||
"time"
|
|
||||||
"net/http"
|
|
||||||
"github.com/gorilla/feeds"
|
|
||||||
"github.com/ewhal/nyaa/config"
|
"github.com/ewhal/nyaa/config"
|
||||||
"github.com/ewhal/nyaa/util/search"
|
"github.com/ewhal/nyaa/util/search"
|
||||||
|
"github.com/gorilla/feeds"
|
||||||
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RssHandler(w http.ResponseWriter, r *http.Request) {
|
func RssHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
_, torrents, _ := search.SearchByQuery( r, 1 )
|
_, torrents := search.SearchByQueryNoCount(r, 1)
|
||||||
created_as_time := time.Now()
|
created_as_time := time.Now()
|
||||||
|
|
||||||
if len(torrents) > 0 {
|
if len(torrents) > 0 {
|
||||||
|
|
|
@ -23,7 +23,7 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
search_param, torrents, nbTorrents := search.SearchByQuery(r, pagenum)
|
search_param, torrents, nbTorrents := search.SearchByQuery(r, pagenum)
|
||||||
|
|
||||||
for i, _ := range torrents {
|
for i := range torrents {
|
||||||
res := torrents[i].ToJson()
|
res := torrents[i].ToJson()
|
||||||
b = append(b, res)
|
b = append(b, res)
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,22 +53,28 @@ func GetTorrentById(id string) (model.Torrents, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int) ([]model.Torrents, int) {
|
func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int) ([]model.Torrents, int) {
|
||||||
|
return getTorrentsOrderBy(parameters, orderBy, limit, offset, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTorrentsOrderByNoCount(parameters *WhereParams, orderBy string, limit int, offset int) (torrents []model.Torrents) {
|
||||||
|
torrents, _ = getTorrentsOrderBy(parameters, orderBy, limit, offset, false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int, countAll bool) ([]model.Torrents, int) {
|
||||||
var torrents []model.Torrents
|
var torrents []model.Torrents
|
||||||
var dbQuery *gorm.DB
|
var dbQuery *gorm.DB
|
||||||
var count int
|
var count int
|
||||||
conditions := "torrent_hash IS NOT NULL" // filter out broken entries
|
|
||||||
if strings.HasPrefix(orderBy, "filesize") {
|
conditions := "torrent_hash IS NOT NULL AND filesize > 0" //filter out broken entries
|
||||||
// torrents w/ NULL filesize fuck up the sorting on postgres
|
|
||||||
// TODO: fix this properly
|
|
||||||
conditions += " AND filesize IS NOT NULL"
|
|
||||||
}
|
|
||||||
|
|
||||||
var params []interface{}
|
var params []interface{}
|
||||||
if parameters != nil { // if there is where parameters
|
if parameters != nil { // if there is where parameters
|
||||||
conditions += " AND " + parameters.Conditions
|
conditions += " AND " + parameters.Conditions
|
||||||
params = parameters.Params
|
params = parameters.Params
|
||||||
}
|
}
|
||||||
db.ORM.Model(&torrents).Where(conditions, params...).Count(&count)
|
if countAll {
|
||||||
|
db.ORM.Model(&torrents).Where(conditions, params...).Count(&count)
|
||||||
|
}
|
||||||
dbQuery = db.ORM.Model(&torrents).Where(conditions, params...)
|
dbQuery = db.ORM.Model(&torrents).Where(conditions, params...)
|
||||||
|
|
||||||
if orderBy == "" { // default OrderBy
|
if orderBy == "" { // default OrderBy
|
||||||
|
@ -86,7 +92,7 @@ func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs
|
||||||
* Get Torrents with where parameters and limits, order by default
|
* Get Torrents with where parameters and limits, order by default
|
||||||
*/
|
*/
|
||||||
func GetTorrents(parameters WhereParams, limit int, offset int) ([]model.Torrents, int) {
|
func GetTorrents(parameters WhereParams, limit int, offset int) ([]model.Torrents, int) {
|
||||||
return GetTorrentsOrderBy(¶meters, "", limit, offset)
|
return getTorrentsOrderBy(¶meters, "", limit, offset, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get Torrents with where parameters but no limit and order by default (get all the torrents corresponding in the db)
|
/* Get Torrents with where parameters but no limit and order by default (get all the torrents corresponding in the db)
|
||||||
|
@ -107,6 +113,11 @@ func GetAllTorrents(limit int, offset int) ([]model.Torrents, int) {
|
||||||
return GetTorrentsOrderBy(nil, "", limit, offset)
|
return GetTorrentsOrderBy(nil, "", limit, offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetAllTorrentsNoCouting(limit int, offset int) (torrents []model.Torrents) {
|
||||||
|
torrents, _ = getTorrentsOrderBy(nil, "", limit, offset, false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func GetAllTorrentsDB() ([]model.Torrents, int) {
|
func GetAllTorrentsDB() ([]model.Torrents, int) {
|
||||||
return GetTorrentsOrderBy(nil, "", 0, 0)
|
return GetTorrentsOrderBy(nil, "", 0, 0)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,6 @@
|
||||||
<label for="solution">Captcha</label>
|
<label for="solution">Captcha</label>
|
||||||
<input type="text" name="captchaID" value="{{.CaptchaID}}" hidden>
|
<input type="text" name="captchaID" value="{{.CaptchaID}}" hidden>
|
||||||
<img src="/captcha/{{.CaptchaID}}.png">
|
<img src="/captcha/{{.CaptchaID}}.png">
|
||||||
<input type="number" name="solution" class="form-control" placeholder="Captcha" required>
|
<input type="text" name="solution" class="form-control" placeholder="Captcha" required>
|
||||||
</div>
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
@ -31,6 +31,16 @@ func Init(backend string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func SearchByQuery(r *http.Request, pagenum int) (SearchParam, []model.Torrents, int) {
|
func SearchByQuery(r *http.Request, pagenum int) (SearchParam, []model.Torrents, int) {
|
||||||
|
return searchByQuery(r, pagenum, true)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func SearchByQueryNoCount(r *http.Request, pagenum int) (param SearchParam, torrents []model.Torrents) {
|
||||||
|
param, torrents, _ = searchByQuery(r, pagenum, false)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func searchByQuery(r *http.Request, pagenum int, count bool) (search_param SearchParam, torrents []model.Torrents, n int) {
|
||||||
maxPerPage, errConv := strconv.Atoi(r.URL.Query().Get("max"))
|
maxPerPage, errConv := strconv.Atoi(r.URL.Query().Get("max"))
|
||||||
if errConv != nil {
|
if errConv != nil {
|
||||||
maxPerPage = 50 // default Value maxPerPage
|
maxPerPage = 50 // default Value maxPerPage
|
||||||
|
@ -39,7 +49,6 @@ func SearchByQuery(r *http.Request, pagenum int) (SearchParam, []model.Torrents,
|
||||||
if maxPerPage > 300 {
|
if maxPerPage > 300 {
|
||||||
maxPerPage = 300
|
maxPerPage = 300
|
||||||
}
|
}
|
||||||
search_param := SearchParam{}
|
|
||||||
search_param.Max = maxPerPage
|
search_param.Max = maxPerPage
|
||||||
search_param.Query = r.URL.Query().Get("q")
|
search_param.Query = r.URL.Query().Get("q")
|
||||||
search_param.Category = r.URL.Query().Get("c")
|
search_param.Category = r.URL.Query().Get("c")
|
||||||
|
@ -82,13 +91,17 @@ func SearchByQuery(r *http.Request, pagenum int) (SearchParam, []model.Torrents,
|
||||||
parameters.Params = append(parameters.Params, search_param.Status)
|
parameters.Params = append(parameters.Params, search_param.Status)
|
||||||
}
|
}
|
||||||
searchQuerySplit := strings.Split(search_param.Query, " ")
|
searchQuerySplit := strings.Split(search_param.Query, " ")
|
||||||
for i, _ := range searchQuerySplit {
|
for i := range searchQuerySplit {
|
||||||
conditions = append(conditions, "torrent_name " + search_op + " ?")
|
conditions = append(conditions, "torrent_name LIKE ?")
|
||||||
parameters.Params = append(parameters.Params, "%"+searchQuerySplit[i]+"%")
|
parameters.Params = append(parameters.Params, "%"+searchQuerySplit[i]+"%")
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
torrents, n := torrentService.GetTorrentsOrderBy(¶meters, order_by, maxPerPage, maxPerPage*(pagenum-1))
|
if count {
|
||||||
return search_param, torrents, n
|
torrents, n = torrentService.GetTorrentsOrderBy(¶meters, order_by, maxPerPage, maxPerPage*(pagenum-1))
|
||||||
|
} else {
|
||||||
|
torrents = torrentService.GetTorrentsOrderByNoCount(¶meters, order_by, maxPerPage, maxPerPage*(pagenum-1))
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
Référencer dans un nouveau ticket