Something went wrong. Try again.
A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
Something went wrong. Try again.
Go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296// SiYuan - Refactor your thinking// Copyright (c) 2020-present, b3log.org//// This program is free software: you can redistribute it and/or modify// it under the terms of the GNU Affero General Public License as published by// the Free Software Foundation, either version 3 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU Affero General Public License for more details.//// You should have received a copy of the GNU Affero General Public License// along with this program. If not, see <https://www.gnu.org/licenses/>.
package model
import ( "fmt" "os" "path/filepath" "sync"
"github.com/88250/gulu" "github.com/siyuan-note/filelock" "github.com/siyuan-note/logging" "github.com/siyuan-note/siyuan/kernel/bazaar" "github.com/siyuan-note/siyuan/kernel/util")
// Petal represents a plugin's management status.type Petal struct { Name string `json:"name"` // Plugin name DisplayName string `json:"displayName"` // Plugin display name Enabled bool `json:"enabled"` // Whether enabled Incompatible bool `json:"incompatible"` // Whether incompatible DisabledInPublish bool `json:"disabledInPublish"` // Whether disabled in publish mode DisallowInstall bool `json:"disallowInstall"` // Whether disallow install
JS string `json:"js"` // JS code CSS string `json:"css"` // CSS code I18n map[string]interface{} `json:"i18n"` // i18n text}
func SetPetalEnabled(name string, enabled bool, frontend string) (ret *Petal, err error) { petals := getPetals()
found, displayName, incompatible, disabledInPublish, disallowInstall := bazaar.ParseInstalledPlugin(name, frontend) if !found { logging.LogErrorf("plugin [%s] not found", name) return }
ret = getPetalByName(name, petals) if nil == ret { ret = &Petal{ Name: name, } petals = append(petals, ret) } ret.DisplayName = displayName ret.Enabled = enabled ret.Incompatible = incompatible ret.DisabledInPublish = disabledInPublish ret.DisallowInstall = disallowInstall
if enabled && incompatible { err = fmt.Errorf(Conf.Language(205)) logging.LogInfof("plugin [%s] is incompatible [%s]", name, frontend) return }
if enabled && disallowInstall { msg := "require upgrade SiYuan to use this plugin [" + name + "]" err = fmt.Errorf(msg) logging.LogInfof(msg) return }
savePetals(petals) loadCode(ret) return}
func getPetalByName(name string, petals []*Petal) (ret *Petal) { for _, p := range petals { if name == p.Name { ret = p break } } return}
func LoadPetals(frontend string, isPublish bool) (ret []*Petal) { ret = []*Petal{}
if Conf.Bazaar.PetalDisabled { return }
if !Conf.Bazaar.Trust { // 移动端没有集市模块,所以要默认开启,桌面端和 Docker 容器需要用户手动确认过信任后才能开启 if util.ContainerStd == util.Container || util.ContainerDocker == util.Container { return } }
petals := getPetals() for _, petal := range petals { _, petal.DisplayName, petal.Incompatible, petal.DisabledInPublish, petal.DisallowInstall = bazaar.ParseInstalledPlugin(petal.Name, frontend) if !petal.Enabled || petal.Incompatible || (isPublish && petal.DisabledInPublish) || petal.DisallowInstall { if petal.DisallowInstall { SetPetalEnabled(petal.Name, false, frontend) logging.LogInfof("plugin [%s] disallowed install, auto disabled", petal.Name) } continue }
loadCode(petal) ret = append(ret, petal) } return}
func loadCode(petal *Petal) { pluginDir := filepath.Join(util.DataDir, "plugins", petal.Name) jsPath := filepath.Join(pluginDir, "index.js") if !filelock.IsExist(jsPath) { logging.LogErrorf("plugin [%s] js not found", petal.Name) return }
data, err := filelock.ReadFile(jsPath) if err != nil { logging.LogErrorf("read plugin [%s] js failed: %s", petal.Name, err) return } petal.JS = string(data)
cssPath := filepath.Join(pluginDir, "index.css") if filelock.IsExist(cssPath) { data, err = filelock.ReadFile(cssPath) if err != nil { logging.LogErrorf("read plugin [%s] css failed: %s", petal.Name, err) } else { petal.CSS = string(data) } }
i18nDir := filepath.Join(pluginDir, "i18n") if gulu.File.IsDir(i18nDir) { langJSONs, readErr := os.ReadDir(i18nDir) if nil != readErr { logging.LogErrorf("read plugin [%s] i18n failed: %s", petal.Name, readErr) } else if 0 < len(langJSONs) { preferredLang := Conf.Lang + ".json" foundPreferredLang := false foundEnUS := false foundZhCN := false for _, langJSON := range langJSONs { if langJSON.Name() == preferredLang { foundPreferredLang = true break } if langJSON.Name() == "en_US.json" { foundEnUS = true } if langJSON.Name() == "zh_CN.json" { foundZhCN = true } }
if !foundPreferredLang { if foundEnUS { preferredLang = "en_US.json" } else if foundZhCN { preferredLang = "zh_CN.json" } else { preferredLang = langJSONs[0].Name() } }
if langFilePath := filepath.Join(i18nDir, preferredLang); gulu.File.IsExist(langFilePath) { data, err = filelock.ReadFile(langFilePath) if err != nil { logging.LogErrorf("read plugin [%s] i18n failed: %s", petal.Name, err) } else { petal.I18n = map[string]interface{}{} if err = gulu.JSON.UnmarshalJSON(data, &petal.I18n); err != nil { logging.LogErrorf("unmarshal plugin [%s] i18n failed: %s", petal.Name, err) } } } } }}
var petalsStoreLock = sync.Mutex{}
func savePetals(petals []*Petal) { petalsStoreLock.Lock() defer petalsStoreLock.Unlock() savePetals0(petals)}
func savePetals0(petals []*Petal) { if 1 > len(petals) { petals = []*Petal{} }
petalDir := filepath.Join(util.DataDir, "storage", "petal") confPath := filepath.Join(petalDir, "petals.json") data, err := gulu.JSON.MarshalIndentJSON(petals, "", "\t") if err != nil { logging.LogErrorf("marshal petals failed: %s", err) return } if err = filelock.WriteFile(confPath, data); err != nil { logging.LogErrorf("write petals [%s] failed: %s", confPath, err) return }}
func getPetals() (ret []*Petal) { petalsStoreLock.Lock() defer petalsStoreLock.Unlock()
ret = []*Petal{} petalDir := filepath.Join(util.DataDir, "storage", "petal") if err := os.MkdirAll(petalDir, 0755); err != nil { logging.LogErrorf("create petal dir [%s] failed: %s", petalDir, err) return }
confPath := filepath.Join(petalDir, "petals.json") if !filelock.IsExist(confPath) { data, err := gulu.JSON.MarshalIndentJSON(ret, "", "\t") if err != nil { logging.LogErrorf("marshal petals failed: %s", err) return } if err = filelock.WriteFile(confPath, data); err != nil { logging.LogErrorf("write petals [%s] failed: %s", confPath, err) return } return }
data, err := filelock.ReadFile(confPath) if err != nil { logging.LogErrorf("read petal file [%s] failed: %s", confPath, err) return }
if err = gulu.JSON.UnmarshalJSON(data, &ret); err != nil { logging.LogErrorf("unmarshal petals failed: %s", err) return }
var tmp []*Petal pluginsDir := filepath.Join(util.DataDir, "plugins") for _, petal := range ret { pluginPath := filepath.Join(pluginsDir, petal.Name) if hasPluginFiles(pluginPath) { tmp = append(tmp, petal) } } if len(tmp) != len(ret) { savePetals0(tmp) ret = tmp } if 1 > len(ret) { ret = []*Petal{} } return}
// hasPluginFiles 检查插件安装目录是否存在且包含文件func hasPluginFiles(pluginPath string) bool { if !filelock.IsExist(pluginPath) { return false } entries, err := os.ReadDir(pluginPath) if err != nil { return false } for _, entry := range entries { if !entry.IsDir() { return true } } return false}