mirror of https://github.com/mindoc-org/mindoc.git
feat:优化代码逻辑
parent
120fdf014c
commit
358057e0bd
|
@ -10,26 +10,26 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"time"
|
"github.com/juju/errors"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"errors"
|
"time"
|
||||||
|
|
||||||
|
"github.com/lifei6671/mindoc/utils/cryptil"
|
||||||
"github.com/lifei6671/mindoc/utils/filetil"
|
"github.com/lifei6671/mindoc/utils/filetil"
|
||||||
"github.com/lifei6671/mindoc/utils/ziptil"
|
"github.com/lifei6671/mindoc/utils/ziptil"
|
||||||
"github.com/lifei6671/mindoc/utils/cryptil"
|
|
||||||
"sync"
|
|
||||||
"html"
|
"html"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Converter struct {
|
type Converter struct {
|
||||||
BasePath string
|
BasePath string
|
||||||
OutputPath string
|
OutputPath string
|
||||||
Config Config
|
Config Config
|
||||||
Debug bool
|
Debug bool
|
||||||
GeneratedCover string
|
GeneratedCover string
|
||||||
ProcessNum int //并发的任务数量
|
ProcessNum int //并发的任务数量
|
||||||
process chan func()
|
process chan func()
|
||||||
limitChan chan bool
|
limitChan chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
//目录结构
|
//目录结构
|
||||||
|
@ -73,10 +73,10 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func CheckConvertCommand() error {
|
func CheckConvertCommand() error {
|
||||||
args := []string{ "--version" }
|
args := []string{"--version"}
|
||||||
cmd := exec.Command(ebookConvert, args...)
|
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
|
// 接口文档 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"
|
cfg.Charset = "utf-8"
|
||||||
}
|
}
|
||||||
converter = &Converter{
|
converter = &Converter{
|
||||||
Config: cfg,
|
Config: cfg,
|
||||||
BasePath: basepath,
|
BasePath: basepath,
|
||||||
Debug: db,
|
Debug: db,
|
||||||
ProcessNum: 1,
|
ProcessNum: 1,
|
||||||
process: make(chan func(),4),
|
process: make(chan func(), 4),
|
||||||
limitChan: make(chan bool,1),
|
limitChan: make(chan bool, 1),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -118,15 +118,15 @@ func (convert *Converter) Convert() (err error) {
|
||||||
if !convert.Debug { //调试模式下不删除生成的文件
|
if !convert.Debug { //调试模式下不删除生成的文件
|
||||||
defer convert.converterDefer() //最后移除创建的多余而文件
|
defer convert.converterDefer() //最后移除创建的多余而文件
|
||||||
}
|
}
|
||||||
if convert.process == nil{
|
if convert.process == nil {
|
||||||
convert.process = make(chan func(),4)
|
convert.process = make(chan func(), 4)
|
||||||
}
|
}
|
||||||
if convert.limitChan == nil {
|
if convert.limitChan == nil {
|
||||||
if convert.ProcessNum <= 0 {
|
if convert.ProcessNum <= 0 {
|
||||||
convert.ProcessNum = 1
|
convert.ProcessNum = 1
|
||||||
}
|
}
|
||||||
convert.limitChan = make(chan bool,convert.ProcessNum)
|
convert.limitChan = make(chan bool, convert.ProcessNum)
|
||||||
for i := 0; i < convert.ProcessNum;i++{
|
for i := 0; i < convert.ProcessNum; i++ {
|
||||||
convert.limitChan <- true
|
convert.limitChan <- true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,7 +153,7 @@ func (convert *Converter) Convert() (err error) {
|
||||||
//将当前文件夹下的所有文件压缩成zip包,然后直接改名成content.epub
|
//将当前文件夹下的所有文件压缩成zip包,然后直接改名成content.epub
|
||||||
f := filepath.Join(convert.OutputPath, "content.epub")
|
f := filepath.Join(convert.OutputPath, "content.epub")
|
||||||
os.Remove(f) //如果原文件存在了,则删除;
|
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)
|
os.Mkdir(convert.BasePath+"/"+output, os.ModePerm)
|
||||||
if len(convert.Config.Format) > 0 {
|
if len(convert.Config.Format) > 0 {
|
||||||
|
@ -164,7 +164,7 @@ func (convert *Converter) Convert() (err error) {
|
||||||
fmt.Println("convert to " + v)
|
fmt.Println("convert to " + v)
|
||||||
switch strings.ToLower(v) {
|
switch strings.ToLower(v) {
|
||||||
case "epub":
|
case "epub":
|
||||||
convert.process <- func() {
|
convert.process <- func() {
|
||||||
if err = convert.convertToEpub(); err != nil {
|
if err = convert.convertToEpub(); err != nil {
|
||||||
errs = append(errs, err.Error())
|
errs = append(errs, err.Error())
|
||||||
fmt.Println("转换EPUB文档失败:" + err.Error())
|
fmt.Println("转换EPUB文档失败:" + err.Error())
|
||||||
|
@ -197,14 +197,14 @@ func (convert *Converter) Convert() (err error) {
|
||||||
close(convert.process)
|
close(convert.process)
|
||||||
}(convert)
|
}(convert)
|
||||||
|
|
||||||
group := sync.WaitGroup{}
|
group := sync.WaitGroup{}
|
||||||
for {
|
for {
|
||||||
action, isClosed := <-convert.process
|
action, isClosed := <-convert.process
|
||||||
if action == nil && !isClosed {
|
if action == nil && !isClosed {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
group.Add(1)
|
group.Add(1)
|
||||||
<- convert.limitChan
|
<-convert.limitChan
|
||||||
go func(group *sync.WaitGroup) {
|
go func(group *sync.WaitGroup) {
|
||||||
action()
|
action()
|
||||||
group.Done()
|
group.Done()
|
||||||
|
@ -502,7 +502,7 @@ func (this *Converter) convertToEpub() (err error) {
|
||||||
//fmt.Println("正在转换EPUB文件", args[0])
|
//fmt.Println("正在转换EPUB文件", args[0])
|
||||||
//return cmd.Run()
|
//return cmd.Run()
|
||||||
|
|
||||||
return filetil.CopyFile(args[0],args[1])
|
return filetil.CopyFile(args[0], args[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
//转成mobi
|
//转成mobi
|
||||||
|
@ -541,19 +541,19 @@ func (this *Converter) convertToPdf() (err error) {
|
||||||
|
|
||||||
//footer template
|
//footer template
|
||||||
if len(this.Config.Footer) > 0 {
|
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)
|
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)
|
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)
|
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)
|
args = append(args, "--pdf-page-margin-bottom", this.Config.MarginBottom)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -573,8 +573,8 @@ func (this *Converter) convertToPdf() (err error) {
|
||||||
// 转成word
|
// 转成word
|
||||||
func (this *Converter) convertToDocx() (err error) {
|
func (this *Converter) convertToDocx() (err error) {
|
||||||
args := []string{
|
args := []string{
|
||||||
filepath.Join(this.OutputPath , "content.epub"),
|
filepath.Join(this.OutputPath, "content.epub"),
|
||||||
filepath.Join(this.OutputPath , output , "book.docx"),
|
filepath.Join(this.OutputPath, output, "book.docx"),
|
||||||
}
|
}
|
||||||
args = append(args, "--docx-no-toc")
|
args = append(args, "--docx-no-toc")
|
||||||
|
|
||||||
|
@ -603,25 +603,3 @@ func (this *Converter) convertToDocx() (err error) {
|
||||||
fmt.Println("正在转换 DOCX 文件", args[0])
|
fmt.Println("正在转换 DOCX 文件", args[0])
|
||||||
return cmd.Run()
|
return cmd.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -97,8 +97,8 @@ func (m *BookResult) FindByIdentify(identify string, memberId int) (*BookResult,
|
||||||
err := NewBook().QueryTable().Filter("identify", identify).One(&book)
|
err := NewBook().QueryTable().Filter("identify", identify).One(&book)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
beego.Error("获取项目失败 ->", err)
|
beego.Error("获取项目失败 ->", errors.Details(err))
|
||||||
return m, err
|
return m, errors.Trace(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
roleId, err := NewBook().FindForRoleId(book.BookId, memberId)
|
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)
|
err = NewRelationship().QueryTable().Filter("book_id", book.BookId).Filter("role_id", 0).One(&relationship2)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Error("根据项目标识查询项目以及指定用户权限的信息 -> ", err)
|
logs.Error("根据项目标识查询项目以及指定用户权限的信息 -> ", errors.Details(err))
|
||||||
return m, ErrPermissionDenied
|
return m, ErrPermissionDenied
|
||||||
}
|
}
|
||||||
|
|
||||||
member, err := NewMember().Find(relationship2.MemberId)
|
member, err := NewMember().Find(relationship2.MemberId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return m, err
|
return m, errors.Trace(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
m.ToBookResult(book)
|
m.ToBookResult(book)
|
||||||
|
@ -240,8 +240,8 @@ func (m *BookResult) ToBookResult(book Book) *BookResult {
|
||||||
func BackgroundConvert(sessionId string, bookResult *BookResult) error {
|
func BackgroundConvert(sessionId string, bookResult *BookResult) error {
|
||||||
|
|
||||||
if err := converter.CheckConvertCommand(); err != nil {
|
if err := converter.CheckConvertCommand(); err != nil {
|
||||||
beego.Error("检查转换程序失败 -> ", err)
|
beego.Error("检查转换程序失败 -> ", errors.Details(err))
|
||||||
return err
|
return errors.Trace(err)
|
||||||
}
|
}
|
||||||
err := exportLimitWorkerChannel.LoadOrStore(bookResult.Identify, func() {
|
err := exportLimitWorkerChannel.LoadOrStore(bookResult.Identify, func() {
|
||||||
if _, err := bookResult.Converter(sessionId); err != nil {
|
if _, err := bookResult.Converter(sessionId); err != nil {
|
||||||
|
@ -251,7 +251,7 @@ func BackgroundConvert(sessionId string, bookResult *BookResult) error {
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
beego.Error("将导出任务加入任务队列失败 -> ", errors.Details(err))
|
beego.Error("将导出任务加入任务队列失败 -> ", errors.Details(err))
|
||||||
return err
|
return errors.Trace(err)
|
||||||
}
|
}
|
||||||
exportLimitWorkerChannel.Start()
|
exportLimitWorkerChannel.Start()
|
||||||
return nil
|
return nil
|
||||||
|
@ -276,15 +276,15 @@ func (m *BookResult) Converter(sessionId string) (ConvertBookResult, error) {
|
||||||
sourceDir := strings.TrimSuffix(tempOutputPath, "source")
|
sourceDir := strings.TrimSuffix(tempOutputPath, "source")
|
||||||
if filetil.FileExists(sourceDir) {
|
if filetil.FileExists(sourceDir) {
|
||||||
if err := os.RemoveAll(sourceDir); err != nil {
|
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 {
|
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 {
|
if err := filetil.MkdirAll(tempOutputPath, 0755); err != nil {
|
||||||
beego.Error("创建目录失败 -> ", tempOutputPath, err)
|
beego.Error("创建目录失败 -> ", tempOutputPath, errors.Details(err))
|
||||||
}
|
}
|
||||||
if err := filetil.MkdirAll(filepath.Join(tempOutputPath, "Images"), 0755); err != nil {
|
if err := filetil.MkdirAll(filepath.Join(tempOutputPath, "Images"), 0755); err != nil {
|
||||||
return convertBookResult, errors.Trace(err)
|
return convertBookResult, errors.Trace(err)
|
||||||
|
@ -302,7 +302,7 @@ func (m *BookResult) Converter(sessionId string) (ConvertBookResult, error) {
|
||||||
|
|
||||||
docs, err := NewDocument().FindListByBookId(m.BookId)
|
docs, err := NewDocument().FindListByBookId(m.BookId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return convertBookResult, err
|
return convertBookResult, errors.Trace(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
tocList := make([]converter.Toc, 0)
|
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 {
|
if tempOutputPath, err = filepath.Abs(tempOutputPath); err != nil {
|
||||||
beego.Error("导出目录配置错误:" + err.Error())
|
beego.Error("导出目录配置错误:" + err.Error())
|
||||||
return convertBookResult, err
|
return convertBookResult, errors.Trace(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, item := range docs {
|
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)
|
f, err := os.OpenFile(fpath, os.O_CREATE|os.O_RDWR, 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return convertBookResult, err
|
return convertBookResult, errors.Trace(err)
|
||||||
}
|
}
|
||||||
var buf bytes.Buffer
|
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 {
|
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()
|
html := buf.String()
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package filetil
|
package filetil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/juju/errors"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -51,7 +52,7 @@ func ScanFiles(dir string) (fl []FileList, err error) {
|
||||||
func CopyFile(source string, dst string) (err error) {
|
func CopyFile(source string, dst string) (err error) {
|
||||||
sourceFile, err := os.Open(source)
|
sourceFile, err := os.Open(source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Trace(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer sourceFile.Close()
|
defer sourceFile.Close()
|
||||||
|
@ -60,16 +61,18 @@ func CopyFile(source string, dst string) (err error) {
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
os.MkdirAll(filepath.Dir(dst),0766)
|
if err := MkdirAll(filepath.Dir(dst),0755);err != nil {
|
||||||
|
return errors.Trace(err)
|
||||||
|
}
|
||||||
}else{
|
}else{
|
||||||
return err
|
return errors.Trace(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
destFile, err := os.Create(dst)
|
destFile, err := os.Create(dst)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Trace(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer destFile.Close()
|
defer destFile.Close()
|
||||||
|
@ -78,9 +81,9 @@ func CopyFile(source string, dst string) (err error) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
sourceInfo, err := os.Stat(source)
|
sourceInfo, err := os.Stat(source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = os.Chmod(dst, sourceInfo.Mode())
|
return errors.Trace(err)
|
||||||
}
|
}
|
||||||
|
err = os.Chmod(dst, sourceInfo.Mode())
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
@ -92,13 +95,13 @@ func CopyDir(source string, dest string) (err error) {
|
||||||
// get properties of source dir
|
// get properties of source dir
|
||||||
sourceInfo, err := os.Stat(source)
|
sourceInfo, err := os.Stat(source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Trace(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// create dest dir
|
// create dest dir
|
||||||
err = os.MkdirAll(dest, sourceInfo.Mode())
|
err = MkdirAll(dest, sourceInfo.Mode())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Trace(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
directory, _ := os.Open(source)
|
directory, _ := os.Open(source)
|
||||||
|
|
Loading…
Reference in New Issue