perf(file): optimize performance of detectSubtitles function in file.go

- Improve performance of the detectSubtitles function in the file.go file for querying subtitle files
- Enhance user experience, especially when filebrowser supports network file systems in the future
This commit is contained in:
gdy666 2023-08-11 00:54:10 +08:00
parent 184b7c14f2
commit b54d5d2083

View File

@ -43,6 +43,7 @@ type FileInfo struct {
Content string `json:"content,omitempty"`
Checksums map[string]string `json:"checksums,omitempty"`
Token string `json:"token,omitempty"`
currentDir []os.FileInfo `json:"-"`
}
// FileOptions are the options when getting a file info.
@ -294,15 +295,23 @@ func (i *FileInfo) detectSubtitles() {
// detect multiple languages. Base*.vtt
// TODO: give subtitles descriptive names (lang) and track attributes
parentDir := strings.TrimRight(i.Path, i.Name)
dir, err := afero.ReadDir(i.Fs, parentDir)
if err == nil {
var dir []os.FileInfo
if len(i.currentDir) > 0 {
dir = i.currentDir
} else {
var err error
dir, err = afero.ReadDir(i.Fs, parentDir)
if err != nil {
return
}
}
base := strings.TrimSuffix(i.Name, ext)
for _, f := range dir {
if !f.IsDir() && strings.HasPrefix(f.Name(), base) && strings.HasSuffix(f.Name(), ".vtt") {
i.Subtitles = append(i.Subtitles, path.Join(parentDir, f.Name()))
}
}
}
}
func (i *FileInfo) readListing(checker rules.Checker, readHeader bool) error {
@ -349,6 +358,7 @@ func (i *FileInfo) readListing(checker rules.Checker, readHeader bool) error {
IsSymlink: isSymlink,
Extension: filepath.Ext(name),
Path: fPath,
currentDir: dir,
}
if file.IsDir {