perf(backend): optimize subtitles detection performance (#2637)

This commit is contained in:
古大羊 2023-08-26 20:53:18 +08:00 committed by GitHub
parent 2c97573301
commit 374bbd3ec1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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