From 96368234de057558dd854179cf94736d51aca530 Mon Sep 17 00:00:00 2001 From: xing Date: Wed, 29 Mar 2023 23:31:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E9=A1=B5=E7=9C=89=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E6=97=B6=E9=97=B4=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.example.yaml | 2 - internal/pkg/cache/cache.go | 4 -- internal/pkg/cache/headerImages.go | 81 ------------------------------ internal/pkg/config/config.go | 35 +++++++------ internal/theme/wp/customheader.go | 78 +++++++++++++++++++++++++++- 5 files changed, 94 insertions(+), 106 deletions(-) delete mode 100644 internal/pkg/cache/headerImages.go diff --git a/config.example.yaml b/config.example.yaml index fd3a006..639cc64 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -58,8 +58,6 @@ cacheTime: userInfoCacheTime: 24h # 单独评论缓存时间 commentsCacheTime: 24h - # 主题的页眉图片缓存时间 - themeHeaderImagCacheTime: 5m # 随机sleep时间 sleepTime: [ 1s,3s ] # 摘要字数 diff --git a/internal/pkg/cache/cache.go b/internal/pkg/cache/cache.go index 66b40e2..2dc4de2 100644 --- a/internal/pkg/cache/cache.go +++ b/internal/pkg/cache/cache.go @@ -42,8 +42,6 @@ var newCommentCache *cache.MapCache[string, string] var allUsernameCache *cache.VarCache[map[string]struct{}] -var headerImagesCache *cache.MapCache[string, []models.PostThumbnail] - func InitActionsCommonCache() { c := config.GetConfig() archivesCaches = &Arch{ @@ -81,8 +79,6 @@ func InitActionsCommonCache() { allUsernameCache = cache.NewVarCache(dao.AllUsername, c.CacheTime.UserInfoCacheTime) - headerImagesCache = cachemanager.MapCacheBy[string](getHeaderImages, c.CacheTime.ThemeHeaderImagCacheTime) - feedCache = cache.NewVarCache(feed, time.Hour) postFeedCache = cachemanager.MapCacheBy[string](postFeed, time.Hour) diff --git a/internal/pkg/cache/headerImages.go b/internal/pkg/cache/headerImages.go deleted file mode 100644 index aeea387..0000000 --- a/internal/pkg/cache/headerImages.go +++ /dev/null @@ -1,81 +0,0 @@ -package cache - -import ( - "context" - "fmt" - "github.com/fthvgb1/wp-go/helper/slice" - str "github.com/fthvgb1/wp-go/helper/strings" - "github.com/fthvgb1/wp-go/internal/pkg/models" - "github.com/fthvgb1/wp-go/internal/wpconfig" - "github.com/fthvgb1/wp-go/model" - "time" -) - -func GetHeaderImages(ctx context.Context, theme string) (r []models.PostThumbnail, err error) { - r, err = headerImagesCache.GetCache(ctx, theme, time.Second, ctx, theme) - return -} - -func getHeaderImages(a ...any) (r []models.PostThumbnail, err error) { - ctx := a[0].(context.Context) - theme := a[1].(string) - meta, err := wpconfig.GetThemeMods(theme) - if err != nil || meta.HeaderImage == "" { - return - } - if "random-uploaded-image" != meta.HeaderImage { - m, er := GetPostById(ctx, uint64(meta.HeaderImagData.AttachmentId)) - if er != nil { - err = er - return - } - m.Thumbnail = thumb(m, theme) - r = []models.PostThumbnail{m.Thumbnail} - return - } - - headers, er := model.Finds[models.Posts](ctx, model.Conditions( - model.Where(model.SqlBuilder{ - {"post_type", "attachment"}, - {"post_status", "inherit"}, - {"meta_value", theme}, - {"meta_key", "_wp_attachment_is_custom_header"}, - }), - model.Fields("a.ID"), - model.Group("a.ID"), - model.Join(model.SqlBuilder{ - {" a", "left join", "wp_postmeta b", "a.ID=b.post_id"}, - }), - )) - - if er != nil { - err = er - return - } - if len(headers) > 0 { - posts, er := GetPostsByIds(ctx, slice.Map(headers, func(t models.Posts) uint64 { - return t.Id - })) - if er != nil { - err = er - return - } - r = slice.Map(posts, func(m models.Posts) models.PostThumbnail { - return thumb(m, theme) - }) - } - return - -} - -func thumb(m models.Posts, theme string) models.PostThumbnail { - m.Thumbnail = wpconfig.Thumbnail(m.AttachmentMetadata, "thumbnail", "", "thumbnail", "post-thumbnail", fmt.Sprintf("%s-thumbnail-avatar", theme)) - m.Thumbnail.Width = m.AttachmentMetadata.Width - m.Thumbnail.Height = m.AttachmentMetadata.Height - if m.Thumbnail.Path != "" { - if len(m.AttachmentMetadata.Sizes) > 0 { - m.Thumbnail.Srcset = str.Join(m.Thumbnail.Path, " 2000w, ", m.Thumbnail.Srcset) - } - } - return m.Thumbnail -} diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 8dbb1a1..707bb89 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -38,24 +38,23 @@ type Config struct { } type CacheTime struct { - CacheControl time.Duration `yaml:"cacheControl"` - RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime"` - CategoryCacheTime time.Duration `yaml:"categoryCacheTime"` - ArchiveCacheTime time.Duration `yaml:"archiveCacheTime"` - ContextPostCacheTime time.Duration `yaml:"contextPostCacheTime"` - RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime"` - DigestCacheTime time.Duration `yaml:"digestCacheTime"` - PostListCacheTime time.Duration `yaml:"postListCacheTime"` - SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"` - MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"` - PostDataCacheTime time.Duration `yaml:"postDataCacheTime"` - PostCommentsCacheTime time.Duration `yaml:"postCommentsCacheTime"` - CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime"` - MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime"` - UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime"` - CommentsCacheTime time.Duration `yaml:"commentsCacheTime"` - ThemeHeaderImagCacheTime time.Duration `yaml:"themeHeaderImagCacheTime"` - SleepTime []time.Duration `yaml:"sleepTime"` + CacheControl time.Duration `yaml:"cacheControl"` + RecentPostCacheTime time.Duration `yaml:"recentPostCacheTime"` + CategoryCacheTime time.Duration `yaml:"categoryCacheTime"` + ArchiveCacheTime time.Duration `yaml:"archiveCacheTime"` + ContextPostCacheTime time.Duration `yaml:"contextPostCacheTime"` + RecentCommentsCacheTime time.Duration `yaml:"recentCommentsCacheTime"` + DigestCacheTime time.Duration `yaml:"digestCacheTime"` + PostListCacheTime time.Duration `yaml:"postListCacheTime"` + SearchPostCacheTime time.Duration `yaml:"searchPostCacheTime"` + MonthPostCacheTime time.Duration `yaml:"monthPostCacheTime"` + PostDataCacheTime time.Duration `yaml:"postDataCacheTime"` + PostCommentsCacheTime time.Duration `yaml:"postCommentsCacheTime"` + CrontabClearCacheTime time.Duration `yaml:"crontabClearCacheTime"` + MaxPostIdCacheTime time.Duration `yaml:"maxPostIdCacheTime"` + UserInfoCacheTime time.Duration `yaml:"userInfoCacheTime"` + CommentsCacheTime time.Duration `yaml:"commentsCacheTime"` + SleepTime []time.Duration `yaml:"sleepTime"` } type Ssl struct { diff --git a/internal/theme/wp/customheader.go b/internal/theme/wp/customheader.go index 7cbd313..ab6055f 100644 --- a/internal/theme/wp/customheader.go +++ b/internal/theme/wp/customheader.go @@ -1,10 +1,15 @@ package wp import ( + "fmt" "github.com/fthvgb1/wp-go/helper/slice" + str "github.com/fthvgb1/wp-go/helper/strings" + "github.com/fthvgb1/wp-go/internal/cmd/reload" "github.com/fthvgb1/wp-go/internal/pkg/cache" "github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/models" + "github.com/fthvgb1/wp-go/internal/wpconfig" + "github.com/fthvgb1/wp-go/model" ) func (h *Handle) DisplayHeaderText() bool { @@ -12,11 +17,20 @@ func (h *Handle) DisplayHeaderText() bool { } func (h *Handle) GetCustomHeader() (r models.PostThumbnail, isRand bool) { - hs, err := cache.GetHeaderImages(h.C, h.theme) + var err error + hs := reload.GetAnyValBys("headerImages", h.theme, func(theme string) []models.PostThumbnail { + hs, er := h.GetHeaderImages(h.theme) + if er != nil { + err = er + return nil + } + return hs + }) if err != nil { logs.ErrPrintln(err, "获取页眉背景图失败") return } + if len(hs) < 1 { return } @@ -26,3 +40,65 @@ func (h *Handle) GetCustomHeader() (r models.PostThumbnail, isRand bool) { r, _ = slice.RandPop(&hs) return } + +func (h *Handle) GetHeaderImages(theme string) (r []models.PostThumbnail, err error) { + meta, err := wpconfig.GetThemeMods(theme) + if err != nil || meta.HeaderImage == "" { + return + } + if "random-uploaded-image" != meta.HeaderImage { + m, er := cache.GetPostById(h.C, uint64(meta.HeaderImagData.AttachmentId)) + if er != nil { + err = er + return + } + m.Thumbnail = thumb(m, theme) + r = []models.PostThumbnail{m.Thumbnail} + return + } + + headers, er := model.Finds[models.Posts](h.C, model.Conditions( + model.Where(model.SqlBuilder{ + {"post_type", "attachment"}, + {"post_status", "inherit"}, + {"meta_value", theme}, + {"meta_key", "_wp_attachment_is_custom_header"}, + }), + model.Fields("a.ID"), + model.Group("a.ID"), + model.Join(model.SqlBuilder{ + {" a", "left join", "wp_postmeta b", "a.ID=b.post_id"}, + }), + )) + + if er != nil { + err = er + return + } + if len(headers) > 0 { + posts, er := cache.GetPostsByIds(h.C, slice.Map(headers, func(t models.Posts) uint64 { + return t.Id + })) + if er != nil { + err = er + return + } + r = slice.Map(posts, func(m models.Posts) models.PostThumbnail { + return thumb(m, theme) + }) + } + return + +} + +func thumb(m models.Posts, theme string) models.PostThumbnail { + m.Thumbnail = wpconfig.Thumbnail(m.AttachmentMetadata, "thumbnail", "", "thumbnail", "post-thumbnail", fmt.Sprintf("%s-thumbnail-avatar", theme)) + m.Thumbnail.Width = m.AttachmentMetadata.Width + m.Thumbnail.Height = m.AttachmentMetadata.Height + if m.Thumbnail.Path != "" { + if len(m.AttachmentMetadata.Sizes) > 0 { + m.Thumbnail.Srcset = str.Join(m.Thumbnail.Path, " 2000w, ", m.Thumbnail.Srcset) + } + } + return m.Thumbnail +}