mirror of
https://github.com/mindoc-org/mindoc.git
synced 2026-02-27 17:03:57 +08:00
解决导出文档失败的BUG
This commit is contained in:
@@ -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":
|
||||||
|
group.Add(1)
|
||||||
|
go func(group *sync.WaitGroup) {
|
||||||
if err = this.convertToEpub(); err != nil {
|
if err = this.convertToEpub(); err != nil {
|
||||||
errs = append(errs, err.Error())
|
errs = append(errs, err.Error())
|
||||||
fmt.Println("转换EPUB文档失败:" + err.Error())
|
fmt.Println("转换EPUB文档失败:" + err.Error())
|
||||||
}
|
}
|
||||||
|
group.Done()
|
||||||
|
}(&group)
|
||||||
|
|
||||||
case "mobi":
|
case "mobi":
|
||||||
|
group.Add(1)
|
||||||
|
go func(group *sync.WaitGroup) {
|
||||||
if err = this.convertToMobi(); err != nil {
|
if err = this.convertToMobi(); err != nil {
|
||||||
errs = append(errs, err.Error())
|
errs = append(errs, err.Error())
|
||||||
fmt.Println("转换MOBI文档失败:" + err.Error())
|
fmt.Println("转换MOBI文档失败:" + err.Error())
|
||||||
}
|
}
|
||||||
|
group.Done()
|
||||||
|
}(&group)
|
||||||
case "pdf":
|
case "pdf":
|
||||||
|
group.Add(1)
|
||||||
|
go func(group *sync.WaitGroup) {
|
||||||
if err = this.convertToPdf(); err != nil {
|
if err = this.convertToPdf(); err != nil {
|
||||||
fmt.Println("转换PDF文档失败:" + err.Error())
|
fmt.Println("转换PDF文档失败:" + err.Error())
|
||||||
errs = append(errs, err.Error())
|
errs = append(errs, err.Error())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
group.Done()
|
||||||
|
}(&group)
|
||||||
case "docx":
|
case "docx":
|
||||||
|
group.Add(1)
|
||||||
|
go func(group *sync.WaitGroup) {
|
||||||
if err = this.convertToDocx(); err != nil {
|
if err = this.convertToDocx(); err != nil {
|
||||||
fmt.Println("转换WORD文档失败:" + err.Error())
|
fmt.Println("转换WORD文档失败:" + err.Error())
|
||||||
errs = append(errs, 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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 += "/"
|
||||||
|
|||||||
Reference in New Issue
Block a user