pages.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.UnmarshalFromOMDB([]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. fmt.Println("Update with", m)
  113. c.Update(updated)
  114. }
  115. }
  116. if files := r.FormValue("files"); files != "" {
  117. m.Files = strings.Split(files, "\n")
  118. fmt.Println("Update with", m)
  119. c.Update(m)
  120. }
  121. }
  122. fileQuery := r.URL.Query().Get("file_query")
  123. var fileResults string
  124. if fileQuery != "" {
  125. fileResults = strings.Join(c.SearchFiles(fileQuery), "\n")
  126. }
  127. b, err := json.MarshalIndent(m, "", " ")
  128. if err != nil {
  129. errs = append(errs, err)
  130. }
  131. t.Execute(w, map[string]interface{}{
  132. "Movie": m,
  133. "MovieJSON": string(b),
  134. "Files": strings.Join(m.Files, "\n"),
  135. "FileQuery": fileQuery,
  136. "FileResults": fileResults,
  137. "Errors": errs,
  138. })
  139. })
  140. }
  141. func List(c *movies.Collection) http.HandlerFunc {
  142. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  143. t, err := template.ParseFiles("templates/list.html")
  144. if err != nil {
  145. fmt.Println(err)
  146. return
  147. }
  148. var errs []error
  149. splitted := strings.Split(r.URL.Path, "/")
  150. if len(splitted) < 3 {
  151. w.WriteHeader(http.StatusNotFound)
  152. return
  153. }
  154. listID := splitted[2]
  155. l, ok := c.GetList(listID)
  156. if !ok {
  157. w.WriteHeader(http.StatusNotFound)
  158. return
  159. }
  160. if r.Method == "POST" {
  161. if r.FormValue("title") != "" || r.FormValue("description") != "" {
  162. l.Title = r.FormValue("title")
  163. l.Description = r.FormValue("description")
  164. c.UpdateList(l)
  165. } else if r.FormValue("imdb_id") != "" {
  166. l.Movies = append(l.Movies, r.FormValue("imdb_id"))
  167. c.UpdateList(l)
  168. }
  169. }
  170. b, err := json.MarshalIndent(l, "", " ")
  171. if err != nil {
  172. errs = append(errs, err)
  173. }
  174. var ms []*movies.Movie
  175. for _, imdbID := range l.Movies {
  176. if m, ok := c.Get(imdbID); ok {
  177. ms = append(ms, m)
  178. }
  179. }
  180. t.Execute(w, map[string]interface{}{
  181. "List": l,
  182. "Description": template.HTML(
  183. blackfriday.Run(
  184. []byte(strings.ReplaceAll(l.Description, "\r\n", "\n")),
  185. blackfriday.WithExtensions(blackfriday.CommonExtensions))),
  186. "Movies": ms,
  187. "ListJSON": string(b),
  188. "Errors": errs,
  189. })
  190. })
  191. }
  192. func Files(c *movies.Collection) http.HandlerFunc {
  193. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  194. if query := r.URL.Query().Get("query"); query != "" {
  195. matches := c.SearchFiles(query)
  196. for _, match := range matches {
  197. fmt.Fprintf(w, "%s\n", match)
  198. }
  199. return
  200. }
  201. t, err := template.ParseFiles("templates/files.html")
  202. if err != nil {
  203. fmt.Println(err)
  204. return
  205. }
  206. var errs []error
  207. if r.Method == "POST" && r.FormValue("scan") == "1" {
  208. if err := c.ScanFiles(os.Getenv("FILE_ROOT")); err != nil {
  209. errs = append(errs, err)
  210. }
  211. }
  212. t.Execute(w, map[string]interface{}{
  213. "Files": c.AllFiles(),
  214. "Errors": errs,
  215. })
  216. })
  217. }
  218. func File(c *movies.Collection, prefix string) http.HandlerFunc {
  219. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  220. file := "/" + strings.TrimPrefix(r.URL.Path, prefix)
  221. c.Files.Serve(w, r, file)
  222. })
  223. }