Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/router/apiHandler.go

204 lignes
4,8 Kio
Go
Brut Vue normale Historique

package router
2017-05-08 11:18:49 +02:00
import (
"encoding/json"
"fmt"
"html"
2017-05-08 11:18:49 +02:00
"net/http"
"strconv"
2017-05-08 11:18:49 +02:00
"time"
2017-05-09 19:58:35 +02:00
"github.com/ewhal/nyaa/config"
"github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model"
2017-05-10 20:42:20 +02:00
"github.com/ewhal/nyaa/service"
2017-05-09 18:37:40 +02:00
"github.com/ewhal/nyaa/service/api"
"github.com/ewhal/nyaa/service/torrent"
"github.com/ewhal/nyaa/util"
"github.com/ewhal/nyaa/util/log"
2017-05-08 11:18:49 +02:00
"github.com/gorilla/mux"
)
func ApiHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
page := vars["page"]
2017-05-10 20:42:20 +02:00
whereParams := serviceBase.WhereParams{}
2017-05-09 18:54:12 +02:00
req := apiService.TorrentsRequest{}
2017-05-09 17:09:45 +02:00
contentType := r.Header.Get("Content-Type")
if contentType == "application/json" {
d := json.NewDecoder(r.Body)
2017-05-09 18:37:40 +02:00
if err := d.Decode(&req); err != nil {
2017-05-09 17:09:45 +02:00
util.SendError(w, err, 502)
}
2017-05-09 18:54:12 +02:00
if req.MaxPerPage == 0 {
req.MaxPerPage = 50
}
if req.Page == 0 {
req.Page = 1
}
2017-05-09 18:37:40 +02:00
whereParams = req.ToParams()
2017-05-09 18:54:12 +02:00
} else {
var err error
maxString := r.URL.Query().Get("max")
if maxString != "" {
req.MaxPerPage, err = strconv.Atoi(maxString)
if !log.CheckError(err) {
req.MaxPerPage = 50 // default Value maxPerPage
}
}
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
}
2017-05-09 18:54:12 +02:00
}
2017-05-09 17:09:45 +02:00
}
2017-05-09 18:54:12 +02:00
torrents, nbTorrents, err := torrentService.GetTorrents(whereParams, req.MaxPerPage, req.MaxPerPage*(req.Page-1))
if err != nil {
util.SendError(w, err, 400)
return
}
2017-05-08 11:18:49 +02:00
b := model.ApiResultJSON{
2017-05-09 01:56:57 +02:00
Torrents: model.TorrentsToJSON(torrents),
}
2017-05-09 18:54:12 +02:00
b.QueryRecordCount = req.MaxPerPage
b.TotalRecordCount = nbTorrents
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func ApiViewHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
torrent, err := torrentService.GetTorrentById(id)
b := torrent.ToJSON()
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
2017-05-08 11:18:49 +02:00
func ApiUploadHandler(w http.ResponseWriter, r *http.Request) {
if config.UploadsDisabled {
2017-05-08 11:18:49 +02:00
http.Error(w, "Error uploads are disabled", http.StatusInternalServerError)
return
}
2017-05-09 20:54:50 +02:00
contentType := r.Header.Get("Content-Type")
2017-05-09 19:37:39 +02:00
if contentType == "application/json" {
2017-05-09 19:58:35 +02:00
token := r.Header.Get("Authorization")
user := model.User{}
db.ORM.Where("api_token = ?", token).First(&user) //i don't like this
if user.ID == 0 {
2017-05-09 22:24:32 +02:00
http.Error(w, apiService.ErrApiKey.Error(), http.StatusForbidden)
2017-05-09 19:58:35 +02:00
return
}
2017-05-08 11:18:49 +02:00
2017-05-09 19:37:39 +02:00
defer r.Body.Close()
2017-05-08 11:18:49 +02:00
//verify token
//token := r.Header.Get("Authorization")
2017-05-09 20:54:50 +02:00
upload := apiService.TorrentRequest{}
2017-05-09 19:37:39 +02:00
d := json.NewDecoder(r.Body)
if err := d.Decode(&upload); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2017-05-09 20:54:50 +02:00
err, code := upload.ValidateUpload()
2017-05-09 19:37:39 +02:00
if err != nil {
2017-05-09 19:58:35 +02:00
http.Error(w, err.Error(), code)
2017-05-09 19:37:39 +02:00
return
}
2017-05-08 11:18:49 +02:00
torrent := model.Torrent{
Name: upload.Name,
Category: upload.Category,
SubCategory: upload.SubCategory,
Status: 1,
Hash: upload.Hash,
Date: time.Now(),
Filesize: 0, //?
Description: upload.Description,
UploaderID: user.ID,
Uploader: &user,
2017-05-09 19:37:39 +02:00
}
db.ORM.Create(&torrent)
2017-05-09 23:21:15 +02:00
if err != nil {
util.SendError(w, err, 500)
return
}
2017-05-09 19:37:39 +02:00
fmt.Printf("%+v\n", torrent)
}
2017-05-08 11:18:49 +02:00
}
2017-05-09 19:37:39 +02:00
2017-05-09 19:58:35 +02:00
func ApiUpdateHandler(w http.ResponseWriter, r *http.Request) {
if config.UploadsDisabled {
2017-05-09 20:54:50 +02:00
http.Error(w, "Error uploads are disabled", http.StatusInternalServerError)
return
}
contentType := r.Header.Get("Content-Type")
if contentType == "application/json" {
token := r.Header.Get("Authorization")
user := model.User{}
db.ORM.Where("api_token = ?", token).First(&user) //i don't like this
if user.ID == 0 {
2017-05-09 22:24:32 +02:00
http.Error(w, apiService.ErrApiKey.Error(), http.StatusForbidden)
2017-05-09 20:54:50 +02:00
return
}
defer r.Body.Close()
update := apiService.UpdateRequest{}
d := json.NewDecoder(r.Body)
if err := d.Decode(&update); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
id := update.ID
torrent := model.Torrent{}
2017-05-09 20:54:50 +02:00
db.ORM.Where("torrent_id = ?", id).First(&torrent)
if torrent.ID == 0 {
2017-05-09 22:24:32 +02:00
http.Error(w, apiService.ErrTorrentId.Error(), http.StatusBadRequest)
2017-05-09 20:54:50 +02:00
return
}
if torrent.UploaderID != 0 && torrent.UploaderID != user.ID { //&& user.Status != mod
2017-05-09 22:24:32 +02:00
http.Error(w, apiService.ErrRights.Error(), http.StatusForbidden)
2017-05-09 20:54:50 +02:00
return
}
2017-05-09 22:24:32 +02:00
2017-05-09 20:54:50 +02:00
err, code := update.Update.ValidateUpdate()
if err != nil {
http.Error(w, err.Error(), code)
return
}
update.UpdateTorrent(&torrent)
2017-05-09 19:58:35 +02:00
2017-05-09 20:54:50 +02:00
db.ORM.Save(&torrent)
2017-05-09 23:21:15 +02:00
if err != nil {
util.SendError(w, err, 500)
return
}
2017-05-09 20:54:50 +02:00
fmt.Printf("%+v\n", torrent)
}
2017-05-09 19:58:35 +02:00
}