Parent
f0b1d8e824
révision
cd3c728b64
|
@ -112,7 +112,8 @@ func GetAvailableLanguages() Languages {
|
|||
return languages
|
||||
}
|
||||
|
||||
func getParentTag(languageTag string) glang.Tag {
|
||||
// GetParentTag returns the highest parent of a language (e.g. fr-fr -> fr)
|
||||
func GetParentTag(languageTag string) glang.Tag {
|
||||
lang := glang.Make(languageTag)
|
||||
for !lang.Parent().IsRoot() {
|
||||
lang = lang.Parent()
|
||||
|
@ -126,7 +127,7 @@ func ParseLanguages(codes []string) Languages {
|
|||
sort.Strings(codes)
|
||||
// Now build languages array
|
||||
for _, languageTag := range codes {
|
||||
lang := getParentTag(languageTag)
|
||||
lang := GetParentTag(languageTag)
|
||||
langs = append(langs, Language{strings.Title(display.Self.Name(glang.Make(languageTag))), lang.String(), languageTag})
|
||||
}
|
||||
return langs
|
||||
|
@ -232,7 +233,7 @@ func (lang *Language) Translate(languageCode template.HTML) string {
|
|||
|
||||
// Translate accepts a languageCode in string and translate the language to the language from the language code in to
|
||||
func Translate(languageCode string, to string) string {
|
||||
langTranslate := display.Tags(getParentTag(to))
|
||||
langTranslate := display.Tags(GetParentTag(to))
|
||||
translated := langTranslate.Name(glang.Make(languageCode))
|
||||
if translated == "Root" {
|
||||
return ""
|
||||
|
|
|
@ -51,7 +51,7 @@ func TestLanguages(t *testing.T) {
|
|||
tags := i18n.LanguageTags()
|
||||
for _, languageTag := range tags {
|
||||
// The matcher will match Swiss German to German.
|
||||
lang := getParentTag(languageTag)
|
||||
lang := GetParentTag(languageTag)
|
||||
if lang.String() == "und" {
|
||||
t.Errorf("Couldn't find the language root for the language %s", languageTag)
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@ func GetTorrentLanguages() publicSettings.Languages {
|
|||
func LanguageExists(languageCode string) bool {
|
||||
langs := GetTorrentLanguages()
|
||||
for _, lang := range langs {
|
||||
if lang.Tag == languageCode {
|
||||
if lang.Code == publicSettings.GetParentTag(languageCode).String() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package torrentValidator
|
||||
|
||||
import "errors"
|
||||
|
||||
var errTorrentNameInvalid = errors.New("torrent_name_invalid")
|
||||
var errTorrentDescInvalid = errors.New("torrent_desc_invalid")
|
||||
var errTorrentMagnetInvalid = errors.New("torrent_magnet_invalid")
|
||||
var errTorrentURIInvalid = errors.New("torrent_uri_invalid")
|
||||
var errTorrentCatInvalid = errors.New("torrent_cat_invalid")
|
||||
var errTorrentLangInvalid = errors.New("torrent_lang_invalid")
|
||||
var errTorrentPrivate = errors.New("torrent_private")
|
||||
var errTorrentNoTrackers = errors.New("torrent_no_working_trackers")
|
||||
var errTorrentAndMagnet = errors.New("torrent_plus_magnet")
|
||||
var errTorrentHashInvalid = errors.New("torrent_hash_invalid")
|
|
@ -3,7 +3,6 @@ package torrentValidator
|
|||
import (
|
||||
"encoding/base32"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/url"
|
||||
|
@ -23,14 +22,14 @@ import (
|
|||
func (r *TorrentRequest) ValidateName() error {
|
||||
// then actually check that we have everything we need
|
||||
if len(r.Name) == 0 {
|
||||
return errors.New("torrent_name_invalid")
|
||||
return errTorrentNameInvalid
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *TorrentRequest) ValidateDescription() error {
|
||||
if len(r.Description) > config.Get().DescriptionLength {
|
||||
return errors.New("torrent_desc_invalid")
|
||||
return errTorrentDescInvalid
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -42,7 +41,7 @@ func (r *TorrentRequest) ValidateMagnet() error {
|
|||
}
|
||||
xt := magnetURL.Query().Get("xt")
|
||||
if !strings.HasPrefix(xt, "urn:btih:") {
|
||||
return errors.New("torrent_magnet_invalid")
|
||||
return errTorrentMagnetInvalid
|
||||
}
|
||||
xt = strings.SplitAfter(xt, ":")[2]
|
||||
r.Infohash = strings.TrimSpace(strings.ToUpper(strings.Split(xt, "&")[0]))
|
||||
|
@ -53,9 +52,9 @@ func (r *TorrentRequest) ValidateMagnet() error {
|
|||
func (r *TorrentRequest) ValidateWebsiteLink() error {
|
||||
if r.WebsiteLink != "" {
|
||||
// WebsiteLink
|
||||
urlRegexp, _ := regexp.Compile(`^(https?:\/\/|ircs?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$`)
|
||||
urlRegexp, _ := regexp.Compile(`^(https?:\/\/|ircs?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*(\/.*)?$`)
|
||||
if !urlRegexp.MatchString(r.WebsiteLink) {
|
||||
return errors.New("torrent_uri_invalid")
|
||||
return errTorrentURIInvalid
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
@ -72,7 +71,7 @@ func (r *TorrentRequest) ValidateHash() error {
|
|||
return err
|
||||
}
|
||||
if !isBase16 {
|
||||
return errors.New("torrent_hash_invalid")
|
||||
return errTorrentHashInvalid
|
||||
}
|
||||
} else {
|
||||
//convert to base16
|
||||
|
@ -92,19 +91,19 @@ func (r *TorrentRequest) ExtractCategory() error {
|
|||
catsSplit := strings.Split(r.Category, "_")
|
||||
// need this to prevent out of index panics
|
||||
if len(catsSplit) != 2 {
|
||||
return errors.New("torrent_cat_invalid")
|
||||
return errTorrentCatInvalid
|
||||
}
|
||||
CatID, err := strconv.Atoi(catsSplit[0])
|
||||
if err != nil {
|
||||
return errors.New("torrent_cat_invalid")
|
||||
return errTorrentCatInvalid
|
||||
}
|
||||
SubCatID, err := strconv.Atoi(catsSplit[1])
|
||||
if err != nil {
|
||||
return errors.New("torrent_cat_invalid")
|
||||
return errTorrentCatInvalid
|
||||
}
|
||||
|
||||
if !categories.Exists(r.Category) {
|
||||
return errors.New("torrent_cat_invalid")
|
||||
return errTorrentCatInvalid
|
||||
}
|
||||
|
||||
r.CategoryID = CatID
|
||||
|
@ -137,7 +136,7 @@ func (r *TorrentRequest) ExtractLanguage() error {
|
|||
}
|
||||
|
||||
if language != "" && !torrentLanguages.LanguageExists(language) {
|
||||
return errors.New("torrent_lang_invalid")
|
||||
return errTorrentLangInvalid
|
||||
}
|
||||
|
||||
if strings.HasPrefix(language, "en") && isEnglishCategory {
|
||||
|
@ -185,12 +184,12 @@ func (r *TorrentRequest) ValidateMultipartUpload(c *gin.Context, uploadFormTorre
|
|||
|
||||
// check a few things
|
||||
if torrent.IsPrivate() {
|
||||
return tfile, errors.New("torrent_private")
|
||||
return tfile, errTorrentPrivate
|
||||
}
|
||||
trackers := torrent.GetAllAnnounceURLS()
|
||||
r.Trackers = CheckTrackers(trackers)
|
||||
if len(r.Trackers) == 0 {
|
||||
return tfile, errors.New("torrent_no_working_trackers")
|
||||
return tfile, errTorrentNoTrackers
|
||||
}
|
||||
|
||||
// Name
|
||||
|
@ -200,7 +199,7 @@ func (r *TorrentRequest) ValidateMultipartUpload(c *gin.Context, uploadFormTorre
|
|||
|
||||
// Magnet link: if a file is provided it should be empty
|
||||
if len(r.Magnet) != 0 {
|
||||
return tfile, errors.New("torrent_plus_magnet")
|
||||
return tfile, errTorrentAndMagnet
|
||||
}
|
||||
|
||||
_, seekErr = tfile.Seek(0, io.SeekStart)
|
||||
|
|
|
@ -0,0 +1,172 @@
|
|||
package torrentValidator
|
||||
|
||||
import (
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"strings"
|
||||
|
||||
"github.com/NyaaPantsu/nyaa/config"
|
||||
"github.com/NyaaPantsu/nyaa/utils/publicSettings"
|
||||
)
|
||||
|
||||
// run before config/parse.go:init()
|
||||
var _ = func() (_ struct{}) {
|
||||
config.ConfigPath = path.Join("..", "..", "..", config.ConfigPath)
|
||||
config.DefaultConfigPath = path.Join("..", "..", "..", config.DefaultConfigPath)
|
||||
config.Reload()
|
||||
config.Get().I18n.Directory = path.Join("..", "..", "..", config.Get().I18n.Directory)
|
||||
return
|
||||
}()
|
||||
|
||||
func TestValidateName(t *testing.T) {
|
||||
r := TorrentRequest{}
|
||||
tests := []struct {
|
||||
Test string
|
||||
Expected error
|
||||
}{
|
||||
{"", errTorrentNameInvalid},
|
||||
{"something", nil},
|
||||
{"fjr*$é)à_'", nil},
|
||||
}
|
||||
for _, test := range tests {
|
||||
r.Name = test.Test
|
||||
err := r.ValidateName()
|
||||
if err != test.Expected {
|
||||
t.Errorf("Validation of torrent name for '%s' doesn't give the expected result, have '%v', wants '%v'", test.Test, err, test.Expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDescription(t *testing.T) {
|
||||
r := TorrentRequest{}
|
||||
config.Get().DescriptionLength = 5
|
||||
tests := []struct {
|
||||
Test string
|
||||
Expected error
|
||||
}{
|
||||
{"", nil},
|
||||
{"something", errTorrentDescInvalid},
|
||||
{"fed", nil},
|
||||
}
|
||||
for _, test := range tests {
|
||||
r.Description = test.Test
|
||||
err := r.ValidateDescription()
|
||||
if err != test.Expected {
|
||||
t.Errorf("Validation of torrent description for '%s' doesn't give the expected result, have '%v', wants '%v'", test.Test, err, test.Expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateMagnet(t *testing.T) {
|
||||
r := TorrentRequest{}
|
||||
config.Get().DescriptionLength = 5
|
||||
tests := []struct {
|
||||
Test string
|
||||
Expected error
|
||||
}{
|
||||
{"", errTorrentMagnetInvalid},
|
||||
{"something", errTorrentMagnetInvalid},
|
||||
{"magnet:?xt=urn:btih:2BCE960D3CF61462DFB68C10C68D20ED56133BAD&dn=The+King%27s+Avatar+%5BQuan+Zhi+Gao+Shou%5D+-+07+-+%5B1080P%5D+-+Vostfr+-+Fastsub+-+BS.mkv&tr=http://nyaa.tracker.wf:7777/announce&tr=http://nyaa.tracker.wf:7777/announce&tr=udp://tracker.opentrackr.org:1337/announce&tr=http://anidex.moe:6969/announce&tr=http://tracker.anirena.com:80/announce&tr=http://tracker.t411.al&tr=udp://tracker.doko.moe:6969", nil},
|
||||
}
|
||||
for _, test := range tests {
|
||||
r.Magnet = test.Test
|
||||
err := r.ValidateMagnet()
|
||||
if err != test.Expected {
|
||||
t.Errorf("Validation of torrent magnet for '%s' doesn't give the expected result, have '%v', wants '%v'", test.Test, err, test.Expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateWebsiteLink(t *testing.T) {
|
||||
r := TorrentRequest{}
|
||||
tests := []struct {
|
||||
Test string
|
||||
Expected error
|
||||
}{
|
||||
{"", nil},
|
||||
{"something", errTorrentURIInvalid},
|
||||
{"https://kkk.cd", nil},
|
||||
{"http://kkk.cd/xd.?lol=eds", nil},
|
||||
{"ircs://kkk.cd", nil},
|
||||
{"irc://kkk.cd/lol", nil},
|
||||
}
|
||||
for _, test := range tests {
|
||||
r.WebsiteLink = test.Test
|
||||
err := r.ValidateWebsiteLink()
|
||||
if err != test.Expected {
|
||||
t.Errorf("Validation of torrent uri for '%s' doesn't give the expected result, have '%v', wants '%v'", test.Test, err, test.Expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateHash(t *testing.T) {
|
||||
r := TorrentRequest{}
|
||||
tests := []struct {
|
||||
Test string
|
||||
Expected error
|
||||
}{
|
||||
{"", errTorrentHashInvalid},
|
||||
{"something", errTorrentHashInvalid},
|
||||
{"2BCE960D3CF61462DFB68C10C68D20ED56133BAD", nil},
|
||||
{"2BCE960D3CF61462DFB68C10C68D20ED56133BADE", errTorrentHashInvalid},
|
||||
}
|
||||
for _, test := range tests {
|
||||
r.Infohash = test.Test
|
||||
err := r.ValidateHash()
|
||||
if err != test.Expected {
|
||||
t.Errorf("Validation of torrent hash for '%s' doesn't give the expected result, have '%v', wants '%v'", test.Test, err, test.Expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractCategory(t *testing.T) {
|
||||
r := TorrentRequest{}
|
||||
tests := []struct {
|
||||
Test string
|
||||
Expected error
|
||||
}{
|
||||
{"", errTorrentCatInvalid},
|
||||
{"something", errTorrentCatInvalid},
|
||||
{"33_5", errTorrentCatInvalid},
|
||||
{"3_", errTorrentCatInvalid},
|
||||
{"3_12", nil},
|
||||
{"_12", errTorrentCatInvalid},
|
||||
}
|
||||
for _, test := range tests {
|
||||
r.Category = test.Test
|
||||
err := r.ExtractCategory()
|
||||
if err != test.Expected {
|
||||
t.Errorf("Validation of torrent category for '%s' doesn't give the expected result, have '%v', wants '%v'", test.Test, err, test.Expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractLanguage(t *testing.T) {
|
||||
var retriever publicSettings.UserRetriever // not required during initialization
|
||||
err := publicSettings.InitI18n(config.Get().I18n, retriever)
|
||||
if err != nil {
|
||||
t.Errorf("failed to initialize language translations: %v", err)
|
||||
}
|
||||
r := TorrentRequest{}
|
||||
tests := []struct {
|
||||
Test string
|
||||
Expected error
|
||||
}{
|
||||
{"", nil},
|
||||
{"something,fr-fr", errTorrentLangInvalid},
|
||||
{"fr-ems", errTorrentLangInvalid},
|
||||
{"fr-fr,en-us", nil},
|
||||
{"es,fr", nil},
|
||||
{"es", nil},
|
||||
{"es-es", nil},
|
||||
{"ca-es", nil},
|
||||
}
|
||||
for _, test := range tests {
|
||||
r.Languages = strings.Split(test.Test, ",")
|
||||
err := r.ExtractLanguage()
|
||||
if err != test.Expected {
|
||||
t.Errorf("Validation of torrent language for '%s' doesn't give the expected result, have '%v', wants '%v'", test.Test, err, test.Expected)
|
||||
}
|
||||
}
|
||||
}
|
Référencer dans un nouveau ticket