解决导出文档失败的BUG

This commit is contained in:
Minho
2018-03-30 17:21:16 +08:00
parent 98e93a19d5
commit 4a6494e9f8
4 changed files with 59 additions and 28 deletions

View File

@@ -17,6 +17,7 @@ import (
"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" "github.com/lifei6671/mindoc/utils/cryptil"
"sync"
) )
type Converter struct { type Converter struct {
@@ -131,32 +132,52 @@ func (this *Converter) Convert() (err error) {
os.Mkdir(this.BasePath+"/"+output, os.ModePerm) os.Mkdir(this.BasePath+"/"+output, os.ModePerm)
if len(this.Config.Format) > 0 { if len(this.Config.Format) > 0 {
var errs []string var errs []string
group := sync.WaitGroup{}
for _, v := range this.Config.Format { for _, v := range this.Config.Format {
fmt.Println("convert to " + v) fmt.Println("convert to " + v)
switch strings.ToLower(v) { switch strings.ToLower(v) {
case "epub": case "epub":
if err = this.convertToEpub(); err != nil { group.Add(1)
errs = append(errs, err.Error()) go func(group *sync.WaitGroup) {
fmt.Println("转换EPUB文档失败" + err.Error()) if err = this.convertToEpub(); err != nil {
} errs = append(errs, err.Error())
case "mobi": fmt.Println("转换EPUB文档失败" + err.Error())
if err = this.convertToMobi(); err != nil { }
errs = append(errs, err.Error()) group.Done()
fmt.Println("转换MOBI文档失败" + err.Error()) }(&group)
}
case "pdf":
if err = this.convertToPdf(); err != nil {
fmt.Println("转换PDF文档失败" + err.Error())
errs = append(errs, err.Error())
} case "mobi":
group.Add(1)
go func(group *sync.WaitGroup) {
if err = this.convertToMobi(); err != nil {
errs = append(errs, err.Error())
fmt.Println("转换MOBI文档失败" + err.Error())
}
group.Done()
}(&group)
case "pdf":
group.Add(1)
go func(group *sync.WaitGroup) {
if err = this.convertToPdf(); err != nil {
fmt.Println("转换PDF文档失败" + err.Error())
errs = append(errs, err.Error())
}
group.Done()
}(&group)
case "docx": case "docx":
if err = this.convertToDocx(); err != nil { group.Add(1)
fmt.Println("转换WORD文档失败" + err.Error()) go func(group *sync.WaitGroup) {
errs = append(errs, err.Error()) if err = this.convertToDocx(); err != nil {
} fmt.Println("转换WORD文档失败" + err.Error())
errs = append(errs, err.Error())
}
group.Done()
}(&group)
} }
} }
group.Wait()
if len(errs) > 0 { if len(errs) > 0 {
err = errors.New(strings.Join(errs, "\n")) err = errors.New(strings.Join(errs, "\n"))
} }
@@ -442,6 +463,7 @@ func (this *Converter) convertToEpub() (err error) {
if this.Debug { if this.Debug {
fmt.Println(cmd.Args) fmt.Println(cmd.Args)
} }
fmt.Println("正在转换EPUB文件", args[0])
return cmd.Run() return cmd.Run()
//return filetil.CopyFile(filepath.Join(this.OutputPath, "content.epub"),filepath.Join(this.OutputPath, output, "book.epub")) //return filetil.CopyFile(filepath.Join(this.OutputPath, "content.epub"),filepath.Join(this.OutputPath, output, "book.epub"))
@@ -457,7 +479,7 @@ func (this *Converter) convertToMobi() (err error) {
if this.Debug { if this.Debug {
fmt.Println(cmd.Args) fmt.Println(cmd.Args)
} }
fmt.Println("正在转换 MOBI 文件", args[0])
return cmd.Run() return cmd.Run()
} }
@@ -508,7 +530,7 @@ func (this *Converter) convertToPdf() (err error) {
if this.Debug { if this.Debug {
fmt.Println(cmd.Args) fmt.Println(cmd.Args)
} }
fmt.Println("正在转换 PDF 文件", args[0])
return cmd.Run() return cmd.Run()
} }
@@ -542,6 +564,7 @@ func (this *Converter) convertToDocx() (err error) {
if this.Debug { if this.Debug {
fmt.Println(cmd.Args) fmt.Println(cmd.Args)
} }
fmt.Println("正在转换 DOCX 文件", args[0])
return cmd.Run() return cmd.Run()
} }

View File

@@ -223,7 +223,7 @@ func (m *BookResult) Converter(sessionId string) (ConvertBookResult, error) {
docxpath := filepath.Join(outputPath, "book.docx") docxpath := filepath.Join(outputPath, "book.docx")
//先将转换的文件储存到临时目录 //先将转换的文件储存到临时目录
tempOutputPath := filepath.Join(os.TempDir(), sessionId, m.Identify) //filepath.Abs(filepath.Join("cache", sessionId)) tempOutputPath := filepath.Join(os.TempDir(), sessionId, m.Identify,"source") //filepath.Abs(filepath.Join("cache", sessionId))
os.MkdirAll(outputPath, 0766) os.MkdirAll(outputPath, 0766)
os.MkdirAll(tempOutputPath, 0766) os.MkdirAll(tempOutputPath, 0766)
@@ -366,21 +366,23 @@ func (m *BookResult) Converter(sessionId string) (ConvertBookResult, error) {
eBookConverter := &converter.Converter{ eBookConverter := &converter.Converter{
BasePath: tempOutputPath, BasePath: tempOutputPath,
OutputPath: strings.TrimSuffix(tempOutputPath, "sources"), OutputPath: filepath.Join(strings.TrimSuffix(tempOutputPath, "source"),"output"),
Config: ebookConfig, Config: ebookConfig,
Debug: true, Debug: true,
} }
os.MkdirAll(eBookConverter.OutputPath,0766)
if err := eBookConverter.Convert(); err != nil { if err := eBookConverter.Convert(); err != nil {
beego.Error("转换文件错误:" + m.BookName + " => " + err.Error()) beego.Error("转换文件错误:" + m.BookName + " => " + err.Error())
return convertBookResult, err return convertBookResult, err
} }
beego.Info("文档转换完成:" + m.BookName) beego.Info("文档转换完成:" + m.BookName)
filetil.CopyFile(mobipath, filepath.Join(tempOutputPath, "output", "book.mobi")) filetil.CopyFile(filepath.Join(eBookConverter.OutputPath,"output", "book.mobi"),mobipath,)
filetil.CopyFile(pdfpath, filepath.Join(tempOutputPath, "output", "book.pdf")) filetil.CopyFile(filepath.Join(eBookConverter.OutputPath,"output", "book.pdf"),pdfpath)
filetil.CopyFile(epubpath, filepath.Join(tempOutputPath, "output", "book.epub")) filetil.CopyFile(filepath.Join(eBookConverter.OutputPath,"output", "book.epub"),epubpath)
filetil.CopyFile(docxpath, filepath.Join(tempOutputPath, "output", "book.docx")) filetil.CopyFile(filepath.Join(eBookConverter.OutputPath,"output", "book.docx"),docxpath)
convertBookResult.MobiPath = mobipath convertBookResult.MobiPath = mobipath
convertBookResult.PDFPath = pdfpath convertBookResult.PDFPath = pdfpath

View File

@@ -87,6 +87,14 @@ func (m *Document) InsertOrUpdate(cols ...string) error {
if m.DocumentId > 0 { if m.DocumentId > 0 {
_, err = o.Update(m, cols...) _, err = o.Update(m, cols...)
} else { } else {
if m.Identify == "" {
book := NewBook()
identify := "docs"
if err := o.QueryTable(book.TableNameWithPrefix()).One(book,"identify");err == nil {
identify = book.Identify
}
m.Identify = fmt.Sprintf("%s-%d%d",identify,m.BookId,time.Now().Unix())
}
_, err = o.Insert(m) _, err = o.Insert(m)
NewBook().ResetDocumentNumber(m.BookId) NewBook().ResetDocumentNumber(m.BookId)
} }

View File

@@ -6,7 +6,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"fmt"
) )
//解压zip文件 //解压zip文件
@@ -71,7 +70,6 @@ func Zip(source, target string) error {
} }
header.Name = strings.TrimPrefix(strings.TrimPrefix(strings.Replace(path, "\\", "/", -1), source), "/") header.Name = strings.TrimPrefix(strings.TrimPrefix(strings.Replace(path, "\\", "/", -1), source), "/")
fmt.Println(header.Name)
if info.IsDir() { if info.IsDir() {
header.Name += "/" header.Name += "/"