🎨 Automatically download network assets when the cloud inbox is moved to docs https://github.com/siyuan-note/siyuan/issues/9775

This commit is contained in:
Daniel 2023-11-29 16:28:35 +08:00
parent 27ffdc45d7
commit 7eb597652d
No known key found for this signature in database
GPG Key ID: 86211BA83DF03017

View File

@ -244,15 +244,23 @@ func NetAssets2LocalAssets(rootID string) (err error) {
SetProxy(httpclient.ProxyFromEnvironment) SetProxy(httpclient.ProxyFromEnvironment)
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus { ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering || (ast.NodeLinkDest != n.Type && !n.IsTextMarkType("a")) { if !entering || (ast.NodeLinkDest != n.Type && !n.IsTextMarkType("a") && ast.NodeAudio != n.Type && ast.NodeVideo != n.Type) {
return ast.WalkContinue return ast.WalkContinue
} }
var dest []byte var dest []byte
if ast.NodeLinkDest == n.Type { if ast.NodeLinkDest == n.Type {
dest = n.Tokens dest = n.Tokens
} else { } else if n.IsTextMarkType("a") {
dest = []byte(n.TextMarkAHref) dest = []byte(n.TextMarkAHref)
} else if ast.NodeAudio == n.Type || ast.NodeVideo == n.Type {
if srcIndex := bytes.Index(n.Tokens, []byte("src=\"")); 0 < srcIndex {
src := n.Tokens[srcIndex+len("src=\""):]
if srcIndex = bytes.Index(src, []byte("\"")); 0 < srcIndex {
src = src[:bytes.Index(src, []byte("\""))]
dest = bytes.TrimSpace(src)
}
}
} }
if util.IsAssetLinkDest(dest) { if util.IsAssetLinkDest(dest) {
@ -278,8 +286,10 @@ func NetAssets2LocalAssets(rootID string) (err error) {
if ast.NodeLinkDest == n.Type { if ast.NodeLinkDest == n.Type {
n.Tokens = []byte("assets/" + name) n.Tokens = []byte("assets/" + name)
} else { } else if n.IsTextMarkType("a") {
n.TextMarkAHref = "assets/" + name n.TextMarkAHref = "assets/" + name
} else if ast.NodeAudio == n.Type || ast.NodeVideo == n.Type {
n.Tokens = bytes.ReplaceAll(n.Tokens, dest, []byte("assets/"+name))
} }
files++ files++
return ast.WalkContinue return ast.WalkContinue
@ -360,8 +370,10 @@ func NetAssets2LocalAssets(rootID string) (err error) {
if ast.NodeLinkDest == n.Type { if ast.NodeLinkDest == n.Type {
n.Tokens = []byte("assets/" + name) n.Tokens = []byte("assets/" + name)
} else { } else if n.IsTextMarkType("a") {
n.TextMarkAHref = "assets/" + name n.TextMarkAHref = "assets/" + name
} else if ast.NodeAudio == n.Type || ast.NodeVideo == n.Type {
n.Tokens = bytes.ReplaceAll(n.Tokens, dest, []byte("assets/"+name))
} }
files++ files++
} }
@ -790,7 +802,7 @@ func UnusedAssets() (ret []string) {
} }
var linkDestFolderPaths, linkDestFilePaths []string var linkDestFolderPaths, linkDestFilePaths []string
for dest, _ := range dests { for dest := range dests {
if !strings.HasPrefix(dest, "assets/") { if !strings.HasPrefix(dest, "assets/") {
continue continue
} }
@ -812,7 +824,7 @@ func UnusedAssets() (ret []string) {
// 排除文件夹链接 // 排除文件夹链接
var toRemoves []string var toRemoves []string
for asset, _ := range assetsPathMap { for asset := range assetsPathMap {
for _, linkDestFolder := range linkDestFolderPaths { for _, linkDestFolder := range linkDestFolderPaths {
if strings.HasPrefix(asset, linkDestFolder) { if strings.HasPrefix(asset, linkDestFolder) {
toRemoves = append(toRemoves, asset) toRemoves = append(toRemoves, asset)
@ -838,7 +850,7 @@ func UnusedAssets() (ret []string) {
} }
var toRemoves []string var toRemoves []string
for asset, _ := range assetsPathMap { for asset := range assetsPathMap {
if strings.HasSuffix(asset, "ocr-texts.json") { if strings.HasSuffix(asset, "ocr-texts.json") {
// 排除 OCR 结果文本 // 排除 OCR 结果文本
toRemoves = append(toRemoves, asset) toRemoves = append(toRemoves, asset)
@ -868,7 +880,7 @@ func UnusedAssets() (ret []string) {
return return
} }
for asset, _ := range assetsPathMap { for asset := range assetsPathMap {
if bytes.Contains(data, []byte(asset)) { if bytes.Contains(data, []byte(asset)) {
toRemoves = append(toRemoves, asset) toRemoves = append(toRemoves, asset)
} }
@ -946,7 +958,7 @@ func MissingAssets() (ret []string) {
} }
} }
for dest, _ := range dests { for dest := range dests {
if !strings.HasPrefix(dest, "assets/") { if !strings.HasPrefix(dest, "assets/") {
continue continue
} }