Browse Source

Add goroutines for asynchronous IO

master
PrivateGER 3 years ago
parent
commit
2e84d8a9b8
Signed by: PrivateGER GPG Key ID: CAE625C962F94C67
  1. 25
      DirectoryIndexers/DirectoryIndexer.go

25
DirectoryIndexers/DirectoryIndexer.go

@ -39,7 +39,12 @@ func Index(path string, results chan FileList) {
}
FL.Files = make(map[string]VideoFile)
var wg sync.WaitGroup
for _, video := range fileList {
wg.Add(1)
go func(video os.FileInfo) {
extension := filepath.Ext(video.Name())[1:]
// check if extension is one of valid yt-dlp extensions, if not ignore file
switch extension {
@ -52,7 +57,8 @@ func Index(path string, results chan FileList) {
"webm":
break
default:
continue
wg.Done()
return
}
id := filenameToID(video.Name())
@ -67,18 +73,25 @@ func Index(path string, results chan FileList) {
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
continue
FL.Unlock()
wg.Done()
return
}
metadataBytes, _ := ioutil.ReadAll(jsonMetadataFile)
_ = jsonMetadataFile.Close()
metadata, err := ParseMetadata(metadataBytes)
if err != nil {
FL.Lock()
FL.Files[id] = videoObject
continue
FL.Unlock()
wg.Done()
return
}
FL.Lock()
FL.Files[id] = VideoFile{
Filename: video.Name(),
Extension: extension,
@ -86,8 +99,14 @@ func Index(path string, results chan FileList) {
Id: id,
Metadata: metadata,
}
FL.Unlock()
wg.Done()
}(video)
}
wg.Wait()
results <- FL
close(results)

Loading…
Cancel
Save