🎨 网络图片转换为本地图片 支持处理 file:// 本地路径图片 Fix https://github.com/siyuan-note/siyuan/issues/6546

This commit is contained in:
Liang Ding 2022-11-11 11:51:14 +08:00
parent 6a31ab85aa
commit b07ff24307
No known key found for this signature in database
GPG Key ID: 136F30F901A2231D
4 changed files with 35 additions and 9 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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 {

View File

@ -208,3 +208,7 @@ func FilterSelfChildDocs(paths []string) (ret []string) {
}
return
}
func IsAssetLinkDest(dest []byte) bool {
return bytes.HasPrefix(dest, []byte("assets/"))
}