🎨 One-click upgrade of downloaded marketplace packages https://github.com/siyuan-note/siyuan/issues/8390

This commit is contained in:
Daniel 2024-03-21 22:35:21 +08:00
parent 4817bc0812
commit 2120637c06
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
9 changed files with 144 additions and 11 deletions

View file

@ -25,6 +25,19 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
func batchUpdatePackage(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)
arg, ok := util.JsonArg(c, ret)
if !ok {
return
}
frontend := arg["frontend"].(string)
model.BatchUpdateBazaarPackages(frontend)
}
func getUpdatedPackage(c *gin.Context) {
ret := gulu.Ret.NewResult()
defer c.JSON(http.StatusOK, ret)

View file

@ -330,7 +330,6 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/graph/getGraph", model.CheckAuth, getGraph)
ginServer.Handle("POST", "/api/graph/getLocalGraph", model.CheckAuth, getLocalGraph)
ginServer.Handle("POST", "/api/bazaar/getUpdatedPackage", model.CheckAuth, getUpdatedPackage)
ginServer.Handle("POST", "/api/bazaar/getBazaarPlugin", model.CheckAuth, getBazaarPlugin)
ginServer.Handle("POST", "/api/bazaar/getInstalledPlugin", model.CheckAuth, getInstalledPlugin)
ginServer.Handle("POST", "/api/bazaar/installBazaarPlugin", model.CheckAuth, model.CheckReadonly, installBazaarPlugin)
@ -352,6 +351,8 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/bazaar/installBazaarTheme", model.CheckAuth, model.CheckReadonly, installBazaarTheme)
ginServer.Handle("POST", "/api/bazaar/uninstallBazaarTheme", model.CheckAuth, model.CheckReadonly, uninstallBazaarTheme)
ginServer.Handle("POST", "/api/bazaar/getBazaarPackageREAME", model.CheckAuth, getBazaarPackageREAME)
ginServer.Handle("POST", "/api/bazaar/getUpdatedPackage", model.CheckAuth, getUpdatedPackage)
ginServer.Handle("POST", "/api/bazaar/batchUpdatePackage", model.CheckAuth, batchUpdatePackage)
ginServer.Handle("POST", "/api/repo/initRepoKey", model.CheckAuth, model.CheckReadonly, initRepoKey)
ginServer.Handle("POST", "/api/repo/initRepoKeyFromPassphrase", model.CheckAuth, model.CheckReadonly, initRepoKeyFromPassphrase)

View file

@ -530,9 +530,25 @@ func renderREADME(repoURL string, mdData []byte) (ret string, err error) {
return
}
var (
packageLocks = map[string]*sync.Mutex{}
packageLocksLock = sync.Mutex{}
)
func downloadPackage(repoURLHash string, pushProgress bool, systemID string) (data []byte, err error) {
packageLocksLock.Lock()
defer packageLocksLock.Unlock()
// repoURLHash: https://github.com/88250/Comfortably-Numb@6286912c381ef3f83e455d06ba4d369c498238dc
pushID := repoURLHash[:strings.LastIndex(repoURLHash, "@")]
repoURL := repoURLHash[:strings.LastIndex(repoURLHash, "@")]
lock, ok := packageLocks[repoURLHash]
if !ok {
lock = &sync.Mutex{}
packageLocks[repoURLHash] = lock
}
lock.Lock()
defer lock.Unlock()
repoURLHash = strings.TrimPrefix(repoURLHash, "https://github.com/")
u := util.BazaarOSSServer + "/package/" + repoURLHash
buf := &bytes.Buffer{}
@ -540,7 +556,7 @@ func downloadPackage(repoURLHash string, pushProgress bool, systemID string) (da
if pushProgress {
progress := float32(info.DownloadedSize) / float32(info.Response.ContentLength)
//logging.LogDebugf("downloading bazaar package [%f]", progress)
util.PushDownloadProgress(pushID, progress)
util.PushDownloadProgress(repoURL, progress)
}
}).Get(u)
if nil != err {

View file

@ -19,17 +19,100 @@ package model
import (
"errors"
"fmt"
"github.com/88250/gulu"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/util"
"path"
"path/filepath"
"strings"
"sync"
"github.com/88250/gulu"
"github.com/siyuan-note/siyuan/kernel/util"
"time"
"github.com/siyuan-note/siyuan/kernel/bazaar"
)
func BatchUpdateBazaarPackages(frontend string) {
plugins, widgets, icons, themes, templates := UpdatedPackages(frontend)
total := len(plugins) + len(widgets) + len(icons) + len(themes) + len(templates)
if 1 > total {
return
}
util.PushEndlessProgress(fmt.Sprintf(Conf.language(235), 1, total))
defer util.PushClearProgress()
count := 1
for _, plugin := range plugins {
err := bazaar.InstallPlugin(plugin.RepoURL, plugin.RepoHash, filepath.Join(util.DataDir, "plugins", plugin.Name), Conf.System.ID)
if nil != err {
logging.LogErrorf("update plugin [%s] failed: %s", plugin.Name, err)
util.PushErrMsg(fmt.Sprintf(Conf.language(238)), 5000)
return
}
count++
util.PushEndlessProgress(fmt.Sprintf(Conf.language(236), count, total, plugin.Name))
}
for _, widget := range widgets {
err := bazaar.InstallWidget(widget.RepoURL, widget.RepoHash, filepath.Join(util.DataDir, "widgets", widget.Name), Conf.System.ID)
if nil != err {
logging.LogErrorf("update widget [%s] failed: %s", widget.Name, err)
util.PushErrMsg(fmt.Sprintf(Conf.language(238)), 5000)
return
}
count++
util.PushEndlessProgress(fmt.Sprintf(Conf.language(236), count, total, widget.Name))
}
for _, icon := range icons {
err := bazaar.InstallIcon(icon.RepoURL, icon.RepoHash, filepath.Join(util.IconsPath, icon.Name), Conf.System.ID)
if nil != err {
logging.LogErrorf("update icon [%s] failed: %s", icon.Name, err)
util.PushErrMsg(fmt.Sprintf(Conf.language(238)), 5000)
return
}
count++
util.PushEndlessProgress(fmt.Sprintf(Conf.language(236), count, total, icon.Name))
}
for _, template := range templates {
err := bazaar.InstallTemplate(template.RepoURL, template.RepoHash, filepath.Join(util.DataDir, "templates", template.Name), Conf.System.ID)
if nil != err {
logging.LogErrorf("update template [%s] failed: %s", template.Name, err)
util.PushErrMsg(fmt.Sprintf(Conf.language(238)), 5000)
return
}
count++
util.PushEndlessProgress(fmt.Sprintf(Conf.language(236), count, total, template.Name))
}
for _, theme := range themes {
err := bazaar.InstallTheme(theme.RepoURL, theme.RepoHash, filepath.Join(util.ThemesPath, theme.Name), Conf.System.ID)
if nil != err {
logging.LogErrorf("update theme [%s] failed: %s", theme.Name, err)
util.PushErrMsg(fmt.Sprintf(Conf.language(238)), 5000)
return
}
count++
util.PushEndlessProgress(fmt.Sprintf(Conf.language(236), count, total, theme.Name))
}
util.ReloadUI()
go func() {
util.WaitForUILoaded()
time.Sleep(500)
util.PushMsg(fmt.Sprintf(Conf.language(237), total), 5000)
}()
return
}
func UpdatedPackages(frontend string) (plugins []*bazaar.Plugin, widgets []*bazaar.Widget, icons []*bazaar.Icon, themes []*bazaar.Theme, templates []*bazaar.Template) {
wg := &sync.WaitGroup{}
wg.Add(5)