From b0312235ab75f9efe99419c146cf95e7dfcb0bc4 Mon Sep 17 00:00:00 2001 From: PrivateGER Date: Mon, 18 Oct 2021 14:11:52 +0200 Subject: [PATCH] Load partial metadata for indexing + dont load already loaded files --- DirectoryIndexers/DirectoryIndexer.go | 30 +++++++++++++++++++++++---- PageHandlers/Search.go | 8 ++++++- PageHandlers/View.go | 3 +-- PageHandlers/templates/index.html | 4 ++-- main.go | 3 ++- 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/DirectoryIndexers/DirectoryIndexer.go b/DirectoryIndexers/DirectoryIndexer.go index caea679..fe97c59 100644 --- a/DirectoryIndexers/DirectoryIndexer.go +++ b/DirectoryIndexers/DirectoryIndexer.go @@ -25,7 +25,7 @@ type VideoFile struct { Metadata Metadata } -func Index(path string, results chan FileList) { +func Index(path string, results chan FileList, oldFileList *FileList) { var FL FileList // Initialize the RWMutex HERE manually because *IT IS A POINTER TO A MUTEX*, so it defaults to a nil value @@ -66,14 +66,36 @@ func Index(path string, results chan FileList) { id := filenameToID(video.Name()) - FL.Lock() - FL.Files[id] = VideoFile{ + // file has already been added before + if _, ok := oldFileList.Files[id]; ok { + FL.Lock() + oldFileList.RLock() + FL.Files[id] = oldFileList.Files[id] + FL.Unlock() + oldFileList.RUnlock() + + wg.Done() + return + } + + videoObject := VideoFile{ Filename: video.Name(), Extension: extension, Title: filenameToTitle(video.Name(), extension), Id: id, Metadata: Metadata{}, } + + metadata, err := LoadMetadata(videoObject, path) + if err == nil { + videoObject.Metadata = Metadata{ + Channel: metadata.Channel, + Thumbnail: metadata.Thumbnail, + } + } + + FL.Lock() + FL.Files[id] = videoObject FL.Unlock() _ = bar.Add(1) @@ -89,7 +111,7 @@ func Index(path string, results chan FileList) { results <- FL close(results) - fmt.Println("Archive scan finished.") + fmt.Println("\nArchive scan finished.") } func filenameToID(filename string) string { diff --git a/PageHandlers/Search.go b/PageHandlers/Search.go index 7b747a4..fcb5d95 100644 --- a/PageHandlers/Search.go +++ b/PageHandlers/Search.go @@ -33,9 +33,15 @@ func SearchHandler(writer http.ResponseWriter, request *http.Request, FL *Direct results = append(results, video) break } + addAsResult := false if strings.Contains(strings.ToUpper(video.Title), strings.ToUpper(keys[0])) { + addAsResult = true + } + if strings.Contains(strings.ToUpper(video.Metadata.Channel), strings.ToUpper(keys[0])) { + addAsResult = true + } + if addAsResult { results = append(results, video) - continue } } diff --git a/PageHandlers/View.go b/PageHandlers/View.go index 2d38a99..491e2f2 100644 --- a/PageHandlers/View.go +++ b/PageHandlers/View.go @@ -5,7 +5,6 @@ import ( "fmt" "log" "net/http" - "reflect" "ytdlp-viewer/DirectoryIndexers" ) @@ -32,7 +31,7 @@ func View(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexe } // if no metadata loaded, do so - if reflect.ValueOf(FL.Files[keys[0]].Metadata).IsZero() { + if FL.Files[keys[0]].Metadata.ChannelID == "" { metadata, err := DirectoryIndexers.LoadMetadata(FL.Files[keys[0]], path) if err == nil { var fileObject DirectoryIndexers.VideoFile diff --git a/PageHandlers/templates/index.html b/PageHandlers/templates/index.html index 670816e..48e9911 100644 --- a/PageHandlers/templates/index.html +++ b/PageHandlers/templates/index.html @@ -16,8 +16,8 @@ {{range .Files}}

{{.Id}} - {{.Title}} - {{ if .Metadata.ChannelID }} [M] {{ end }} - {{ if .Metadata.ChannelID }} - {{.Metadata.Channel }} {{ end }}

+ {{ if .Metadata.Channel }} [M] {{ end }} + {{ if .Metadata.Channel }} - {{.Metadata.Channel }} {{ end }}

{{end}} {{end}} diff --git a/main.go b/main.go index 9059ed5..70e32d1 100644 --- a/main.go +++ b/main.go @@ -29,12 +29,13 @@ func main() { resultChannel := make(chan DirectoryIndexers.FileList) fmt.Println("Starting scanner at", path) - go DirectoryIndexers.Index(path, resultChannel) + go DirectoryIndexers.Index(path, resultChannel, &FL) refreshedFL := <-resultChannel FL.Lock() FL.Files = refreshedFL.Files FL.Unlock() + fmt.Println("File list refreshed") time.Sleep(60 * time.Second) }