🎨 Add Duplicate non-mirror database block https://github.com/siyuan-note/siyuan/issues/11460

This commit is contained in:
Daniel 2024-05-23 18:00:35 +08:00
parent aaeda056fa
commit f197a4ea87
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 64 additions and 0 deletions

View file

@ -27,6 +27,29 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
func duplicateAttributeViewBlock(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
arg, ok := util.JsonArg(c, ret)
if !ok {
return
}
avID := arg["avID"].(string)
newAvID, newBlockID, err := model.DuplicateDatabaseBlock(avID)
if nil != err {
ret.Code = -1
ret.Msg = err.Error()
return
}
ret.Data = map[string]interface{}{
"avID": newAvID,
"blockID": newBlockID,
}
}
func getAttributeViewKeysByAvID(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)

View file

@ -425,6 +425,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/av/setDatabaseBlockView", model.CheckAuth, model.CheckReadonly, setDatabaseBlockView)
ginServer.Handle("POST", "/api/av/getMirrorDatabaseBlocks", model.CheckAuth, model.CheckReadonly, getMirrorDatabaseBlocks)
ginServer.Handle("POST", "/api/av/getAttributeViewKeysByAvID", model.CheckAuth, model.CheckReadonly, getAttributeViewKeysByAvID)
ginServer.Handle("POST", "/api/av/duplicateAttributeViewBlock", model.CheckAuth, model.CheckReadonly, duplicateAttributeViewBlock)
ginServer.Handle("POST", "/api/ai/chatGPT", model.CheckAuth, chatGPT)
ginServer.Handle("POST", "/api/ai/chatGPTWithAction", model.CheckAuth, chatGPTWithAction)

View file

@ -41,6 +41,46 @@ import (
"github.com/xrash/smetrics"
)
func DuplicateDatabaseBlock(avID string) (newAvID, newBlockID string, err error) {
storageAvDir := filepath.Join(util.DataDir, "storage", "av")
oldAvPath := filepath.Join(storageAvDir, avID+".json")
newAvID, newBlockID = ast.NewNodeID(), ast.NewNodeID()
oldAv, err := av.ParseAttributeView(avID)
if nil != err {
return
}
data, err := filelock.ReadFile(oldAvPath)
if nil != err {
logging.LogErrorf("read attribute view [%s] failed: %s", avID, err)
return
}
data = bytes.ReplaceAll(data, []byte(avID), []byte(newAvID))
av.UpsertBlockRel(newAvID, newBlockID)
newAv := &av.AttributeView{}
if err = gulu.JSON.UnmarshalJSON(data, newAv); nil != err {
logging.LogErrorf("unmarshal attribute view [%s] failed: %s", newAvID, err)
return
}
newAv.Name = oldAv.Name + " (Duplicated " + time.Now().Format("2006-01-02 15:04:05") + ")"
data, err = gulu.JSON.MarshalJSON(newAv)
if nil != err {
logging.LogErrorf("marshal attribute view [%s] failed: %s", newAvID, err)
return
}
newAvPath := filepath.Join(storageAvDir, newAvID+".json")
if err = filelock.WriteFile(newAvPath, data); nil != err {
logging.LogErrorf("write attribute view [%s] failed: %s", newAvID, err)
return
}
return
}
func GetAttributeViewKeysByAvID(avID string) (ret []*av.Key) {
ret = []*av.Key{}