Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Sep 1, 2022
2 parents 7fa6316 + 74b73d0 commit 7034ab3
Show file tree
Hide file tree
Showing 15 changed files with 261 additions and 100 deletions.
2 changes: 1 addition & 1 deletion app/stage/protyle/js/lute/lute.min.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions kernel/api/bazaar.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/88250/gulu"
"github.com/gin-gonic/gin"
"github.com/siyuan-note/siyuan/kernel/bazaar"
"github.com/siyuan-note/siyuan/kernel/model"
"github.com/siyuan-note/siyuan/kernel/util"
)
Expand Down Expand Up @@ -164,6 +165,15 @@ func getBazaarTemplate(c *gin.Context) {
}
}

func getInstalledTemplate(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)

ret.Data = map[string]interface{}{
"packages": bazaar.InstalledTemplates(),
}
}

func installBazaarTemplate(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
Expand Down Expand Up @@ -221,6 +231,15 @@ func getBazaarTheme(c *gin.Context) {
}
}

func getInstalledTheme(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)

ret.Data = map[string]interface{}{
"packages": bazaar.InstalledThemes(),
}
}

func installBazaarTheme(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
Expand Down
2 changes: 2 additions & 0 deletions kernel/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,11 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/bazaar/installBazaarIcon", model.CheckAuth, installBazaarIcon)
ginServer.Handle("POST", "/api/bazaar/uninstallBazaarIcon", model.CheckAuth, uninstallBazaarIcon)
ginServer.Handle("POST", "/api/bazaar/getBazaarTemplate", model.CheckAuth, getBazaarTemplate)
ginServer.Handle("POST", "/api/bazaar/getInstalledTemplate", model.CheckAuth, getInstalledTemplate)
ginServer.Handle("POST", "/api/bazaar/installBazaarTemplate", model.CheckAuth, installBazaarTemplate)
ginServer.Handle("POST", "/api/bazaar/uninstallBazaarTemplate", model.CheckAuth, uninstallBazaarTemplate)
ginServer.Handle("POST", "/api/bazaar/getBazaarTheme", model.CheckAuth, getBazaarTheme)
ginServer.Handle("POST", "/api/bazaar/getInstalledTheme", model.CheckAuth, getInstalledTheme)
ginServer.Handle("POST", "/api/bazaar/installBazaarTheme", model.CheckAuth, installBazaarTheme)
ginServer.Handle("POST", "/api/bazaar/uninstallBazaarTheme", model.CheckAuth, uninstallBazaarTheme)
ginServer.Handle("POST", "/api/bazaar/getBazaarPackageREAME", model.CheckAuth, getBazaarPackageREAME)
Expand Down
19 changes: 3 additions & 16 deletions kernel/bazaar/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,13 @@ type Icon struct {

func Icons() (icons []*Icon) {
icons = []*Icon{}
result, err := util.GetRhyResult(false)
if nil != err {
return
}

bazaarIndex := getBazaarIndex()
bazaarHash := result["bazaar"].(string)
result = map[string]interface{}{}
request := httpclient.NewBrowserRequest()
u := util.BazaarOSSServer + "/bazaar@" + bazaarHash + "/stage/icons.json"
resp, err := request.SetResult(&result).Get(u)
pkgIndex, err := getPkgIndex("icons")
if nil != err {
logging.LogErrorf("get community stage index [%s] failed: %s", u, err)
return
}
if 200 != resp.StatusCode {
logging.LogErrorf("get community stage index [%s] failed: %d", u, resp.StatusCode)
return
}
repos := result["repos"].([]interface{})
bazaarIndex := getBazaarIndex()
repos := pkgIndex["repos"].([]interface{})
waitGroup := &sync.WaitGroup{}
lock := &sync.Mutex{}
p, _ := ants.NewPoolWithFunc(2, func(arg interface{}) {
Expand Down
84 changes: 84 additions & 0 deletions kernel/bazaar/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,90 @@ import (
"golang.org/x/text/transform"
)

func TemplateJSON(templateDirName string) (ret map[string]interface{}, err error) {
p := filepath.Join(util.ThemesPath, templateDirName, "template.json")
if !gulu.File.IsExist(p) {
err = os.ErrNotExist
return
}
data, err := os.ReadFile(p)
if nil != err {
logging.LogErrorf("read template.json [%s] failed: %s", p, err)
return
}
if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
logging.LogErrorf("parse template.json [%s] failed: %s", p, err)
return
}
if 4 > len(ret) {
logging.LogWarnf("invalid template.json [%s]", p)
return nil, errors.New("invalid template.json")
}
return
}

func ThemeJSON(themeDirName string) (ret map[string]interface{}, err error) {
p := filepath.Join(util.ThemesPath, themeDirName, "theme.json")
if !gulu.File.IsExist(p) {
err = os.ErrNotExist
return
}
data, err := os.ReadFile(p)
if nil != err {
logging.LogErrorf("read theme.json [%s] failed: %s", p, err)
return
}
if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
logging.LogErrorf("parse theme.json [%s] failed: %s", p, err)
return
}
if 5 > len(ret) {
logging.LogWarnf("invalid theme.json [%s]", p)
return nil, errors.New("invalid theme.json")
}
return
}

func getPkgIndex(pkgType string) (ret map[string]interface{}, err error) {
ret, err = util.GetRhyResult(false)
if nil != err {
return
}

bazaarHash := ret["bazaar"].(string)
ret = map[string]interface{}{}
request := httpclient.NewBrowserRequest()
u := util.BazaarOSSServer + "/bazaar@" + bazaarHash + "/stage/" + pkgType + ".json"
resp, reqErr := request.SetResult(&ret).Get(u)
if nil != reqErr {
logging.LogErrorf("get community stage index [%s] failed: %s", u, reqErr)
return
}
if 200 != resp.StatusCode {
logging.LogErrorf("get community stage index [%s] failed: %d", u, resp.StatusCode)
return
}
return
}

func isOutdatedPkg(fullURL, version string, pkgIndex map[string]interface{}) bool {
if !strings.HasPrefix(fullURL, "https://github.com/") {
return false
}

url := strings.TrimPrefix(fullURL, "https://github.com/")
repos := pkgIndex["repos"].([]interface{})
for _, repo := range repos {
r := repo.(map[string]interface{})
repoURL := r["url"].(string)
repoVer := r["version"].(string)
if url == repoURL && version != repoVer {
return true
}
}
return false
}

func GetPackageREADME(repoURL, repoHash string, systemID string) (ret string) {
repoURLHash := repoURL + "@" + repoHash
data, err := downloadPackage(repoURLHash+"/README.md", false, systemID)
Expand Down
69 changes: 52 additions & 17 deletions kernel/bazaar/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ package bazaar
import (
"errors"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"time"

"github.com/88250/gulu"
"github.com/dustin/go-humanize"
"github.com/panjf2000/ants/v2"
"github.com/siyuan-note/httpclient"
Expand Down Expand Up @@ -57,27 +59,13 @@ type Template struct {

func Templates() (templates []*Template) {
templates = []*Template{}
result, err := util.GetRhyResult(false)

pkgIndex, err := getPkgIndex("templates")
if nil != err {
return
}

bazaarIndex := getBazaarIndex()
bazaarHash := result["bazaar"].(string)
result = map[string]interface{}{}
request := httpclient.NewBrowserRequest()
u := util.BazaarOSSServer + "/bazaar@" + bazaarHash + "/stage/templates.json"
resp, reqErr := request.SetResult(&result).Get(u)
if nil != reqErr {
logging.LogErrorf("get community stage index [%s] failed: %s", u, reqErr)
return
}
if 200 != resp.StatusCode {
logging.LogErrorf("get community stage index [%s] failed: %d", u, resp.StatusCode)
return
}

repos := result["repos"].([]interface{})
repos := pkgIndex["repos"].([]interface{})
waitGroup := &sync.WaitGroup{}
lock := &sync.Mutex{}
p, _ := ants.NewPoolWithFunc(2, func(arg interface{}) {
Expand Down Expand Up @@ -130,6 +118,53 @@ func Templates() (templates []*Template) {
return
}

func InstalledTemplates() (ret []*Template) {
dir, err := os.Open(filepath.Join(util.DataDir, "templates"))
if nil != err {
logging.LogWarnf("open templates folder [%s] failed: %s", util.ThemesPath, err)
return
}
templateDirs, err := dir.Readdir(-1)
if nil != err {
logging.LogWarnf("read templates folder failed: %s", err)
return
}
dir.Close()

for _, templateDir := range templateDirs {
if !templateDir.IsDir() {
continue
}
dirName := templateDir.Name()
templateConf, parseErr := TemplateJSON(dirName)
if nil != parseErr || nil == templateConf {
continue
}

template := &Template{}
template.Name = templateConf["name"].(string)
template.Author = templateConf["author"].(string)
template.URL = templateConf["url"].(string)
template.Version = templateConf["version"].(string)
template.RepoURL = template.URL
template.PreviewURL = "/templates/" + dirName + "/preview.png"
template.PreviewURLThumb = "/templates/" + dirName + "/preview.png"
template.Updated = templateDir.ModTime().Format("2006-01-02 15:04:05")
template.Size = templateDir.Size()
template.HSize = humanize.Bytes(uint64(template.Size))
template.HUpdated = formatUpdated(template.Updated)
readme, readErr := os.ReadFile(filepath.Join(util.DataDir, "templates", dirName, "README.md"))
if nil != readErr {
logging.LogWarnf("read install template README.md failed: %s", readErr)
continue
}
template.README = gulu.Str.FromBytes(readme)

ret = append(ret, template)
}
return
}

func InstallTemplate(repoURL, repoHash, installPath string, systemID string) error {
repoURLHash := repoURL + "@" + repoHash
data, err := downloadPackage(repoURLHash, true, systemID)
Expand Down
85 changes: 67 additions & 18 deletions kernel/bazaar/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -58,27 +60,13 @@ type Theme struct {

func Themes() (ret []*Theme) {
ret = []*Theme{}
result, err := util.GetRhyResult(false)

pkgIndex, err := getPkgIndex("themes")
if nil != err {
return
}

bazaarIndex := getBazaarIndex()
bazaarHash := result["bazaar"].(string)
result = map[string]interface{}{}
request := httpclient.NewBrowserRequest()
u := util.BazaarOSSServer + "/bazaar@" + bazaarHash + "/stage/themes.json"
resp, reqErr := request.SetResult(&result).Get(u)
if nil != reqErr {
logging.LogErrorf("get community stage index [%s] failed: %s", u, reqErr)
return
}
if 200 != resp.StatusCode {
logging.LogErrorf("get community stage index [%s] failed: %d", u, resp.StatusCode)
return
}

repos := result["repos"].([]interface{})
repos := pkgIndex["repos"].([]interface{})
waitGroup := &sync.WaitGroup{}
lock := &sync.Mutex{}
p, _ := ants.NewPoolWithFunc(8, func(arg interface{}) {
Expand All @@ -95,7 +83,7 @@ func Themes() (ret []*Theme) {
return
}
if 200 != innerResp.StatusCode {
logging.LogErrorf("get bazaar package [%s] failed: %d", innerU, resp.StatusCode)
logging.LogErrorf("get bazaar package [%s] failed: %d", innerU, innerResp.StatusCode)
return
}

Expand Down Expand Up @@ -129,6 +117,67 @@ func Themes() (ret []*Theme) {
return
}

func InstalledThemes() (ret []*Theme) {
dir, err := os.Open(util.ThemesPath)
if nil != err {
logging.LogWarnf("open appearance themes folder [%s] failed: %s", util.ThemesPath, err)
return
}
themeDirs, err := dir.Readdir(-1)
if nil != err {
logging.LogWarnf("read appearance themes folder failed: %s", err)
return
}
dir.Close()

pkgIndex, err := getPkgIndex("themes")
if nil != err {
return
}

for _, themeDir := range themeDirs {
if !themeDir.IsDir() {
continue
}
dirName := themeDir.Name()
if isBuiltInTheme(dirName) {
continue
}

themeConf, parseErr := ThemeJSON(dirName)
if nil != parseErr || nil == themeConf {
continue
}

theme := &Theme{}
theme.Name = themeConf["name"].(string)
theme.Author = themeConf["author"].(string)
theme.URL = themeConf["url"].(string)
theme.Version = themeConf["version"].(string)
theme.Modes = make([]string, 0, len(themeConf["modes"].([]interface{})))
theme.RepoURL = theme.URL
theme.PreviewURL = "/appearance/themes/" + dirName + "/preview.png"
theme.PreviewURLThumb = "/appearance/themes/" + dirName + "/preview.png"
theme.Updated = themeDir.ModTime().Format("2006-01-02 15:04:05")
theme.Size = themeDir.Size()
theme.HSize = humanize.Bytes(uint64(theme.Size))
theme.HUpdated = formatUpdated(theme.Updated)
readme, readErr := os.ReadFile(filepath.Join(util.ThemesPath, dirName, "README.md"))
if nil != readErr {
logging.LogWarnf("read install theme README.md failed: %s", readErr)
continue
}
theme.README = gulu.Str.FromBytes(readme)
theme.Outdated = isOutdatedPkg(theme.URL, theme.Version, pkgIndex)
ret = append(ret, theme)
}
return
}

func isBuiltInTheme(dirName string) bool {
return "daylight" == dirName || "midnight" == dirName
}

func InstallTheme(repoURL, repoHash, installPath string, systemID string) error {
repoURLHash := repoURL + "@" + repoHash
data, err := downloadPackage(repoURLHash, true, systemID)
Expand Down
Loading

0 comments on commit 7034ab3

Please sign in to comment.