Browse Source

Make metadata loading hot

master
PrivateGER 3 years ago
parent
commit
0eedeb9389
No known key found for this signature in database GPG Key ID: B9918E382ED08059
  1. 31
      DirectoryIndexers/DirectoryIndexer.go
  2. 22
      DirectoryIndexers/MetadataParser.go
  3. 18
      PageHandlers/View.go
  4. 2
      main.go

31
DirectoryIndexers/DirectoryIndexer.go

@ -66,42 +66,13 @@ func Index(path string, results chan FileList) {
id := filenameToID(video.Name()) id := filenameToID(video.Name())
videoObject := VideoFile{
Filename: video.Name(),
Extension: extension,
Title: filenameToTitle(video.Name(), extension),
Id: id,
}
metadataFilename := strings.TrimSuffix(video.Name(), filepath.Ext(video.Name())) + ".info.json"
jsonMetadataFile, err := os.Open(path + metadataFilename)
if err != nil {
FL.Lock()
FL.Files[id] = videoObject
FL.Unlock()
wg.Done()
return
}
metadataBytes, _ := ioutil.ReadAll(jsonMetadataFile)
_ = jsonMetadataFile.Close()
metadata, err := ParseMetadata(metadataBytes)
if err != nil {
FL.Lock()
FL.Files[id] = videoObject
FL.Unlock()
wg.Done()
return
}
FL.Lock() FL.Lock()
FL.Files[id] = VideoFile{ FL.Files[id] = 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{},
} }
FL.Unlock() FL.Unlock()

22
DirectoryIndexers/MetadataParser.go

@ -3,6 +3,10 @@ package DirectoryIndexers
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
) )
type Metadata struct { type Metadata struct {
@ -50,3 +54,21 @@ func ParseMetadata(jsonBytes []byte) (Metadata, error) {
} }
return meta, err return meta, err
} }
func LoadMetadata(videoobject VideoFile, path string) (Metadata, error) {
metadataFilename := strings.TrimSuffix(videoobject.Filename, filepath.Ext(videoobject.Filename)) + ".info.json"
jsonMetadataFile, err := os.Open(path + metadataFilename)
if err != nil {
return Metadata{}, err
}
metadataBytes, _ := ioutil.ReadAll(jsonMetadataFile)
_ = jsonMetadataFile.Close()
metadata, err := ParseMetadata(metadataBytes)
if err != nil {
return Metadata{}, err
}
return metadata, nil
}

18
PageHandlers/View.go

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"reflect"
"ytdlp-viewer/DirectoryIndexers" "ytdlp-viewer/DirectoryIndexers"
) )
@ -16,9 +17,9 @@ type ViewPageData struct {
Metadata DirectoryIndexers.Metadata Metadata DirectoryIndexers.Metadata
} }
func View(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexers.FileList) { func View(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexers.FileList, path string) {
FL.RLock() FL.Lock()
defer FL.RUnlock() defer FL.Unlock()
keys, ok := request.URL.Query()["id"] keys, ok := request.URL.Query()["id"]
if !ok || len(keys[0]) < 1 { if !ok || len(keys[0]) < 1 {
@ -30,6 +31,17 @@ func View(writer http.ResponseWriter, request *http.Request, FL *DirectoryIndexe
return return
} }
// if no metadata loaded, do so
if reflect.ValueOf(FL.Files[keys[0]].Metadata).IsZero() {
metadata, err := DirectoryIndexers.LoadMetadata(FL.Files[keys[0]], path)
if err == nil {
var fileObject DirectoryIndexers.VideoFile
fileObject = FL.Files[keys[0]]
fileObject.Metadata = metadata
FL.Files[keys[0]] = fileObject
}
}
video := FL.Files[keys[0]] video := FL.Files[keys[0]]
data := ViewPageData{ data := ViewPageData{
Title: video.Title, Title: video.Title,

2
main.go

@ -50,7 +50,7 @@ func main() {
}) })
http.HandleFunc("/view", func(writer http.ResponseWriter, request *http.Request) { http.HandleFunc("/view", func(writer http.ResponseWriter, request *http.Request) {
fmt.Println(time.Now().String(),request.URL) fmt.Println(time.Now().String(),request.URL)
PageHandlers.View(writer, request, &FL) PageHandlers.View(writer, request, &FL, path)
}) })
http.Handle("/videos/", http.StripPrefix("/videos/", http.FileServer(http.Dir(path)))) http.Handle("/videos/", http.StripPrefix("/videos/", http.FileServer(http.Dir(path))))

Loading…
Cancel
Save