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. 28
      DirectoryIndexers/DirectoryIndexer.go
  2. 8
      PageHandlers/Search.go
  3. 3
      PageHandlers/View.go
  4. 4
      PageHandlers/templates/index.html
  5. 3
      main.go

28
DirectoryIndexers/DirectoryIndexer.go

@ -25,7 +25,7 @@ type VideoFile struct {
Metadata Metadata Metadata Metadata
} }
func Index(path string, results chan FileList) { func Index(path string, results chan FileList, oldFileList *FileList) {
var FL FileList var FL FileList
// Initialize the RWMutex HERE manually because *IT IS A POINTER TO A MUTEX*, so it defaults to a nil value // 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()) id := filenameToID(video.Name())
// file has already been added before
if _, ok := oldFileList.Files[id]; ok {
FL.Lock() FL.Lock()
FL.Files[id] = VideoFile{ oldFileList.RLock()
FL.Files[id] = oldFileList.Files[id]
FL.Unlock()
oldFileList.RUnlock()
wg.Done()
return
}
videoObject := VideoFile{
Filename: video.Name(), Filename: video.Name(),
Extension: extension, Extension: extension,
Title: filenameToTitle(video.Name(), extension), Title: filenameToTitle(video.Name(), extension),
Id: id, Id: id,
Metadata: Metadata{}, 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() FL.Unlock()
_ = bar.Add(1) _ = bar.Add(1)
@ -89,7 +111,7 @@ func Index(path string, results chan FileList) {
results <- FL results <- FL
close(results) close(results)
fmt.Println("Archive scan finished.") fmt.Println("\nArchive scan finished.")
} }
func filenameToID(filename string) string { 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) results = append(results, video)
break break
} }
addAsResult := false
if strings.Contains(strings.ToUpper(video.Title), strings.ToUpper(keys[0])) { 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) results = append(results, video)
continue
} }
} }

3
PageHandlers/View.go

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"reflect"
"ytdlp-viewer/DirectoryIndexers" "ytdlp-viewer/DirectoryIndexers"
) )
@ -32,7 +31,7 @@ func View(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexe
} }
// if no metadata loaded, do so // 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) metadata, err := DirectoryIndexers.LoadMetadata(FL.Files[keys[0]], path)
if err == nil { if err == nil {
var fileObject DirectoryIndexers.VideoFile var fileObject DirectoryIndexers.VideoFile

4
PageHandlers/templates/index.html

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

3
main.go

@ -29,12 +29,13 @@ func main() {
resultChannel := make(chan DirectoryIndexers.FileList) resultChannel := make(chan DirectoryIndexers.FileList)
fmt.Println("Starting scanner at", path) fmt.Println("Starting scanner at", path)
go DirectoryIndexers.Index(path, resultChannel) go DirectoryIndexers.Index(path, resultChannel, &FL)
refreshedFL := <-resultChannel refreshedFL := <-resultChannel
FL.Lock() FL.Lock()
FL.Files = refreshedFL.Files FL.Files = refreshedFL.Files
FL.Unlock() FL.Unlock()
fmt.Println("File list refreshed")
time.Sleep(60 * time.Second) time.Sleep(60 * time.Second)
} }

Loading…
Cancel
Save