upload changes
Cette révision appartient à :
Parent
c9bb89d551
révision
c59e15e610
4 fichiers modifiés avec 99 ajouts et 63 suppressions
6
data.json
Fichier normal
6
data.json
Fichier normal
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"name":"akfdskjfkasdf;dsjaf;sdjkfdsj;kfjsd;kfjsdkfjk;sdjfskdjf;ksdjkkfsdjkfsdjkjfsda;fjsdjfksfdsfhdsjkfskhfkjshfkshkdsjkdhkhdsjadjf;s",
|
||||
"category":3,
|
||||
"sub_category":13,
|
||||
"magnet":"magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a&dn"
|
||||
}
|
|
@ -9,8 +9,8 @@ import (
|
|||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/ewhal/nyaa/config"
|
||||
"github.com/ewhal/nyaa/db"
|
||||
//"github.com/ewhal/nyaa/config"
|
||||
//"github.com/ewhal/nyaa/db"
|
||||
"github.com/ewhal/nyaa/model"
|
||||
"github.com/ewhal/nyaa/service/api"
|
||||
"github.com/ewhal/nyaa/service/torrent"
|
||||
|
@ -76,14 +76,10 @@ func ApiViewHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
vars := mux.Vars(r)
|
||||
id := vars["id"]
|
||||
b := model.ApiResultJson{Torrents: []model.TorrentsJson{}}
|
||||
|
||||
torrent, err := torrentService.GetTorrentById(id)
|
||||
res := torrent.ToJson()
|
||||
b.Torrents = append(b.Torrents, res)
|
||||
b := torrent.ToJson()
|
||||
|
||||
b.QueryRecordCount = 1
|
||||
b.TotalRecordCount = 1
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
err = json.NewEncoder(w).Encode(b)
|
||||
|
||||
|
@ -98,34 +94,43 @@ func ApiUploadHandler(w http.ResponseWriter, r *http.Request) {
|
|||
http.Error(w, "Error uploads are disabled", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
|
||||
defer r.Body.Close()
|
||||
if contentType == "application/json" {
|
||||
//verify token
|
||||
//token := r.Header.Get("Authorization")
|
||||
|
||||
//verify token
|
||||
//token := r.Header.Get("Authorization")
|
||||
defer r.Body.Close()
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
b := model.TorrentsJson{}
|
||||
err := decoder.Decode(&b)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
upload := apiService.UploadRequest{}
|
||||
d := json.NewDecoder(r.Body)
|
||||
if err := d.Decode(&upload); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
err, code := upload.Validate()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), code) //406?
|
||||
return
|
||||
}
|
||||
|
||||
torrent := model.Torrents{
|
||||
Name: upload.Name,
|
||||
Category: upload.Category,
|
||||
Sub_Category: upload.SubCategory,
|
||||
Status: 1,
|
||||
Hash: upload.Hash,
|
||||
Date: time.Now(),
|
||||
Filesize: 0, //?
|
||||
Description: upload.Description,
|
||||
|
||||
//UploadeId:
|
||||
//Uploader:
|
||||
}
|
||||
|
||||
db.ORM.Create(&torrent)
|
||||
fmt.Printf("%+v\n", torrent)
|
||||
}
|
||||
category, sub_category, err := ValidateJson(&b)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError) //406?
|
||||
return
|
||||
}
|
||||
|
||||
torrent := model.Torrents{
|
||||
Name: b.Name,
|
||||
Category: category,
|
||||
Sub_Category: sub_category,
|
||||
Status: 1,
|
||||
Hash: b.Hash,
|
||||
Date: time.Now(),
|
||||
Filesize: 0,
|
||||
Description: string(b.Description)}
|
||||
db.ORM.Create(&torrent)
|
||||
fmt.Printf("%+v\n", torrent)
|
||||
}
|
||||
|
||||
func ApiUpdateHandler(w http.ResponseWriter, r *http.Requet)
|
||||
|
|
|
@ -15,7 +15,6 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/ewhal/nyaa/config"
|
||||
"github.com/ewhal/nyaa/model"
|
||||
"github.com/ewhal/nyaa/service/captcha"
|
||||
"github.com/ewhal/nyaa/util"
|
||||
"github.com/ewhal/nyaa/util/metainfo"
|
||||
|
@ -192,35 +191,6 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func ValidateJson(j *model.TorrentsJson) (int, int, error) {
|
||||
//Name length ?
|
||||
var category, sub_category int
|
||||
|
||||
category, err := strconv.Atoi(j.Category)
|
||||
if err != nil {
|
||||
return category, sub_category, err
|
||||
}
|
||||
sub_category, err = strconv.Atoi(j.Sub_Category)
|
||||
if err != nil {
|
||||
return category, sub_category, err
|
||||
}
|
||||
|
||||
magnetUrl, parseErr := url.Parse(string(j.Magnet)) //?
|
||||
if parseErr != nil {
|
||||
return category, sub_category, metainfo.ErrInvalidTorrentFile
|
||||
}
|
||||
exactTopic := magnetUrl.Query().Get("xt")
|
||||
if !strings.HasPrefix(exactTopic, "urn:btih:") {
|
||||
return category, sub_category, metainfo.ErrInvalidTorrentFile
|
||||
}
|
||||
j.Hash = strings.ToUpper(strings.TrimPrefix(exactTopic, "urn:btih:"))
|
||||
matched, err := regexp.MatchString("^[0-9A-F]{40}$", j.Hash)
|
||||
if err != nil || !matched {
|
||||
return category, sub_category, metainfo.ErrInvalidTorrentFile
|
||||
}
|
||||
return category, sub_category, nil
|
||||
}
|
||||
|
||||
func WriteTorrentToDisk(file multipart.File, name string, fullpath *string) error {
|
||||
file.Seek(0, io.SeekStart)
|
||||
b, err := ioutil.ReadAll(file)
|
||||
|
|
|
@ -1,7 +1,12 @@
|
|||
package apiService
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/ewhal/nyaa/service/torrent"
|
||||
)
|
||||
|
@ -20,6 +25,16 @@ type TorrentsRequest struct {
|
|||
MaxPerPage int `json:"limit"`
|
||||
}
|
||||
|
||||
//accept torrent files?
|
||||
type UploadRequest struct {
|
||||
Name string `json:"name"`
|
||||
Hash string `json:"hash"`
|
||||
Magnet string `json:"magnet"`
|
||||
Category int `json:"category"`
|
||||
SubCategory int `json:"sub_category"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
func (r *TorrentsRequest) ToParams() torrentService.WhereParams {
|
||||
res := torrentService.WhereParams{}
|
||||
conditions := ""
|
||||
|
@ -38,3 +53,43 @@ func (r *TorrentsRequest) ToParams() torrentService.WhereParams {
|
|||
res.Conditions = conditions
|
||||
return res
|
||||
}
|
||||
|
||||
var ErrShortName = errors.New("file name should be at least 100 characters long")
|
||||
var ErrCategory = errors.New("this category doesn't exist")
|
||||
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) {
|
||||
if len(r.Name) < 100 {
|
||||
return ErrShortName, http.StatusNotAcceptable
|
||||
}
|
||||
if r.Category == 0 {
|
||||
return ErrCategory, http.StatusNotAcceptable
|
||||
}
|
||||
if r.SubCategory == 0 {
|
||||
return ErrSubCategory, http.StatusNotAcceptable
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
Référencer dans un nouveau ticket