diff --git a/converter/converter.go b/converter/converter.go index b4d32dc4..0390caea 100644 --- a/converter/converter.go +++ b/converter/converter.go @@ -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() } - - - - - - - - - - - - - - - - - - - - - - diff --git a/models/BookResult.go b/models/BookResult.go index 6ad32d2f..4d523615 100644 --- a/models/BookResult.go +++ b/models/BookResult.go @@ -97,8 +97,8 @@ func (m *BookResult) FindByIdentify(identify string, memberId int) (*BookResult, err := NewBook().QueryTable().Filter("identify", identify).One(&book) if err != nil { - beego.Error("获取项目失败 ->", err) - return m, err + beego.Error("获取项目失败 ->", errors.Details(err)) + return m, errors.Trace(err) } roleId, err := NewBook().FindForRoleId(book.BookId, memberId) @@ -112,13 +112,13 @@ func (m *BookResult) FindByIdentify(identify string, memberId int) (*BookResult, err = NewRelationship().QueryTable().Filter("book_id", book.BookId).Filter("role_id", 0).One(&relationship2) if err != nil { - logs.Error("根据项目标识查询项目以及指定用户权限的信息 -> ", err) + logs.Error("根据项目标识查询项目以及指定用户权限的信息 -> ", errors.Details(err)) return m, ErrPermissionDenied } member, err := NewMember().Find(relationship2.MemberId) if err != nil { - return m, err + return m, errors.Trace(err) } m.ToBookResult(book) @@ -240,8 +240,8 @@ func (m *BookResult) ToBookResult(book Book) *BookResult { func BackgroundConvert(sessionId string, bookResult *BookResult) error { if err := converter.CheckConvertCommand(); err != nil { - beego.Error("检查转换程序失败 -> ", err) - return err + beego.Error("检查转换程序失败 -> ", errors.Details(err)) + return errors.Trace(err) } err := exportLimitWorkerChannel.LoadOrStore(bookResult.Identify, func() { if _, err := bookResult.Converter(sessionId); err != nil { @@ -251,7 +251,7 @@ func BackgroundConvert(sessionId string, bookResult *BookResult) error { if err != nil { beego.Error("将导出任务加入任务队列失败 -> ", errors.Details(err)) - return err + return errors.Trace(err) } exportLimitWorkerChannel.Start() return nil @@ -276,15 +276,15 @@ func (m *BookResult) Converter(sessionId string) (ConvertBookResult, error) { sourceDir := strings.TrimSuffix(tempOutputPath, "source") if filetil.FileExists(sourceDir) { if err := os.RemoveAll(sourceDir); err != nil { - beego.Error("删除临时目录失败 ->", sourceDir, err) + beego.Error("删除临时目录失败 ->", sourceDir, errors.Details(err)) } } if err := filetil.MkdirAll(outputPath, 0755); err != nil { - beego.Error("创建目录失败 -> ", outputPath, err) + beego.Error("创建目录失败 -> ", outputPath, errors.Details(err)) } - if err := os.MkdirAll(tempOutputPath, 0755); err != nil { - beego.Error("创建目录失败 -> ", tempOutputPath, err) + if err := filetil.MkdirAll(tempOutputPath, 0755); err != nil { + beego.Error("创建目录失败 -> ", tempOutputPath, errors.Details(err)) } if err := filetil.MkdirAll(filepath.Join(tempOutputPath, "Images"), 0755); err != nil { return convertBookResult, errors.Trace(err) @@ -302,7 +302,7 @@ func (m *BookResult) Converter(sessionId string) (ConvertBookResult, error) { docs, err := NewDocument().FindListByBookId(m.BookId) if err != nil { - return convertBookResult, err + return convertBookResult, errors.Trace(err) } tocList := make([]converter.Toc, 0) @@ -363,7 +363,7 @@ func (m *BookResult) Converter(sessionId string) (ConvertBookResult, error) { if tempOutputPath, err = filepath.Abs(tempOutputPath); err != nil { beego.Error("导出目录配置错误:" + err.Error()) - return convertBookResult, err + return convertBookResult, errors.Trace(err) } for _, item := range docs { @@ -372,12 +372,12 @@ func (m *BookResult) Converter(sessionId string) (ConvertBookResult, error) { f, err := os.OpenFile(fpath, os.O_CREATE|os.O_RDWR, 0755) if err != nil { - return convertBookResult, err + return convertBookResult, errors.Trace(err) } var buf bytes.Buffer if err := beego.ExecuteViewPathTemplate(&buf, "document/export.tpl", viewPath, map[string]interface{}{"Model": m, "Lists": item, "BaseUrl": conf.BaseUrl}); err != nil { - return convertBookResult, err + return convertBookResult, errors.Trace(err) } html := buf.String() diff --git a/utils/filetil/filetil.go b/utils/filetil/filetil.go index 578b0c89..311ca21c 100644 --- a/utils/filetil/filetil.go +++ b/utils/filetil/filetil.go @@ -1,6 +1,7 @@ package filetil import ( + "github.com/juju/errors" "os" "path/filepath" "strings" @@ -51,7 +52,7 @@ func ScanFiles(dir string) (fl []FileList, err error) { func CopyFile(source string, dst string) (err error) { sourceFile, err := os.Open(source) if err != nil { - return err + return errors.Trace(err) } defer sourceFile.Close() @@ -60,16 +61,18 @@ func CopyFile(source string, dst string) (err error) { if err != nil { if os.IsNotExist(err) { - os.MkdirAll(filepath.Dir(dst),0766) + if err := MkdirAll(filepath.Dir(dst),0755);err != nil { + return errors.Trace(err) + } }else{ - return err + return errors.Trace(err) } } destFile, err := os.Create(dst) if err != nil { - return err + return errors.Trace(err) } defer destFile.Close() @@ -78,9 +81,9 @@ func CopyFile(source string, dst string) (err error) { if err == nil { sourceInfo, err := os.Stat(source) if err != nil { - err = os.Chmod(dst, sourceInfo.Mode()) + return errors.Trace(err) } - + err = os.Chmod(dst, sourceInfo.Mode()) } return @@ -92,13 +95,13 @@ func CopyDir(source string, dest string) (err error) { // get properties of source dir sourceInfo, err := os.Stat(source) if err != nil { - return err + return errors.Trace(err) } // create dest dir - err = os.MkdirAll(dest, sourceInfo.Mode()) + err = MkdirAll(dest, sourceInfo.Mode()) if err != nil { - return err + return errors.Trace(err) } directory, _ := os.Open(source)