🎨 Supports converting network assets in the database to local https://github.com/siyuan-note/siyuan/issues/12096

This commit is contained in:
Daniel 2024-07-26 18:55:01 +08:00
parent df8d70995c
commit b4ece87cc0
No known key found for this signature in database
GPG Key ID: 86211BA83DF03017

View File

@ -100,11 +100,12 @@ func NetAssets2LocalAssets(rootID string, onlyImg bool, originalURL string) (err
destNodes := getRemoteAssetsLinkDestsInTree(tree, onlyImg)
for _, destNode := range destNodes {
dest := getRemoteAssetsLinkDest(destNode, onlyImg)
if "" == dest {
dests := getRemoteAssetsLinkDests(destNode, onlyImg)
if 1 > len(dests) {
continue
}
for _, dest := range dests {
if strings.HasPrefix(strings.ToLower(dest), "file://") { // 处理本地文件链接
u := dest[7:]
unescaped, _ := url.PathUnescape(u)
@ -157,7 +158,12 @@ func NetAssets2LocalAssets(rootID string, onlyImg bool, originalURL string) (err
// u = strings.Replace(u, "/0?", "/640?", 1)
//}
}
util.PushUpdateMsg(msgId, fmt.Sprintf(Conf.Language(119), u), 15000)
displayU := u
if 64 < len(displayU) {
displayU = displayU[:64] + "..."
}
util.PushUpdateMsg(msgId, fmt.Sprintf(Conf.Language(119), displayU), 15000)
request := browserClient.R()
request.SetRetryCount(1).SetRetryFixedInterval(3 * time.Second)
if "" != originalURL {
@ -227,6 +233,7 @@ func NetAssets2LocalAssets(rootID string, onlyImg bool, originalURL string) (err
continue
}
}
}
if 0 < files {
util.PushUpdateMsg(msgId, Conf.Language(113), 7000)
@ -1051,23 +1058,83 @@ func setAssetsLinkDest(node *ast.Node, oldDest, dest string) {
}
}
func getRemoteAssetsLinkDest(node *ast.Node, onlyImg bool) (ret string) {
func getRemoteAssetsLinkDests(node *ast.Node, onlyImg bool) (ret []string) {
if onlyImg {
if ast.NodeLinkDest == node.Type && node.ParentIs(ast.NodeImage) {
ret = string(node.Tokens)
if ast.NodeLinkDest == node.Type {
if node.ParentIs(ast.NodeImage) {
if !util.IsAssetLinkDest(node.Tokens) {
ret = append(ret, string(node.Tokens))
}
}
} else if ast.NodeAttributeView == node.Type {
attrView, _ := av.ParseAttributeView(node.AttributeViewID)
if nil == attrView {
return
}
for _, keyValues := range attrView.KeyValues {
if av.KeyTypeMAsset != keyValues.Key.Type {
continue
}
for _, value := range keyValues.Values {
if 1 > len(value.MAsset) {
continue
}
for _, asset := range value.MAsset {
if av.AssetTypeImage != asset.Type {
continue
}
dest := asset.Content
if !util.IsAssetLinkDest([]byte(dest)) {
ret = append(ret, strings.TrimSpace(dest))
}
}
}
}
}
} else {
if ast.NodeLinkDest == node.Type {
ret = string(node.Tokens)
} else if node.IsTextMarkType("a") {
ret = node.TextMarkAHref
} else if ast.NodeAudio == node.Type || ast.NodeVideo == node.Type {
ret = treenode.GetNodeSrcTokens(node)
if !util.IsAssetLinkDest(node.Tokens) {
ret = append(ret, string(node.Tokens))
}
} else if node.IsTextMarkType("a") {
if !util.IsAssetLinkDest([]byte(node.TextMarkAHref)) {
ret = append(ret, node.TextMarkAHref)
}
} else if ast.NodeAudio == node.Type || ast.NodeVideo == node.Type {
src := treenode.GetNodeSrcTokens(node)
if !util.IsAssetLinkDest([]byte(src)) {
ret = append(ret, src)
}
} else if ast.NodeAttributeView == node.Type {
attrView, _ := av.ParseAttributeView(node.AttributeViewID)
if nil == attrView {
return
}
if util.IsAssetLinkDest([]byte(ret)) {
ret = ""
for _, keyValues := range attrView.KeyValues {
if av.KeyTypeMAsset != keyValues.Key.Type {
continue
}
for _, value := range keyValues.Values {
if 1 > len(value.MAsset) {
continue
}
for _, asset := range value.MAsset {
dest := asset.Content
if !util.IsAssetLinkDest([]byte(dest)) {
ret = append(ret, strings.TrimSpace(dest))
}
}
}
}
}
}
return
}
@ -1078,8 +1145,8 @@ func getRemoteAssetsLinkDestsInTree(tree *parse.Tree, onlyImg bool) (nodes []*as
return ast.WalkContinue
}
dest := getRemoteAssetsLinkDest(n, onlyImg)
if "" == dest {
dests := getRemoteAssetsLinkDests(n, onlyImg)
if 1 > len(dests) {
return ast.WalkContinue
}