From 4eec37e7c9cb532d9ef934b9477bb587f2860a7b Mon Sep 17 00:00:00 2001 From: Augists Date: Fri, 11 Feb 2022 13:31:03 +0800 Subject: [PATCH] fix bugs --- go.mod | 1 + go.sum | 4 ++++ models/BookModel.go | 50 +++++++++++++++++++++++---------------------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index ad9a1f1b..7d2418c5 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/kardianos/service v1.1.0 github.com/lib/pq v1.7.0 // indirect github.com/lifei6671/gocaptcha v0.1.1 + github.com/mattn/go-runewidth v0.0.13 github.com/mattn/go-sqlite3 v2.0.3+incompatible github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/russross/blackfriday/v2 v2.1.0 diff --git a/go.sum b/go.sum index e74efd15..7140e3f5 100644 --- a/go.sum +++ b/go.sum @@ -126,6 +126,8 @@ github.com/lib/pq v1.7.0 h1:h93mCPfUSkaul3Ka/VG8uZdmW1uMHDGxzu0NWHuJmHY= github.com/lib/pq v1.7.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lifei6671/gocaptcha v0.1.1 h1:5cvU3w0bK8eJm1P6AiQoPuicoZVAgKKpREBxXF9IaHo= github.com/lifei6671/gocaptcha v0.1.1/go.mod h1:6QlTU2WzFhzqylAJWSo3OANfKCraGccJwbK01P5fFmI= +github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= +github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U= github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= @@ -174,6 +176,8 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/prometheus/procfs v0.1.3 h1:F0+tqvhOksq22sc6iCHF5WGlWjdwj92p0udFh1VFBS8= github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= diff --git a/models/BookModel.go b/models/BookModel.go index 67fda8ce..db0f0b94 100644 --- a/models/BookModel.go +++ b/models/BookModel.go @@ -979,49 +979,51 @@ func (book *Book) ImportBook(zipPath string, lang string) error { } // 导入docx项目 -func (book *Book) ImportWordBook(docxPath string, lang string) error { +func (book *Book) ImportWordBook(docxPath string, lang string) (err error) { if !filetil.FileExists(docxPath) { return errors.New("文件不存在") } - docxPath = strings.Replace(docxPath, "\\", "/", -1) + docxPath = strings.Replace(docxPath, "\\", "/", -1) o := orm.NewOrm() o.Insert(book) relationship := NewRelationship() relationship.BookId = book.BookId - relationship.RoldId = 0 + // relationship.RoldId = 0 relationship.MemberId = book.MemberId relationship.Insert() - doc := NewDocument() - doc.BookId = book.BookId - doc.MemberId = book.MemberId - docIdentify := strings.Replace(strings.TrimPrefix(docxPath, os.TempDir()+"/"), "/", "-", -1) + doc := NewDocument() + doc.BookId = book.BookId + doc.MemberId = book.MemberId + docIdentify := strings.Replace(strings.TrimPrefix(docxPath, os.TempDir()+"/"), "/", "-", -1) - if ok, err := regexp.MatchString(`[a-z]+[a-zA-Z0-9_.\-]*$`, docIdentify); !ok || err != nil { - docIdentify = "import-" + docIdentify - } + if ok, err := regexp.MatchString(`[a-z]+[a-zA-Z0-9_.\-]*$`, docIdentify); !ok || err != nil { + docIdentify = "import-" + docIdentify + } - doc.Identify = docIdentify + doc.Identify = docIdentify - if doc.Markdown, err := util.Docx2md(docxPath, false); err != nil { - logs.Error("导入doc项目转换异常 => ", err) - exit(1) - } + if doc.Markdown, err = utils.Docx2md(docxPath, false); err != nil { + logs.Error("导入doc项目转换异常 => ", err) + return err + } - doc.Content = string(blackfriday.Run([]byte(doc.Markdown))) + doc.Content = string(blackfriday.Run([]byte(doc.Markdown))) - doc.Version = time.Now().Unix() + doc.Version = time.Now().Unix() - for _, line := range strings.Split(doc.Markdown, "\n") { - if strings.HasPrefix(line, "#") { - docName := strings.TrimLeft(line, "#") - break - } - } + var docName string + for _, line := range strings.Split(doc.Markdown, "\n") { + if strings.HasPrefix(line, "#") { + docName = strings.TrimLeft(line, "#") + break + } + } - doc.DocumentName = strings.TrimSpace(docName) + doc.DocumentName = strings.TrimSpace(docName) + return err } func (book *Book) FindForRoleId(bookId, memberId int) (conf.BookRole, error) {