From 979f59a13de45519eba9da90401891ad478516f2 Mon Sep 17 00:00:00 2001 From: PrivateGER Date: Sat, 16 Oct 2021 17:49:58 +0200 Subject: [PATCH] Restructure code + switch to RWMutex --- .../DirectoryIndexer.go | 26 ++-- PageHandlers/Index.go | 43 ++++++ PageHandlers/Search.go | 66 +++++++++ PageHandlers/View.go | 58 ++++++++ .../templates}/index.html | 0 .../templates}/search.html | 0 .../templates}/view.html | 11 +- go.mod | 4 +- main.go | 139 ++---------------- 9 files changed, 207 insertions(+), 140 deletions(-) rename DirectoryIndexer.go => DirectoryIndexers/DirectoryIndexer.go (84%) create mode 100644 PageHandlers/Index.go create mode 100644 PageHandlers/Search.go create mode 100644 PageHandlers/View.go rename {templates => PageHandlers/templates}/index.html (100%) rename {templates => PageHandlers/templates}/search.html (100%) rename {templates => PageHandlers/templates}/view.html (70%) diff --git a/DirectoryIndexer.go b/DirectoryIndexers/DirectoryIndexer.go similarity index 84% rename from DirectoryIndexer.go rename to DirectoryIndexers/DirectoryIndexer.go index 980ff59..54d5557 100644 --- a/DirectoryIndexer.go +++ b/DirectoryIndexers/DirectoryIndexer.go @@ -1,4 +1,4 @@ -package main +package DirectoryIndexers import ( "fmt" @@ -11,8 +11,8 @@ import ( ) type FileList struct { - files map[string]VideoFile - mu sync.Mutex + Files map[string]VideoFile + *sync.RWMutex } type VideoFile struct { @@ -22,13 +22,12 @@ type VideoFile struct { Id string } -var FL FileList +func Index(path string, results chan FileList) { + var FL FileList -func ScanDirectory(group *sync.WaitGroup, path string) { - defer group.Done() - - FL.mu.Lock() - defer FL.mu.Unlock() + // Initialize the RWMutex HERE manually because *IT IS A POINTER TO A MUTEX*, so it defaults to a nil value + FL.RWMutex = &sync.RWMutex{} + FL.Lock() fmt.Println("Scanning archive...") @@ -38,7 +37,7 @@ func ScanDirectory(group *sync.WaitGroup, path string) { return } - FL.files = make(map[string]VideoFile) + FL.Files = make(map[string]VideoFile) for _, video := range fileList { extension := filepath.Ext(video.Name())[1:] // check if extension is one of valid yt-dlp extensions, if not ignore file @@ -57,7 +56,7 @@ func ScanDirectory(group *sync.WaitGroup, path string) { id := filenameToID(video.Name()) - FL.files[id] = VideoFile{ + FL.Files[id] = VideoFile{ Filename: video.Name(), Extension: extension, Title: filenameToTitle(video.Name(), extension), @@ -65,6 +64,11 @@ func ScanDirectory(group *sync.WaitGroup, path string) { } } + FL.Unlock() + + results <- FL + close(results) + fmt.Println("Archive scan finished.") } diff --git a/PageHandlers/Index.go b/PageHandlers/Index.go new file mode 100644 index 0000000..cf322a6 --- /dev/null +++ b/PageHandlers/Index.go @@ -0,0 +1,43 @@ +package PageHandlers + +import ( + _ "embed" + "fmt" + "html/template" + "net/http" + "strconv" + "ytdlp-viewer/DirectoryIndexers" +) + +//go:embed templates/index.html +var indexTmplSource string +var indexTmpl *template.Template + +type IndexPageData struct { + FileCount string + Files map[string]DirectoryIndexers.VideoFile +} + +func init() { + var err error + indexTmpl = template.New("index.tmpl") + indexTmpl, err = indexTmpl.Parse(indexTmplSource) + if err != nil { + fmt.Println(err) + } +} + +func Index(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexers.FileList) { + FL.RLock() + defer FL.RUnlock() + + data := IndexPageData{ + FileCount: strconv.Itoa(len(FL.Files)), + Files: FL.Files, + } + + err := indexTmpl.Execute(writer, data) + if err != nil { + fmt.Println(err) + } +} diff --git a/PageHandlers/Search.go b/PageHandlers/Search.go new file mode 100644 index 0000000..4b9e7fc --- /dev/null +++ b/PageHandlers/Search.go @@ -0,0 +1,66 @@ +package PageHandlers + +import ( + _ "embed" + "fmt" + "html/template" + "log" + "net/http" + "strconv" + "strings" + "ytdlp-viewer/DirectoryIndexers" +) + +//go:embed templates/search.html +var searchTmplSource string +var searchTmpl *template.Template + +func init() { + var err error + searchTmpl = template.New("search.tmpl") + searchTmpl, err = searchTmpl.Parse(searchTmplSource) + if err != nil { + fmt.Println(err) + } +} + +type SearchPageData struct { + Results []DirectoryIndexers.VideoFile + ResultCount string + SearchTerm string +} + +func SearchHandler(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexers.FileList) { + FL.RLock() + defer FL.RUnlock() + + keys, ok := request.URL.Query()["term"] + if !ok || len(keys[0]) < 1 { + log.Println("Url Param 'term' is missing") + return + } + + var results []DirectoryIndexers.VideoFile + + for _, video := range FL.Files { + if video.Id == keys[0] { + results = append(results, video) + break + } + if strings.Contains(strings.ToUpper(video.Title), strings.ToUpper(keys[0])) { + results = append(results, video) + continue + } + } + + data := SearchPageData{ + Results: results, + ResultCount: strconv.Itoa(len(results)), + SearchTerm: keys[0], + } + + err := searchTmpl.Execute(writer, data) + if err != nil { + fmt.Println(err) + } +} diff --git a/PageHandlers/View.go b/PageHandlers/View.go new file mode 100644 index 0000000..2ab8cec --- /dev/null +++ b/PageHandlers/View.go @@ -0,0 +1,58 @@ +package PageHandlers + +import ( + _ "embed" + "fmt" + "html/template" + "log" + "net/http" + "ytdlp-viewer/DirectoryIndexers" +) + +//go:embed templates/view.html +var viewTmplSource string +var viewTmpl *template.Template + +type ViewPageData struct { + Title string + Filename string + Id string + Extension string +} + +func init() { + var err error + viewTmpl = template.New("view.tmpl") + viewTmpl, err = viewTmpl.Parse(viewTmplSource) + if err != nil { + fmt.Println(err) + } +} + +func View(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexers.FileList) { + FL.RLock() + defer FL.RUnlock() + + keys, ok := request.URL.Query()["id"] + if !ok || len(keys[0]) < 1 { + log.Println("Url Param 'id' is missing") + return + } + + if _, ok := FL.Files[keys[0]]; !ok { + return + } + + video := FL.Files[keys[0]] + data := ViewPageData{ + Title: video.Title, + Filename: video.Filename, + Id: video.Id, + Extension: video.Extension, + } + + err := viewTmpl.Execute(writer, data) + if err != nil { + fmt.Println(err) + } +} \ No newline at end of file diff --git a/templates/index.html b/PageHandlers/templates/index.html similarity index 100% rename from templates/index.html rename to PageHandlers/templates/index.html diff --git a/templates/search.html b/PageHandlers/templates/search.html similarity index 100% rename from templates/search.html rename to PageHandlers/templates/search.html diff --git a/templates/view.html b/PageHandlers/templates/view.html similarity index 70% rename from templates/view.html rename to PageHandlers/templates/view.html index 04acccb..b6fe92f 100644 --- a/templates/view.html +++ b/PageHandlers/templates/view.html @@ -5,16 +5,23 @@ {{.Title}} | {{.Id}} + -