Browse Source

修复 windows 下蓝光在 Emby 扫描时候遇到路径拼接bug

Signed-off-by: allan716 <[email protected]>
allan716 3 years ago
parent
commit
7a6735ff8e

+ 1 - 0
.gitignore

@@ -87,3 +87,4 @@ TestData/
 /internal/logic/task_queue/task_queue
 /internal/logic/task_queue/Logs
 /task_queue
+/internal/pkg/sub_file_hash/Logs

+ 2 - 5
go.mod

@@ -7,7 +7,7 @@ require (
 	github.com/StalkR/imdb v1.0.7
 	github.com/Tnze/go.num/v2 v2.0.0-20191006170829-cb483d4c9152
 	github.com/abadojack/whatlanggo v1.0.1
-	github.com/allanpk716/ChineseSubModels v0.0.2
+	github.com/allanpk716/ChineseSubModels v0.0.3
 	github.com/allanpk716/fake-useragent v0.2.1
 	github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
 	github.com/baabaaox/go-webrtcvad v1.0.1
@@ -68,10 +68,7 @@ require (
 	gorm.io/gorm v1.22.5
 )
 
-require (
-	github.com/davecgh/go-spew v1.1.1
-	github.com/dgraph-io/badger/v3 v3.2103.2
-)
+require github.com/dgraph-io/badger/v3 v3.2103.2
 
 require (
 	github.com/EDDYCJY/fake-useragent v0.2.0 // indirect

+ 2 - 2
go.sum

@@ -51,8 +51,8 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
 github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
 github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
 github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
-github.com/allanpk716/ChineseSubModels v0.0.2 h1:8bBe1M0RWuQxM05ucoLsG4WpIHKwcf7aeQzk/k3hXps=
-github.com/allanpk716/ChineseSubModels v0.0.2/go.mod h1:4Yd7MJ5jPxCKSHUJkJT8wlrC8wj2Rh5D4yLtXAEp1uE=
+github.com/allanpk716/ChineseSubModels v0.0.3 h1:GqNzuak+1vu+llqRul6n9r85cMK38iT3mkT9pF815+o=
+github.com/allanpk716/ChineseSubModels v0.0.3/go.mod h1:4Yd7MJ5jPxCKSHUJkJT8wlrC8wj2Rh5D4yLtXAEp1uE=
 github.com/allanpk716/fake-useragent v0.2.1 h1:5EurHovAMGNwMZNbZ2cFOH8HsLdHwopnnKWCsV3PtNI=
 github.com/allanpk716/fake-useragent v0.2.1/go.mod h1:U3tmrxBYvSPeQKQ/9x59CXJPkHbU4x4eXw3ji5TDhPk=
 github.com/allanpk716/go-parse-torrent-name v0.0.0-20220212145434-430ea25cbba7 h1:5AYgeYsVjtDPG5sUS77lyeEPiRMduIKstLjkBLiVPRc=

+ 7 - 4
internal/logic/emby_helper/embyhelper.go

@@ -378,11 +378,13 @@ func (em *EmbyHelper) findMappingPathWithMixInfo(mixInfo *emby.EmbyMixInfo, isMo
 
 		if len(mixInfo.VideoInfo.MediaSources) > 0 && mixInfo.VideoInfo.MediaSources[0].Container == "bluray" {
 			// 这个就是蓝光了
-			fakeVideoFPath := filepath.Join(mixInfo.VideoInfo.Path, filepath.Base(mixInfo.VideoInfo.Path)+common2.VideoExtMp4)
+			// 先替换再拼接,不然会出现拼接完成后,在 Windows 下会把 /mnt/share1/电影 变为这样了 \mnt\share1\电影\失控玩家 (2021)\失控玩家 (2021).mp4
+			videoReplacedDirFPath := strings.ReplaceAll(mixInfo.VideoInfo.Path, pathSlices[0].Path, nowPhRootPath)
+			fakeVideoFPath := filepath.Join(videoReplacedDirFPath, filepath.Base(mixInfo.VideoInfo.Path)+common2.VideoExtMp4)
 			mixInfo.PhysicalVideoFileFullPath = strings.ReplaceAll(fakeVideoFPath, pathSlices[0].Path, nowPhRootPath)
 			// 这个电影的文件夹
-			mixInfo.VideoFolderName = filepath.Base(filepath.Dir(fakeVideoFPath))
-			mixInfo.VideoFileName = filepath.Base(fakeVideoFPath)
+			mixInfo.VideoFolderName = filepath.Base(mixInfo.VideoInfo.Path)
+			mixInfo.VideoFileName = filepath.Base(mixInfo.VideoInfo.Path) + common2.VideoExtMp4
 		} else {
 			// 常规的电影情况,也就是有一个具体的视频文件 .mp4 or .mkv
 			mixInfo.PhysicalVideoFileFullPath = strings.ReplaceAll(mixInfo.VideoInfo.Path, pathSlices[0].Path, nowPhRootPath)
@@ -451,7 +453,8 @@ func (em *EmbyHelper) getMoreVideoInfoList(videoIdList []string, isMovieOrSeries
 		return &mixInfo, nil
 	}
 
-	p, err := ants.NewPoolWithFunc(em.threads, func(inData interface{}) {
+	// em.threads
+	p, err := ants.NewPoolWithFunc(1, func(inData interface{}) {
 		data := inData.(InputData)
 		defer data.Wg.Done()
 		ctx, cancel := context.WithTimeout(context.Background(), em.timeOut)

+ 10 - 10
internal/logic/scan_played_video_subinfo/scan_played_video_subinfo.go

@@ -315,10 +315,10 @@ func (s *ScanPlayedVideoSubInfo) dealOneVideo(index int, videoFPath, orgSubFPath
 
 	s.log.Debugln(3)
 
-	// 当前扫描到的找个字幕的 sha1 是否已经存在与缓存中了
-	tmpSHA1String, err := my_util.GetFileSHA1String(orgSubFPath)
+	// 当前扫描到的找个字幕的 sha256 是否已经存在与缓存中了
+	tmpSHA256String, err := my_util.GetFileSHA256String(orgSubFPath)
 	if err != nil {
-		s.log.Warningln("ScanPlayedVideoSubInfo.Scan", videoTypes, "orgSubFPath.GetFileSHA1String", videoFPath, err)
+		s.log.Warningln("ScanPlayedVideoSubInfo.Scan", videoTypes, "orgSubFPath.GetFileSHA256String", videoFPath, err)
 		return
 	}
 
@@ -327,8 +327,8 @@ func (s *ScanPlayedVideoSubInfo) dealOneVideo(index int, videoFPath, orgSubFPath
 	// 判断找到的关联字幕信息是否已经存在了,不存在则新增关联
 	for _, cacheInfo := range imdbInfo.VideoSubInfos {
 
-		if cacheInfo.SHA1 == tmpSHA1String {
-			s.log.Warningln("ScanPlayedVideoSubInfo.Scan", videoTypes, "SHA1 Exist == true, Skip", orgSubFPath)
+		if cacheInfo.SHA256 == tmpSHA256String {
+			s.log.Warningln("ScanPlayedVideoSubInfo.Scan", videoTypes, "SHA256 Exist == true, Skip", orgSubFPath)
 			return
 		}
 	}
@@ -369,10 +369,10 @@ func (s *ScanPlayedVideoSubInfo) dealOneVideo(index int, videoFPath, orgSubFPath
 
 	s.log.Debugln(8)
 
-	// 计算需要插入字幕的 sha1
-	saveSHA1String, err := my_util.GetFileSHA1String(subCacheFPath)
+	// 计算需要插入字幕的 sha256
+	saveSHA256String, err := my_util.GetFileSHA256String(subCacheFPath)
 	if err != nil {
-		s.log.Warningln("ScanPlayedVideoSubInfo.Scan", videoTypes, "GetFileSHA1String", videoFPath, err)
+		s.log.Warningln("ScanPlayedVideoSubInfo.Scan", videoTypes, "GetFileSHA256String", videoFPath, err)
 		return
 	}
 
@@ -387,7 +387,7 @@ func (s *ScanPlayedVideoSubInfo) dealOneVideo(index int, videoFPath, orgSubFPath
 		fileInfo.Lang.String(),
 		subRelPath,
 		extraSubPreName,
-		saveSHA1String,
+		saveSHA256String,
 	)
 
 	if isMovie == false {
@@ -421,7 +421,7 @@ func (s *ScanPlayedVideoSubInfo) delSubInfo(imdbInfo *models.IMDBInfo, cacheInfo
 	}
 	// 继续删除这个对象
 	dao.GetDb().Delete(cacheInfo)
-	s.log.Infoln("Delete Not Exist or SHA1 not the same, Sub Association", cacheInfo.SubName)
+	s.log.Infoln("Delete Not Exist or SHA256 not the same, Sub Association", cacheInfo.SubName)
 
 	return true
 }

+ 20 - 0
internal/pkg/my_util/util.go

@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"crypto/md5"
 	"crypto/sha1"
+	"crypto/sha256"
 	"encoding/binary"
 	"encoding/hex"
 	"fmt"
@@ -702,6 +703,25 @@ func GetFileSHA1String(fileFPath string) (string, error) {
 	return fmt.Sprintf("%x", md5.Sum(hashBytes)), nil
 }
 
+// GetFileSHA256String 获取文件的 SHA256 字符串
+func GetFileSHA256String(fileFPath string) (string, error) {
+
+	fp, err := os.Open(fileFPath)
+	if err != nil {
+		return "", err
+	}
+	defer func() {
+		_ = fp.Close()
+	}()
+
+	partAll, err := io.ReadAll(fp)
+	if err != nil {
+		return "", err
+	}
+
+	return fmt.Sprintf("%x", sha256.Sum256(partAll)), nil
+}
+
 func GetRestOfDaySec() time.Duration {
 
 	nowTime := time.Now()

+ 7 - 11
internal/types/task_queue/task_queue.go

@@ -1,7 +1,7 @@
 package task_queue
 
 import (
-	"crypto/sha1"
+	"crypto/sha256"
 	"fmt"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_file_hash"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/common"
@@ -32,23 +32,19 @@ func NewOneJob(videoType common.VideoType, videoFPath string, taskPriority int,
 
 	ob := &OneJob{VideoType: videoType, VideoFPath: videoFPath, TaskPriority: taskPriority}
 
-	sha1FilePathID := func() string {
-		// ID 由 SHA1 来计算出来作为唯一性
-		h := sha1.New()
-		h.Write([]byte(videoFPath))
-		bs := h.Sum(nil)
-		return fmt.Sprintf("%x", bs)
+	sha256FilePathID := func() string {
+		return fmt.Sprintf("%x", sha256.Sum256([]byte(videoFPath)))
 	}
 
 	/*
 		sub_file_hash.Calculate 现在支持内部的 fake 蓝光视频地址了,会解析到 BDMV 中最大的那个视频流文件来计算
-		所以上面这个函数如果 errors 了,才需要使用这个伪造的路径进行 sha1 加密即可
+		所以上面这个函数如果 errors 了,才需要使用这个伪造的路径进行 sha256 加密即可
 	*/
-	sha1String, err := sub_file_hash.Calculate(videoFPath)
+	sha256String, err := sub_file_hash.Calculate(videoFPath)
 	if err != nil {
-		ob.Id = sha1FilePathID()
+		ob.Id = sha256FilePathID()
 	} else {
-		ob.Id = sha1String
+		ob.Id = sha256String
 	}
 
 	ob.VideoName = filepath.Base(videoFPath)