pages.go 5.6 KB

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