From ce16601bcc2baf4ad77ee3986ab21275b0a9d9b0 Mon Sep 17 00:00:00 2001 From: ayame-git Date: Sat, 6 May 2017 11:36:37 +0300 Subject: [PATCH] basic upload --- main.go | 1 - router/router.go | 1 + router/templateVariables.go | 76 ++++++++++++++++++++++++------------ router/uploadHandler.go | 35 +++++++++++++++++ router/viewTorrentHandler.go | 12 +++--- templates/index.html | 1 + templates/upload.html | 69 ++++++++++++++++++++++++++++++++ 7 files changed, 164 insertions(+), 31 deletions(-) create mode 100644 router/uploadHandler.go create mode 100644 templates/upload.html diff --git a/main.go b/main.go index 477cda11..7b91acff 100755 --- a/main.go +++ b/main.go @@ -29,7 +29,6 @@ func RunServer(conf *config.Config) { } func main() { - conf := config.NewConfig() conf_bind := conf.BindFlags() defaults := flag.Bool("print-defaults", false, "print the default configuration file on stdout") diff --git a/router/router.go b/router/router.go index 8b6076da..4b073854 100644 --- a/router/router.go +++ b/router/router.go @@ -28,4 +28,5 @@ func init() { Router.HandleFunc("/faq", FaqHandler).Name("faq") Router.HandleFunc("/feed", RssHandler).Name("feed") Router.HandleFunc("/view/{id}", ViewHandler).Name("view_torrent") + Router.HandleFunc("/upload", UploadHandler).Name("upload") } diff --git a/router/templateVariables.go b/router/templateVariables.go index a361d260..97f9e644 100644 --- a/router/templateVariables.go +++ b/router/templateVariables.go @@ -1,8 +1,8 @@ package router import ( - "github.com/gorilla/mux" "github.com/ewhal/nyaa/model" + "github.com/gorilla/mux" "net/url" ) @@ -14,28 +14,35 @@ import ( type FaqTemplateVariables struct { Navigation Navigation - Search SearchForm + Search SearchForm URL *url.URL // For parsing Url in templates Route *mux.Route // For getting current route in templates } type ViewTemplateVariables struct { - Torrent model.TorrentsJson - Search SearchForm - Navigation Navigation - URL *url.URL // For parsing Url in templates - Route *mux.Route // For getting current route in templates + Torrent model.TorrentsJson + Search SearchForm + Navigation Navigation + URL *url.URL // For parsing Url in templates + Route *mux.Route // For getting current route in templates } type HomeTemplateVariables struct { ListTorrents []model.TorrentsJson ListCategories []model.Categories - Search SearchForm + Search SearchForm Navigation Navigation URL *url.URL // For parsing Url in templates Route *mux.Route // For getting current route in templates } +type UploadTemplateVariables struct { + Upload UploadForm + Search SearchForm + Navigation Navigation + URL *url.URL + Route *mux.Route +} /* * Variables used by the upper ones @@ -48,31 +55,52 @@ type Navigation struct { } type SearchForm struct { - Query string - Status string - Category string - Sort string - Order string + Query string + Status string + Category string + Sort string + Order string HideAdvancedSearch bool } +type UploadForm struct { + Name string + Magnet string + Category string + Description string +} + // Some Default Values to ease things out func NewSearchForm(params ...string) SearchForm { searchForm := SearchForm{} - if (len(params) > 1) { + if len(params) > 1 { searchForm.Category = params[0] - } else { - searchForm.Category = "_" + } else { + searchForm.Category = "_" } - if (len(params) > 2) { - searchForm.Sort = params[0] - } else { - searchForm.Sort = "torrent_id" + if len(params) > 2 { + searchForm.Sort = params[1] + } else { + searchForm.Sort = "torrent_id" } - if (len(params) > 3) { - searchForm.Order = params[0] - } else { - searchForm.Order = "DESC" + if len(params) > 3 { + searchForm.Order = params[2] + } else { + searchForm.Order = "DESC" } return searchForm } +func NewUploadForm(params ...string) UploadForm { + uploadForm := UploadForm{} + if len(params) > 1 { + uploadForm.Category = params[0] + } else { + uploadForm.Category = "3_12" + } + if len(params) > 2 { + uploadForm.Description = params[1] + } else { + uploadForm.Description = "Description" + } + return uploadForm +} diff --git a/router/uploadHandler.go b/router/uploadHandler.go new file mode 100644 index 00000000..5505a0d9 --- /dev/null +++ b/router/uploadHandler.go @@ -0,0 +1,35 @@ +package router + +import ( + "github.com/gorilla/mux" + "html/template" + "net/http" +) + +func UploadHandler(w http.ResponseWriter, r *http.Request) { + var templates = template.Must(template.New("upload").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/upload.html")) + templates.ParseGlob("templates/_*.html") // common + + var uploadForm UploadForm + if r.Method == "POST" { + err := r.ParseForm() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + uploadForm = UploadForm{ + r.Form.Get("name"), + r.Form.Get("magnet"), + r.Form.Get("c"), + r.Form.Get("desc"), + } + //validate name + hash + //add to db and redirect depending on result + } + + htv := UploadTemplateVariables{uploadForm, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)} + + err := templates.ExecuteTemplate(w, "index.html", htv) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } +} diff --git a/router/viewTorrentHandler.go b/router/viewTorrentHandler.go index 0d30094f..cf205338 100644 --- a/router/viewTorrentHandler.go +++ b/router/viewTorrentHandler.go @@ -1,15 +1,15 @@ package router -import( - "net/http" - "html/template" - "github.com/gorilla/mux" +import ( "github.com/ewhal/nyaa/service/torrent" + "github.com/gorilla/mux" + "html/template" + "net/http" ) func ViewHandler(w http.ResponseWriter, r *http.Request) { var templates = template.Must(template.New("view").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/view.html")) - templates.ParseGlob("templates/_*.html") // common + templates.ParseGlob("templates/_*.html") // common vars := mux.Vars(r) id := vars["id"] @@ -22,4 +22,4 @@ func ViewHandler(w http.ResponseWriter, r *http.Request) { if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } -} \ No newline at end of file +} diff --git a/templates/index.html b/templates/index.html index df126a58..7951bcab 100755 --- a/templates/index.html +++ b/templates/index.html @@ -41,6 +41,7 @@