Browse Source

Add goroutines for asynchronous IO

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

109
DirectoryIndexers/DirectoryIndexer.go

@ -39,55 +39,74 @@ 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 {
extension := filepath.Ext(video.Name())[1:] wg.Add(1)
// check if extension is one of valid yt-dlp extensions, if not ignore file go func(video os.FileInfo) {
switch extension {
case "3gp", extension := filepath.Ext(video.Name())[1:]
"aac", // check if extension is one of valid yt-dlp extensions, if not ignore file
"flv", switch extension {
"m4a", case "3gp",
"mp3", "aac",
"mp4", "flv",
"webm": "m4a",
"mp3",
"mp4",
"webm":
break break
default: default:
continue wg.Done()
} return
}
id := filenameToID(video.Name())
id := filenameToID(video.Name())
videoObject := VideoFile{
Filename: video.Name(), videoObject := VideoFile{
Extension: extension, Filename: video.Name(),
Title: filenameToTitle(video.Name(), extension), Extension: extension,
Id: id, Title: filenameToTitle(video.Name(), extension),
} Id: id,
}
metadataFilename := strings.TrimSuffix(video.Name(), filepath.Ext(video.Name())) + ".info.json"
jsonMetadataFile, err := os.Open(path + metadataFilename) metadataFilename := strings.TrimSuffix(video.Name(), filepath.Ext(video.Name())) + ".info.json"
if err != nil { jsonMetadataFile, err := os.Open(path + metadataFilename)
FL.Files[id] = videoObject if err != nil {
continue FL.Lock()
} FL.Files[id] = videoObject
metadataBytes, _ := ioutil.ReadAll(jsonMetadataFile) FL.Unlock()
_ = jsonMetadataFile.Close() wg.Done()
return
metadata, err := ParseMetadata(metadataBytes) }
if err != nil { metadataBytes, _ := ioutil.ReadAll(jsonMetadataFile)
FL.Files[id] = videoObject _ = jsonMetadataFile.Close()
continue
} metadata, err := ParseMetadata(metadataBytes)
if err != nil {
FL.Files[id] = VideoFile{ FL.Lock()
Filename: video.Name(), FL.Files[id] = videoObject
Extension: extension, FL.Unlock()
Title: filenameToTitle(video.Name(), extension), wg.Done()
Id: id, return
Metadata: metadata, }
}
FL.Lock()
FL.Files[id] = VideoFile{
Filename: video.Name(),
Extension: extension,
Title: filenameToTitle(video.Name(), extension),
Id: id,
Metadata: metadata,
}
FL.Unlock()
wg.Done()
}(video)
} }
wg.Wait()
results <- FL results <- FL
close(results) close(results)

Loading…
Cancel
Save