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 (
|
|
|
|
|
|
"os"
|
2022-09-01 19:37:33 +08:00
|
|
|
|
"path/filepath"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
"sort"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2024-04-24 19:51:15 +08:00
|
|
|
|
"github.com/88250/go-humanize"
|
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"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Template struct {
|
2023-05-05 23:07:06 +08:00
|
|
|
|
*Package
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-03 10:07:16 +08:00
|
|
|
|
// Templates 返回集市模板列表
|
2022-07-09 11:22:51 +08:00
|
|
|
|
func Templates() (templates []*Template) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
templates = []*Template{}
|
2026-02-03 10:07:16 +08:00
|
|
|
|
result := getStageAndBazaar("templates")
|
2022-09-01 20:10:36 +08:00
|
|
|
|
|
2026-02-03 10:07:16 +08:00
|
|
|
|
if !result.Online {
|
2024-08-11 10:01:02 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-02-03 10:07:16 +08:00
|
|
|
|
if result.StageErr != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2026-02-03 10:07:16 +08:00
|
|
|
|
if 1 > len(result.BazaarIndex) {
|
2025-03-08 11:51:35 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-03 10:07:16 +08:00
|
|
|
|
for _, repo := range result.StageIndex.Repos {
|
|
|
|
|
|
if nil == repo.Package {
|
|
|
|
|
|
continue
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2026-02-03 10:07:16 +08:00
|
|
|
|
template := buildTemplateFromStageRepo(repo, result.BazaarIndex)
|
|
|
|
|
|
if nil != template {
|
|
|
|
|
|
templates = append(templates, template)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
templates = filterLegacyTemplates(templates)
|
|
|
|
|
|
|
|
|
|
|
|
sort.Slice(templates, func(i, j int) bool { return templates[i].Updated > templates[j].Updated })
|
|
|
|
|
|
return
|
2022-09-01 19:37:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-03 10:07:16 +08:00
|
|
|
|
// buildTemplateFromStageRepo 使用 stage 内嵌的 package 构建 *Template,不发起 HTTP 请求。
|
|
|
|
|
|
func buildTemplateFromStageRepo(repo *StageRepo, bazaarIndex map[string]*bazaarPackage) *Template {
|
|
|
|
|
|
pkg := *repo.Package
|
|
|
|
|
|
pkg.URL = strings.TrimSuffix(pkg.URL, "/")
|
|
|
|
|
|
repoURLHash := strings.Split(repo.URL, "@")
|
|
|
|
|
|
if 2 != len(repoURLHash) {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
pkg.RepoURL = "https://github.com/" + repoURLHash[0]
|
|
|
|
|
|
pkg.RepoHash = repoURLHash[1]
|
|
|
|
|
|
pkg.PreviewURL = util.BazaarOSSServer + "/package/" + repo.URL + "/preview.png?imageslim"
|
|
|
|
|
|
pkg.PreviewURLThumb = util.BazaarOSSServer + "/package/" + repo.URL + "/preview.png?imageView2/2/w/436/h/232"
|
|
|
|
|
|
pkg.IconURL = util.BazaarOSSServer + "/package/" + repo.URL + "/icon.png"
|
|
|
|
|
|
pkg.Updated = repo.Updated
|
|
|
|
|
|
pkg.Stars = repo.Stars
|
|
|
|
|
|
pkg.OpenIssues = repo.OpenIssues
|
|
|
|
|
|
pkg.Size = repo.Size
|
|
|
|
|
|
pkg.HSize = humanize.BytesCustomCeil(uint64(pkg.Size), 2)
|
|
|
|
|
|
pkg.InstallSize = repo.InstallSize
|
|
|
|
|
|
pkg.HInstallSize = humanize.BytesCustomCeil(uint64(pkg.InstallSize), 2)
|
|
|
|
|
|
pkg.HUpdated = formatUpdated(pkg.Updated)
|
|
|
|
|
|
pkg.PreferredFunding = getPreferredFunding(pkg.Funding)
|
|
|
|
|
|
pkg.PreferredName = GetPreferredName(&pkg)
|
|
|
|
|
|
pkg.PreferredDesc = getPreferredDesc(pkg.Description)
|
|
|
|
|
|
pkg.DisallowInstall = disallowInstallBazaarPackage(&pkg)
|
|
|
|
|
|
pkg.DisallowUpdate = disallowInstallBazaarPackage(&pkg)
|
|
|
|
|
|
pkg.UpdateRequiredMinAppVer = pkg.MinAppVersion
|
|
|
|
|
|
if bp := bazaarIndex[repoURLHash[0]]; nil != bp {
|
|
|
|
|
|
pkg.Downloads = bp.Downloads
|
|
|
|
|
|
}
|
|
|
|
|
|
packageInstallSizeCache.SetDefault(pkg.RepoURL, pkg.InstallSize)
|
|
|
|
|
|
return &Template{Package: &pkg}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-01 19:37:33 +08:00
|
|
|
|
func InstalledTemplates() (ret []*Template) {
|
2022-09-01 21:48:50 +08:00
|
|
|
|
ret = []*Template{}
|
2023-05-05 09:46:17 +08:00
|
|
|
|
|
|
|
|
|
|
templatesPath := filepath.Join(util.DataDir, "templates")
|
2023-05-16 10:34:38 +08:00
|
|
|
|
if !util.IsPathRegularDirOrSymlinkDir(templatesPath) {
|
2023-05-05 09:46:17 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
templateDirs, err := os.ReadDir(templatesPath)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-09-01 19:37:33 +08:00
|
|
|
|
logging.LogWarnf("read templates folder failed: %s", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-01 21:24:18 +08:00
|
|
|
|
bazaarTemplates := Templates()
|
|
|
|
|
|
|
2022-09-01 19:37:33 +08:00
|
|
|
|
for _, templateDir := range templateDirs {
|
2023-05-16 10:34:38 +08:00
|
|
|
|
if !util.IsDirRegularOrSymlink(templateDir) {
|
2022-09-01 19:37:33 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
dirName := templateDir.Name()
|
2022-09-01 21:24:18 +08:00
|
|
|
|
|
2023-05-05 23:07:06 +08:00
|
|
|
|
template, parseErr := TemplateJSON(dirName)
|
|
|
|
|
|
if nil != parseErr || nil == template {
|
2022-09-01 19:37:33 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-01 16:44:15 +08:00
|
|
|
|
template.RepoURL = template.URL
|
2025-12-27 12:41:03 +08:00
|
|
|
|
template.DisallowInstall = disallowInstallBazaarPackage(template.Package)
|
2025-12-30 17:27:23 +08:00
|
|
|
|
if bazaarPkg := getBazaarTemplate(template.Name, bazaarTemplates); nil != bazaarPkg {
|
|
|
|
|
|
template.DisallowUpdate = disallowInstallBazaarPackage(bazaarPkg.Package)
|
|
|
|
|
|
template.UpdateRequiredMinAppVer = bazaarPkg.MinAppVersion
|
2026-02-01 16:44:15 +08:00
|
|
|
|
template.RepoURL = bazaarPkg.RepoURL
|
2025-12-30 17:27:23 +08:00
|
|
|
|
}
|
2022-09-03 23:54:26 +08:00
|
|
|
|
|
2025-12-27 12:41:03 +08:00
|
|
|
|
installPath := filepath.Join(util.DataDir, "templates", dirName)
|
2022-09-01 22:32:43 +08:00
|
|
|
|
template.Installed = true
|
2022-09-01 19:37:33 +08:00
|
|
|
|
template.PreviewURL = "/templates/" + dirName + "/preview.png"
|
|
|
|
|
|
template.PreviewURLThumb = "/templates/" + dirName + "/preview.png"
|
2023-05-05 15:46:09 +08:00
|
|
|
|
template.IconURL = "/templates/" + dirName + "/icon.png"
|
2023-05-05 22:00:51 +08:00
|
|
|
|
template.PreferredFunding = getPreferredFunding(template.Funding)
|
2024-04-07 10:03:31 +08:00
|
|
|
|
template.PreferredName = GetPreferredName(template.Package)
|
2023-05-05 22:16:26 +08:00
|
|
|
|
template.PreferredDesc = getPreferredDesc(template.Description)
|
2025-12-10 11:26:54 +08:00
|
|
|
|
info, statErr := os.Stat(filepath.Join(installPath, "template.json"))
|
2022-09-04 00:06:45 +08:00
|
|
|
|
if nil != statErr {
|
2025-12-10 11:26:54 +08:00
|
|
|
|
logging.LogWarnf("stat install template.json failed: %s", statErr)
|
2022-09-04 00:06:45 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
template.HInstallDate = info.ModTime().Format("2006-01-02")
|
2024-04-28 22:48:05 +08:00
|
|
|
|
if installSize, ok := packageInstallSizeCache.Get(template.RepoURL); ok {
|
|
|
|
|
|
template.InstallSize = installSize.(int64)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
is, _ := util.SizeOfDirectory(installPath)
|
|
|
|
|
|
template.InstallSize = is
|
|
|
|
|
|
packageInstallSizeCache.SetDefault(template.RepoURL, is)
|
|
|
|
|
|
}
|
|
|
|
|
|
template.HInstallSize = humanize.BytesCustomCeil(uint64(template.InstallSize), 2)
|
2025-12-10 11:26:54 +08:00
|
|
|
|
template.PreferredReadme = loadInstalledReadme(installPath, "/templates/"+dirName+"/", template.Readme)
|
2022-09-02 11:03:10 +08:00
|
|
|
|
template.Outdated = isOutdatedTemplate(template, bazaarTemplates)
|
2022-09-01 19:37:33 +08:00
|
|
|
|
ret = append(ret, template)
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 17:27:23 +08:00
|
|
|
|
func getBazaarTemplate(name string, templates []*Template) *Template {
|
|
|
|
|
|
for _, p := range templates {
|
|
|
|
|
|
if p.Name == name {
|
|
|
|
|
|
return p
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-01 11:53:07 +08:00
|
|
|
|
func InstallTemplate(repoURL, repoHash, installPath string, systemID string) error {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
repoURLHash := repoURL + "@" + repoHash
|
2022-09-01 11:53:07 +08:00
|
|
|
|
data, err := downloadPackage(repoURLHash, true, systemID)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
2024-04-14 23:52:09 +08:00
|
|
|
|
return installPackage(data, installPath, repoURLHash)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func UninstallTemplate(installPath string) error {
|
2024-04-14 23:52:09 +08:00
|
|
|
|
return uninstallPackage(installPath)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func filterLegacyTemplates(templates []*Template) (ret []*Template) {
|
|
|
|
|
|
verTime, _ := time.Parse("2006-01-02T15:04:05", "2021-05-12T00:00:00")
|
|
|
|
|
|
for _, theme := range templates {
|
|
|
|
|
|
if "" != theme.Updated {
|
|
|
|
|
|
updated := theme.Updated[:len("2006-01-02T15:04:05")]
|
|
|
|
|
|
t, err := time.Parse("2006-01-02T15:04:05", updated)
|
2024-09-04 04:40:50 +03:00
|
|
|
|
if err != nil {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("convert update time [%s] failed: %s", updated, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if t.After(verTime) {
|
|
|
|
|
|
ret = append(ret, theme)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|