Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Cette révision appartient à :
ayame-git 2017-05-09 21:54:50 +03:00
Parent f9f2214acf
révision 59ed2ec4c6
3 fichiers modifiés avec 115 ajouts et 8 suppressions

Voir le fichier

@ -94,10 +94,9 @@ func ApiUploadHandler(w http.ResponseWriter, r *http.Request) {
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
@ -108,13 +107,13 @@ func ApiUploadHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
upload := apiService.UploadRequest{}
upload := apiService.TorrentRequest{}
d := json.NewDecoder(r.Body)
if err := d.Decode(&upload); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err, code := upload.Validate()
err, code := upload.ValidateUpload()
if err != nil {
http.Error(w, err.Error(), code)
return
@ -139,5 +138,50 @@ func ApiUploadHandler(w http.ResponseWriter, r *http.Request) {
}
func ApiUpdateHandler(w http.ResponseWriter, r *http.Request) {
if config.UploadsDisabled == 1 {
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 {
http.Error(w, "incorrect api key", http.StatusForbidden)
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.Torrents{}
db.ORM.Where("torrent_id = ?", id).First(&torrent)
if torrent.Id == 0 {
http.Error(w, "incorrect id", http.StatusBadRequest)
return
}
if torrent.UploaderId != 0 && torrent.UploaderId != user.Id { //&& user is not mod
http.Error(w, "not enough rights to edit torrent", http.StatusForbidden)
return
}
err, code := update.Update.ValidateUpdate()
if err != nil {
http.Error(w, err.Error(), code)
return
}
fmt.Printf("%+v\n", torrent)
update.UpdateTorrent(&torrent)
db.ORM.Save(&torrent)
fmt.Printf("%+v\n", torrent)
}
}

Voir le fichier

@ -24,7 +24,7 @@ func init() {
gzipAPIHandler := handlers.CompressHandler(http.HandlerFunc(ApiHandler))
gzipAPIViewHandler := handlers.CompressHandler(http.HandlerFunc(ApiViewHandler))
gzipAPIUploadHandler := handlers.CompressHandler(http.HandlerFunc(ApiUploadHandler))
//gzipAPIUpdateHandler := handlers.CompressHandler(http.HandlerFunc(ApiUpdateHandler))
gzipAPIUpdateHandler := handlers.CompressHandler(http.HandlerFunc(ApiUpdateHandler))
gzipFaqHandler := handlers.CompressHandler(http.HandlerFunc(FaqHandler))
gzipRssHandler := handlers.CompressHandler(http.HandlerFunc(RssHandler))
gzipViewHandler := handlers.CompressHandler(http.HandlerFunc(ViewHandler))
@ -52,7 +52,7 @@ func init() {
Router.Handle("/api/{page:[0-9]*}", gzipAPIHandler).Methods("GET")
Router.Handle("/api/view/{id}", gzipAPIViewHandler).Methods("GET")
Router.Handle("/api/upload", gzipAPIUploadHandler).Methods("POST")
//Router.Handle("/api/update", gzipAPIUpdateHandler).Methods("PUT")
Router.Handle("/api/update", gzipAPIUpdateHandler).Methods("PUT")
Router.Handle("/faq", gzipFaqHandler).Name("faq")
Router.Handle("/feed", gzipRssHandler).Name("feed")
Router.Handle("/view/{id}", gzipViewHandler).Methods("GET").Name("view_torrent")

Voir le fichier

@ -8,6 +8,7 @@ import (
"regexp"
"strings"
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/torrent"
)
@ -26,7 +27,7 @@ type TorrentsRequest struct {
}
//accept torrent files?
type UploadRequest struct {
type TorrentRequest struct {
Name string `json:"name"`
Hash string `json:"hash"`
Magnet string `json:"magnet"`
@ -35,6 +36,11 @@ type UploadRequest struct {
Description string `json:"description"`
}
type UpdateRequest struct {
Id int `json:"id"`
Update TorrentRequest `json:"update"`
}
func (r *TorrentsRequest) ToParams() torrentService.WhereParams {
res := torrentService.WhereParams{}
conditions := ""
@ -60,7 +66,9 @@ var ErrSubCategory = errors.New("this sub category doesn't exist")
var ErrMagnet = errors.New("incorrect magnet")
var ErrHash = errors.New("incorrect hash")
func (r *UploadRequest) Validate() (error, int) {
//rewrite validators!!!
func (r *TorrentRequest) ValidateUpload() (error, int) {
if len(r.Name) < 100 {
return ErrShortName, http.StatusNotAcceptable
}
@ -93,3 +101,58 @@ func (r *UploadRequest) Validate() (error, int) {
return nil, http.StatusOK
}
func (r *TorrentRequest) ValidateUpdate() (error, int) {
if len(r.Name) < 100 && len(r.Name) != 0 {
return ErrShortName, http.StatusNotAcceptable
}
/*if r.Category == 0 {
return ErrCategory, http.StatusNotAcceptable
}
if r.SubCategory == 0 {
return ErrSubCategory, http.StatusNotAcceptable
}*/
if r.Magnet != "" || r.Hash != "" {
if r.Hash == "" {
magnetUrl, err := url.Parse(string(r.Magnet)) //?
if err != nil {
return err, http.StatusInternalServerError
}
exactTopic := magnetUrl.Query().Get("xt")
if !strings.HasPrefix(exactTopic, "urn:btih:") {
return ErrMagnet, http.StatusNotAcceptable
}
r.Hash = strings.ToUpper(strings.TrimPrefix(exactTopic, "urn:btih:"))
}
matched, err := regexp.MatchString("^[0-9A-F]{40}$", r.Hash)
if err != nil {
return err, http.StatusInternalServerError
}
if !matched {
return ErrHash, http.StatusNotAcceptable
}
}
return nil, http.StatusOK
}
//rewrite with reflect ?
func (r *UpdateRequest) UpdateTorrent(t *model.Torrents) {
if r.Update.Name != "" {
t.Name = r.Update.Name
}
if r.Update.Hash != "" {
t.Hash = r.Update.Hash
}
if r.Update.Category != 0 {
t.Category = r.Update.Category
}
if r.Update.SubCategory != 0 {
t.Sub_Category = r.Update.SubCategory
}
if r.Update.Description != "" {
t.Description = r.Update.Description
}
}