-
Notifications
You must be signed in to change notification settings - Fork 2
/
go-player.go
339 lines (306 loc) · 7.75 KB
/
go-player.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package main
import (
"bufio"
"fmt"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
)
// DATA STRUCTURES
type ConfigDetails struct {
firstStart bool
templateDirectory string
FilePathList []string
templateFileList []string
}
type Movie struct {
FullFilePath string
FileName string
Folder string
}
type PageData struct {
MovieList []Movie
CurrentFilm string
Player Player
}
// GLOBALS DECLARED HERE
var extensionList = [][]byte{
{'.', 'm', 'k', 'v'},
{'.', 'm', 'p', 'g'},
{'.', 'a', 'v', 'i'},
{'.', 'm', '4', 'v'},
{'.', 'm', 'p', '4'},
{'.', 'd', 'a', 't'}}
var config = ConfigDetails{}
var templates map[string]*template.Template
var pageData = PageData{}
var currentPath string
var currentBase string
// FUNCTIONS FROM HERE
func visit(path string, f os.FileInfo, err error) error {
bpath := []byte(strings.ToLower(path))
bpath = bpath[len(bpath)-4:]
for i := 0; i < len(extensionList); i++ {
if reflect.DeepEqual(bpath, extensionList[i]) {
// get folder name
movie := Movie{path, f.Name(), getFolderName(path)}
pageData.MovieList = append(pageData.MovieList, movie)
}
}
return nil
}
func getFolderName(path string) string {
f := filepath.Dir(path)
if f == currentPath {
return currentBase
}
return strings.Replace(f, currentPath, currentBase, 1)
}
// func getFolderNameByDepth(path string, depth int) string {
// f := filepath.Dir(path)
// sep := string(os.PathSeparator)
// tmp := strings.Split(f, sep)
// d := len(tmp)
// if d <= depth {
// return f
// }
// f = ""
// for i := depth; i > 0; i-- {
// f += tmp[d-i]
// if i > 1 {
// f += sep
// }
// }
// return f
// }
func generateMovies() error {
startingCounter := len(pageData.MovieList)
if len(config.FilePathList) > 0 {
for index, path := range config.FilePathList {
currentPath = filepath.Dir(path)
currentBase = filepath.Base(currentPath)
fmt.Println("Traversing: ", path)
err := filepath.Walk(path, visit)
if err != nil || len(pageData.MovieList) == startingCounter {
config.FilePathList = append(config.FilePathList[:index], config.FilePathList[index+1:]...)
if err == nil {
err = fmt.Errorf("No files found.")
}
return err
}
startingCounter = len(pageData.MovieList)
}
}
fmt.Printf("file import complete: %d files imported\n", len(pageData.MovieList))
return nil
}
// THE VIEW CODE IS HERE
func generateTemplates() {
templates = make(map[string]*template.Template)
modulus := template.FuncMap{"mod": func(i, j int) bool { return i%j == 0 }}
for _, tmpl := range config.templateFileList {
t := template.New("base.html").Funcs(modulus)
templates[tmpl] = template.Must(t.ParseFiles(config.templateDirectory+"base.html", config.templateDirectory+tmpl))
}
}
func renderTemplate(pageStruct interface{}, w http.ResponseWriter, tmpl string) {
var err error
if pageStruct == nil {
err = templates[tmpl].Execute(w, pageData)
} else {
err = templates[tmpl].Execute(w, pageStruct)
}
if err != nil {
log.Printf("The follwing error occurred: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func refreshList() error {
if pageData.Player.Playing {
player := pageData.Player
currentFilm := pageData.CurrentFilm
pageData = PageData{}
err := generateMovies()
pageData.CurrentFilm = currentFilm
pageData.Player = player
return err
}
pageData = PageData{}
err := generateMovies()
return err
}
// HANDLERS ARE HERE
func indexHandler(w http.ResponseWriter, r *http.Request) {
if config.firstStart {
http.Redirect(w, r, "/setup", http.StatusFound)
return
}
err := r.ParseForm()
if err != nil {
panic(err)
}
group := r.URL.Query().Get("grp")
var tmpl string
if pageData.Player.Playing {
if group == "f" {
tmpl = "alreadyplayingf.html"
} else {
tmpl = "alreadyplaying.html"
}
} else {
if group == "f" {
tmpl = "indexf.html"
} else {
tmpl = "index.html"
}
}
if r.Method == "POST" {
err := refreshList()
if err != nil {
panic(err)
}
}
renderTemplate(nil, w, tmpl)
}
func aboutHandler(w http.ResponseWriter, r *http.Request) {
renderTemplate(nil, w, "about.html")
}
func setupHandler(w http.ResponseWriter, r *http.Request) {
tmpl := "setup.html"
err := r.ParseForm()
if err != nil {
panic(err)
}
if r.Method == "POST" {
config.firstStart = false
if _, ok := r.Form["addFilePath"]; ok {
alreadyExists := false
newPath := r.Form["filepath"][0]
for _, path := range config.FilePathList {
if path == newPath {
alreadyExists = true
break
}
}
if !alreadyExists {
config.FilePathList = append(config.FilePathList, r.Form["filepath"][0])
}
refreshCheck(&tmpl)
} else if _, ok := r.Form["deleteFilePath"]; ok {
if i, err := strconv.Atoi(r.Form["deleteFilePath"][0]); err == nil {
config.FilePathList = append(config.FilePathList[:i], config.FilePathList[i+1:]...)
if len(config.FilePathList) == 0 {
config.firstStart = true
}
refreshCheck(&tmpl)
} else {
panic(err)
}
} else if _, ok := r.Form["saveSetup"]; ok {
// check if there is something to save
if len(config.FilePathList) > 0 {
// Open file using READ & WRITE permission.
var file, err = os.OpenFile("config", os.O_RDWR, 0644)
if err != nil {
panic(err)
}
defer file.Close()
for _, path := range config.FilePathList {
_, err := file.WriteString(path + "\n")
if err != nil {
panic(err)
}
}
err = file.Sync()
if err != nil {
panic(err)
}
tmpl = "index.html"
}
}
}
renderTemplate(config, w, tmpl)
}
func refreshCheck(tmpl *string) {
if err := refreshList(); err != nil {
config.firstStart = true
*tmpl = "nothingfound.html"
}
}
func movieHandler(w http.ResponseWriter, r *http.Request) {
command := r.URL.Query().Get("command")
film := r.URL.Query().Get("movie")
if pageData.Player.Playing == false {
if film == "" {
log.Println("No film was selected")
http.Redirect(w, r, "/", http.StatusFound)
return
}
err := pageData.Player.StartFilm(film)
if err != nil {
log.Printf("Following error occurred: %v\n", err)
http.Redirect(w, r, "/", http.StatusFound)
return
}
pageData.CurrentFilm = film
} else if pageData.Player.Playing && (film == "" || pageData.Player.FilmName == film) {
if command == "kill" {
err := pageData.Player.EndFilm()
if err != nil {
log.Printf("Following error occurred: %v\n", err)
}
http.Redirect(w, r, "/", http.StatusFound)
return
} else if command != "" {
err := pageData.Player.SendCommandToFilm(command)
if err != nil {
log.Printf("Following error occurred: %v\n", err)
}
}
} else {
http.Redirect(w, r, "/", http.StatusFound)
return
}
renderTemplate(nil, w, "movie.html")
}
// IT ALL STARTS HERE
func initConfigDetails() {
config.firstStart = true
config.templateDirectory = "./templates/"
config.templateFileList = append(config.templateFileList,
"index.html", "indexf.html", "about.html", "movie.html", "alreadyplaying.html", "alreadyplayingf.html", "setup.html", "nothingfound.html")
}
func initPaths() {
file, err := os.Open("config")
if err != nil {
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
config.FilePathList = append(config.FilePathList, scanner.Text())
}
if err := scanner.Err(); err != nil {
panic(err)
}
if len(config.FilePathList) > 0 {
config.firstStart = false
}
}
func main() {
initConfigDetails()
generateTemplates()
initPaths()
refreshList()
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/", indexHandler)
http.HandleFunc("/about", aboutHandler)
http.HandleFunc("/setup", setupHandler)
http.HandleFunc("/movie", movieHandler)
http.ListenAndServe(":8080", nil)
}