Browse Source

新增,支持用户自定义添加视频后缀名,相同提交见 #22 。 fix #25

Signed-off-by: allan716 <[email protected]>
allan716 4 years ago
parent
commit
0fe6f0d56f
4 changed files with 32 additions and 25 deletions
  1. 5 0
      internal/pkg/config.go
  2. 5 4
      internal/pkg/globalvalue.go
  3. 17 18
      internal/pkg/util.go
  4. 5 3
      internal/types/config.go

+ 5 - 0
internal/pkg/config.go

@@ -4,6 +4,7 @@ import (
 	"errors"
 	"github.com/allanpk716/ChineseSubFinder/internal/types"
 	"github.com/spf13/viper"
+	"strings"
 	"sync"
 )
 
@@ -18,6 +19,10 @@ func GetConfig() *types.Config {
 		if err != nil {
 			panic("GetConfig - readConfig " + err.Error())
 		}
+		// 读取用户自定义的视频后缀名列表
+		for _, customExt := range strings.Split(config.CustomVideoExts, ",") {
+			customVideoExts = append(customVideoExts, "."+customExt)
+		}
 	})
 	return config
 }

+ 5 - 4
internal/pkg/globalvalue.go

@@ -2,8 +2,9 @@ package pkg
 
 // util.go
 var (
-	defDebugFolder = ""
-	defTmpFolder   = ""
-	wantedExtList  = make([]string, 0) // 人工确认的需要监控的视频后缀名
-	defExtList     = make([]string, 0) // 内置支持的视频后缀名列表
+	defDebugFolder  = ""
+	defTmpFolder    = ""
+	wantedExtMap    = make(map[string]string) // 人工确认的需要监控的视频后缀名
+	defExtMap       = make(map[string]string) // 内置支持的视频后缀名列表
+	customVideoExts = make([]string, 0)       // 用户额外自定义的视频后缀名列表
 )

+ 17 - 18
internal/pkg/util.go

@@ -276,26 +276,25 @@ func SearchMatchedVideoFile(dir string) ([]string, error) {
 
 // IsWantedVideoExtDef 后缀名是否符合规则
 func IsWantedVideoExtDef(fileName string) bool {
-	// TODO 强制使用固定的视频后缀名匹配列表,后续有需求再考虑额实现外部可配置的列表
-
-	if len(wantedExtList) < 1 {
-		defExtList = append(defExtList, common.VideoExtMp4)
-		defExtList = append(defExtList, common.VideoExtMkv)
-		defExtList = append(defExtList, common.VideoExtRmvb)
-		defExtList = append(defExtList, common.VideoExtIso)
-
-		wantedExtList = append(defExtList, common.VideoExtMp4)
-		wantedExtList = append(defExtList, common.VideoExtMkv)
-		wantedExtList = append(defExtList, common.VideoExtRmvb)
-		wantedExtList = append(defExtList, common.VideoExtIso)
-	}
-	fileName = strings.ToLower(filepath.Ext(fileName))
-	for _, s := range wantedExtList {
-		if s == fileName {
-			return true
+
+	if len(wantedExtMap) < 1 {
+		defExtMap[common.VideoExtMp4] = common.VideoExtMp4
+		defExtMap[common.VideoExtMkv] = common.VideoExtMkv
+		defExtMap[common.VideoExtRmvb] = common.VideoExtRmvb
+		defExtMap[common.VideoExtIso] = common.VideoExtIso
+
+		wantedExtMap[common.VideoExtMp4] = common.VideoExtMp4
+		wantedExtMap[common.VideoExtMkv] = common.VideoExtMkv
+		wantedExtMap[common.VideoExtRmvb] = common.VideoExtRmvb
+		wantedExtMap[common.VideoExtIso] = common.VideoExtIso
+
+		for _, videoExt := range customVideoExts {
+			wantedExtMap[videoExt] = videoExt
 		}
 	}
-	return false
+	fileExt := strings.ToLower(filepath.Ext(fileName))
+	_, bFound := wantedExtMap[fileExt]
+	return bFound
 }
 
 func GetEpisodeKeyName(season, eps int) string {

+ 5 - 3
internal/types/config.go

@@ -13,7 +13,9 @@ type Config struct {
 	EmbyConfig                    emby.EmbyConfig // Emby API 高阶设置参数
 	SaveMultiSub                  bool            // 保存多个网站的 Top 1 字幕
 	SaveOneSeasonSub              bool            // 保存整个季度的字幕
-	MovieFolder                   string          // 电影文件夹
-	SeriesFolder                  string          // 连续剧文件夹
-	AnimeFolder                   string          // 日本动画文件夹,很可能不会实现该功能
+	CustomVideoExts               string          // 自定义视频扩展名,多个扩展名用英文逗号分隔。是在原有基础上新增。
+
+	MovieFolder  string // 电影文件夹
+	SeriesFolder string // 连续剧文件夹
+	AnimeFolder  string // 日本动画文件夹,很可能不会实现该功能
 }