diff --git a/router/template_functions.go b/router/template_functions.go index 9c8e15c5..172ffaff 100644 --- a/router/template_functions.go +++ b/router/template_functions.go @@ -10,6 +10,7 @@ import ( "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/service/user/permission" "github.com/NyaaPantsu/nyaa/util" + "github.com/NyaaPantsu/nyaa/util/categories" "github.com/NyaaPantsu/nyaa/util/languages" ) @@ -161,51 +162,14 @@ var FuncMap = template.FuncMap{ // because time.* isn't available in templates... return t.Format(time.RFC3339) }, - "Category_Sukebei": func(category string, sub_category string) string { - s := category + "_" + sub_category; e := "" - switch s { - default: e = "" - case "1_": e = "art" - case "1_1": e = "art_anime" - case "1_2": e = "art_doujinshi" - case "1_3": e = "art_games" - case "1_4": e = "art_manga" - case "1_5": e = "art_pictures" - case "2_": e = "real_life" - case "2_1": e = "real_life_photobooks_and_pictures" - case "2_2": e = "real_life_videos" - } - return e - }, - "Category_Nyaa": func(category string, sub_category string) string { - s := category + "_" + sub_category; e := "" - switch s { - default: e = "" - case "3_": e = "anime" - case "3_12": e = "anime_amv" - case "3_5": e = "anime_english_translated" - case "3_13": e = "anime_non_english_translated" - case "3_6": e = "anime_raw" - case "2_": e = "audio" - case "2_3": e = "audio_lossless" - case "2_4": e = "audio_lossy" - case "4_": e = "literature" - case "4_7": e = "literature_english_translated" - case "4_8": e = "literature_raw" - case "4_14": e = "literature_non_english_translated" - case "5_": e = "live_action" - case "5_9": e = "live_action_english_translated" - case "5_10": e = "live_action_idol_pv" - case "5_18": e = "live_action_non_english_translated" - case "5_11": e = "live_action_raw" - case "6_": e = "pictures" - case "6_15": e = "pictures_graphics" - case "6_16": e = "pictures_photos" - case "1_": e = "software" - case "1_1": e = "software_applications" - case "1_2": e = "software_games" - } - return e + "CategoryID": func(category string, sub_category string) string { + s := category + "_" + sub_category + + if category, ok := categories.GetCategories()[s]; ok { + return category + } else { + return "" + } }, "fileSize": func(filesize int64, T languages.TemplateTfunc) template.HTML { if (filesize == 0) { diff --git a/router/upload.go b/router/upload.go index 74ce5abc..9d880815 100644 --- a/router/upload.go +++ b/router/upload.go @@ -19,6 +19,7 @@ import ( "github.com/NyaaPantsu/nyaa/config" "github.com/NyaaPantsu/nyaa/service/upload" "github.com/NyaaPantsu/nyaa/util" + "github.com/NyaaPantsu/nyaa/util/categories" "github.com/NyaaPantsu/nyaa/util/metainfo" "github.com/microcosm-cc/bluemonday" "github.com/zeebo/bencode" @@ -117,6 +118,10 @@ func (f *UploadForm) ExtractInfo(r *http.Request) error { return ErrInvalidTorrentCategory } + if !categories.CategoryExists(f.Category) { + return ErrInvalidTorrentCategory + } + f.CategoryID = CatID f.SubCategoryID = SubCatID } else { diff --git a/templates/_user_list_torrents.html b/templates/_user_list_torrents.html index aff9cf43..2488df4b 100644 --- a/templates/_user_list_torrents.html +++ b/templates/_user_list_torrents.html @@ -21,9 +21,9 @@ {{ if Sukebei }} - + {{ else }} - + {{ end}} diff --git a/templates/home.html b/templates/home.html index b3532c85..27941d35 100644 --- a/templates/home.html +++ b/templates/home.html @@ -47,9 +47,9 @@ Your browser does not support the audio element. {{ if Sukebei }} - + {{ else }} - + {{ end}} diff --git a/templates/view.html b/templates/view.html index b20cc6a9..b3d62102 100644 --- a/templates/view.html +++ b/templates/view.html @@ -22,11 +22,11 @@
{{ if Sukebei }} - - {{ call $.T (Category_Sukebei .Category .SubCategory) }} + + {{ call $.T (CategoryID .Category .SubCategory) }} {{ else }} - - {{ call $.T (Category_Nyaa .Category .SubCategory) }} + + {{ call $.T (CategoryID .Category .SubCategory) }} {{ end }}

diff --git a/util/categories/categories.go b/util/categories/categories.go new file mode 100644 index 00000000..916dafb3 --- /dev/null +++ b/util/categories/categories.go @@ -0,0 +1,60 @@ +package categories + +import ( + "github.com/NyaaPantsu/nyaa/config" +) + +var categories map[string]string + +func GetCategories() map[string]string { + if categories != nil { + return categories + } + + if config.IsSukebei() { + categories = map[string]string{ + "1_": "art", + "1_1": "art_anime", + "1_2": "art_doujinshi", + "1_3": "art_games", + "1_4": "art_manga", + "1_5": "art_pictures", + "2_": "real_life", + "2_1": "real_life_photobooks_and_pictures", + "2_2": "real_life_videos", + } + } else { + categories = map[string]string{ + "3_": "anime", + "3_12": "anime_amv", + "3_5": "anime_english_translated", + "3_13": "anime_non_english_translated", + "3_6": "anime_raw", + "2_": "audio", + "2_3": "audio_lossless", + "2_4": "audio_lossy", + "4_": "literature", + "4_7": "literature_english_translated", + "4_8": "literature_raw", + "4_14": "literature_non_english_translated", + "5_": "live_action", + "5_9": "live_action_english_translated", + "5_10": "live_action_idol_pv", + "5_18": "live_action_non_english_translated", + "5_11": "live_action_raw", + "6_": "pictures", + "6_15": "pictures_graphics", + "6_16": "pictures_photos", + "1_": "software", + "1_1": "software_applications", + "1_2": "software_games", + } + } + + return categories +} + +func CategoryExists(category string) bool { + _, exists := GetCategories()[category] + return exists +}