From b07ff243071d85dfa8bbe8d583a267c97cda7d75 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 11 Nov 2022 11:51:14 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20`=E7=BD=91=E7=BB=9C=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E4=B8=BA=E6=9C=AC=E5=9C=B0=E5=9B=BE=E7=89=87?= =?UTF-8?q?`=20=E6=94=AF=E6=8C=81=E5=A4=84=E7=90=86=20`file://`=20?= =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E8=B7=AF=E5=BE=84=E5=9B=BE=E7=89=87=20Fix=20?= =?UTF-8?q?https://github.com/siyuan-note/siyuan/issues/6546?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/assets.go | 30 ++++++++++++++++++++++++++++-- kernel/sql/aseet.go | 2 +- kernel/sql/database.go | 8 ++------ kernel/util/path.go | 4 ++++ 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/kernel/model/assets.go b/kernel/model/assets.go index c8bcd1332..d67fed919 100644 --- a/kernel/model/assets.go +++ b/kernel/model/assets.go @@ -92,7 +92,33 @@ func NetImg2LocalAssets(rootID string) (err error) { if ast.NodeImage == n.Type { linkDest := n.ChildByType(ast.NodeLinkDest) dest := linkDest.Tokens - if !sql.IsAssetLinkDest(dest) && (bytes.HasPrefix(bytes.ToLower(dest), []byte("https://")) || bytes.HasPrefix(bytes.ToLower(dest), []byte("http://"))) { + if util.IsAssetLinkDest(dest) { + return ast.WalkSkipChildren + } + + if bytes.HasPrefix(bytes.ToLower(dest), []byte("file://")) { + // `网络图片转换为本地图片` 支持处理 `file://` 本地路径图片 https://github.com/siyuan-note/siyuan/issues/6546 + + u := string(dest)[7:] + if !gulu.File.IsExist(u) { + return ast.WalkSkipChildren + } + + name := filepath.Base(u) + name = "net-img-" + name + name = util.AssetName(name) + writePath := filepath.Join(assetsDirPath, name) + if err = gulu.File.Copy(u, writePath); nil != err { + logging.LogErrorf("copy [%s] to [%s] failed: %s", u, writePath, err) + return ast.WalkSkipChildren + } + + linkDest.Tokens = []byte("assets/" + name) + files++ + return ast.WalkSkipChildren + } + + if bytes.HasPrefix(bytes.ToLower(dest), []byte("https://")) || bytes.HasPrefix(bytes.ToLower(dest), []byte("http://")) { u := string(dest) if strings.Contains(u, "qpic.cn") { // 改进 `网络图片转换为本地图片` 微信图片拉取 https://github.com/siyuan-note/siyuan/issues/5052 @@ -592,7 +618,7 @@ func UnusedAssets() (ret []string) { if titleImgPath := treenode.GetDocTitleImgPath(tree.Root); "" != titleImgPath { // 题头图计入 - if !sql.IsAssetLinkDest([]byte(titleImgPath)) { + if !util.IsAssetLinkDest([]byte(titleImgPath)) { continue } dests[titleImgPath] = true diff --git a/kernel/sql/aseet.go b/kernel/sql/aseet.go index a3dff9767..4040944a3 100644 --- a/kernel/sql/aseet.go +++ b/kernel/sql/aseet.go @@ -64,7 +64,7 @@ func docTagSpans(n *ast.Node) (ret []*Span) { func docTitleImgAsset(root *ast.Node) *Asset { if p := treenode.GetDocTitleImgPath(root); "" != p { - if !IsAssetLinkDest([]byte(p)) { + if !util.IsAssetLinkDest([]byte(p)) { return nil } diff --git a/kernel/sql/database.go b/kernel/sql/database.go index 45037f38d..6be61e024 100644 --- a/kernel/sql/database.go +++ b/kernel/sql/database.go @@ -582,7 +582,7 @@ func buildSpanFromNode(n *ast.Node, tree *parse.Tree, rootID, boxID, p string) ( // assetsLinkDestsInTree - if !IsAssetLinkDest(destNode.Tokens) { + if !util.IsAssetLinkDest(destNode.Tokens) { return } @@ -690,7 +690,7 @@ func buildSpanFromNode(n *ast.Node, tree *parse.Tree, rootID, boxID, p string) ( return } - if !IsAssetLinkDest(src) { + if !util.IsAssetLinkDest(src) { walkStatus = ast.WalkContinue return } @@ -1247,10 +1247,6 @@ func ialAttr(ial, name string) (ret string) { return } -func IsAssetLinkDest(dest []byte) bool { - return bytes.HasPrefix(dest, []byte("assets/")) -} - func removeDatabaseFile() (err error) { err = os.RemoveAll(util.DBPath) if nil != err { diff --git a/kernel/util/path.go b/kernel/util/path.go index 741e8354a..a9cc81a79 100644 --- a/kernel/util/path.go +++ b/kernel/util/path.go @@ -208,3 +208,7 @@ func FilterSelfChildDocs(paths []string) (ret []string) { } return } + +func IsAssetLinkDest(dest []byte) bool { + return bytes.HasPrefix(dest, []byte("assets/")) +}