实现导入

pull/219/head
Minho 2018-01-30 18:13:25 +08:00
parent fa9388056c
commit 8e4bb134d6
5 changed files with 179 additions and 0 deletions

View File

@ -20,6 +20,8 @@ import (
"github.com/lifei6671/mindoc/models"
"github.com/lifei6671/mindoc/utils"
"github.com/lifei6671/mindoc/utils/pagination"
"net/http"
"github.com/lifei6671/mindoc/converter"
)
type BookController struct {
@ -449,6 +451,30 @@ func (c *BookController) Create() {
c.JsonResult(6001, "error")
}
func (c *BookController) Import() {
file, moreFile, err := c.GetFile("import-file")
if err == http.ErrMissingFile {
c.JsonResult(6003, "没有发现需要上传的文件")
}
defer file.Close()
beego.Info(moreFile.Filename)
tempPath := filepath.Join(os.TempDir(),c.CruSession.SessionID())
os.MkdirAll(tempPath,0766)
tempPath = filepath.Join(tempPath,moreFile.Filename)
err = c.SaveToFile("import-file", tempPath)
converter.Resolve(tempPath)
}
// CreateToken 创建访问来令牌.
func (c *BookController) CreateToken() {

View File

@ -542,3 +542,25 @@ func (this *Converter) convertToDocx() (err error) {
}
return cmd.Run()
}

118
converter/import.go 100644
View File

@ -0,0 +1,118 @@
package converter
import (
"errors"
"github.com/lifei6671/mindoc/utils"
"os"
"path/filepath"
"crypto/md5"
"io"
"fmt"
"os/exec"
"github.com/lifei6671/mindoc/utils/ziptil"
"io/ioutil"
"encoding/xml"
)
type ResolveResult struct {
}
type XmlResult struct {
XMLName xml.Name `xml:"ncx"`
Head XmlHead `xml:"head"`
NavMap XmlTocNavMap `xml:"navMap"`
Title string `xml:"docTitle>text"`
}
type XmlHead struct {
XMLName xml.Name `xml:"head"`
Meta []XmlMeta `xml:"meta"`
}
type XmlMeta struct {
XMLName xml.Name `xml:"meta"`
Content string `xml:"content,attr"`
Name string `xml:"name,attr"`
}
type XmlDocTitle struct {
Text string `xml:"text"`
}
type XmlTocNavMap struct {
XMLName xml.Name `xml:"navMap"`
NavPoint []XmlNavPoint `xml:"navPoint"`
}
type XmlNavPoint struct {
XMLName xml.Name `xml:"navPoint"`
Content XmlContent `xml:"content"`
NavLabel string `xml:"navLabel>text"`
}
type XmlContent struct {
XMLName xml.Name `xml:"content"`
Src string `xml:"src,attr"`
}
type XmlNavLabel struct {
}
func Resolve(p string) (ResolveResult,error) {
result := ResolveResult{
}
if !utils.FileExists(p) {
return result,errors.New("文件不存在 " + p)
}
w := md5.New()
io.WriteString(w, p) //将str写入到w中
md5str := fmt.Sprintf("%x", w.Sum(nil)) //w.Sum(nil)将w的hash转成[]byte格式
tempPath := filepath.Join(os.TempDir(),md5str)
os.MkdirAll(tempPath,0766)
epub := filepath.Join(tempPath , "book.epub")
args := []string{p,epub}
cmd := exec.Command(ebookConvert, args...)
if err := cmd.Run(); err != nil {
fmt.Println("执行转换命令失败:" + err.Error())
return result,err
}
fmt.Println(epub)
unzipPath := filepath.Join(tempPath,"output")
if err := ziptil.Unzip(epub, unzipPath); err != nil {
fmt.Println("解压缩失败:" + err.Error())
return result,err
}
xmlPath := filepath.Join(unzipPath,"toc.ncx")
data,err := ioutil.ReadFile(xmlPath);
if err != nil {
fmt.Println("toc.ncx 文件不存在:" + err.Error())
return result,err
}
v := XmlResult{}
err = xml.Unmarshal([]byte(data), &v)
if err != nil {
fmt.Println("解析XML失败" + err.Error())
return result,err
}
fmt.Println(v)
return result,nil
}

12
import.go 100644
View File

@ -0,0 +1,12 @@
package main
import (
"github.com/lifei6671/mindoc/converter"
)
func main() {
p := "F:/官网API对接-跨境.docx"
converter.Resolve(p)
}

View File

@ -49,6 +49,7 @@ func init() {
beego.Router("/book/users/create", &controllers.BookMemberController{}, "post:AddMember")
beego.Router("/book/users/change", &controllers.BookMemberController{}, "post:ChangeRole")
beego.Router("/book/users/delete", &controllers.BookMemberController{}, "post:RemoveMember")
beego.Router("/book/users/import", &controllers.BookController{},"post:Import")
beego.Router("/book/setting/save", &controllers.BookController{}, "post:SaveBook")
beego.Router("/book/setting/open", &controllers.BookController{}, "post:PrivatelyOwned")