mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-17 09:30:48 +08:00
🎨 集市支持已安装的包单独显示 https://github.com/siyuan-note/siyuan/issues/5678
This commit is contained in:
parent
6072f8a3a1
commit
4585cff7e6
@ -62,6 +62,28 @@ type Package struct {
|
|||||||
Downloads int `json:"downloads"`
|
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) {
|
func IconJSON(iconDirName string) (ret map[string]interface{}, err error) {
|
||||||
p := filepath.Join(util.ThemesPath, iconDirName, "icon.json")
|
p := filepath.Join(util.ThemesPath, iconDirName, "icon.json")
|
||||||
if !gulu.File.IsExist(p) {
|
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) {
|
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) {
|
if !gulu.File.IsExist(p) {
|
||||||
err = os.ErrNotExist
|
err = os.ErrNotExist
|
||||||
return
|
return
|
||||||
@ -178,6 +200,20 @@ func isOutdatedIcon(fullURL, version string, bazaarIcons []*Icon) bool {
|
|||||||
return false
|
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 {
|
func isOutdatedTemplate(fullURL, version string, bazaarTemplates []*Template) bool {
|
||||||
if !strings.HasPrefix(fullURL, "https://github.com/") {
|
if !strings.HasPrefix(fullURL, "https://github.com/") {
|
||||||
return false
|
return false
|
||||||
|
@ -19,10 +19,12 @@ package bazaar
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/88250/gulu"
|
||||||
"github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
ants "github.com/panjf2000/ants/v2"
|
ants "github.com/panjf2000/ants/v2"
|
||||||
"github.com/siyuan-note/httpclient"
|
"github.com/siyuan-note/httpclient"
|
||||||
@ -94,6 +96,56 @@ func Widgets() (widgets []*Widget) {
|
|||||||
return
|
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 {
|
func InstallWidget(repoURL, repoHash, installPath string, systemID string) error {
|
||||||
repoURLHash := repoURL + "@" + repoHash
|
repoURLHash := repoURL + "@" + repoHash
|
||||||
data, err := downloadPackage(repoURLHash, true, systemID)
|
data, err := downloadPackage(repoURLHash, true, systemID)
|
||||||
|
Loading…
Reference in New Issue
Block a user