mirror of https://github.com/mindoc-org/mindoc.git
feat:优化代码逻辑
parent
120fdf014c
commit
358057e0bd
|
@ -10,15 +10,15 @@ 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 {
|
||||||
|
@ -76,7 +76,7 @@ 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
|
||||||
|
@ -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