From 44d75f506d9b0573d362cc13ee6230d295c4e282 Mon Sep 17 00:00:00 2001 From: tomleb Date: Wed, 10 May 2017 23:06:12 -0400 Subject: [PATCH] Add dumps view --- config/dumps.go | 6 ++++ model/dumps.go | 33 ++++++++++++++++++++ router/databaseDumpHandler.go | 58 +++++++++++++++++++++++++++++++++++ router/router.go | 17 ++++++++-- router/template.go | 7 ++++- router/templateVariables.go | 10 ++++++ templates/dumps.html | 45 +++++++++++++++++++++++++++ 7 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 config/dumps.go create mode 100644 model/dumps.go create mode 100644 router/databaseDumpHandler.go create mode 100644 templates/dumps.html diff --git a/config/dumps.go b/config/dumps.go new file mode 100644 index 00000000..cfc16b18 --- /dev/null +++ b/config/dumps.go @@ -0,0 +1,6 @@ +package config + +const ( + DefaultDatabaseDumpPath = "./public/dumps/" + DefaultGPGPublicKeyPath = "./public/gpg/" +) diff --git a/model/dumps.go b/model/dumps.go new file mode 100644 index 00000000..36882e40 --- /dev/null +++ b/model/dumps.go @@ -0,0 +1,33 @@ +package model + +import ( + "time" + "html/template" + + "github.com/ewhal/nyaa/util" +) + +type DatabaseDump struct { + Date time.Time + Filesize int64 + Name string + TorrentLink string +} + +type DatabaseDumpJSON struct { + Date string `json:"date"` + Filesize string `json:"filesize"` + Name string `json:"name"` + //Magnet template.URL `json:"magnet"` + TorrentLink template.URL `json:"torrent"` +} + +func (dump *DatabaseDump) ToJSON() DatabaseDumpJSON { + json := DatabaseDumpJSON{ + Date: dump.Date.Format(time.RFC3339), + Filesize: util.FormatFilesize2(dump.Filesize), + Name: dump.Name, + TorrentLink: template.URL(dump.TorrentLink), + } + return json +} diff --git a/router/databaseDumpHandler.go b/router/databaseDumpHandler.go new file mode 100644 index 00000000..9f08400c --- /dev/null +++ b/router/databaseDumpHandler.go @@ -0,0 +1,58 @@ +package router + +import ( + "io/ioutil" + "os" + "net/http" + "fmt" + "time" + + "github.com/ewhal/nyaa/config" + "github.com/ewhal/nyaa/model" + "github.com/ewhal/nyaa/util/languages" + "github.com/ewhal/nyaa/util/log" + "github.com/ewhal/nyaa/util/metainfo" + "github.com/gorilla/mux" +) + +func DatabaseDumpHandler(w http.ResponseWriter, r *http.Request) { + // db params url + var err error + // TODO Use config from cli + files, _ := ioutil.ReadDir(config.DefaultDatabaseDumpPath) + if len(files) <= 0 { + return + } + var dumpsJson []model.DatabaseDumpJSON + // TODO Filter *.torrent files + for _, f := range files { + // TODO Use config from cli + file, err := os.Open(config.DefaultDatabaseDumpPath + f.Name()) + if err != nil { + continue + } + var tf metainfo.TorrentFile + err = tf.Decode(file) + if err != nil { + log.CheckError(err) + fmt.Println(err) + continue + } + dump := model.DatabaseDump{ + Date: time.Now(), + Filesize: int64(tf.TotalSize()), + Name: tf.TorrentName(), + TorrentLink: "/dbdumps/" + f.Name()} + dumpsJson = append(dumpsJson, dump.ToJSON()) + } + + // TODO Remove ? + navigationTorrents := Navigation{0, 0, 0, "search_page"} + languages.SetTranslationFromRequest(databaseDumpTemplate, r, "en-us") + dtv := DatabaseDumpTemplateVariables{dumpsJson, "/gpg/gpg.pub", NewSearchForm(), navigationTorrents, GetUser(r), r.URL, mux.CurrentRoute(r)} + err = databaseDumpTemplate.ExecuteTemplate(w, "index.html", dtv) + if err != nil { + log.Errorf("DatabaseDump(): %s", err) + } +} + diff --git a/router/router.go b/router/router.go index ab420f9a..dafc4394 100755 --- a/router/router.go +++ b/router/router.go @@ -15,6 +15,12 @@ func init() { cssHandler := http.FileServer(http.Dir("./public/css/")) jsHandler := http.FileServer(http.Dir("./public/js/")) imgHandler := http.FileServer(http.Dir("./public/img/")) + // TODO Use config from cli + // TODO Make sure the directory exists + dumpsHandler := http.FileServer(http.Dir(config.DefaultDatabaseDumpPath)) + // TODO Use config from cli + // TODO Make sure the directory exists + gpgKeyHandler := http.FileServer(http.Dir(config.DefaultGPGPublicKeyPath)) gzipHomeHandler := http.HandlerFunc(HomeHandler) gzipAPIHandler := http.HandlerFunc(ApiHandler) gzipAPIViewHandler := http.HandlerFunc(ApiViewHandler) @@ -22,6 +28,9 @@ func init() { gzipUserProfileHandler := http.HandlerFunc(UserProfileHandler) gzipUserDetailsHandler := http.HandlerFunc(UserDetailsHandler) gzipUserProfileFormHandler := http.HandlerFunc(UserProfileFormHandler) + gzipDumpsHandler := handlers.CompressHandler(dumpsHandler) + gzipGpgKeyHandler := handlers.CompressHandler(gpgKeyHandler) + /* // Enable GZIP compression for all handlers except imgHandler and captcha gzipCSSHandler := cssHandler) @@ -50,10 +59,10 @@ func init() { gzipCommentDeleteModPanel := http.HandlerFunc(CommentDeleteModPanel) gzipTorrentDeleteModPanel := http.HandlerFunc(TorrentDeleteModPanel) gzipTorrentReportDeleteModPanel := http.HandlerFunc(TorrentReportDeleteModPanel)*/ - //gzipTorrentReportCreateHandler := http.HandlerFunc(CreateTorrentReportHandler) //gzipTorrentReportDeleteHandler := http.HandlerFunc(DeleteTorrentReportHandler) //gzipTorrentDeleteHandler := http.HandlerFunc(DeleteTorrentHandler) + gzipDatabaseDumpHandler := handlers.CompressHandler(http.HandlerFunc(DatabaseDumpHandler)) Router = mux.NewRouter() @@ -61,7 +70,9 @@ func init() { http.Handle("/css/", http.StripPrefix("/css/", cssHandler)) http.Handle("/js/", http.StripPrefix("/js/", jsHandler)) http.Handle("/img/", http.StripPrefix("/img/", imgHandler)) - Router.Handle("/", wrapHandler(gzipHomeHandler)).Name("home") + http.Handle("/dbdumps/", http.StripPrefix("/dbdumps/", wrapHandler(gzipDumpsHandler))) + http.Handle("/gpg/", http.StripPrefix("/gpg/", wrapHandler(gzipGpgKeyHandler))) + Router.Handle("/", gzipHomeHandler).Name("home") Router.Handle("/page/{page:[0-9]+}", wrapHandler(gzipHomeHandler)).Name("home_page") Router.HandleFunc("/search", SearchHandler).Name("search") Router.HandleFunc("/search/{page}", SearchHandler).Name("search_page") @@ -110,6 +121,8 @@ func init() { Router.PathPrefix("/captcha").Methods("GET").HandlerFunc(captcha.ServeFiles) + Router.Handle("/dumps", gzipDatabaseDumpHandler).Name("dump").Methods("GET") + Router.HandleFunc("/language", SeeLanguagesHandler).Methods("GET").Name("see_languages") Router.HandleFunc("/language", ChangeLanguageHandler).Methods("POST").Name("change_language") diff --git a/router/template.go b/router/template.go index 46b0a528..0a66df12 100644 --- a/router/template.go +++ b/router/template.go @@ -7,7 +7,7 @@ import ( var TemplateDir = "templates" -var homeTemplate, searchTemplate, faqTemplate, uploadTemplate, viewTemplate, viewRegisterTemplate, viewLoginTemplate, viewRegisterSuccessTemplate, viewVerifySuccessTemplate, viewProfileTemplate, viewProfileEditTemplate, viewUserDeleteTemplate, notFoundTemplate, changeLanguageTemplate *template.Template +var homeTemplate, searchTemplate, faqTemplate, uploadTemplate, viewTemplate, viewRegisterTemplate, viewLoginTemplate, viewRegisterSuccessTemplate, viewVerifySuccessTemplate, viewProfileTemplate, viewProfileEditTemplate, viewUserDeleteTemplate, notFoundTemplate, changeLanguageTemplate, databaseDumpTemplate *template.Template var panelIndex, panelTorrentList, panelUserList, panelCommentList, panelTorrentEd, panelTorrentReportList, panelTorrentReassign *template.Template @@ -21,6 +21,11 @@ type templateLoader struct { // ReloadTemplates reloads templates on runtime func ReloadTemplates() { pubTempls := []templateLoader{ + templateLoader{ + templ: &databaseDumpTemplate, + name: "dump", + file: "dumps.html", + }, templateLoader{ templ: &homeTemplate, name: "home", diff --git a/router/templateVariables.go b/router/templateVariables.go index e7a858b4..8d983fcd 100644 --- a/router/templateVariables.go +++ b/router/templateVariables.go @@ -104,6 +104,16 @@ type HomeTemplateVariables struct { Route *mux.Route // For getting current route in templates } +type DatabaseDumpTemplateVariables struct { + ListDumps []model.DatabaseDumpJSON + GPGLink string + Search SearchForm + Navigation Navigation + User *model.User + URL *url.URL // For parsing Url in templates + Route *mux.Route // For getting current route in templates +} + type UploadTemplateVariables struct { Upload UploadForm Search SearchForm diff --git a/templates/dumps.html b/templates/dumps.html new file mode 100644 index 00000000..0d01ea76 --- /dev/null +++ b/templates/dumps.html @@ -0,0 +1,45 @@ +{{define "title"}}{{T "home"}}{{end}} +{{define "contclass"}}cont-home{{end}} +{{define "content"}} +
+
+{{end}}