mirror of
https://github.com/mindoc-org/mindoc.git
synced 2025-04-05 20:17:53 +08:00
优化导入逻辑,兼容含有BOM头的文件
This commit is contained in:
parent
3a1337c0bd
commit
297a7a1b11
@ -528,10 +528,11 @@ func (book *Book) ImportBook(zipPath string) error {
|
||||
doc.Identify = docIdentify
|
||||
//匹配图片,如果图片语法是在代码块中,这里同样会处理
|
||||
re := regexp.MustCompile(`!\[(.*?)\]\((.*?)\)`)
|
||||
markdown, err := ioutil.ReadFile(path)
|
||||
markdown, err := filetil.ReadFileAndIgnoreUTF8BOM(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//处理图片
|
||||
doc.Markdown = re.ReplaceAllStringFunc(string(markdown), func(image string) string {
|
||||
|
||||
@ -720,6 +721,7 @@ func (book *Book) ImportBook(zipPath string) error {
|
||||
beego.Error("导入项目异常 => ", err)
|
||||
book.Description = "【项目导入存在错误:" + err.Error() + "】"
|
||||
}
|
||||
beego.Info("项目导入完毕 => ",book.BookName)
|
||||
book.ReleaseContent(book.BookId)
|
||||
return err
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ import (
|
||||
"io"
|
||||
"fmt"
|
||||
"math"
|
||||
"io/ioutil"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
//==================================
|
||||
@ -217,4 +219,39 @@ func HasFileOfExt(path string,exts []string) bool {
|
||||
})
|
||||
|
||||
return err == os.ErrExist
|
||||
}
|
||||
//忽略字符串中的BOM头
|
||||
func ReadFileAndIgnoreUTF8BOM(filename string) ([]byte,error) {
|
||||
|
||||
data,err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil,err
|
||||
}
|
||||
if data == nil {
|
||||
return nil,nil
|
||||
}
|
||||
data = bytes.Replace(data,[]byte("\r"),[]byte(""),-1)
|
||||
if len(data) >= 3 && data[0] == 0xef && data[1] == 0xbb && data[2] == 0xbf {
|
||||
return data[3:],err
|
||||
}
|
||||
|
||||
|
||||
return data,nil
|
||||
|
||||
//fd, err := os.Open(filename)
|
||||
//
|
||||
//if err != nil {
|
||||
// return nil,err
|
||||
//}
|
||||
//bom := [3]byte{}
|
||||
//
|
||||
//_, err = io.ReadFull(fd, bom[:])
|
||||
//if err != nil {
|
||||
// return nil,err
|
||||
//}
|
||||
//if bom[0] != 0xef || bom[1] != 0xbb || bom[2] != 0xbf {
|
||||
// _, err = fd.Seek(0, 0)
|
||||
//}
|
||||
//
|
||||
//return ioutil.ReadAll(fd)
|
||||
}
|
Loading…
Reference in New Issue
Block a user