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

@ -29,20 +29,21 @@ const PermDir = 0755
// FileInfo describes a file. // FileInfo describes a file.
type FileInfo struct { type FileInfo struct {
*Listing *Listing
Fs afero.Fs `json:"-"` Fs afero.Fs `json:"-"`
Path string `json:"path"` Path string `json:"path"`
Name string `json:"name"` Name string `json:"name"`
Size int64 `json:"size"` Size int64 `json:"size"`
Extension string `json:"extension"` Extension string `json:"extension"`
ModTime time.Time `json:"modified"` ModTime time.Time `json:"modified"`
Mode os.FileMode `json:"mode"` Mode os.FileMode `json:"mode"`
IsDir bool `json:"isDir"` IsDir bool `json:"isDir"`
IsSymlink bool `json:"isSymlink"` IsSymlink bool `json:"isSymlink"`
Type string `json:"type"` Type string `json:"type"`
Subtitles []string `json:"subtitles,omitempty"` Subtitles []string `json:"subtitles,omitempty"`
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,13 +295,21 @@ 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 {
base := strings.TrimSuffix(i.Name, ext) dir = i.currentDir
for _, f := range dir { } else {
if !f.IsDir() && strings.HasPrefix(f.Name(), base) && strings.HasSuffix(f.Name(), ".vtt") { var err error
i.Subtitles = append(i.Subtitles, path.Join(parentDir, f.Name())) 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()))
} }
} }
} }
@ -340,15 +349,16 @@ func (i *FileInfo) readListing(checker rules.Checker, readHeader bool) error {
} }
file := &FileInfo{ file := &FileInfo{
Fs: i.Fs, Fs: i.Fs,
Name: name, Name: name,
Size: f.Size(), Size: f.Size(),
ModTime: f.ModTime(), ModTime: f.ModTime(),
Mode: f.Mode(), Mode: f.Mode(),
IsDir: f.IsDir(), IsDir: f.IsDir(),
IsSymlink: isSymlink, IsSymlink: isSymlink,
Extension: filepath.Ext(name), Extension: filepath.Ext(name),
Path: fPath, Path: fPath,
currentDir: dir,
} }
if file.IsDir { if file.IsDir {