소스 검색

解决导出文档失败的BUG

Minho 7 년 전
부모
커밋
4a6494e9f8
4개의 변경된 파일58개의 추가작업 그리고 27개의 파일을 삭제
  1. 42 19
      converter/converter.go
  2. 8 6
      models/book_result.go
  3. 8 0
      models/document.go
  4. 0 2
      utils/ziptil/ziptil.go

+ 42 - 19
converter/converter.go

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

+ 8 - 6
models/book_result.go

@@ -223,7 +223,7 @@ func (m *BookResult) Converter(sessionId string) (ConvertBookResult, error) {
 	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(tempOutputPath, 0766)
@@ -366,21 +366,23 @@ func (m *BookResult) Converter(sessionId string) (ConvertBookResult, error) {
 
 	eBookConverter := &converter.Converter{
 		BasePath:   tempOutputPath,
-		OutputPath: strings.TrimSuffix(tempOutputPath, "sources"),
+		OutputPath: filepath.Join(strings.TrimSuffix(tempOutputPath, "source"),"output"),
 		Config:     ebookConfig,
 		Debug:      true,
 	}
 
+	os.MkdirAll(eBookConverter.OutputPath,0766)
+
 	if err := eBookConverter.Convert(); err != nil {
 		beego.Error("转换文件错误:" + m.BookName + " => " + err.Error())
 		return convertBookResult, err
 	}
 	beego.Info("文档转换完成:" + m.BookName)
 
-	filetil.CopyFile(mobipath, filepath.Join(tempOutputPath, "output", "book.mobi"))
-	filetil.CopyFile(pdfpath, filepath.Join(tempOutputPath, "output", "book.pdf"))
-	filetil.CopyFile(epubpath, filepath.Join(tempOutputPath, "output", "book.epub"))
-	filetil.CopyFile(docxpath, filepath.Join(tempOutputPath, "output", "book.docx"))
+	filetil.CopyFile(filepath.Join(eBookConverter.OutputPath,"output", "book.mobi"),mobipath,)
+	filetil.CopyFile(filepath.Join(eBookConverter.OutputPath,"output", "book.pdf"),pdfpath)
+	filetil.CopyFile(filepath.Join(eBookConverter.OutputPath,"output", "book.epub"),epubpath)
+	filetil.CopyFile(filepath.Join(eBookConverter.OutputPath,"output", "book.docx"),docxpath)
 
 	convertBookResult.MobiPath = mobipath
 	convertBookResult.PDFPath = pdfpath

+ 8 - 0
models/document.go

@@ -87,6 +87,14 @@ func (m *Document) InsertOrUpdate(cols ...string) error {
 	if m.DocumentId > 0 {
 		_, err = o.Update(m, cols...)
 	} 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)
 		NewBook().ResetDocumentNumber(m.BookId)
 	}

+ 0 - 2
utils/ziptil/ziptil.go

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