Fix torznab to /api also
Torznab can still be accessed to /feed/torznab and also to /api?t=something
Cette révision appartient à :
Parent
abcdfd426b
révision
8bddc86843
|
@ -27,69 +27,74 @@ import (
|
||||||
|
|
||||||
// APIHandler : Controller for api request on torrent list
|
// APIHandler : Controller for api request on torrent list
|
||||||
func APIHandler(w http.ResponseWriter, r *http.Request) {
|
func APIHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
t := r.URL.Query().Get("t")
|
||||||
page := vars["page"]
|
if t != "" {
|
||||||
whereParams := serviceBase.WhereParams{}
|
RSSTorznabHandler(w, r)
|
||||||
req := apiService.TorrentsRequest{}
|
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
contentType := r.Header.Get("Content-Type")
|
|
||||||
if contentType == "application/json" {
|
|
||||||
d := json.NewDecoder(r.Body)
|
|
||||||
if err := d.Decode(&req); err != nil {
|
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
}
|
|
||||||
|
|
||||||
if req.MaxPerPage == 0 {
|
|
||||||
req.MaxPerPage = config.Conf.Navigation.TorrentsPerPage
|
|
||||||
}
|
|
||||||
if req.Page <= 0 {
|
|
||||||
req.Page = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
whereParams = req.ToParams()
|
|
||||||
} else {
|
} else {
|
||||||
var err error
|
vars := mux.Vars(r)
|
||||||
maxString := r.URL.Query().Get("max")
|
page := vars["page"]
|
||||||
if maxString != "" {
|
whereParams := serviceBase.WhereParams{}
|
||||||
req.MaxPerPage, err = strconv.Atoi(maxString)
|
req := apiService.TorrentsRequest{}
|
||||||
if !log.CheckError(err) {
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
contentType := r.Header.Get("Content-Type")
|
||||||
|
if contentType == "application/json" {
|
||||||
|
d := json.NewDecoder(r.Body)
|
||||||
|
if err := d.Decode(&req); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.MaxPerPage == 0 {
|
||||||
req.MaxPerPage = config.Conf.Navigation.TorrentsPerPage
|
req.MaxPerPage = config.Conf.Navigation.TorrentsPerPage
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
req.MaxPerPage = config.Conf.Navigation.TorrentsPerPage
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Page = 1
|
|
||||||
if page != "" {
|
|
||||||
req.Page, err = strconv.Atoi(html.EscapeString(page))
|
|
||||||
if !log.CheckError(err) {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if req.Page <= 0 {
|
if req.Page <= 0 {
|
||||||
NotFoundHandler(w, r)
|
req.Page = 1
|
||||||
return
|
}
|
||||||
|
|
||||||
|
whereParams = req.ToParams()
|
||||||
|
} else {
|
||||||
|
var err error
|
||||||
|
maxString := r.URL.Query().Get("max")
|
||||||
|
if maxString != "" {
|
||||||
|
req.MaxPerPage, err = strconv.Atoi(maxString)
|
||||||
|
if !log.CheckError(err) {
|
||||||
|
req.MaxPerPage = config.Conf.Navigation.TorrentsPerPage
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
req.MaxPerPage = config.Conf.Navigation.TorrentsPerPage
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Page = 1
|
||||||
|
if page != "" {
|
||||||
|
req.Page, err = strconv.Atoi(html.EscapeString(page))
|
||||||
|
if !log.CheckError(err) {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if req.Page <= 0 {
|
||||||
|
NotFoundHandler(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
torrents, nbTorrents, err := torrentService.GetTorrents(whereParams, req.MaxPerPage, req.MaxPerPage*(req.Page-1))
|
torrents, nbTorrents, err := torrentService.GetTorrents(whereParams, req.MaxPerPage, req.MaxPerPage*(req.Page-1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
b := model.APIResultJSON{
|
b := model.APIResultJSON{
|
||||||
Torrents: model.TorrentsToJSON(torrents),
|
Torrents: model.TorrentsToJSON(torrents),
|
||||||
}
|
}
|
||||||
b.QueryRecordCount = req.MaxPerPage
|
b.QueryRecordCount = req.MaxPerPage
|
||||||
b.TotalRecordCount = nbTorrents
|
b.TotalRecordCount = nbTorrents
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
err = json.NewEncoder(w).Encode(b)
|
err = json.NewEncoder(w).Encode(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Référencer dans un nouveau ticket