ila 4 tahun lalu
induk
melakukan
0474fed7ac
3 mengubah file dengan 54 tambahan dan 9 penghapusan
  1. 26 0
      core/command_line.go
  2. 24 5
      core/file.go
  3. 4 4
      image/img.go

+ 26 - 0
core/command_line.go

@@ -0,0 +1,26 @@
+package core
+
+import (
+	"context"
+	"errors"
+	"os/exec"
+	"time"
+)
+
+func Command(name string, arg ...string) (string, error) {
+	ctxt, cancel := context.WithTimeout(context.Background(), 120*time.Second)
+	defer cancel() //releases resources if slowOperation completes before timeout elapses
+
+	cmd := exec.CommandContext(ctxt, name, arg...)
+	//当经过Timeout时间后,程序依然没有运行完,则会杀掉进程,ctxt也会有err信息
+	if out, err := cmd.Output(); err != nil {
+		//检测报错是否是因为超时引起的
+		if ctxt.Err() != nil && ctxt.Err() == context.DeadlineExceeded {
+			return "", errors.New("command timeout")
+
+		}
+		return string(out), err
+	} else {
+		return string(out), nil
+	}
+}

+ 24 - 5
core/file.go

@@ -8,10 +8,27 @@ import (
 	"sort"
 )
 
-// @title    FileExists
-// @description    检测文件是否存在
-// @param     filePath  文件路径
-// @return    bool 是否存在,error  是否成功
+// GetAllFilePath 获取指定目录内的文件和子目录的文件,递归获取
+func GetAllFilePath(filePaths []string, currentPath string) (excelPaths []string, err error) {
+	//2021-12-23
+	files, err := ioutil.ReadDir(currentPath)
+
+	if err != nil {
+		log.Printf("filepath.Glob error : %v\n", err)
+		return filePaths, err
+	}
+	for _, f := range files {
+		if f.IsDir() {
+			return GetAllFilePath(filePaths, filepath.Join(currentPath, f.Name()))
+		} else {
+			filePaths = append(filePaths, filepath.Join(currentPath, f.Name()))
+		}
+
+	}
+	return filePaths, nil
+}
+
+// FileExists 检测文件是否存在
 func FileExists(filePath string) (bool, error) {
 	_, err := os.Stat(filePath)
 	if err == nil {
@@ -38,6 +55,7 @@ func GetFileFromFolder(folder string) ([]string, error) {
 	return filePaths, nil
 }
 
+// RemoveFiles 删除指定路径和指定后缀的文件
 func RemoveFiles(folder, suffix string) error {
 	filePaths, err := GetFileFromFolder(folder)
 
@@ -60,8 +78,9 @@ func RemoveFiles(folder, suffix string) error {
 	return nil
 }
 
-//2021-11-03
+// GetSubTitleByModTime 从指定路径获取字幕文件,按mod时间排序
 func GetSubTitleByModTime(folder string) (string, error) {
+	//2021-11-03
 	filePaths := []string{}
 	files, err := ioutil.ReadDir(folder)
 

+ 4 - 4
image/img.go

@@ -11,11 +11,11 @@ import (
 	"strings"
 )
 
-// @title   loadImage
+// @title   LoadImage
 // @description    读取图片内容
 // @param
 // @return
-func loadImage(imgPath string) (image.Image, error) {
+func LoadImage(imgPath string) (image.Image, error) {
 	var img image.Image
 	var err error
 
@@ -64,9 +64,9 @@ func WriteImage(imgPath string, imgData *image.RGBA) error {
 // @param
 // @return
 func TrimImg(imgPath, subImgPath string) error {
-	img, err := loadImage(imgPath)
+	img, err := LoadImage(imgPath)
 	if err != nil {
-		log.Printf("loadImage  error : %v\n", err)
+		log.Printf("LoadImage  error : %v\n", err)
 		return err
 	}