|
@@ -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
|
|
@@ -191,3 +196,35 @@ func List(c *movies.Collection) http.HandlerFunc {
|
191
|
196
|
})
|
192
|
197
|
})
|
193
|
198
|
}
|
|
199
|
+
|
|
200
|
+func Files(c *movies.Collection) http.HandlerFunc {
|
|
201
|
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
202
|
+ if query := r.URL.Query().Get("query"); query != "" {
|
|
203
|
+ matches := c.SearchFiles(query)
|
|
204
|
+
|
|
205
|
+ for _, match := range matches {
|
|
206
|
+ fmt.Fprintf(w, "%s\n", match)
|
|
207
|
+ }
|
|
208
|
+ return
|
|
209
|
+ }
|
|
210
|
+
|
|
211
|
+ t, err := template.ParseFiles("templates/files.html")
|
|
212
|
+ if err != nil {
|
|
213
|
+ fmt.Println(err)
|
|
214
|
+ return
|
|
215
|
+ }
|
|
216
|
+
|
|
217
|
+ var errs []error
|
|
218
|
+
|
|
219
|
+ if r.Method == "POST" && r.FormValue("scan") == "1" {
|
|
220
|
+ if err := c.ScanFiles(os.Getenv("FILE_ROOT")); err != nil {
|
|
221
|
+ errs = append(errs, err)
|
|
222
|
+ }
|
|
223
|
+ }
|
|
224
|
+
|
|
225
|
+ t.Execute(w, map[string]interface{}{
|
|
226
|
+ "Files": c.AllFiles(),
|
|
227
|
+ "Errors": errs,
|
|
228
|
+ })
|
|
229
|
+ })
|
|
230
|
+}
|