🎨 Support searching EPUB asset content Fix https://github.com/siyuan-note/siyuan/issues/9000

This commit is contained in:
Daniel 2023-08-18 16:20:32 +08:00
parent 732c0805ca
commit 04cf28c962
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
4 changed files with 47 additions and 1 deletions

View file

@ -39,6 +39,7 @@ import (
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/task"
"github.com/siyuan-note/siyuan/kernel/util"
"github.com/wmentor/epub"
"github.com/xuri/excelize/v2"
)
@ -456,6 +457,7 @@ func NewAssetsSearcher() *AssetsSearcher {
".pptx": &PptxAssetParser{},
".xlsx": &XlsxAssetParser{},
".pdf": &PdfAssetParser{},
".epub": &EpubAssetParser{},
},
lock: &sync.Mutex{},
@ -750,3 +752,41 @@ func (parser *PdfAssetParser) Parse(absPath string) (ret *AssetParseResult) {
}
return
}
type EpubAssetParser struct {
}
func (parser *EpubAssetParser) Parse(absPath string) (ret *AssetParseResult) {
if !strings.HasSuffix(strings.ToLower(absPath), ".epub") {
return
}
if !gulu.File.IsExist(absPath) {
return
}
tmp := copyTempAsset(absPath)
if "" == tmp {
return
}
defer os.RemoveAll(tmp)
f, err := os.Open(tmp)
if nil != err {
logging.LogErrorf("open [%s] failed: [%s]", tmp, err)
return
}
defer f.Close()
buf := bytes.Buffer{}
if err = epub.ToTxt(tmp, &buf); nil != err {
logging.LogErrorf("convert [%s] failed: [%s]", tmp, err)
return
}
content := normalizeAssetContent(buf.String())
ret = &AssetParseResult{
Content: content,
}
return
}