Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Cette révision appartient à :
ayame-git 2017-05-08 12:18:49 +03:00
Parent 581500e100
révision e6d207c653
3 fichiers modifiés avec 89 ajouts et 8 suppressions

Voir le fichier

@ -1,26 +1,38 @@
package router
import(
"github.com/gorilla/mux"
"net/http"
import (
"encoding/json"
"fmt"
"html"
"net/http"
"strconv"
"time"
"github.com/ewhal/nyaa/config"
"github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/service/torrent"
"encoding/json"
"github.com/gorilla/mux"
)
func ApiHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
page := vars["page"]
maxPerPage, errConv := strconv.Atoi(r.URL.Query().Get("max"))
if errConv != nil {
maxPerPage = 50 // default Value maxPerPage
}
nbTorrents := 0
pagenum, _ := strconv.Atoi(html.EscapeString(page))
if pagenum == 0 {
pagenum = 1
}
b := model.ApiResultJson{Torrents: []model.TorrentsJson{}}
maxPerPage := 50
nbTorrents := 0
torrents, nbTorrents := torrentService.GetAllTorrents(maxPerPage, maxPerPage*(pagenum-1))
for i, _ := range torrents {
res := torrents[i].ToJson()
b.Torrents = append(b.Torrents, res)
@ -57,3 +69,40 @@ func ApiViewHandler(w http.ResponseWriter, r *http.Request) {
return
}
}
func ApiUploadHandler(w http.ResponseWriter, r *http.Request) {
if config.UploadsDisabled == 1 {
http.Error(w, "Error uploads are disabled", http.StatusInternalServerError)
return
}
defer r.Body.Close()
//verify token
//token := r.Header.Get("Authorization")
decoder := json.NewDecoder(r.Body)
b := model.TorrentsJson{}
err := decoder.Decode(&b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
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().Unix(),
Filesize: 0,
Description: string(b.Description)}
db.ORM.Create(&torrent)
fmt.Printf("%+v\n", torrent)
}

Voir le fichier

@ -23,6 +23,7 @@ func init() {
gzipSearchHandler := handlers.CompressHandler(http.HandlerFunc(SearchHandler))
gzipAPIHandler := handlers.CompressHandler(http.HandlerFunc(ApiHandler))
gzipAPIViewHandler := handlers.CompressHandler(http.HandlerFunc(ApiViewHandler))
gzipAPIUploadHandler := handlers.CompressHandler(http.HandlerFunc(ApiUploadHandler))
gzipFaqHandler := handlers.CompressHandler(http.HandlerFunc(FaqHandler))
gzipRssHandler := handlers.CompressHandler(http.HandlerFunc(RssHandler))
gzipViewHandler := handlers.CompressHandler(http.HandlerFunc(ViewHandler))
@ -43,6 +44,7 @@ func init() {
Router.Handle("/search", gzipSearchHandler).Name("search")
Router.Handle("/search/{page}", gzipSearchHandler).Name("search_page")
Router.Handle("/api/{page}", gzipAPIHandler).Methods("GET")
Router.Handle("/api/upload", gzipAPIUploadHandler).Methods("POST")
Router.Handle("/api/view/{id}", gzipAPIViewHandler).Methods("GET")
Router.Handle("/faq", gzipFaqHandler).Name("faq")
Router.Handle("/feed", gzipRssHandler).Name("feed")

Voir le fichier

@ -15,6 +15,7 @@ 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"
@ -191,6 +192,35 @@ 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)