|
@@ -5,6 +5,7 @@ import (
|
5
|
5
|
"fmt"
|
6
|
6
|
"html/template"
|
7
|
7
|
"net/http"
|
|
8
|
+ "os"
|
8
|
9
|
"strings"
|
9
|
10
|
|
10
|
11
|
"gogs.gildas.ch/gildas/movies"
|
|
@@ -16,6 +17,7 @@ func Router(c *movies.Collection) http.HandlerFunc {
|
16
|
17
|
homeHandler := Home(c)
|
17
|
18
|
movieHandler := Movie(c)
|
18
|
19
|
listHandler := List(c)
|
|
20
|
+ filesHandler := Files(c)
|
19
|
21
|
|
20
|
22
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
21
|
23
|
path := r.URL.Path
|
|
@@ -23,6 +25,9 @@ func Router(c *movies.Collection) http.HandlerFunc {
|
23
|
25
|
case path == "/":
|
24
|
26
|
homeHandler(w, r)
|
25
|
27
|
return
|
|
28
|
+ case path == "/files":
|
|
29
|
+ filesHandler(w, r)
|
|
30
|
+ return
|
26
|
31
|
case path == "/style.css":
|
27
|
32
|
http.ServeFile(w, r, "templates/style.css")
|
28
|
33
|
return
|
|
@@ -108,15 +113,28 @@ func Movie(c *movies.Collection) http.HandlerFunc {
|
108
|
113
|
}
|
109
|
114
|
|
110
|
115
|
if r.Method == "POST" {
|
111
|
|
- updated, err := movies.Unmarshal([]byte(r.FormValue("movie_json")))
|
112
|
|
- if err != nil {
|
113
|
|
- errs = append(errs, err)
|
114
|
|
- } else if imdbID != updated.IMDBID {
|
115
|
|
- errs = append(errs, fmt.Errorf("you cannot change the imdb id."))
|
116
|
|
- } else {
|
117
|
|
- m = updated
|
118
|
|
- c.Update(updated)
|
|
116
|
+ if movieJSON := r.FormValue("movie_json"); movieJSON != "" {
|
|
117
|
+ updated, err := movies.Unmarshal([]byte(movieJSON))
|
|
118
|
+ if err != nil {
|
|
119
|
+ errs = append(errs, err)
|
|
120
|
+ } else if imdbID != updated.IMDBID {
|
|
121
|
+ errs = append(errs, fmt.Errorf("you cannot change the imdb id."))
|
|
122
|
+ } else {
|
|
123
|
+ m = updated
|
|
124
|
+ c.Update(updated)
|
|
125
|
+ }
|
119
|
126
|
}
|
|
127
|
+
|
|
128
|
+ if files := r.FormValue("files"); files != "" {
|
|
129
|
+ m.Files = strings.Split(files, "\n")
|
|
130
|
+ c.Update(m)
|
|
131
|
+ }
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ fileQuery := r.URL.Query().Get("file_query")
|
|
135
|
+ var fileResults string
|
|
136
|
+ if fileQuery != "" {
|
|
137
|
+ fileResults = strings.Join(c.SearchFiles(fileQuery), "\n")
|
120
|
138
|
}
|
121
|
139
|
|
122
|
140
|
b, err := json.MarshalIndent(m, "", " ")
|
|
@@ -125,9 +143,12 @@ func Movie(c *movies.Collection) http.HandlerFunc {
|
125
|
143
|
}
|
126
|
144
|
|
127
|
145
|
t.Execute(w, map[string]interface{}{
|
128
|
|
- "Movie": m,
|
129
|
|
- "MovieJSON": string(b),
|
130
|
|
- "Errors": errs,
|
|
146
|
+ "Movie": m,
|
|
147
|
+ "MovieJSON": string(b),
|
|
148
|
+ "Files": strings.Join(m.Files, "\n"),
|
|
149
|
+ "FileQuery": fileQuery,
|
|
150
|
+ "FileResults": fileResults,
|
|
151
|
+ "Errors": errs,
|
131
|
152
|
})
|
132
|
153
|
})
|
133
|
154
|
}
|
|
@@ -191,3 +212,35 @@ func List(c *movies.Collection) http.HandlerFunc {
|
191
|
212
|
})
|
192
|
213
|
})
|
193
|
214
|
}
|
|
215
|
+
|
|
216
|
+func Files(c *movies.Collection) http.HandlerFunc {
|
|
217
|
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
218
|
+ if query := r.URL.Query().Get("query"); query != "" {
|
|
219
|
+ matches := c.SearchFiles(query)
|
|
220
|
+
|
|
221
|
+ for _, match := range matches {
|
|
222
|
+ fmt.Fprintf(w, "%s\n", match)
|
|
223
|
+ }
|
|
224
|
+ return
|
|
225
|
+ }
|
|
226
|
+
|
|
227
|
+ t, err := template.ParseFiles("templates/files.html")
|
|
228
|
+ if err != nil {
|
|
229
|
+ fmt.Println(err)
|
|
230
|
+ return
|
|
231
|
+ }
|
|
232
|
+
|
|
233
|
+ var errs []error
|
|
234
|
+
|
|
235
|
+ if r.Method == "POST" && r.FormValue("scan") == "1" {
|
|
236
|
+ if err := c.ScanFiles(os.Getenv("FILE_ROOT")); err != nil {
|
|
237
|
+ errs = append(errs, err)
|
|
238
|
+ }
|
|
239
|
+ }
|
|
240
|
+
|
|
241
|
+ t.Execute(w, map[string]interface{}{
|
|
242
|
+ "Files": c.AllFiles(),
|
|
243
|
+ "Errors": errs,
|
|
244
|
+ })
|
|
245
|
+ })
|
|
246
|
+}
|