update
Cette révision appartient à :
Parent
f9f2214acf
révision
59ed2ec4c6
3 fichiers modifiés avec 115 ajouts et 8 suppressions
|
@ -94,10 +94,9 @@ func ApiUploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, "Error uploads are disabled", http.StatusInternalServerError)
|
http.Error(w, "Error uploads are disabled", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
contentType := r.Header.Get("Content-Type")
|
contentType := r.Header.Get("Content-Type")
|
||||||
|
|
||||||
if contentType == "application/json" {
|
if contentType == "application/json" {
|
||||||
|
|
||||||
token := r.Header.Get("Authorization")
|
token := r.Header.Get("Authorization")
|
||||||
user := model.User{}
|
user := model.User{}
|
||||||
db.ORM.Where("api_token = ?", token).First(&user) //i don't like this
|
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()
|
defer r.Body.Close()
|
||||||
|
|
||||||
upload := apiService.UploadRequest{}
|
upload := apiService.TorrentRequest{}
|
||||||
d := json.NewDecoder(r.Body)
|
d := json.NewDecoder(r.Body)
|
||||||
if err := d.Decode(&upload); err != nil {
|
if err := d.Decode(&upload); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err, code := upload.Validate()
|
err, code := upload.ValidateUpload()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), code)
|
http.Error(w, err.Error(), code)
|
||||||
return
|
return
|
||||||
|
@ -139,5 +138,50 @@ func ApiUploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ApiUpdateHandler(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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ func init() {
|
||||||
gzipAPIHandler := handlers.CompressHandler(http.HandlerFunc(ApiHandler))
|
gzipAPIHandler := handlers.CompressHandler(http.HandlerFunc(ApiHandler))
|
||||||
gzipAPIViewHandler := handlers.CompressHandler(http.HandlerFunc(ApiViewHandler))
|
gzipAPIViewHandler := handlers.CompressHandler(http.HandlerFunc(ApiViewHandler))
|
||||||
gzipAPIUploadHandler := handlers.CompressHandler(http.HandlerFunc(ApiUploadHandler))
|
gzipAPIUploadHandler := handlers.CompressHandler(http.HandlerFunc(ApiUploadHandler))
|
||||||
//gzipAPIUpdateHandler := handlers.CompressHandler(http.HandlerFunc(ApiUpdateHandler))
|
gzipAPIUpdateHandler := handlers.CompressHandler(http.HandlerFunc(ApiUpdateHandler))
|
||||||
gzipFaqHandler := handlers.CompressHandler(http.HandlerFunc(FaqHandler))
|
gzipFaqHandler := handlers.CompressHandler(http.HandlerFunc(FaqHandler))
|
||||||
gzipRssHandler := handlers.CompressHandler(http.HandlerFunc(RssHandler))
|
gzipRssHandler := handlers.CompressHandler(http.HandlerFunc(RssHandler))
|
||||||
gzipViewHandler := handlers.CompressHandler(http.HandlerFunc(ViewHandler))
|
gzipViewHandler := handlers.CompressHandler(http.HandlerFunc(ViewHandler))
|
||||||
|
@ -52,7 +52,7 @@ func init() {
|
||||||
Router.Handle("/api/{page:[0-9]*}", gzipAPIHandler).Methods("GET")
|
Router.Handle("/api/{page:[0-9]*}", gzipAPIHandler).Methods("GET")
|
||||||
Router.Handle("/api/view/{id}", gzipAPIViewHandler).Methods("GET")
|
Router.Handle("/api/view/{id}", gzipAPIViewHandler).Methods("GET")
|
||||||
Router.Handle("/api/upload", gzipAPIUploadHandler).Methods("POST")
|
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("/faq", gzipFaqHandler).Name("faq")
|
||||||
Router.Handle("/feed", gzipRssHandler).Name("feed")
|
Router.Handle("/feed", gzipRssHandler).Name("feed")
|
||||||
Router.Handle("/view/{id}", gzipViewHandler).Methods("GET").Name("view_torrent")
|
Router.Handle("/view/{id}", gzipViewHandler).Methods("GET").Name("view_torrent")
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/ewhal/nyaa/model"
|
||||||
"github.com/ewhal/nyaa/service/torrent"
|
"github.com/ewhal/nyaa/service/torrent"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -26,7 +27,7 @@ type TorrentsRequest struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
//accept torrent files?
|
//accept torrent files?
|
||||||
type UploadRequest struct {
|
type TorrentRequest struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Hash string `json:"hash"`
|
Hash string `json:"hash"`
|
||||||
Magnet string `json:"magnet"`
|
Magnet string `json:"magnet"`
|
||||||
|
@ -35,6 +36,11 @@ type UploadRequest struct {
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UpdateRequest struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
Update TorrentRequest `json:"update"`
|
||||||
|
}
|
||||||
|
|
||||||
func (r *TorrentsRequest) ToParams() torrentService.WhereParams {
|
func (r *TorrentsRequest) ToParams() torrentService.WhereParams {
|
||||||
res := torrentService.WhereParams{}
|
res := torrentService.WhereParams{}
|
||||||
conditions := ""
|
conditions := ""
|
||||||
|
@ -60,7 +66,9 @@ var ErrSubCategory = errors.New("this sub category doesn't exist")
|
||||||
var ErrMagnet = errors.New("incorrect magnet")
|
var ErrMagnet = errors.New("incorrect magnet")
|
||||||
var ErrHash = errors.New("incorrect hash")
|
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 {
|
if len(r.Name) < 100 {
|
||||||
return ErrShortName, http.StatusNotAcceptable
|
return ErrShortName, http.StatusNotAcceptable
|
||||||
}
|
}
|
||||||
|
@ -93,3 +101,58 @@ func (r *UploadRequest) Validate() (error, int) {
|
||||||
|
|
||||||
return nil, http.StatusOK
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Référencer dans un nouveau ticket