2023-06-24 20:39:55 +08:00
|
|
|
|
// SiYuan - Refactor your thinking
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// Copyright (c) 2020-present, b3log.org
|
|
|
|
|
|
//
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
//
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
package bazaar
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
"errors"
|
2024-04-14 23:52:09 +08:00
|
|
|
|
"fmt"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
"os"
|
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"sync"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/88250/gulu"
|
|
|
|
|
|
"github.com/araddon/dateparse"
|
|
|
|
|
|
"github.com/imroc/req/v3"
|
2024-04-12 09:15:03 +08:00
|
|
|
|
gcache "github.com/patrickmn/go-cache"
|
2022-09-29 21:52:01 +08:00
|
|
|
|
"github.com/siyuan-note/filelock"
|
2022-06-23 01:22:28 +08:00
|
|
|
|
"github.com/siyuan-note/httpclient"
|
2022-07-17 12:22:32 +08:00
|
|
|
|
"github.com/siyuan-note/logging"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
2023-08-30 20:46:00 +08:00
|
|
|
|
"golang.org/x/mod/semver"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2026-02-03 09:59:36 +08:00
|
|
|
|
// LocaleStrings 表示按语种 key 的字符串表,key 为语种如 "default"、"en_US"、"zh_CN" 等
|
|
|
|
|
|
type LocaleStrings map[string]string
|
2023-05-05 22:00:51 +08:00
|
|
|
|
|
2023-05-04 19:06:54 +08:00
|
|
|
|
type Funding struct {
|
|
|
|
|
|
OpenCollective string `json:"openCollective"`
|
|
|
|
|
|
Patreon string `json:"patreon"`
|
|
|
|
|
|
GitHub string `json:"github"`
|
|
|
|
|
|
Custom []string `json:"custom"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-01 21:24:18 +08:00
|
|
|
|
type Package struct {
|
2026-02-03 09:59:36 +08:00
|
|
|
|
Author string `json:"author"`
|
|
|
|
|
|
URL string `json:"url"`
|
|
|
|
|
|
Version string `json:"version"`
|
|
|
|
|
|
MinAppVersion string `json:"minAppVersion"`
|
|
|
|
|
|
DisabledInPublish bool `json:"disabledInPublish"`
|
|
|
|
|
|
Backends []string `json:"backends"`
|
|
|
|
|
|
Frontends []string `json:"frontends"`
|
|
|
|
|
|
DisplayName LocaleStrings `json:"displayName"`
|
|
|
|
|
|
Description LocaleStrings `json:"description"`
|
|
|
|
|
|
Readme LocaleStrings `json:"readme"`
|
|
|
|
|
|
Funding *Funding `json:"funding"`
|
|
|
|
|
|
Keywords []string `json:"keywords"`
|
2023-05-05 22:00:51 +08:00
|
|
|
|
|
|
|
|
|
|
PreferredFunding string `json:"preferredFunding"`
|
2023-05-07 17:10:23 +08:00
|
|
|
|
PreferredName string `json:"preferredName"`
|
2023-05-05 22:00:51 +08:00
|
|
|
|
PreferredDesc string `json:"preferredDesc"`
|
2023-05-09 09:12:29 +08:00
|
|
|
|
PreferredReadme string `json:"preferredReadme"`
|
2022-09-01 21:24:18 +08:00
|
|
|
|
|
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
RepoURL string `json:"repoURL"`
|
|
|
|
|
|
RepoHash string `json:"repoHash"`
|
|
|
|
|
|
PreviewURL string `json:"previewURL"`
|
|
|
|
|
|
PreviewURLThumb string `json:"previewURLThumb"`
|
2023-05-05 15:46:09 +08:00
|
|
|
|
IconURL string `json:"iconURL"`
|
2022-09-01 21:24:18 +08:00
|
|
|
|
|
2025-12-30 17:27:23 +08:00
|
|
|
|
Installed bool `json:"installed"`
|
|
|
|
|
|
Outdated bool `json:"outdated"`
|
|
|
|
|
|
Current bool `json:"current"`
|
|
|
|
|
|
Updated string `json:"updated"`
|
|
|
|
|
|
Stars int `json:"stars"`
|
|
|
|
|
|
OpenIssues int `json:"openIssues"`
|
|
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
|
|
HSize string `json:"hSize"`
|
|
|
|
|
|
InstallSize int64 `json:"installSize"`
|
|
|
|
|
|
HInstallSize string `json:"hInstallSize"`
|
|
|
|
|
|
HInstallDate string `json:"hInstallDate"`
|
|
|
|
|
|
HUpdated string `json:"hUpdated"`
|
|
|
|
|
|
Downloads int `json:"downloads"`
|
|
|
|
|
|
DisallowInstall bool `json:"disallowInstall"`
|
|
|
|
|
|
DisallowUpdate bool `json:"disallowUpdate"`
|
|
|
|
|
|
UpdateRequiredMinAppVer string `json:"updateRequiredMinAppVer"`
|
2023-05-29 20:16:23 +08:00
|
|
|
|
|
|
|
|
|
|
Incompatible bool `json:"incompatible"`
|
2022-09-01 21:24:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-05 23:07:06 +08:00
|
|
|
|
type StageRepo struct {
|
2024-04-28 22:48:05 +08:00
|
|
|
|
URL string `json:"url"`
|
|
|
|
|
|
Updated string `json:"updated"`
|
|
|
|
|
|
Stars int `json:"stars"`
|
|
|
|
|
|
OpenIssues int `json:"openIssues"`
|
|
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
|
|
InstallSize int64 `json:"installSize"`
|
2023-05-05 22:00:51 +08:00
|
|
|
|
|
2026-02-03 10:07:16 +08:00
|
|
|
|
// Package 与 stage/*.json 内嵌的完整 package 一致,可直接用于构建列表
|
|
|
|
|
|
Package *Package `json:"package"`
|
2023-05-05 23:07:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type StageIndex struct {
|
|
|
|
|
|
Repos []*StageRepo `json:"repos"`
|
2023-05-05 16:43:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-03 09:59:36 +08:00
|
|
|
|
// getPreferredLocaleString 从 LocaleStrings 中按当前语种取值,无则回退 default、en_US,再回退 fallback。
|
|
|
|
|
|
func getPreferredLocaleString(m LocaleStrings, fallback string) string {
|
|
|
|
|
|
if len(m) == 0 {
|
|
|
|
|
|
return fallback
|
2023-05-05 23:28:50 +08:00
|
|
|
|
}
|
2026-02-03 09:59:36 +08:00
|
|
|
|
if v := strings.TrimSpace(m[util.Lang]); "" != v {
|
|
|
|
|
|
return v
|
2025-12-17 07:43:05 +08:00
|
|
|
|
}
|
2026-02-03 09:59:36 +08:00
|
|
|
|
if v := strings.TrimSpace(m["default"]); "" != v {
|
|
|
|
|
|
return v
|
2025-12-17 07:43:05 +08:00
|
|
|
|
}
|
2026-02-03 09:59:36 +08:00
|
|
|
|
if v := strings.TrimSpace(m["en_US"]); "" != v {
|
|
|
|
|
|
return v
|
2024-11-08 15:02:56 +08:00
|
|
|
|
}
|
2026-02-03 09:59:36 +08:00
|
|
|
|
return fallback
|
2023-05-05 23:28:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-07 10:03:31 +08:00
|
|
|
|
func GetPreferredName(pkg *Package) string {
|
2026-02-03 09:59:36 +08:00
|
|
|
|
return getPreferredLocaleString(pkg.DisplayName, pkg.Name)
|
2023-05-07 17:10:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-03 09:59:36 +08:00
|
|
|
|
func getPreferredDesc(desc LocaleStrings) string {
|
|
|
|
|
|
return getPreferredLocaleString(desc, "")
|
|
|
|
|
|
}
|
2025-12-17 07:43:05 +08:00
|
|
|
|
|
2026-02-03 09:59:36 +08:00
|
|
|
|
func getPreferredReadme(readme LocaleStrings) string {
|
|
|
|
|
|
return getPreferredLocaleString(readme, "README.md")
|
2023-05-05 22:16:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-05 22:00:51 +08:00
|
|
|
|
func getPreferredFunding(funding *Funding) string {
|
2023-05-05 22:01:42 +08:00
|
|
|
|
if nil == funding {
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-05 22:00:51 +08:00
|
|
|
|
if "" != funding.OpenCollective {
|
2025-12-10 11:26:54 +08:00
|
|
|
|
if strings.HasPrefix(funding.OpenCollective, "http://") || strings.HasPrefix(funding.OpenCollective, "https://") {
|
|
|
|
|
|
return funding.OpenCollective
|
|
|
|
|
|
}
|
2023-05-06 20:53:12 +08:00
|
|
|
|
return "https://opencollective.com/" + funding.OpenCollective
|
2023-05-05 22:00:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
if "" != funding.Patreon {
|
2025-12-10 11:26:54 +08:00
|
|
|
|
if strings.HasPrefix(funding.Patreon, "http://") || strings.HasPrefix(funding.Patreon, "https://") {
|
|
|
|
|
|
return funding.Patreon
|
|
|
|
|
|
}
|
2023-05-06 20:53:12 +08:00
|
|
|
|
return "https://www.patreon.com/" + funding.Patreon
|
2023-05-05 22:00:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
if "" != funding.GitHub {
|
2025-12-10 11:26:54 +08:00
|
|
|
|
if strings.HasPrefix(funding.GitHub, "http://") || strings.HasPrefix(funding.GitHub, "https://") {
|
|
|
|
|
|
return funding.GitHub
|
|
|
|
|
|
}
|
2023-05-06 20:53:12 +08:00
|
|
|
|
return "https://github.com/sponsors/" + funding.GitHub
|
2023-05-05 22:00:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
if 0 < len(funding.Custom) {
|
|
|
|
|
|
return funding.Custom[0]
|
|
|
|
|
|
}
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-05 23:07:06 +08:00
|
|
|
|
func PluginJSON(pluginDirName string) (ret *Plugin, err error) {
|
2023-04-25 18:52:19 +08:00
|
|
|
|
p := filepath.Join(util.DataDir, "plugins", pluginDirName, "plugin.json")
|
2023-11-06 22:13:04 +08:00
|
|
|
|
if !filelock.IsExist(p) {
|
2023-04-25 18:52:19 +08:00
|
|
|
|
err = os.ErrNotExist
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-11-06 22:13:04 +08:00
|
|
|
|
data, err := filelock.ReadFile(p)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2023-04-25 18:52:19 +08:00
|
|
|
|
logging.LogErrorf("read plugin.json [%s] failed: %s", p, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, &ret); err != nil {
|
2023-04-25 18:52:19 +08:00
|
|
|
|
logging.LogErrorf("parse plugin.json [%s] failed: %s", p, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-05-05 23:07:06 +08:00
|
|
|
|
|
|
|
|
|
|
ret.URL = strings.TrimSuffix(ret.URL, "/")
|
2023-04-25 18:52:19 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-05 23:07:06 +08:00
|
|
|
|
func WidgetJSON(widgetDirName string) (ret *Widget, err error) {
|
2022-09-02 10:41:03 +08:00
|
|
|
|
p := filepath.Join(util.DataDir, "widgets", widgetDirName, "widget.json")
|
2023-11-06 22:13:04 +08:00
|
|
|
|
if !filelock.IsExist(p) {
|
2022-09-01 21:33:24 +08:00
|
|
|
|
err = os.ErrNotExist
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-11-06 22:13:04 +08:00
|
|
|
|
data, err := filelock.ReadFile(p)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-09-01 21:33:24 +08:00
|
|
|
|
logging.LogErrorf("read widget.json [%s] failed: %s", p, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, &ret); err != nil {
|
2022-09-01 21:33:24 +08:00
|
|
|
|
logging.LogErrorf("parse widget.json [%s] failed: %s", p, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-05-05 23:07:06 +08:00
|
|
|
|
|
|
|
|
|
|
ret.URL = strings.TrimSuffix(ret.URL, "/")
|
2022-09-01 21:33:24 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-05 23:07:06 +08:00
|
|
|
|
func IconJSON(iconDirName string) (ret *Icon, err error) {
|
2022-09-02 11:03:10 +08:00
|
|
|
|
p := filepath.Join(util.IconsPath, iconDirName, "icon.json")
|
2022-09-01 21:24:18 +08:00
|
|
|
|
if !gulu.File.IsExist(p) {
|
|
|
|
|
|
err = os.ErrNotExist
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
data, err := os.ReadFile(p)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-09-01 21:24:18 +08:00
|
|
|
|
logging.LogErrorf("read icon.json [%s] failed: %s", p, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, &ret); err != nil {
|
2022-09-01 21:24:18 +08:00
|
|
|
|
logging.LogErrorf("parse icon.json [%s] failed: %s", p, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-05-05 23:07:06 +08:00
|
|
|
|
|
|
|
|
|
|
ret.URL = strings.TrimSuffix(ret.URL, "/")
|
2022-09-01 21:24:18 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-05 23:07:06 +08:00
|
|
|
|
func TemplateJSON(templateDirName string) (ret *Template, err error) {
|
2022-09-02 10:32:24 +08:00
|
|
|
|
p := filepath.Join(util.DataDir, "templates", templateDirName, "template.json")
|
2023-11-06 22:13:04 +08:00
|
|
|
|
if !filelock.IsExist(p) {
|
2022-09-01 19:37:33 +08:00
|
|
|
|
err = os.ErrNotExist
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-11-06 22:13:04 +08:00
|
|
|
|
data, err := filelock.ReadFile(p)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-09-01 19:37:33 +08:00
|
|
|
|
logging.LogErrorf("read template.json [%s] failed: %s", p, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, &ret); err != nil {
|
2022-09-01 19:37:33 +08:00
|
|
|
|
logging.LogErrorf("parse template.json [%s] failed: %s", p, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-05-05 23:07:06 +08:00
|
|
|
|
|
|
|
|
|
|
ret.URL = strings.TrimSuffix(ret.URL, "/")
|
2022-09-01 19:37:33 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-05 23:07:06 +08:00
|
|
|
|
func ThemeJSON(themeDirName string) (ret *Theme, err error) {
|
2022-09-01 19:37:33 +08:00
|
|
|
|
p := filepath.Join(util.ThemesPath, themeDirName, "theme.json")
|
|
|
|
|
|
if !gulu.File.IsExist(p) {
|
|
|
|
|
|
err = os.ErrNotExist
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
data, err := os.ReadFile(p)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-09-01 19:37:33 +08:00
|
|
|
|
logging.LogErrorf("read theme.json [%s] failed: %s", p, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-05-05 23:07:06 +08:00
|
|
|
|
|
|
|
|
|
|
ret = &Theme{}
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(data, &ret); err != nil {
|
2022-09-01 19:37:33 +08:00
|
|
|
|
logging.LogErrorf("parse theme.json [%s] failed: %s", p, err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-05-05 23:07:06 +08:00
|
|
|
|
|
|
|
|
|
|
ret.URL = strings.TrimSuffix(ret.URL, "/")
|
2022-09-01 19:37:33 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-21 22:35:21 +08:00
|
|
|
|
var (
|
|
|
|
|
|
packageLocks = map[string]*sync.Mutex{}
|
|
|
|
|
|
packageLocksLock = sync.Mutex{}
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2022-09-01 11:53:07 +08:00
|
|
|
|
func downloadPackage(repoURLHash string, pushProgress bool, systemID string) (data []byte, err error) {
|
2024-03-21 22:35:21 +08:00
|
|
|
|
packageLocksLock.Lock()
|
|
|
|
|
|
defer packageLocksLock.Unlock()
|
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// repoURLHash: https://github.com/88250/Comfortably-Numb@6286912c381ef3f83e455d06ba4d369c498238dc
|
2024-03-21 22:35:21 +08:00
|
|
|
|
repoURL := repoURLHash[:strings.LastIndex(repoURLHash, "@")]
|
|
|
|
|
|
lock, ok := packageLocks[repoURLHash]
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
lock = &sync.Mutex{}
|
|
|
|
|
|
packageLocks[repoURLHash] = lock
|
|
|
|
|
|
}
|
|
|
|
|
|
lock.Lock()
|
|
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
repoURLHash = strings.TrimPrefix(repoURLHash, "https://github.com/")
|
2022-09-01 11:53:07 +08:00
|
|
|
|
u := util.BazaarOSSServer + "/package/" + repoURLHash
|
2022-05-26 15:18:53 +08:00
|
|
|
|
buf := &bytes.Buffer{}
|
2024-04-21 19:45:04 +08:00
|
|
|
|
resp, err := httpclient.NewCloudFileRequest2m().SetOutput(buf).SetDownloadCallback(func(info req.DownloadInfo) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if pushProgress {
|
2022-12-13 10:45:44 +08:00
|
|
|
|
progress := float32(info.DownloadedSize) / float32(info.Response.ContentLength)
|
2022-12-29 19:41:47 +08:00
|
|
|
|
//logging.LogDebugf("downloading bazaar package [%f]", progress)
|
2024-03-21 22:35:21 +08:00
|
|
|
|
util.PushDownloadProgress(repoURL, progress)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}).Get(u)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-12-13 10:45:44 +08:00
|
|
|
|
logging.LogErrorf("get bazaar package [%s] failed: %s", u, err)
|
2023-06-09 13:14:33 +08:00
|
|
|
|
return nil, errors.New("get bazaar package failed, please check your network")
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
if 200 != resp.StatusCode {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("get bazaar package [%s] failed: %d", u, resp.StatusCode)
|
2023-06-09 13:12:19 +08:00
|
|
|
|
return nil, errors.New("get bazaar package failed: " + resp.Status)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
data = buf.Bytes()
|
|
|
|
|
|
|
2022-07-09 11:22:51 +08:00
|
|
|
|
go incPackageDownloads(repoURLHash, systemID)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-07-09 11:22:51 +08:00
|
|
|
|
func incPackageDownloads(repoURLHash, systemID string) {
|
2023-05-05 23:28:50 +08:00
|
|
|
|
if strings.Contains(repoURLHash, ".md") || "" == systemID {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
repo := strings.Split(repoURLHash, "@")[0]
|
2023-06-20 11:48:44 +08:00
|
|
|
|
u := util.GetCloudServer() + "/apis/siyuan/bazaar/addBazaarPackageDownloadCount"
|
2022-12-05 22:59:59 +08:00
|
|
|
|
httpclient.NewCloudRequest30s().SetBody(
|
2022-05-26 15:18:53 +08:00
|
|
|
|
map[string]interface{}{
|
|
|
|
|
|
"systemID": systemID,
|
|
|
|
|
|
"repo": repo,
|
|
|
|
|
|
}).Post(u)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-14 23:52:09 +08:00
|
|
|
|
func uninstallPackage(installPath string) (err error) {
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = os.RemoveAll(installPath); err != nil {
|
2024-04-14 23:52:09 +08:00
|
|
|
|
logging.LogErrorf("remove [%s] failed: %s", installPath, err)
|
|
|
|
|
|
return fmt.Errorf("remove community package [%s] failed", filepath.Base(installPath))
|
|
|
|
|
|
}
|
|
|
|
|
|
packageCache.Flush()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func installPackage(data []byte, installPath, repoURLHash string) (err error) {
|
|
|
|
|
|
err = installPackage0(data, installPath)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2024-04-14 23:52:09 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
packageCache.Delete(strings.TrimPrefix(repoURLHash, "https://github.com/"))
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func installPackage0(data []byte, installPath string) (err error) {
|
2023-05-05 15:38:44 +08:00
|
|
|
|
tmpPackage := filepath.Join(util.TempDir, "bazaar", "package")
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = os.MkdirAll(tmpPackage, 0755); err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
name := gulu.Rand.String(7)
|
2023-05-05 15:38:44 +08:00
|
|
|
|
tmp := filepath.Join(tmpPackage, name+".zip")
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = os.WriteFile(tmp, data, 0644); err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-05 15:38:44 +08:00
|
|
|
|
unzipPath := filepath.Join(tmpPackage, name)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = gulu.Zip.Unzip(tmp, unzipPath); err != nil {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("write file [%s] failed: %s", installPath, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dirs, err := os.ReadDir(unzipPath)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2023-05-05 15:38:44 +08:00
|
|
|
|
|
|
|
|
|
|
srcPath := unzipPath
|
2023-05-08 12:00:32 +08:00
|
|
|
|
if 1 == len(dirs) && dirs[0].IsDir() {
|
2023-05-05 15:38:44 +08:00
|
|
|
|
srcPath = filepath.Join(unzipPath, dirs[0].Name())
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2023-05-05 15:38:44 +08:00
|
|
|
|
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err = filelock.Copy(srcPath, installPath); err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func formatUpdated(updated string) (ret string) {
|
|
|
|
|
|
t, e := dateparse.ParseIn(updated, time.Now().Location())
|
|
|
|
|
|
if nil == e {
|
|
|
|
|
|
ret = t.Format("2006-01-02")
|
|
|
|
|
|
} else {
|
|
|
|
|
|
if strings.Contains(updated, "T") {
|
|
|
|
|
|
ret = updated[:strings.Index(updated, "T")]
|
|
|
|
|
|
} else {
|
|
|
|
|
|
ret = strings.ReplaceAll(strings.ReplaceAll(updated, "T", ""), "Z", "")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-24 10:34:15 +08:00
|
|
|
|
// Add marketplace package config item `minAppVersion` https://github.com/siyuan-note/siyuan/issues/8330
|
2025-12-25 22:33:10 +08:00
|
|
|
|
func disallowInstallBazaarPackage(pkg *Package) bool {
|
2026-02-03 10:07:16 +08:00
|
|
|
|
// 如果包没有指定 minAppVersion,则允许安装
|
2025-09-04 10:22:57 +08:00
|
|
|
|
if "" == pkg.MinAppVersion {
|
2026-02-03 10:07:16 +08:00
|
|
|
|
return false
|
2023-05-24 10:38:30 +08:00
|
|
|
|
}
|
2025-09-04 10:22:57 +08:00
|
|
|
|
|
2026-02-03 10:07:16 +08:00
|
|
|
|
// 如果包要求的 minAppVersion 大于当前版本,则不允许安装
|
2023-05-29 20:16:23 +08:00
|
|
|
|
if 0 < semver.Compare("v"+pkg.MinAppVersion, "v"+util.Ver) {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
2023-05-24 10:38:30 +08:00
|
|
|
|
}
|
2024-04-12 09:15:03 +08:00
|
|
|
|
|
|
|
|
|
|
var packageCache = gcache.New(6*time.Hour, 30*time.Minute) // [repoURL]*Package
|
2024-04-28 22:48:05 +08:00
|
|
|
|
|
2024-10-26 21:36:08 +08:00
|
|
|
|
func CleanBazaarPackageCache() {
|
|
|
|
|
|
packageCache.Flush()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-28 22:48:05 +08:00
|
|
|
|
var packageInstallSizeCache = gcache.New(48*time.Hour, 6*time.Hour) // [repoURL]*int64
|