Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Merge pull request #156 from azhao12345/upload-fixes

Upload fixes
Cette révision appartient à :
akuma06 2017-05-08 03:37:05 +02:00 révisé par GitHub
révision 18a470b87f
2 fichiers modifiés avec 12 ajouts et 20 suppressions

Voir le fichier

@ -10,9 +10,9 @@ import (
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"regexp"
"github.com/ewhal/nyaa/config"
"github.com/ewhal/nyaa/service/captcha"
@ -24,10 +24,10 @@ import (
// UploadForm serializing HTTP form for torrent upload
type UploadForm struct {
Name string
Magnet string
Category string
Description string
Name string
Magnet string
Category string
Description string
captcha.Captcha
Infohash string
@ -54,7 +54,6 @@ const UploadFormCategory = "c"
// form value for description
const UploadFormDescription = "desc"
// error indicating that you can't send both a magnet link and torrent
var ErrTorrentPlusMagnet = errors.New("upload either a torrent file or magnet link, not both")
@ -86,6 +85,11 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error {
f.Magnet = r.FormValue(UploadFormMagnet)
f.Captcha = captcha.Extract(r)
if !captcha.Authenticate(f.Captcha) {
// TODO: Prettier passing of mistyoed captcha errors
return errors.New(captcha.ErrInvalidCaptcha.Error())
}
// trim whitespaces
f.Name = util.TrimWhitespaces(f.Name)
f.Description = p.Sanitize(util.TrimWhitespaces(f.Description))
@ -109,7 +113,6 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error {
return ErrInvalidTorrentCategory
}
// first: parse torrent file (if any) to fill missing information
tfile, _, err := r.FormFile(UploadFormTorrent)
if err == nil {
@ -142,7 +145,7 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error {
}
binInfohash := torrent.Infohash()
f.Infohash = strings.ToUpper(hex.EncodeToString(binInfohash[:]))
f.Magnet = util.InfoHashToMagnet(f.Infohash, f.Name)
f.Magnet = util.InfoHashToMagnet(f.Infohash, f.Name, trackers...)
// extract filesize
f.Filesize = int64(torrent.TotalSize())
@ -166,7 +169,6 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error {
f.Filepath = ""
}
// then actually check that we have everything we need
if len(f.Name) == 0 {
return ErrInvalidTorrentName
@ -176,10 +178,9 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error {
// return ErrInvalidTorrentDescription
//}
// after data has been checked & extracted, write it to disk
if len(config.TorrentFileStorage) > 0 {
err := WriteTorrentToDisk(tfile, f.Infohash + ".torrent", &f.Filepath)
err := WriteTorrentToDisk(tfile, f.Infohash+".torrent", &f.Filepath)
if err != nil {
return err
}

Voir le fichier

@ -3,7 +3,6 @@ package router
import (
"fmt"
"net/http"
"os"
"strconv"
"time"
@ -26,14 +25,6 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) {
// validation is done in ExtractInfo()
err = uploadForm.ExtractInfo(r)
if err == nil {
if !captcha.Authenticate(uploadForm.Captcha) {
// TODO: Prettier passing of mistyoed captcha errors
http.Error(w, captcha.ErrInvalidCaptcha.Error(), 403)
if len(uploadForm.Filepath) > 0 {
os.Remove(uploadForm.Filepath)
}
return
}
//add to db and redirect depending on result
torrent := model.Torrents{