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

Loading…
Cancel
Save