From 515f08f91fc5ff33c58e7cea59b02fa888510272 Mon Sep 17 00:00:00 2001 From: HelixBePraised Date: Fri, 5 Apr 2019 15:03:44 -0400 Subject: Rough breakup, going to be doing a lot of changes soon --- errors.go | 14 ++++++ files.go | 64 ++++++++++++++++++++++++ handlers.go | 102 ++++++++++++++++++++++++++++++++++++++ main.go | 159 ------------------------------------------------------------ 4 files changed, 180 insertions(+), 159 deletions(-) create mode 100644 errors.go create mode 100644 files.go create mode 100644 handlers.go diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..819a2a9 --- /dev/null +++ b/errors.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + "net/http" +) + +//Fine for now, possibly want to do more with errors +func check(err error, w http.ResponseWriter) { + if err != nil { + fmt.Println(err) + fmt.Fprintf(w, "%s", err) + } +} \ No newline at end of file diff --git a/files.go b/files.go new file mode 100644 index 0000000..c9c6175 --- /dev/null +++ b/files.go @@ -0,0 +1,64 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "strings" +) + +func GetMediaInformation(path string) map[string]string { + m := make(map[string]string) + + files, _ := ioutil.ReadDir(path) + + for _, file := range files { + m[file.Name()] = "/view/" + file.Name() + } + + // err := filepath.Walk(path, func(walkPath string, info os.FileInfo, err error) error { + // if info.IsDir() { + // name := info.Name() + // m[name] = walkPath + // } + // + // return nil + // }) + + // if err != nil { + // fmt.Printf("%s", err) + // } + + return m +} +func GetShowInfo() { + files, _ := ioutil.ReadDir("./media/shows/") + + for _, dir := range files { + + shows[dir.Name()] = map[string]map[string]string{} + + var seasonName string + + _ = filepath.Walk("./media/shows/"+dir.Name(), func(path string, file os.FileInfo, err error) error { + + if err != nil { + fmt.Printf("Error: %s\n0", err) + return nil + } + + if file.IsDir() { + seasonName = file.Name() + shows[dir.Name()][seasonName] = map[string]string{} + + } else { + title := file.Name() + title = strings.Replace(title, filepath.Ext(file.Name()), "", -1) + shows[dir.Name()][seasonName][file.Name()] = path + } + + return nil + }) + } +} diff --git a/handlers.go b/handlers.go new file mode 100644 index 0000000..9728fcc --- /dev/null +++ b/handlers.go @@ -0,0 +1,102 @@ +package main + +import ( + "github.com/gorilla/mux" + "net/http" +) + +func index(w http.ResponseWriter, req *http.Request) { + _ = req.URL.Path + p := Page{ + Title: "Home", + } + + err := tmpl.ExecuteTemplate(w, "home.gohtml", p) + check(err, w) + +} + +func movieHandler(w http.ResponseWriter, req *http.Request) { + p := Page{ + Title: "Movies", + MediaTitleAndLink: movies, + } + + err := tmpl.ExecuteTemplate(w, "movieIndex.gohtml", p) + check(err, w) + +} + +func showsHandler(w http.ResponseWriter, req *http.Request) { + vars := mux.Vars(req) + var p Page + var title string + if vars["show"] == "" { + var m = map[string]string{} + + for show := range shows { + m[show] = "/shows/" + show + } + + p = Page{ + Title: "Shows", + MediaTitleAndLink: m, + } + + } else if vars["season"] == "" { + var m = map[string]string{} + + for season := range shows[vars["show"]] { + if season == vars["show"] { + continue + } + m[season] = "/shows/" + vars["show"] + "/" + season + } + + title = vars["show"] + + p = Page{ + Title: title, + MediaTitleAndLink: m, + } + + } else if vars["episode"] == "" { + var m = map[string]string{} + + for episode := range shows[vars["show"]][vars["season"]] { + m[episode] = "/view/" + vars["show"] + "/" + vars["season"] + "/" + episode + } + + title = vars["show"] + " | " + vars["season"] + + p = Page{ + Title: title, + MediaTitleAndLink: m, + } + } + + err := tmpl.ExecuteTemplate(w, "movieIndex.gohtml", p) + check(err, w) +} + +func movieViewerHandler(w http.ResponseWriter, req *http.Request) { + var url, title string + vars := mux.Vars(req) + + if vars["season"] == "" { + url = "/files/movies/" + vars["movieOrShow"] + title = vars["movieOrShow"] + } else { + url = "/files/shows/" + vars["movieOrShow"] + "/" + vars["season"] + "/" + vars["episode"] + title = vars["movieOrShow"] + } + + p := Page{ + Title: title, + MediaSrc: url, + } + + err := tmpl.ExecuteTemplate(w, "viewer.gohtml", p) + check(err, w) + +} \ No newline at end of file diff --git a/main.go b/main.go index 360a7af..07ce444 100644 --- a/main.go +++ b/main.go @@ -66,162 +66,3 @@ func main() { http.ListenAndServe(":8080", r) } - -func index(w http.ResponseWriter, req *http.Request) { - _ = req.URL.Path - p := Page{ - Title: "Home", - } - - err := tmpl.ExecuteTemplate(w, "home.gohtml", p) - check(err, w) - -} - -func movieHandler(w http.ResponseWriter, req *http.Request) { - p := Page{ - Title: "Movies", - MediaTitleAndLink: movies, - } - - err := tmpl.ExecuteTemplate(w, "movieIndex.gohtml", p) - check(err, w) - -} - -func showsHandler(w http.ResponseWriter, req *http.Request) { - vars := mux.Vars(req) - var p Page - var title string - if vars["show"] == "" { - var m = map[string]string{} - - for show := range shows { - m[show] = "/shows/" + show - } - - p = Page{ - Title: "Shows", - MediaTitleAndLink: m, - } - - } else if vars["season"] == "" { - var m = map[string]string{} - - for season := range shows[vars["show"]] { - if season == vars["show"] { - continue - } - m[season] = "/shows/" + vars["show"] + "/" + season - } - - title = vars["show"] - - p = Page{ - Title: title, - MediaTitleAndLink: m, - } - - } else if vars["episode"] == "" { - var m = map[string]string{} - - for episode := range shows[vars["show"]][vars["season"]] { - m[episode] = "/view/" + vars["show"] + "/" + vars["season"] + "/" + episode - } - - title = vars["show"] + " | " + vars["season"] - - p = Page{ - Title: title, - MediaTitleAndLink: m, - } - } - - err := tmpl.ExecuteTemplate(w, "movieIndex.gohtml", p) - check(err, w) -} - -func movieViewerHandler(w http.ResponseWriter, req *http.Request) { - var url, title string - vars := mux.Vars(req) - - if vars["season"] == "" { - url = "/files/movies/" + vars["movieOrShow"] - title = vars["movieOrShow"] - } else { - url = "/files/shows/" + vars["movieOrShow"] + "/" + vars["season"] + "/" + vars["episode"] - title = vars["movieOrShow"] - } - - p := Page{ - Title: title, - MediaSrc: url, - } - - err := tmpl.ExecuteTemplate(w, "viewer.gohtml", p) - check(err, w) - -} - -//Fine for now, possibly want to do more with errors -func check(err error, w http.ResponseWriter) { - if err != nil { - fmt.Println(err) - fmt.Fprintf(w, "%s", err) - } -} - -func GetMediaInformation(path string) map[string]string { - m := make(map[string]string) - - files, _ := ioutil.ReadDir(path) - - for _, file := range files { - m[file.Name()] = "/view/" + file.Name() - } - - // err := filepath.Walk(path, func(walkPath string, info os.FileInfo, err error) error { - // if info.IsDir() { - // name := info.Name() - // m[name] = walkPath - // } - // - // return nil - // }) - - // if err != nil { - // fmt.Printf("%s", err) - // } - - return m -} -func GetShowInfo() { - files, _ := ioutil.ReadDir("./media/shows/") - - for _, dir := range files { - - shows[dir.Name()] = map[string]map[string]string{} - - var seasonName string - - _ = filepath.Walk("./media/shows/"+dir.Name(), func(path string, file os.FileInfo, err error) error { - - if err != nil { - fmt.Printf("Error: %s\n0", err) - return nil - } - - if file.IsDir() { - seasonName = file.Name() - shows[dir.Name()][seasonName] = map[string]string{} - - } else { - title := file.Name() - title = strings.Replace(title, filepath.Ext(file.Name()), "", -1) - shows[dir.Name()][seasonName][file.Name()] = path - } - - return nil - }) - } -} -- cgit v1.2.3