Skip to content

Commit

Permalink
fix: 修复epub生成失败的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ystyle committed Sep 19, 2022
1 parent 2945291 commit 84f74f1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
9 changes: 4 additions & 5 deletions epub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"bytes"
"fmt"
"github.com/bmaupin/go-epub"
"io/ioutil"
"os"
"path"
"path/filepath"
"time"
)

Expand All @@ -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)
}
Expand Down
14 changes: 9 additions & 5 deletions tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
}

Expand All @@ -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
}
Expand All @@ -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)
Expand Down

0 comments on commit 84f74f1

Please sign in to comment.