From 84f74f132c8c6d2d14b3e2f8a55e6e712e7e391f Mon Sep 17 00:00:00 2001 From: ystyle Date: Mon, 19 Sep 2022 16:36:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Depub=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- epub.go | 9 ++++----- tools.go | 14 +++++++++----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/epub.go b/epub.go index b93d2cd..ac93cd2 100644 --- a/epub.go +++ b/epub.go @@ -4,9 +4,8 @@ import ( "bytes" "fmt" "github.com/bmaupin/go-epub" - "io/ioutil" "os" - "path" + "path/filepath" "time" ) @@ -25,15 +24,15 @@ func (convert EpubConverter) Build(book Book) error { fmt.Println("正在生成epub") start := time.Now() // 写入样式 - tempDir, err := ioutil.TempDir("", "kaf-cli") + tempDir, err := os.MkdirTemp("", "kaf-cli") defer func() { if err := os.RemoveAll(tempDir); err != nil { panic(fmt.Sprintf("创建临时文件夹失败: %s", err)) } }() - pageStylesFile := path.Join(tempDir, "page_styles.css") + pageStylesFile := filepath.Join(tempDir, "page_styles.css") - err = ioutil.WriteFile(pageStylesFile, []byte(fmt.Sprintf(cssContent, book.Align, book.Bottom, book.Indent)), 0666) + err = os.WriteFile(pageStylesFile, []byte(fmt.Sprintf(cssContent, book.Align, book.Bottom, book.Indent)), 0666) if err != nil { return fmt.Errorf("无法写入样式文件: %w", err) } diff --git a/tools.go b/tools.go index e8fb6c9..7203370 100644 --- a/tools.go +++ b/tools.go @@ -2,12 +2,11 @@ package kafcli import ( "fmt" - "golang.org/x/sys/execabs" "io/ioutil" "math/rand" "os" "os/exec" - "path" + "path/filepath" "runtime" "strings" "time" @@ -27,6 +26,7 @@ func parseLang(lang string) string { func run(command string, args ...string) error { cmd := exec.Command(command, args...) cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout return cmd.Run() } @@ -35,16 +35,17 @@ func lookKindlegen() string { if runtime.GOOS == "windows" { command = "kindlegen.exe" } - kindlegen, err := execabs.LookPath(command) + kindlegen, err := exec.LookPath(command) if err != nil { currentDir, err := os.Executable() if err != nil { return "" } - kindlegen = path.Join(path.Dir(currentDir), command) + kindlegen = filepath.Join(filepath.Dir(currentDir), command) if exist, _ := isExists(kindlegen); !exist { return "" } + fmt.Println("kindlegen: ", kindlegen) } return kindlegen } @@ -54,7 +55,10 @@ func converToMobi(bookname, lang string) { fmt.Printf("\n检测到Kindle格式转换器: %s,正在把书籍转换成Kindle格式...\n", command) fmt.Println("转换mobi比较花时间, 大约耗时1-10分钟, 请等待...") start := time.Now() - run(command, "-dont_append_source", "-locale", lang, "-c1", bookname) + err := run(command, "-dont_append_source", "-locale", lang, "-c1", bookname) + if err != nil { + panic(err) + } // 计算耗时 end := time.Now().Sub(start) fmt.Println("转换为mobi格式耗时:", end)