Browse Source

Load partial metadata for indexing + dont load already loaded files

master
PrivateGER 3 years ago
parent
commit
b0312235ab
No known key found for this signature in database GPG Key ID: B9918E382ED08059
  1. 30
      DirectoryIndexers/DirectoryIndexer.go
  2. 8
      PageHandlers/Search.go
  3. 3
      PageHandlers/View.go
  4. 4
      PageHandlers/templates/index.html
  5. 3
      main.go

30
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 {

8
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
}
}

3
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

4
PageHandlers/templates/index.html

@ -16,8 +16,8 @@
{{range .Files}}
<p><a href="/view?id={{.Id}}">{{.Id}} - {{.Title}}</a>
{{ if .Metadata.ChannelID }} [M] {{ end }}
{{ if .Metadata.ChannelID }} - {{.Metadata.Channel }} {{ end }}<br /></p>
{{ if .Metadata.Channel }} [M] {{ end }}
{{ if .Metadata.Channel }} - {{.Metadata.Channel }} {{ end }}<br /></p>
{{end}}
</div>
{{end}}

3
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)
}

Loading…
Cancel
Save