Pārlūkot izejas kodu

重构使用 ioutil 模块到 os 或者 io

Signed-off-by: allan716 <[email protected]>
allan716 3 gadi atpakaļ
vecāks
revīzija
6ff585b601

+ 3 - 3
internal/logic/charset/charset.go

@@ -4,7 +4,7 @@ import (
 	"bytes"
 	"errors"
 	"fmt"
-	"io/ioutil"
+	"io"
 
 	"golang.org/x/text/encoding"
 	"golang.org/x/text/encoding/ianaindex"
@@ -65,7 +65,7 @@ func Convert(dstCharset Charset, srcCharset Charset, src string) (dst string, er
 	// Converting <src> to UTF-8.
 	if srcCharset != "UTF-8" {
 		if e := getEncoding(srcCharset); e != nil {
-			tmp, err := ioutil.ReadAll(
+			tmp, err := io.ReadAll(
 				transform.NewReader(bytes.NewReader([]byte(src)), e.NewDecoder()),
 			)
 			if err != nil {
@@ -79,7 +79,7 @@ func Convert(dstCharset Charset, srcCharset Charset, src string) (dst string, er
 	// Do the converting from UTF-8 to <dstCharset>.
 	if dstCharset != "UTF-8" {
 		if e := getEncoding(dstCharset); e != nil {
-			tmp, err := ioutil.ReadAll(
+			tmp, err := io.ReadAll(
 				transform.NewReader(bytes.NewReader([]byte(src)), e.NewEncoder()),
 			)
 			if err != nil {

+ 2 - 2
internal/logic/movie_helper/moviehelper.go

@@ -13,7 +13,7 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/types"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/supplier"
 	"github.com/jinzhu/now"
-	"io/ioutil"
+	"os"
 	"path/filepath"
 	"strings"
 	"time"
@@ -71,7 +71,7 @@ func MovieHasChineseSub(videoFilePath string) (bool, []string, []string, error)
 	dir := filepath.Dir(videoFilePath)
 	videoFileName := filepath.Base(videoFilePath)
 	videoFileName = strings.ReplaceAll(videoFileName, filepath.Ext(videoFileName), "")
-	files, err := ioutil.ReadDir(dir)
+	files, err := os.ReadDir(dir)
 	if err != nil {
 		return false, nil, nil, err
 	}

+ 2 - 2
internal/logic/series_helper/seriesHelper.go

@@ -18,7 +18,7 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/types/series"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/supplier"
 	"github.com/jinzhu/now"
-	"io/ioutil"
+	"os"
 	"path/filepath"
 	"strconv"
 	"strings"
@@ -224,7 +224,7 @@ func SetTheSpecifiedEps2Download(seriesInfo *series.SeriesInfo, epsMap map[int]i
 func GetSeriesList(dir string) ([]string, error) {
 
 	var seriesDirList = make([]string, 0)
-	files, err := ioutil.ReadDir(dir)
+	files, err := os.ReadDir(dir)
 	if err != nil {
 		return nil, err
 	}

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

@@ -6,7 +6,7 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/regex_things"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/subparser"
 	"github.com/emirpasic/gods/maps/treemap"
-	"io/ioutil"
+	"os"
 	"path/filepath"
 	"strings"
 )
@@ -32,7 +32,7 @@ func (p Parser) DetermineFileTypeFromFile(filePath string) (bool, *subparser.Fil
 
 	log_helper.GetLogger().Debugln("DetermineFileTypeFromFile", p.GetParserName(), filePath)
 
-	fBytes, err := ioutil.ReadFile(filePath)
+	fBytes, err := os.ReadFile(filePath)
 	if err != nil {
 		return false, nil, err
 	}

+ 9 - 2
internal/logic/sub_parser/srt/srt.go

@@ -5,7 +5,7 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/regex_things"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/subparser"
-	"io/ioutil"
+	"os"
 	"path/filepath"
 	"strings"
 )
@@ -31,7 +31,7 @@ func (p Parser) DetermineFileTypeFromFile(filePath string) (bool, *subparser.Fil
 
 	log_helper.GetLogger().Debugln("DetermineFileTypeFromFile", p.GetParserName(), filePath)
 
-	fBytes, err := ioutil.ReadFile(filePath)
+	fBytes, err := os.ReadFile(filePath)
 	if err != nil {
 		return false, nil, err
 	}
@@ -113,3 +113,10 @@ func (p Parser) DetermineFileTypeFromBytes(inBytes []byte, nowExt string) (bool,
 	subFileInfo.OtherLines = otherLines
 	return true, &subFileInfo, nil
 }
+
+func (p Parser) parseContent(inBytes []byte) {
+
+	allString := string(inBytes)
+	// 注意,需要替换掉 \r 不然正则表达式会有问题
+	allString = strings.ReplaceAll(allString, "\r", "")
+}

+ 19 - 0
internal/logic/sub_parser/srt/srt_test.go

@@ -1,8 +1,10 @@
 package srt
 
 import (
+	lan "github.com/allanpk716/ChineseSubFinder/internal/pkg/language"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/language"
+	"os"
 	"path/filepath"
 	"testing"
 )
@@ -61,3 +63,20 @@ func TestParser_DetermineFileType(t *testing.T) {
 		})
 	}
 }
+
+func TestParser_parseContent(t *testing.T) {
+
+	testDataPath := "../../../../TestData/sub_parser/org"
+	srtFileFPath := filepath.Join(testDataPath, "[zimuku]_5_Loki.S01E02.The.Variant.1080p.DSNP.WEB-DL.DDP5.1.Atmos.H.264-CM.chs&eng.srt")
+
+	fBytes, err := os.ReadFile(srtFileFPath)
+	if err != nil {
+		t.Fatal(err)
+	}
+	inBytes, err := lan.ChangeFileCoding2UTF8(fBytes)
+	if err != nil {
+
+		t.Fatal(err)
+	}
+	NewParser().parseContent(inBytes)
+}

+ 1 - 2
internal/logic/sub_supplier/subhd/subhd.go

@@ -21,7 +21,6 @@ import (
 	"github.com/nfnt/resize"
 	"github.com/sirupsen/logrus"
 	"image/jpeg"
-	"io/ioutil"
 	"math"
 	"net/url"
 	"os"
@@ -509,7 +508,7 @@ func (s Supplier) downloadSubFile(browser *rod.Browser, page *rod.Page, hasWater
 			info := wait()
 			downloadPath := filepath.Join(tmpDir, info.GUID)
 			defer func() { _ = os.Remove(downloadPath) }()
-			b, err := ioutil.ReadFile(downloadPath)
+			b, err := os.ReadFile(downloadPath)
 			if err != nil {
 				return nil, "", err
 			}

+ 2 - 3
internal/pkg/archive_helper/unarchiveFile.go

@@ -11,7 +11,6 @@ import (
 	"golang.org/x/text/encoding/simplifiedchinese"
 	"golang.org/x/text/transform"
 	"io"
-	"io/ioutil"
 	"path/filepath"
 	"strings"
 )
@@ -107,7 +106,7 @@ func processOneFile(f archiver.File, notUTF8 bool, desRootPath string) error {
 		//}
 		i := bytes.NewReader([]byte(f.Name()))
 		decoder := transform.NewReader(i, simplifiedchinese.GB18030.NewDecoder())
-		content, _ := ioutil.ReadAll(decoder)
+		content, _ := io.ReadAll(decoder)
 		decodeName = string(content)
 		//decodeName = string(ouBytes)
 	}
@@ -157,7 +156,7 @@ func un7zOneFile(file *sevenzip.File, desRootPath string) error {
 	}
 	defer rc.Close()
 
-	data, err := ioutil.ReadAll(rc)
+	data, err := io.ReadAll(rc)
 	if err != nil {
 		return err
 	}

+ 2 - 3
internal/pkg/decode/decode.go

@@ -7,7 +7,6 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/types"
 	"github.com/beevik/etree"
 	PTN "github.com/middelink/go-parse-torrent-name"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"regexp"
@@ -106,7 +105,7 @@ func GetImdbInfo4Movie(movieFileFullPath string) (types.VideoIMDBInfo, error) {
 	movieNameNfoFPath := ""
 	// 通用的 *.nfo
 	nfoFilePath := ""
-	dir, err := ioutil.ReadDir(dirPth)
+	dir, err := os.ReadDir(dirPth)
 	if err != nil {
 		return imdbInfo, err
 	}
@@ -166,7 +165,7 @@ func GetImdbInfo4Movie(movieFileFullPath string) (types.VideoIMDBInfo, error) {
 
 func GetImdbInfo4SeriesDir(seriesDir string) (types.VideoIMDBInfo, error) {
 	imdbInfo := types.VideoIMDBInfo{}
-	dir, err := ioutil.ReadDir(seriesDir)
+	dir, err := os.ReadDir(seriesDir)
 	if err != nil {
 		return imdbInfo, err
 	}

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

@@ -2,7 +2,7 @@ package ffmpeg_helper
 
 import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
-	"io/ioutil"
+	"os"
 	"path/filepath"
 	"testing"
 )
@@ -27,7 +27,7 @@ func TestGetFFMPEGInfo(t *testing.T) {
 }
 
 func readString(filePath string) string {
-	bytes, err := ioutil.ReadFile(filePath)
+	bytes, err := os.ReadFile(filePath)
 	if err != nil {
 		return ""
 	}

+ 2 - 3
internal/pkg/my_util/folder.go

@@ -2,7 +2,6 @@ package my_util
 
 import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/global_value"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 )
@@ -117,7 +116,7 @@ func ClearRootTmpFolder() error {
 	}
 
 	pathSep := string(os.PathSeparator)
-	files, err := ioutil.ReadDir(nowTmpFolder)
+	files, err := os.ReadDir(nowTmpFolder)
 	if err != nil {
 		return err
 	}
@@ -180,7 +179,7 @@ func GetSubFixCacheFolderByName(folderName string) (string, error) {
 // ClearFolder 清空文件夹
 func ClearFolder(folderFullPath string) error {
 	pathSep := string(os.PathSeparator)
-	files, err := ioutil.ReadDir(folderFullPath)
+	files, err := os.ReadDir(folderFullPath)
 	if err != nil {
 		return err
 	}

+ 6 - 7
internal/pkg/my_util/util.go

@@ -10,7 +10,6 @@ import (
 	browser "github.com/allanpk716/fake-useragent"
 	"github.com/go-resty/resty/v2"
 	"io"
-	"io/ioutil"
 	"math"
 	"net/http"
 	"os"
@@ -145,7 +144,7 @@ func SearchMatchedVideoFile(dir string) ([]string, error) {
 
 	var fileFullPathList = make([]string, 0)
 	pathSep := string(os.PathSeparator)
-	files, err := ioutil.ReadDir(dir)
+	files, err := os.ReadDir(dir)
 	if err != nil {
 		return nil, err
 	}
@@ -223,18 +222,18 @@ func CopyFile(src, dst string) error {
 // CopyDir copies a whole directory recursively
 func CopyDir(src string, dst string) error {
 	var err error
-	var fds []os.FileInfo
-	var srcinfo os.FileInfo
+	var fds []os.DirEntry
+	var srcInfo os.FileInfo
 
-	if srcinfo, err = os.Stat(src); err != nil {
+	if srcInfo, err = os.Stat(src); err != nil {
 		return err
 	}
 
-	if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
+	if err = os.MkdirAll(dst, srcInfo.Mode()); err != nil {
 		return err
 	}
 
-	if fds, err = ioutil.ReadDir(src); err != nil {
+	if fds, err = os.ReadDir(src); err != nil {
 		return err
 	}
 	for _, fd := range fds {

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

@@ -14,7 +14,6 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/types/subparser"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/supplier"
 	"github.com/go-rod/rod/lib/utils"
-	"io/ioutil"
 	"math"
 	"os"
 	"path/filepath"
@@ -203,7 +202,7 @@ func SearchMatchedSubFileByDir(dir string) ([]string, error) {
 	// 这里有个梗,会出现 __MACOSX 这类文件夹,那么里面会有一样的文件,需要用文件大小排除一下,至少大于 1 kb 吧
 	var fileFullPathList = make([]string, 0)
 	pathSep := string(os.PathSeparator)
-	files, err := ioutil.ReadDir(dir)
+	files, err := os.ReadDir(dir)
 	if err != nil {
 		return nil, err
 	}
@@ -240,7 +239,7 @@ func SearchMatchedSubFileByOneVideo(oneVideoFullPath string) ([]string, error) {
 	fileName = strings.ToLower(fileName)
 	fileName = strings.ReplaceAll(fileName, filepath.Ext(fileName), "")
 	pathSep := string(os.PathSeparator)
-	files, err := ioutil.ReadDir(dir)
+	files, err := os.ReadDir(dir)
 	if err != nil {
 		return nil, err
 	}
@@ -281,7 +280,7 @@ func SearchVideoMatchSubFileAndRemoveExtMark(oneVideoFullPath string) error {
 	fileName = strings.ToLower(fileName)
 	fileName = strings.ReplaceAll(fileName, filepath.Ext(fileName), "")
 	pathSep := string(os.PathSeparator)
-	files, err := ioutil.ReadDir(dir)
+	files, err := os.ReadDir(dir)
 	if err != nil {
 		return err
 	}
@@ -335,7 +334,7 @@ func DeleteOneSeasonSubCacheFolder(seriesDir string) error {
 	if err != nil {
 		return err
 	}
-	files, err := ioutil.ReadDir(debugFolderByName)
+	files, err := os.ReadDir(debugFolderByName)
 	if err != nil {
 		return err
 	}

+ 1 - 2
internal/pkg/sub_timeline_fixer/restore.go

@@ -3,7 +3,6 @@ package sub_timeline_fixer
 import (
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"strings"
@@ -49,7 +48,7 @@ func searchBackUpSubFile(dir string) ([]string, error) {
 
 	var fileFullPathList = make([]string, 0)
 	pathSep := string(os.PathSeparator)
-	files, err := ioutil.ReadDir(dir)
+	files, err := os.ReadDir(dir)
 	if err != nil {
 		return nil, err
 	}