pages.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package pages
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "html/template"
  6. "net/http"
  7. "os"
  8. "strings"
  9. "gogs.gildas.ch/gildas/movies"
  10. "gogs.gildas.ch/gildas/movies/omdb"
  11. "gopkg.in/russross/blackfriday.v2"
  12. )
  13. func Router(c *movies.Collection) http.HandlerFunc {
  14. homeHandler := Home(c)
  15. movieHandler := Movie(c)
  16. listHandler := List(c)
  17. filesHandler := Files(c)
  18. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  19. path := r.URL.Path
  20. switch {
  21. case path == "/":
  22. homeHandler(w, r)
  23. return
  24. case path == "/files":
  25. filesHandler(w, r)
  26. return
  27. case path == "/style.css":
  28. http.ServeFile(w, r, "templates/style.css")
  29. return
  30. case strings.HasPrefix(path, "/l/"):
  31. listHandler(w, r)
  32. return
  33. case strings.HasPrefix(path, "/tt"):
  34. movieHandler(w, r)
  35. return
  36. }
  37. })
  38. }
  39. func Home(c *movies.Collection) http.HandlerFunc {
  40. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  41. t, err := template.ParseFiles("templates/home.html")
  42. if err != nil {
  43. fmt.Println(err)
  44. return
  45. }
  46. var errs []error
  47. if r.Method == "POST" {
  48. if r.FormValue("omdb_json") != "" {
  49. m, err := movies.Unmarshal([]byte(r.FormValue("omdb_json")))
  50. if err != nil {
  51. w.WriteHeader(http.StatusBadRequest)
  52. errs = append(errs, err)
  53. } else {
  54. c.Add(m)
  55. }
  56. }
  57. if r.FormValue("list-id") != "" {
  58. if err := c.AddList(r.FormValue("list-id"), r.FormValue("list-title")); err != nil {
  59. errs = append(errs, err)
  60. }
  61. }
  62. }
  63. var omdbString string
  64. if imdbID := r.URL.Query().Get("imdb_id"); imdbID != "" {
  65. b, err := omdb.Get(imdbID)
  66. if err != nil {
  67. errs = append(errs, err)
  68. } else {
  69. omdbString = string(b)
  70. }
  71. }
  72. t.Execute(w, map[string]interface{}{
  73. "Collection": c,
  74. "IMDBID": r.URL.Query().Get("imdb_id"),
  75. "OMDBString": omdbString,
  76. "Errors": errs,
  77. })
  78. })
  79. }
  80. func Movie(c *movies.Collection) http.HandlerFunc {
  81. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  82. t, err := template.ParseFiles("templates/movie.html")
  83. if err != nil {
  84. fmt.Println(err)
  85. return
  86. }
  87. var errs []error
  88. splitted := strings.Split(r.URL.Path, "/")
  89. if len(splitted) < 2 {
  90. w.WriteHeader(http.StatusNotFound)
  91. return
  92. }
  93. imdbID := splitted[1]
  94. m, ok := c.Get(imdbID)
  95. if !ok {
  96. w.WriteHeader(http.StatusNotFound)
  97. return
  98. }
  99. if r.Method == "POST" {
  100. if movieJSON := r.FormValue("movie_json"); movieJSON != "" {
  101. updated, err := movies.Unmarshal([]byte(movieJSON))
  102. if err != nil {
  103. errs = append(errs, err)
  104. } else if imdbID != updated.IMDBID {
  105. errs = append(errs, fmt.Errorf("you cannot change the imdb id."))
  106. } else {
  107. m = updated
  108. c.Update(updated)
  109. }
  110. }
  111. if files := r.FormValue("files"); files != "" {
  112. m.Files = strings.Split(files, "\n")
  113. c.Update(m)
  114. }
  115. }
  116. fileQuery := r.URL.Query().Get("file_query")
  117. var fileResults string
  118. if fileQuery != "" {
  119. fileResults = strings.Join(c.SearchFiles(fileQuery), "\n")
  120. }
  121. b, err := json.MarshalIndent(m, "", " ")
  122. if err != nil {
  123. errs = append(errs, err)
  124. }
  125. t.Execute(w, map[string]interface{}{
  126. "Movie": m,
  127. "MovieJSON": string(b),
  128. "Files": strings.Join(m.Files, "\n"),
  129. "FileQuery": fileQuery,
  130. "FileResults": fileResults,
  131. "Errors": errs,
  132. })
  133. })
  134. }
  135. func List(c *movies.Collection) http.HandlerFunc {
  136. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  137. t, err := template.ParseFiles("templates/list.html")
  138. if err != nil {
  139. fmt.Println(err)
  140. return
  141. }
  142. var errs []error
  143. splitted := strings.Split(r.URL.Path, "/")
  144. if len(splitted) < 3 {
  145. w.WriteHeader(http.StatusNotFound)
  146. return
  147. }
  148. listID := splitted[2]
  149. l, ok := c.GetList(listID)
  150. if !ok {
  151. w.WriteHeader(http.StatusNotFound)
  152. return
  153. }
  154. if r.Method == "POST" {
  155. if r.FormValue("title") != "" || r.FormValue("description") != "" {
  156. l.Title = r.FormValue("title")
  157. l.Description = r.FormValue("description")
  158. c.UpdateList(l)
  159. } else if r.FormValue("imdb_id") != "" {
  160. l.Movies = append(l.Movies, r.FormValue("imdb_id"))
  161. c.UpdateList(l)
  162. }
  163. }
  164. b, err := json.MarshalIndent(l, "", " ")
  165. if err != nil {
  166. errs = append(errs, err)
  167. }
  168. var ms []*movies.Movie
  169. for _, imdbID := range l.Movies {
  170. if m, ok := c.Get(imdbID); ok {
  171. ms = append(ms, m)
  172. }
  173. }
  174. t.Execute(w, map[string]interface{}{
  175. "List": l,
  176. "Description": template.HTML(
  177. blackfriday.Run(
  178. []byte(strings.ReplaceAll(l.Description, "\r\n", "\n")),
  179. blackfriday.WithExtensions(blackfriday.CommonExtensions))),
  180. "Movies": ms,
  181. "ListJSON": string(b),
  182. "Errors": errs,
  183. })
  184. })
  185. }
  186. func Files(c *movies.Collection) http.HandlerFunc {
  187. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  188. if query := r.URL.Query().Get("query"); query != "" {
  189. matches := c.SearchFiles(query)
  190. for _, match := range matches {
  191. fmt.Fprintf(w, "%s\n", match)
  192. }
  193. return
  194. }
  195. t, err := template.ParseFiles("templates/files.html")
  196. if err != nil {
  197. fmt.Println(err)
  198. return
  199. }
  200. var errs []error
  201. if r.Method == "POST" && r.FormValue("scan") == "1" {
  202. if err := c.ScanFiles(os.Getenv("FILE_ROOT")); err != nil {
  203. errs = append(errs, err)
  204. }
  205. }
  206. t.Execute(w, map[string]interface{}{
  207. "Files": c.AllFiles(),
  208. "Errors": errs,
  209. })
  210. })
  211. }