wp-go/internal/cache/postmeta.go

59 lines
1.3 KiB
Go
Raw Normal View History

2023-01-12 12:42:16 +00:00
package cache
import (
"context"
"github/fthvgb1/wp-go/helper"
2023-01-13 06:04:32 +00:00
wp2 "github/fthvgb1/wp-go/internal/pkg/models"
"strconv"
"time"
)
func GetPostMetaByPostIds(ctx context.Context, ids []uint64) (r []map[string]any, err error) {
r, err = postMetaCache.GetCacheBatch(ctx, ids, time.Second, ctx, ids)
return
}
func GetPostMetaByPostId(ctx context.Context, id uint64) (r map[string]any, err error) {
r, err = postMetaCache.GetCache(ctx, id, time.Second, ctx, id)
return
}
2023-01-12 10:17:34 +00:00
func ToPostThumbnail(c context.Context, postId uint64) (r wp2.PostThumbnail) {
meta, err := GetPostMetaByPostId(c, postId)
if err == nil {
m, ok := meta["_thumbnail_id"]
if ok {
id, err := strconv.ParseUint(m.(string), 10, 64)
if err == nil {
mm, err := GetPostMetaByPostId(c, id)
if err == nil {
f, ok := mm["_wp_attached_file"]
if ok {
ff, ok := f.(string)
if ok && ff != "" {
r.Path = ff
}
}
2023-01-11 05:56:56 +00:00
tt, ok := helper.GetStrMapAnyVal[map[string]any]("_wp_attachment_metadata.sizes.post-thumbnail", mm)
if ok && tt != nil {
width, ok := tt["width"]
if ok {
2023-01-11 05:56:56 +00:00
w, ok := width.(int)
if ok {
2023-01-11 05:56:56 +00:00
r.Width = w
}
2023-01-11 05:56:56 +00:00
}
height, ok := tt["height"]
if ok {
h, ok := height.(int)
if ok {
2023-01-11 05:56:56 +00:00
r.Height = h
}
}
}
}
}
}
}
return
}