mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 07:00:12 +01:00
🐛 复制资源文件后导出再导入后丢失 Fix https://github.com/siyuan-note/siyuan/issues/5320
This commit is contained in:
parent
ea4f722edb
commit
a0a3d2f28e
3 changed files with 34 additions and 25 deletions
|
|
@ -394,6 +394,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
|
||||||
})
|
})
|
||||||
|
|
||||||
targetPaths := map[string]string{}
|
targetPaths := map[string]string{}
|
||||||
|
assetsDone := map[string]string{}
|
||||||
|
|
||||||
// md 转换 sy
|
// md 转换 sy
|
||||||
i := 0
|
i := 0
|
||||||
|
|
@ -481,16 +482,20 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
|
||||||
}
|
}
|
||||||
fullPath, exist = assets[absDest]
|
fullPath, exist = assets[absDest]
|
||||||
if exist {
|
if exist {
|
||||||
name := filepath.Base(fullPath)
|
existName := assetsDone[absDest]
|
||||||
ext := filepath.Ext(name)
|
var name string
|
||||||
name = strings.TrimSuffix(name, ext)
|
if "" == existName {
|
||||||
name += "-" + ast.NewNodeID() + ext
|
name = filepath.Base(fullPath)
|
||||||
|
name = util.AssetName(name)
|
||||||
assetTargetPath := filepath.Join(assetDirPath, name)
|
assetTargetPath := filepath.Join(assetDirPath, name)
|
||||||
delete(assets, absDest)
|
|
||||||
if err = gulu.File.Copy(fullPath, assetTargetPath); nil != err {
|
if err = gulu.File.Copy(fullPath, assetTargetPath); nil != err {
|
||||||
util.LogErrorf("copy asset from [%s] to [%s] failed: %s", fullPath, assetTargetPath, err)
|
util.LogErrorf("copy asset from [%s] to [%s] failed: %s", fullPath, assetTargetPath, err)
|
||||||
return ast.WalkContinue
|
return ast.WalkContinue
|
||||||
}
|
}
|
||||||
|
assetsDone[absDest] = name
|
||||||
|
} else {
|
||||||
|
name = existName
|
||||||
|
}
|
||||||
n.Tokens = gulu.Str.ToBytes("assets/" + name)
|
n.Tokens = gulu.Str.ToBytes("assets/" + name)
|
||||||
}
|
}
|
||||||
return ast.WalkContinue
|
return ast.WalkContinue
|
||||||
|
|
@ -568,9 +573,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
|
||||||
}
|
}
|
||||||
if exist {
|
if exist {
|
||||||
name := filepath.Base(absolutePath)
|
name := filepath.Base(absolutePath)
|
||||||
ext := filepath.Ext(name)
|
name = util.AssetName(name)
|
||||||
name = strings.TrimSuffix(name, ext)
|
|
||||||
name += "-" + ast.NewNodeID() + ext
|
|
||||||
assetTargetPath := filepath.Join(assetDirPath, name)
|
assetTargetPath := filepath.Join(assetDirPath, name)
|
||||||
if err = gulu.File.CopyFile(absolutePath, assetTargetPath); nil != err {
|
if err = gulu.File.CopyFile(absolutePath, assetTargetPath); nil != err {
|
||||||
util.LogErrorf("copy asset from [%s] to [%s] failed: %s", absolutePath, assetTargetPath, err)
|
util.LogErrorf("copy asset from [%s] to [%s] failed: %s", absolutePath, assetTargetPath, err)
|
||||||
|
|
|
||||||
|
|
@ -159,19 +159,7 @@ func Upload(c *gin.Context) {
|
||||||
// 已经存在同样数据的资源文件的话不重复保存
|
// 已经存在同样数据的资源文件的话不重复保存
|
||||||
succMap[baseName] = existAsset.Path
|
succMap[baseName] = existAsset.Path
|
||||||
} else {
|
} else {
|
||||||
_, id := util.LastID(fName)
|
fName = util.AssetName(fName)
|
||||||
ext := path.Ext(fName)
|
|
||||||
fName = fName[0 : len(fName)-len(ext)]
|
|
||||||
if !util.IsIDPattern(id) {
|
|
||||||
id = ast.NewNodeID()
|
|
||||||
fName = fName + "-" + id + ext
|
|
||||||
} else {
|
|
||||||
if !util.IsIDPattern(fName) {
|
|
||||||
fName = fName[:len(fName)-len(id)-1] + "-" + id + ext
|
|
||||||
} else {
|
|
||||||
fName = fName + ext
|
|
||||||
}
|
|
||||||
}
|
|
||||||
writePath := filepath.Join(assetsDirPath, fName)
|
writePath := filepath.Join(assetsDirPath, fName)
|
||||||
if _, err = f.Seek(0, io.SeekStart); nil != err {
|
if _, err = f.Seek(0, io.SeekStart); nil != err {
|
||||||
errFiles = append(errFiles, fName)
|
errFiles = append(errFiles, fName)
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/88250/gulu"
|
"github.com/88250/gulu"
|
||||||
|
"github.com/88250/lute/ast"
|
||||||
)
|
)
|
||||||
|
|
||||||
func IsEmptyDir(p string) bool {
|
func IsEmptyDir(p string) bool {
|
||||||
|
|
@ -47,6 +48,23 @@ func RemoveID(name string) string {
|
||||||
return name + ext
|
return name + ext
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AssetName(name string) string {
|
||||||
|
_, id := LastID(name)
|
||||||
|
ext := path.Ext(name)
|
||||||
|
name = name[0 : len(name)-len(ext)]
|
||||||
|
if !IsIDPattern(id) {
|
||||||
|
id = ast.NewNodeID()
|
||||||
|
name = name + "-" + id + ext
|
||||||
|
} else {
|
||||||
|
if !IsIDPattern(name) {
|
||||||
|
name = name[:len(name)-len(id)-1] + "-" + id + ext
|
||||||
|
} else {
|
||||||
|
name = name + ext
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
func LastID(p string) (name, id string) {
|
func LastID(p string) (name, id string) {
|
||||||
name = path.Base(p)
|
name = path.Base(p)
|
||||||
ext := path.Ext(name)
|
ext := path.Ext(name)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue