Use os.ReadDir instead of glob for subtitles

This commit is contained in:
Zoe Roux 2024-08-09 01:01:23 +02:00
parent 4e79f38d1e
commit ab86cfca3a

View File

@ -3,6 +3,7 @@ package src
import ( import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -14,17 +15,23 @@ var separator = regexp.MustCompile(`[\s.]+`)
func (mi *MediaInfo) SearchExternalSubtitles() error { func (mi *MediaInfo) SearchExternalSubtitles() error {
base_path := strings.TrimSuffix(mi.Path, filepath.Ext(mi.Path)) base_path := strings.TrimSuffix(mi.Path, filepath.Ext(mi.Path))
matches, err := filepath.Glob(fmt.Sprintf("%s*", base_path)) dir, err := os.ReadDir(filepath.Dir(mi.Path))
if err != nil { if err != nil {
return err return err
} }
outer: outer:
for _, match := range matches { for _, entry := range dir {
match := filepath.Join(filepath.Dir(mi.Path), entry.Name())
if entry.IsDir() || !strings.HasPrefix(match, base_path) {
continue
}
for codec, ext := range SubtitleExtensions { for codec, ext := range SubtitleExtensions {
if strings.HasSuffix(match, ext) { if strings.HasSuffix(match, ext) {
link := fmt.Sprintf( link := fmt.Sprintf(
"%s/direct/%s", "%s/%s/direct/%s",
Settings.RoutePrefix,
base64.RawURLEncoding.EncodeToString([]byte(match)), base64.RawURLEncoding.EncodeToString([]byte(match)),
filepath.Base(match), filepath.Base(match),
) )