From 4585cff7e6487e11a738d5656e2e3f48316f01b7 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Thu, 1 Sep 2022 21:33:24 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E9=9B=86=E5=B8=82=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=B7=B2=E5=AE=89=E8=A3=85=E7=9A=84=E5=8C=85=E5=8D=95=E7=8B=AC?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=20https://github.com/siyuan-note/siyuan/issu?= =?UTF-8?q?es/5678?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/bazaar/package.go | 38 ++++++++++++++++++++++++++++- kernel/bazaar/widget.go | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/kernel/bazaar/package.go b/kernel/bazaar/package.go index a54ef9bc4..987f45f5f 100644 --- a/kernel/bazaar/package.go +++ b/kernel/bazaar/package.go @@ -62,6 +62,28 @@ type Package struct { Downloads int `json:"downloads"` } +func WidgetJSON(widgetDirName string) (ret map[string]interface{}, err error) { + p := filepath.Join(util.DataDir, widgetDirName, "widget.json") + if !gulu.File.IsExist(p) { + err = os.ErrNotExist + return + } + data, err := os.ReadFile(p) + if nil != err { + logging.LogErrorf("read widget.json [%s] failed: %s", p, err) + return + } + if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err { + logging.LogErrorf("parse widget.json [%s] failed: %s", p, err) + return + } + if 4 > len(ret) { + logging.LogWarnf("invalid widget.json [%s]", p) + return nil, errors.New("invalid widget.json") + } + return +} + func IconJSON(iconDirName string) (ret map[string]interface{}, err error) { p := filepath.Join(util.ThemesPath, iconDirName, "icon.json") if !gulu.File.IsExist(p) { @@ -85,7 +107,7 @@ func IconJSON(iconDirName string) (ret map[string]interface{}, err error) { } func TemplateJSON(templateDirName string) (ret map[string]interface{}, err error) { - p := filepath.Join(util.ThemesPath, templateDirName, "template.json") + p := filepath.Join(util.DataDir, templateDirName, "template.json") if !gulu.File.IsExist(p) { err = os.ErrNotExist return @@ -178,6 +200,20 @@ func isOutdatedIcon(fullURL, version string, bazaarIcons []*Icon) bool { return false } +func isOutdatedWidget(fullURL, version string, bazaarWidgets []*Widget) bool { + if !strings.HasPrefix(fullURL, "https://github.com/") { + return false + } + + url := strings.TrimPrefix(fullURL, "https://github.com/") + for _, pkg := range bazaarWidgets { + if url == pkg.URL && version != pkg.Version { + return true + } + } + return false +} + func isOutdatedTemplate(fullURL, version string, bazaarTemplates []*Template) bool { if !strings.HasPrefix(fullURL, "https://github.com/") { return false diff --git a/kernel/bazaar/widget.go b/kernel/bazaar/widget.go index a0b93b1d9..c821623cf 100644 --- a/kernel/bazaar/widget.go +++ b/kernel/bazaar/widget.go @@ -19,10 +19,12 @@ package bazaar import ( "errors" "os" + "path/filepath" "sort" "strings" "sync" + "github.com/88250/gulu" "github.com/dustin/go-humanize" ants "github.com/panjf2000/ants/v2" "github.com/siyuan-note/httpclient" @@ -94,6 +96,56 @@ func Widgets() (widgets []*Widget) { return } +func InstalledWidgets() (ret []*Icon) { + dir, err := os.Open(filepath.Join(util.DataDir, "widgets")) + if nil != err { + logging.LogWarnf("open widgets folder [%s] failed: %s", util.ThemesPath, err) + return + } + widgetDirs, err := dir.Readdir(-1) + if nil != err { + logging.LogWarnf("read widgets folder failed: %s", err) + return + } + dir.Close() + + bazaarWidgets := Widgets() + + for _, widgetDir := range widgetDirs { + if !widgetDir.IsDir() { + continue + } + dirName := widgetDir.Name() + + widgetConf, parseErr := WidgetJSON(dirName) + if nil != parseErr || nil == widgetConf { + continue + } + + icon := &Icon{} + icon.Name = widgetConf["name"].(string) + icon.Author = widgetConf["author"].(string) + icon.URL = widgetConf["url"].(string) + icon.Version = widgetConf["version"].(string) + icon.RepoURL = icon.URL + icon.PreviewURL = "/widgets/" + dirName + "/preview.png" + icon.PreviewURLThumb = "/widgets/" + dirName + "/preview.png" + icon.Updated = widgetDir.ModTime().Format("2006-01-02 15:04:05") + icon.Size = widgetDir.Size() + icon.HSize = humanize.Bytes(uint64(icon.Size)) + icon.HUpdated = formatUpdated(icon.Updated) + readme, readErr := os.ReadFile(filepath.Join(util.DataDir, "widgets", dirName, "README.md")) + if nil != readErr { + logging.LogWarnf("read install icon README.md failed: %s", readErr) + continue + } + icon.README = gulu.Str.FromBytes(readme) + icon.Outdated = isOutdatedWidget(icon.URL, icon.Version, bazaarWidgets) + ret = append(ret, icon) + } + return +} + func InstallWidget(repoURL, repoHash, installPath string, systemID string) error { repoURLHash := repoURL + "@" + repoHash data, err := downloadPackage(repoURLHash, true, systemID)