|
@@ -10,26 +10,26 @@ import (
|
|
|
"path/filepath"
|
|
|
"strings"
|
|
|
|
|
|
- "time"
|
|
|
+ "github.com/juju/errors"
|
|
|
"os/exec"
|
|
|
- "errors"
|
|
|
+ "time"
|
|
|
|
|
|
+ "github.com/lifei6671/mindoc/utils/cryptil"
|
|
|
"github.com/lifei6671/mindoc/utils/filetil"
|
|
|
"github.com/lifei6671/mindoc/utils/ziptil"
|
|
|
- "github.com/lifei6671/mindoc/utils/cryptil"
|
|
|
- "sync"
|
|
|
"html"
|
|
|
+ "sync"
|
|
|
)
|
|
|
|
|
|
type Converter struct {
|
|
|
BasePath string
|
|
|
- OutputPath string
|
|
|
+ OutputPath string
|
|
|
Config Config
|
|
|
Debug bool
|
|
|
GeneratedCover string
|
|
|
- ProcessNum int //并发的任务数量
|
|
|
- process chan func()
|
|
|
- limitChan chan bool
|
|
|
+ ProcessNum int //并发的任务数量
|
|
|
+ process chan func()
|
|
|
+ limitChan chan bool
|
|
|
}
|
|
|
|
|
|
//目录结构
|
|
@@ -73,10 +73,10 @@ var (
|
|
|
)
|
|
|
|
|
|
func CheckConvertCommand() error {
|
|
|
- args := []string{ "--version" }
|
|
|
+ args := []string{"--version"}
|
|
|
cmd := exec.Command(ebookConvert, args...)
|
|
|
|
|
|
- return cmd.Run()
|
|
|
+ return errors.Trace(cmd.Run())
|
|
|
}
|
|
|
|
|
|
// 接口文档 https://manual.calibre-ebook.com/generated/en/ebook-convert.html#table-of-contents
|
|
@@ -101,12 +101,12 @@ func NewConverter(configFile string, debug ...bool) (converter *Converter, err e
|
|
|
cfg.Charset = "utf-8"
|
|
|
}
|
|
|
converter = &Converter{
|
|
|
- Config: cfg,
|
|
|
- BasePath: basepath,
|
|
|
- Debug: db,
|
|
|
+ Config: cfg,
|
|
|
+ BasePath: basepath,
|
|
|
+ Debug: db,
|
|
|
ProcessNum: 1,
|
|
|
- process: make(chan func(),4),
|
|
|
- limitChan: make(chan bool,1),
|
|
|
+ process: make(chan func(), 4),
|
|
|
+ limitChan: make(chan bool, 1),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -118,15 +118,15 @@ func (convert *Converter) Convert() (err error) {
|
|
|
if !convert.Debug { //调试模式下不删除生成的文件
|
|
|
defer convert.converterDefer() //最后移除创建的多余而文件
|
|
|
}
|
|
|
- if convert.process == nil{
|
|
|
- convert.process = make(chan func(),4)
|
|
|
+ if convert.process == nil {
|
|
|
+ convert.process = make(chan func(), 4)
|
|
|
}
|
|
|
if convert.limitChan == nil {
|
|
|
if convert.ProcessNum <= 0 {
|
|
|
convert.ProcessNum = 1
|
|
|
}
|
|
|
- convert.limitChan = make(chan bool,convert.ProcessNum)
|
|
|
- for i := 0; i < convert.ProcessNum;i++{
|
|
|
+ convert.limitChan = make(chan bool, convert.ProcessNum)
|
|
|
+ for i := 0; i < convert.ProcessNum; i++ {
|
|
|
convert.limitChan <- true
|
|
|
}
|
|
|
}
|
|
@@ -153,7 +153,7 @@ func (convert *Converter) Convert() (err error) {
|
|
|
//将当前文件夹下的所有文件压缩成zip包,然后直接改名成content.epub
|
|
|
f := filepath.Join(convert.OutputPath, "content.epub")
|
|
|
os.Remove(f) //如果原文件存在了,则删除;
|
|
|
- if err = ziptil.Zip(convert.BasePath,f); err == nil {
|
|
|
+ if err = ziptil.Zip(convert.BasePath, f); err == nil {
|
|
|
//创建导出文件夹
|
|
|
os.Mkdir(convert.BasePath+"/"+output, os.ModePerm)
|
|
|
if len(convert.Config.Format) > 0 {
|
|
@@ -164,7 +164,7 @@ func (convert *Converter) Convert() (err error) {
|
|
|
fmt.Println("convert to " + v)
|
|
|
switch strings.ToLower(v) {
|
|
|
case "epub":
|
|
|
- convert.process <- func() {
|
|
|
+ convert.process <- func() {
|
|
|
if err = convert.convertToEpub(); err != nil {
|
|
|
errs = append(errs, err.Error())
|
|
|
fmt.Println("转换EPUB文档失败:" + err.Error())
|
|
@@ -197,14 +197,14 @@ func (convert *Converter) Convert() (err error) {
|
|
|
close(convert.process)
|
|
|
}(convert)
|
|
|
|
|
|
- group := sync.WaitGroup{}
|
|
|
+ group := sync.WaitGroup{}
|
|
|
for {
|
|
|
action, isClosed := <-convert.process
|
|
|
if action == nil && !isClosed {
|
|
|
break;
|
|
|
}
|
|
|
group.Add(1)
|
|
|
- <- convert.limitChan
|
|
|
+ <-convert.limitChan
|
|
|
go func(group *sync.WaitGroup) {
|
|
|
action()
|
|
|
group.Done()
|
|
@@ -502,7 +502,7 @@ func (this *Converter) convertToEpub() (err error) {
|
|
|
//fmt.Println("正在转换EPUB文件", args[0])
|
|
|
//return cmd.Run()
|
|
|
|
|
|
- return filetil.CopyFile(args[0],args[1])
|
|
|
+ return filetil.CopyFile(args[0], args[1])
|
|
|
}
|
|
|
|
|
|
//转成mobi
|
|
@@ -541,19 +541,19 @@ func (this *Converter) convertToPdf() (err error) {
|
|
|
|
|
|
//footer template
|
|
|
if len(this.Config.Footer) > 0 {
|
|
|
- args = append(args, "--pdf-footer-template",this.Config.Footer)
|
|
|
+ args = append(args, "--pdf-footer-template", this.Config.Footer)
|
|
|
}
|
|
|
|
|
|
- if strings.Count(this.Config.MarginLeft,"") > 0 {
|
|
|
+ if strings.Count(this.Config.MarginLeft, "") > 0 {
|
|
|
args = append(args, "--pdf-page-margin-left", this.Config.MarginLeft)
|
|
|
}
|
|
|
- if strings.Count(this.Config.MarginTop,"") > 0 {
|
|
|
+ if strings.Count(this.Config.MarginTop, "") > 0 {
|
|
|
args = append(args, "--pdf-page-margin-top", this.Config.MarginTop)
|
|
|
}
|
|
|
- if strings.Count(this.Config.MarginRight,"") > 0 {
|
|
|
+ if strings.Count(this.Config.MarginRight, "") > 0 {
|
|
|
args = append(args, "--pdf-page-margin-right", this.Config.MarginRight)
|
|
|
}
|
|
|
- if strings.Count(this.Config.MarginBottom,"") > 0 {
|
|
|
+ if strings.Count(this.Config.MarginBottom, "") > 0 {
|
|
|
args = append(args, "--pdf-page-margin-bottom", this.Config.MarginBottom)
|
|
|
}
|
|
|
|
|
@@ -573,8 +573,8 @@ func (this *Converter) convertToPdf() (err error) {
|
|
|
// 转成word
|
|
|
func (this *Converter) convertToDocx() (err error) {
|
|
|
args := []string{
|
|
|
- filepath.Join(this.OutputPath , "content.epub"),
|
|
|
- filepath.Join(this.OutputPath , output , "book.docx"),
|
|
|
+ filepath.Join(this.OutputPath, "content.epub"),
|
|
|
+ filepath.Join(this.OutputPath, output, "book.docx"),
|
|
|
}
|
|
|
args = append(args, "--docx-no-toc")
|
|
|
|
|
@@ -603,25 +603,3 @@ func (this *Converter) convertToDocx() (err error) {
|
|
|
fmt.Println("正在转换 DOCX 文件", args[0])
|
|
|
return cmd.Run()
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|