浏览代码

完成缓存目录的统一的重构 fix #115

Signed-off-by: allan716 <[email protected]>
allan716 3 年之前
父节点
当前提交
7c4c866e75

+ 0 - 2
internal/common/constvalue.go

@@ -27,8 +27,6 @@ const (
 	VideoExtMkv  = ".mkv"
 	VideoExtRmvb = ".rmvb"
 	VideoExtIso  = ".iso"
-
-	SubTmpFolderName = "subtmp"
 )
 
 const (

+ 18 - 33
internal/downloader.go

@@ -13,7 +13,6 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_supplier/zimuku"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/decode"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_folder"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	subcommon "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/common"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
@@ -154,7 +153,7 @@ func (d Downloader) RefreshEmbySubList() error {
 func (d Downloader) DownloadSub4Movie(dir string) error {
 	defer func() {
 		// 所有的电影字幕下载完成,抉择完成,需要清理缓存目录
-		err := my_folder.ClearRootTmpFolder()
+		err := my_util.ClearRootTmpFolder()
 		if err != nil {
 			d.log.Error("ClearRootTmpFolder", err)
 		}
@@ -270,7 +269,7 @@ func (d Downloader) DownloadSub4Series(dir string) error {
 	var err error
 	defer func() {
 		// 所有的连续剧字幕下载完成,抉择完成,需要清理缓存目录
-		err := my_folder.ClearRootTmpFolder()
+		err := my_util.ClearRootTmpFolder()
 		if err != nil {
 			d.log.Error("ClearRootTmpFolder", err)
 		}
@@ -423,12 +422,13 @@ func (d Downloader) oneVideoSelectBestSub(oneVideoFullPath string, organizeSubFi
 	}
 
 	var err error
-	// 得到目标视频文件的根目录
-	videoRootPath := filepath.Dir(oneVideoFullPath)
+	// 得到目标视频文件的文件名
+	videoFileName := filepath.Base(oneVideoFullPath)
 	// -------------------------------------------------
 	// 调试缓存,把下载好的字幕写到对应的视频目录下,方便调试
 	if d.reqParam.DebugMode == true {
-		err = d.copySubFile2DesFolder(videoRootPath, organizeSubFiles)
+
+		err = my_util.CopyFiles2DebugFolder([]string{videoFileName}, organizeSubFiles)
 		if err != nil {
 			d.log.Errorln("copySubFile2DesFolder", err)
 		}
@@ -516,7 +516,7 @@ func (d Downloader) oneVideoSelectBestSub(oneVideoFullPath string, organizeSubFi
 	// -------------------------------------------------
 }
 
-// saveFullSeasonSub 这里就需要单独存储到连续剧每一季的文件夹的特殊文件夹中
+// saveFullSeasonSub 这里就需要单独存储到连续剧每一季的文件夹的特殊文件夹中。需要跟 DeleteOneSeasonSubCacheFolder 关联起来
 func (d Downloader) saveFullSeasonSub(seriesInfo *series.SeriesInfo, organizeSubFiles map[string][]string) map[string][]string {
 
 	var fullSeasonSubDict = make(map[string][]string)
@@ -529,12 +529,19 @@ func (d Downloader) saveFullSeasonSub(seriesInfo *series.SeriesInfo, organizeSub
 		}
 		for _, sub := range subs {
 			subFileName := filepath.Base(sub)
-			newSeasonSubRootPath := filepath.Join(seriesInfo.DirPath, "Sub_"+seasonKey)
-			_ = os.MkdirAll(newSeasonSubRootPath, os.ModePerm)
+
+			newSeasonSubRootPath, err := my_util.GetDebugFolderByName([]string{
+				filepath.Base(seriesInfo.DirPath),
+				"Sub_" + seasonKey})
+			if err != nil {
+				d.log.Errorln("saveFullSeasonSub.GetDebugFolderByName", subFileName, err)
+				continue
+			}
+
 			newSubFullPath := filepath.Join(newSeasonSubRootPath, subFileName)
-			err := my_util.CopyFile(sub, newSubFullPath)
+			err = my_util.CopyFile(sub, newSubFullPath)
 			if err != nil {
-				d.log.Errorln("saveFullSeasonSub", subFileName, err)
+				d.log.Errorln("saveFullSeasonSub.CopyFile", subFileName, err)
 				continue
 			}
 			// 从字幕的文件名推断是 哪一季 的 那一集
@@ -590,28 +597,6 @@ func (d Downloader) writeSubFile2VideoPath(videoFileFullPath string, finalSubFil
 	return nil
 }
 
-// copySubFile2DesFolder 拷贝字幕文件到目标文件夹
-func (d Downloader) copySubFile2DesFolder(desFolder string, subFiles []string) error {
-
-	// 需要进行字幕文件的缓存
-	// 把缓存的文件夹新建出来
-	desFolderFullPath := filepath.Join(desFolder, common.SubTmpFolderName)
-	err := os.MkdirAll(desFolderFullPath, os.ModePerm)
-	if err != nil {
-		return err
-	}
-	// 复制下载在 tmp 文件夹中的字幕文件到视频文件夹下面
-	for _, subFile := range subFiles {
-		newFn := filepath.Join(desFolderFullPath, filepath.Base(subFile))
-		err = my_util.CopyFile(subFile, newFn)
-		if err != nil {
-			return err
-		}
-	}
-
-	return nil
-}
-
 type InputData struct {
 	OneVideoFullPath string
 	Index            int

+ 3 - 3
internal/logic/sub_parser/ass/ass.go

@@ -29,9 +29,9 @@ func (p Parser) GetParserName() string {
 */
 func (p Parser) DetermineFileTypeFromFile(filePath string) (bool, *subparser.FileInfo, error) {
 	nowExt := filepath.Ext(filePath)
-	//if strings.ToLower(nowExt) != common.SubExtASS && strings.ToLower(nowExt) != common.SubExtSSA {
-	//	return false, nil, nil
-	//}
+
+	log_helper.GetLogger().Debugln("DetermineFileTypeFromFile", p.GetParserName(), filePath)
+
 	fBytes, err := ioutil.ReadFile(filePath)
 	if err != nil {
 		return false, nil, err

+ 1 - 0
internal/logic/sub_parser/ass/ass_test.go

@@ -3,6 +3,7 @@ package ass
 import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/language"
+	"path/filepath"
 	"testing"
 )
 

+ 3 - 3
internal/logic/sub_parser/srt/srt.go

@@ -28,9 +28,9 @@ func (p Parser) GetParserName() string {
 */
 func (p Parser) DetermineFileTypeFromFile(filePath string) (bool, *subparser.FileInfo, error) {
 	nowExt := filepath.Ext(filePath)
-	//if strings.ToLower(nowExt) != common.SubExtSRT {
-	//	return false, nil, nil
-	//}
+
+	log_helper.GetLogger().Debugln("DetermineFileTypeFromFile", p.GetParserName(), filePath)
+
 	fBytes, err := ioutil.ReadFile(filePath)
 	if err != nil {
 		return false, nil, err

+ 1 - 2
internal/logic/sub_timeline_fixer/sub_timeline_fixer_helper.go

@@ -8,7 +8,6 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/ass"
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/srt"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_folder"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	formatterEmby "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/emby"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_formatter/normal"
@@ -230,7 +229,7 @@ func (s SubTimelineFixerHelper) fixSubTimeline(enSubFile emby.SubInfo, containCh
 	//infoSrcNameWithOutExt := strings.Replace(infoSrc.Name, path.Ext(infoSrc.Name), "", -1)
 
 	// 把原始的文件缓存下来,新建缓存的文件夹
-	subFixCacheRootPath, err := my_folder.GetRootSubFixCacheFolder()
+	subFixCacheRootPath, err := my_util.GetRootSubFixCacheFolder()
 	if err != nil {
 		return false, nil, fixedSubName, err
 	}

+ 2 - 2
internal/pkg/debug_view/debug_view.go

@@ -2,7 +2,7 @@ package debug_view
 
 import (
 	"fmt"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_folder"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/vad"
 	"github.com/go-echarts/go-echarts/v2/charts"
@@ -42,7 +42,7 @@ func SaveDebugChartBase(vadList []vad.VADInfo, title, subTitle string) error {
 	line.SetXAxis(xAxis).
 		AddSeries("VAD", lineData)
 
-	rootDebugFolder, err := my_folder.GetRootDebugFolder()
+	rootDebugFolder, err := my_util.GetRootDebugFolder()
 	if err != nil {
 		return err
 	}

+ 1 - 2
internal/pkg/ffmpeg_helper/ffmpeg_helper.go

@@ -9,7 +9,6 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/sub_parser/srt"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/language"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_folder"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_parser_hub"
 	"github.com/tidwall/gjson"
@@ -96,7 +95,7 @@ func (f *FFMPEGHelper) GetFFMPEGInfo(videoFileFullPath string, exportType Export
 		// 说明缓存不存在,需要导出,这里需要注意,如果导出失败了,这个文件夹要清理掉
 		if my_util.IsDir(nowCacheFolderPath) == true {
 			// 如果存在则,先清空一个这个文件夹
-			err = my_folder.ClearFolder(nowCacheFolderPath)
+			err = my_util.ClearFolder(nowCacheFolderPath)
 			if err != nil {
 				bok = false
 				return bok, nil, err

+ 1 - 2
internal/pkg/ffmpeg_helper/ffmpeg_info.go

@@ -3,7 +3,6 @@ package ffmpeg_helper
 import (
 	"github.com/allanpk716/ChineseSubFinder/internal/common"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_folder"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_parser_hub"
@@ -32,7 +31,7 @@ func NewFFMPEGInfo(videoFullPath string) *FFMPEGInfo {
 // csf-cache/当前的视频文件名(不带后缀)
 func (f *FFMPEGInfo) GetCacheFolderFPath() (string, error) {
 	noExtVideoName := strings.ReplaceAll(filepath.Base(f.VideoFullPath), filepath.Ext(f.VideoFullPath), "")
-	return my_folder.GetSubFixCacheFolderByName(noExtVideoName)
+	return my_util.GetSubFixCacheFolderByName(noExtVideoName)
 }
 
 // IsExported 是否已经导出过,如果没有导出或者导出不完整为 false

+ 88 - 33
internal/pkg/my_folder/folder.go → internal/pkg/my_util/folder.go

@@ -1,4 +1,4 @@
-package my_folder
+package my_util
 
 import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/global_value"
@@ -7,6 +7,10 @@ import (
 	"path/filepath"
 )
 
+// --------------------------------------------------------------
+// Debug
+// --------------------------------------------------------------
+
 // GetRootDebugFolder 在程序的根目录新建,调试用文件夹
 func GetRootDebugFolder() (string, error) {
 	if global_value.DefDebugFolder == "" {
@@ -22,6 +26,49 @@ func GetRootDebugFolder() (string, error) {
 	return global_value.DefDebugFolder, nil
 }
 
+// GetDebugFolderByName 根据传入的 strings (["aa", "bb"]) 组成  DebugFolder/aa/bb 这样的路径
+func GetDebugFolderByName(names []string) (string, error) {
+
+	rootPath, err := GetRootDebugFolder()
+	if err != nil {
+		return "", err
+	}
+
+	tmpFolderFullPath := ""
+	for _, name := range names {
+		tmpFolderFullPath = filepath.Join(rootPath, name)
+	}
+	err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
+	if err != nil {
+		return "", err
+	}
+
+	return tmpFolderFullPath, nil
+}
+
+// CopyFiles2DebugFolder 把文件放入到 Debug 文件夹中,新建 desFolderName 文件夹
+func CopyFiles2DebugFolder(names []string, subFiles []string) error {
+	debugFolderByName, err := GetDebugFolderByName(names)
+	if err != nil {
+		return err
+	}
+
+	// 复制下载在 tmp 文件夹中的字幕文件到视频文件夹下面
+	for _, subFile := range subFiles {
+		newFn := filepath.Join(debugFolderByName, filepath.Base(subFile))
+		err = CopyFile(subFile, newFn)
+		if err != nil {
+			return err
+		}
+	}
+
+	return nil
+}
+
+// --------------------------------------------------------------
+// Tmp
+// --------------------------------------------------------------
+
 // GetRootTmpFolder 在程序的根目录新建,取缓用文件夹,每一个视频的缓存将在其中额外新建子集文件夹
 func GetRootTmpFolder() (string, error) {
 	if global_value.DefTmpFolder == "" {
@@ -37,24 +84,9 @@ func GetRootTmpFolder() (string, error) {
 	return global_value.DefTmpFolder, nil
 }
 
-// GetRootSubFixCacheFolder 在程序的根目录新建,字幕时间校正的缓存文件夹
-func GetRootSubFixCacheFolder() (string, error) {
-	if global_value.DefSubFixCacheFolder == "" {
-		nowProcessRoot, _ := os.Getwd()
-		nowProcessRoot = filepath.Join(nowProcessRoot, SubFixCacheFolder)
-		err := os.MkdirAll(nowProcessRoot, os.ModePerm)
-		if err != nil {
-			return "", err
-		}
-		global_value.DefSubFixCacheFolder = nowProcessRoot
-		return nowProcessRoot, err
-	}
-	return global_value.DefSubFixCacheFolder, nil
-}
-
-// GetSubFixCacheFolderByName 获取缓存的文件夹,没有则新建
-func GetSubFixCacheFolderByName(folderName string) (string, error) {
-	rootPath, err := GetRootSubFixCacheFolder()
+// GetTmpFolderByName 获取缓存的文件夹,没有则新建
+func GetTmpFolderByName(folderName string) (string, error) {
+	rootPath, err := GetRootTmpFolder()
 	if err != nil {
 		return "", err
 	}
@@ -66,6 +98,17 @@ func GetSubFixCacheFolderByName(folderName string) (string, error) {
 	return tmpFolderFullPath, nil
 }
 
+// ClearTmpFolderByName 清理指定的缓存文件夹
+func ClearTmpFolderByName(folderName string) error {
+
+	nowTmpFolder, err := GetTmpFolderByName(folderName)
+	if err != nil {
+		return err
+	}
+
+	return ClearFolder(nowTmpFolder)
+}
+
 // ClearRootTmpFolder 清理缓存的根目录,将里面的子文件夹一并清理
 func ClearRootTmpFolder() error {
 	nowTmpFolder, err := GetRootTmpFolder()
@@ -97,9 +140,28 @@ func ClearRootTmpFolder() error {
 	return nil
 }
 
-// GetTmpFolderByName 获取缓存的文件夹,没有则新建
-func GetTmpFolderByName(folderName string) (string, error) {
-	rootPath, err := GetRootTmpFolder()
+// --------------------------------------------------------------
+// Sub Fix Cache
+// --------------------------------------------------------------
+
+// GetRootSubFixCacheFolder 在程序的根目录新建,字幕时间校正的缓存文件夹
+func GetRootSubFixCacheFolder() (string, error) {
+	if global_value.DefSubFixCacheFolder == "" {
+		nowProcessRoot, _ := os.Getwd()
+		nowProcessRoot = filepath.Join(nowProcessRoot, SubFixCacheFolder)
+		err := os.MkdirAll(nowProcessRoot, os.ModePerm)
+		if err != nil {
+			return "", err
+		}
+		global_value.DefSubFixCacheFolder = nowProcessRoot
+		return nowProcessRoot, err
+	}
+	return global_value.DefSubFixCacheFolder, nil
+}
+
+// GetSubFixCacheFolderByName 获取缓存的文件夹,没有则新建
+func GetSubFixCacheFolderByName(folderName string) (string, error) {
+	rootPath, err := GetRootSubFixCacheFolder()
 	if err != nil {
 		return "", err
 	}
@@ -111,6 +173,10 @@ func GetTmpFolderByName(folderName string) (string, error) {
 	return tmpFolderFullPath, nil
 }
 
+// --------------------------------------------------------------
+// Common
+// --------------------------------------------------------------
+
 // ClearFolder 清空文件夹
 func ClearFolder(folderFullPath string) error {
 	pathSep := string(os.PathSeparator)
@@ -137,17 +203,6 @@ func ClearFolder(folderFullPath string) error {
 	return nil
 }
 
-// ClearTmpFolderByName 清理指定的缓存文件夹
-func ClearTmpFolderByName(folderName string) error {
-
-	nowTmpFolder, err := GetTmpFolderByName(folderName)
-	if err != nil {
-		return err
-	}
-
-	return ClearFolder(nowTmpFolder)
-}
-
 const (
 	DebugFolder       = "CSF-DebugThings" // 调试相关的文件夹
 	TmpFolder         = "CSF-TmpThings"   // 临时缓存的文件夹

+ 1 - 2
internal/pkg/my_util/util.go

@@ -5,7 +5,6 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/common"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/global_value"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_folder"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/regex_things"
 	"github.com/allanpk716/ChineseSubFinder/internal/types"
 	browser "github.com/allanpk716/fake-useragent"
@@ -262,7 +261,7 @@ func CopyTestData(srcDir string) (string, error) {
 	testDir := filepath.Join(srcDir, "test")
 
 	if IsDir(testDir) == true {
-		err := my_folder.ClearFolder(testDir)
+		err := ClearFolder(testDir)
 		if err != nil {
 			return "", err
 		}

+ 7 - 4
internal/pkg/sub_helper/sub_helper.go

@@ -7,7 +7,6 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/decode"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/language"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_folder"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/regex_things"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_parser_hub"
@@ -30,7 +29,7 @@ func OrganizeDlSubFiles(tmpFolderName string, subInfos []supplier.SubInfo) (map[
 	// 缓存列表,整理后的字幕列表
 	// SxEx - []string 字幕的路径
 	var siteSubInfoDict = make(map[string][]string)
-	tmpFolderFullPath, err := my_folder.GetTmpFolderByName(tmpFolderName)
+	tmpFolderFullPath, err := my_util.GetTmpFolderByName(tmpFolderName)
 	if err != nil {
 		return nil, err
 	}
@@ -332,7 +331,11 @@ func SearchVideoMatchSubFileAndRemoveExtMark(oneVideoFullPath string) error {
 // DeleteOneSeasonSubCacheFolder 删除一个连续剧中的所有一季字幕的缓存文件夹
 func DeleteOneSeasonSubCacheFolder(seriesDir string) error {
 
-	files, err := ioutil.ReadDir(seriesDir)
+	debugFolderByName, err := my_util.GetDebugFolderByName([]string{filepath.Base(seriesDir)})
+	if err != nil {
+		return err
+	}
+	files, err := ioutil.ReadDir(debugFolderByName)
 	if err != nil {
 		return err
 	}
@@ -344,7 +347,7 @@ func DeleteOneSeasonSubCacheFolder(seriesDir string) error {
 				continue
 			}
 
-			fullPath := seriesDir + pathSep + curFile.Name()
+			fullPath := debugFolderByName + pathSep + curFile.Name()
 			err = os.RemoveAll(fullPath)
 			if err != nil {
 				return err