Jelajahi Sumber

:art: 集市支持已安装的包单独显示 https://github.com/siyuan-note/siyuan/issues/5678

Liang Ding 2 tahun lalu
induk
melakukan
4585cff7e6
2 mengubah file dengan 89 tambahan dan 1 penghapusan
  1. 37 1
      kernel/bazaar/package.go
  2. 52 0
      kernel/bazaar/widget.go

+ 37 - 1
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

+ 52 - 0
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)