From bc6f6e467ea67b16a260813a11d4fe73c6709073 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Tue, 27 Sep 2022 14:45:24 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E8=B6=85=E9=93=BE=E6=8E=A5=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=E6=9B=B4=E5=A5=BD=E5=9C=B0=E5=85=BC=E5=AE=B9=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E8=B7=AF=E5=BE=84=20https://github.com/siyuan-note/si?= =?UTF-8?q?yuan/issues/5980?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/api/asset.go | 7 ++++++- kernel/model/upload.go | 12 ++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/kernel/api/asset.go b/kernel/api/asset.go index 59e3a2704..dd8185f82 100644 --- a/kernel/api/asset.go +++ b/kernel/api/asset.go @@ -231,8 +231,13 @@ func insertLocalAssets(c *gin.Context) { for _, pathArg := range assetPathsArg { assetPaths = append(assetPaths, pathArg.(string)) } + isUpload := true + isUploadArg := arg["isUpload"] + if nil != isUploadArg { + isUpload = isUploadArg.(bool) + } id := arg["id"].(string) - succMap, err := model.InsertLocalAssets(id, assetPaths) + succMap, err := model.InsertLocalAssets(id, assetPaths, isUpload) if nil != err { ret.Code = -1 ret.Msg = err.Error() diff --git a/kernel/model/upload.go b/kernel/model/upload.go index 41d34ce7c..12ffb93a4 100644 --- a/kernel/model/upload.go +++ b/kernel/model/upload.go @@ -34,7 +34,7 @@ import ( "github.com/siyuan-note/siyuan/kernel/util" ) -func InsertLocalAssets(id string, assetPaths []string) (succMap map[string]interface{}, err error) { +func InsertLocalAssets(id string, assetPaths []string, isUpload bool) (succMap map[string]interface{}, err error) { succMap = map[string]interface{}{} bt := treenode.GetBlockTree(id) @@ -44,9 +44,9 @@ func InsertLocalAssets(id string, assetPaths []string) (succMap map[string]inter } docDirLocalPath := filepath.Join(util.DataDir, bt.BoxID, path.Dir(bt.Path)) - assets := getAssetsDir(filepath.Join(util.DataDir, bt.BoxID), docDirLocalPath) - if !gulu.File.IsExist(assets) { - if err = os.MkdirAll(assets, 0755); nil != err { + assetsDirPath := getAssetsDir(filepath.Join(util.DataDir, bt.BoxID), docDirLocalPath) + if !gulu.File.IsExist(assetsDirPath) { + if err = os.MkdirAll(assetsDirPath, 0755); nil != err { return } } @@ -59,7 +59,7 @@ func InsertLocalAssets(id string, assetPaths []string) (succMap map[string]inter ext = strings.ToLower(ext) fName += ext baseName := fName - if gulu.File.IsDir(p) { + if gulu.File.IsDir(p) || !isUpload { succMap[baseName] = "file://" + p continue } @@ -87,7 +87,7 @@ func InsertLocalAssets(id string, assetPaths []string) (succMap map[string]inter ext := path.Ext(fName) fName = fName[0 : len(fName)-len(ext)] fName = fName + "-" + ast.NewNodeID() + ext - writePath := filepath.Join(assets, fName) + writePath := filepath.Join(assetsDirPath, fName) if _, err = f.Seek(0, io.SeekStart); nil != err { f.Close() return