This commit is contained in:
Liang Ding 2023-05-05 16:20:23 +08:00
parent 5a6db7fdb4
commit 109462a22f
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
4 changed files with 60 additions and 2 deletions

View file

@ -22,6 +22,7 @@ import (
"github.com/88250/gulu"
"github.com/gin-gonic/gin"
"github.com/siyuan-note/siyuan/kernel/model"
"github.com/siyuan-note/siyuan/kernel/util"
)
func loadPetals(c *gin.Context) {
@ -31,3 +32,17 @@ func loadPetals(c *gin.Context) {
petals := model.LoadPetals()
ret.Data = petals
}
func setPetalEnabled(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
arg, ok := util.JsonArg(c, ret)
if !ok {
return
}
packageName := arg["packageName"].(string)
enabled := arg["enabled"].(bool)
model.SetPetalEnabled(packageName, enabled)
}

View file

@ -345,4 +345,5 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/ai/chatGPTWithAction", model.CheckAuth, model.CheckReadonly, chatGPTWithAction)
ginServer.Handle("POST", "/api/petal/loadPetals", model.CheckAuth, model.CheckReadonly, loadPetals)
ginServer.Handle("POST", "/api/petal/setPetalEnabled", model.CheckAuth, model.CheckReadonly, setPetalEnabled)
}

View file

@ -40,7 +40,46 @@ type Petal struct {
I18n map[string]interface{} `json:"i18n"` // i18n text
}
func SetPetalEnabled(name string, enabled bool) {
petals := []*Petal{}
petalDir := filepath.Join(util.DataDir, "storage", "petal")
confPath := filepath.Join(petalDir, "petals.json")
data, err := filelock.ReadFile(confPath)
if nil != err {
logging.LogErrorf("read petal file [%s] failed: %s", confPath, err)
return
}
if err = gulu.JSON.UnmarshalJSON(data, &petals); nil != err {
logging.LogErrorf("unmarshal petals failed: %s", err)
return
}
plugins := bazaar.InstalledPlugins()
for _, plugin := range plugins {
id := hash(plugin.URL)
petal := getPetalByID(id, petals)
if nil == petal {
continue
}
petal.Enabled = enabled
break
}
if data, err = gulu.JSON.MarshalIndentJSON(petals, "", "\t"); nil != err {
logging.LogErrorf("marshal petals failed: %s", err)
return
}
if err = filelock.WriteFile(confPath, data); nil != err {
logging.LogErrorf("write petals [%s] failed: %s", confPath, err)
return
}
}
func LoadPetals() (ret []*Petal) {
ret = []*Petal{}
petalDir := filepath.Join(util.DataDir, "storage", "petal")
if err := os.MkdirAll(petalDir, 0755); nil != err {
logging.LogErrorf("create petal dir [%s] failed: %s", petalDir, err)
@ -48,8 +87,6 @@ func LoadPetals() (ret []*Petal) {
}
confPath := filepath.Join(petalDir, "petals.json")
ret = []*Petal{}
if !gulu.File.IsExist(confPath) {
data, err := gulu.JSON.MarshalIndentJSON(ret, "", "\t")
if nil != err {

View file

@ -72,6 +72,7 @@ func Serve(fastMode bool) {
serveWebSocket(ginServer)
serveExport(ginServer)
serveWidgets(ginServer)
servePlugins(ginServer)
serveEmojis(ginServer)
serveTemplates(ginServer)
api.ServeAPI(ginServer)
@ -174,6 +175,10 @@ func serveWidgets(ginServer *gin.Engine) {
ginServer.Static("/widgets/", filepath.Join(util.DataDir, "widgets"))
}
func servePlugins(ginServer *gin.Engine) {
ginServer.Static("/plugins/", filepath.Join(util.DataDir, "plugins"))
}
func serveEmojis(ginServer *gin.Engine) {
ginServer.Static("/emojis/", filepath.Join(util.DataDir, "emojis"))
}