2 Commits 039aa791f2 ... 4d72c98c35

Author SHA1 Message Date
  Gildas Chabot 4d72c98c35 Add files to movies 4 years ago
  Gildas Chabot e8200dabc8 Add files page 4 years ago
6 changed files with 147 additions and 11 deletions
  1. 30 0
      collection.go
  2. 4 0
      files.go
  3. 2 0
      movies.go
  4. 64 11
      pages/pages.go
  5. 25 0
      templates/files.html
  6. 22 0
      templates/movie.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

+ 2 - 0
movies.go

@@ -16,6 +16,8 @@ type Movie struct {
16 16
 	Year     string
17 17
 	Runtime  string
18 18
 
19
+	Files []string
20
+
19 21
 	OMDB OMDBMovie
20 22
 }
21 23
 

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

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

+ 22 - 0
templates/movie.html

@@ -18,6 +18,28 @@
18 18
             <div>{{ .Movie.Director }}</div>
19 19
         </div>
20 20
 
21
+        <ul class="file-list">
22
+            {{ range $f := .Movie.Files }}
23
+            <li>{{ $f }}</li>
24
+            {{ end }}
25
+        </ul>
26
+
27
+        <div>
28
+            <form method="post">
29
+                <p>Update file list: <input type="submit" /><br />
30
+                    <textarea name="files" style="width:500px;height:140px;">{{ .Files }}</textarea>
31
+                </p>
32
+            </form>
33
+
34
+            <form method="get">
35
+                <p>
36
+                    Search by filename: <input type="text" name="file_query" value="{{ .FileQuery }}" />
37
+                    <input type="submit" />
38
+                </p>
39
+            </form>
40
+            <textarea style="width:500px;height:140px;">{{ .FileResults }}</textarea>
41
+        </div>
42
+
21 43
         <form method="post">
22 44
             <p>Update movie: <input type="submit" /><br />
23 45
                 <textarea name="movie_json" style="width:90%;height:500px;">{{ .MovieJSON }}</textarea>