Browse Source

Add files page

Gildas Chabot 4 years ago
parent
commit
e8200dabc8
4 changed files with 96 additions and 0 deletions
  1. 30 0
      collection.go
  2. 4 0
      files.go
  3. 37 0
      pages/pages.go
  4. 25 0
      templates/files.html

+ 30 - 0
collection.go

@@ -4,6 +4,7 @@ import (
4 4
 	"encoding/json"
5 5
 	"fmt"
6 6
 	"io/ioutil"
7
+	"os"
7 8
 	"sync"
8 9
 )
9 10
 
@@ -14,6 +15,8 @@ type Collection struct {
14 15
 	Lists   []*List
15 16
 	listMap map[string]*List
16 17
 
18
+	Files *FileSource
19
+
17 20
 	mutex      sync.RWMutex
18 21
 	hasChanged bool
19 22
 }
@@ -163,3 +166,30 @@ func (c *Collection) UpdateList(l *List) {
163 166
 
164 167
 	c.hasChanged = true
165 168
 }
169
+
170
+func (c *Collection) AllFiles() []string {
171
+	if c.Files == nil {
172
+		return nil
173
+	}
174
+	return c.Files.Files
175
+}
176
+
177
+func (c *Collection) SearchFiles(query string) []string {
178
+	return c.Files.Search(query)
179
+}
180
+
181
+func (c *Collection) ScanFiles(root string) error {
182
+	c.mutex.Lock()
183
+	defer c.mutex.Unlock()
184
+
185
+	if c.Files == nil {
186
+		c.Files = &FileSource{}
187
+	}
188
+
189
+	if err := c.Files.Scan(os.Getenv("FILE_ROOT")); err != nil {
190
+		return err
191
+	}
192
+
193
+	c.hasChanged = true
194
+	return nil
195
+}

+ 4 - 0
files.go

@@ -35,6 +35,10 @@ func (fs *FileSource) Scan(root string) error {
35 35
 }
36 36
 
37 37
 func (fs *FileSource) Search(query string) []string {
38
+	if fs == nil {
39
+		return nil
40
+	}
41
+
38 42
 	split := strings.Split(query, " ")
39 43
 
40 44
 	var res []string

+ 37 - 0
pages/pages.go

@@ -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
+}

+ 25 - 0
templates/files.html

@@ -0,0 +1,25 @@
1
+<html>
2
+    <head>
3
+        <title>gildas.ch</title>
4
+
5
+        <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
6
+        <link rel="stylesheet" href="/style.css">
7
+    </head>
8
+    <body>
9
+        {{ range $e := .Errors }}
10
+        <div class="error">{{ $e }}</div>
11
+        {{ end }}
12
+
13
+        <form method="post">
14
+            <p>Scan files: <input type="submit" />
15
+                <input type="hidden" name="scan" value="1" />
16
+            </p>
17
+        </form>
18
+
19
+        <ul class="file-list">
20
+            {{ range $f := .Files }}
21
+            <li>{{ $f }}</li>
22
+            {{ end }}
23
+        </ul>
24
+    </body>
25
+</html>