Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Vanessa 2023-12-20 22:23:44 +08:00
commit a208740195
3 changed files with 14 additions and 9 deletions

View file

@ -62,7 +62,7 @@ func docTagSpans(n *ast.Node) (ret []*Span) {
return
}
func docTitleImgAsset(root *ast.Node) *Asset {
func docTitleImgAsset(root *ast.Node, boxLocalPath, docDirLocalPath string) *Asset {
if p := treenode.GetDocTitleImgPath(root); "" != p {
if !util.IsAssetLinkDest([]byte(p)) {
return nil
@ -70,18 +70,21 @@ func docTitleImgAsset(root *ast.Node) *Asset {
var hash string
var err error
absPath := filepath.Join(util.DataDir, p)
if hash, err = util.GetEtag(absPath); nil != err {
logging.LogErrorf("read asset [%s] data failed: %s", absPath, err)
return nil
if lp := assetLocalPath(p, boxLocalPath, docDirLocalPath); "" != lp {
hash, err = util.GetEtag(lp)
if nil != err {
logging.LogErrorf("calc asset [%s] hash failed: %s", lp, err)
return nil
}
}
name, _ := util.LastID(p)
asset := &Asset{
ID: ast.NewNodeID(),
BlockID: root.ID,
RootID: root.ID,
Box: root.Box,
DocPath: p,
DocPath: root.Path,
Path: p,
Name: name,
Title: "title-img",

View file

@ -683,7 +683,7 @@ func buildSpanFromNode(n *ast.Node, tree *parse.Tree, rootID, boxID, p string) (
walkStatus = ast.WalkSkipChildren
return
case ast.NodeDocument:
if asset := docTitleImgAsset(n); nil != asset {
if asset := docTitleImgAsset(n, boxLocalPath, docDirLocalPath); nil != asset {
assets = append(assets, asset)
}
if tags := docTagSpans(n); 0 < len(tags) {

View file

@ -22,6 +22,8 @@ import (
"encoding/base64"
"io"
"os"
"github.com/siyuan-note/filelock"
)
// 以下是七牛云 Hash 算法实现 https://github.com/qiniu/qetag/blob/master/qetag.go
@ -53,11 +55,11 @@ func GetEtagByHandle(f io.Reader, size int64) (etag string, err error) {
}
func GetEtag(filename string) (etag string, err error) {
f, err := os.Open(filename)
f, err := filelock.OpenFile(filename, os.O_RDONLY, 0644)
if err != nil {
return
}
defer f.Close()
defer filelock.CloseFile(f)
fi, err := f.Stat()
if err != nil {