2023-01-12 12:42:16 +00:00
|
|
|
package cache
|
2023-01-10 12:09:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github/fthvgb1/wp-go/helper"
|
2023-01-12 12:42:16 +00:00
|
|
|
wp2 "github/fthvgb1/wp-go/internal/models"
|
2023-01-10 12:09:55 +00:00
|
|
|
"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) {
|
2023-01-10 12:09:55 +00:00
|
|
|
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"]
|
2023-01-10 12:09:55 +00:00
|
|
|
if ok {
|
2023-01-11 05:56:56 +00:00
|
|
|
w, ok := width.(int)
|
2023-01-10 12:09:55 +00:00
|
|
|
if ok {
|
2023-01-11 05:56:56 +00:00
|
|
|
r.Width = w
|
2023-01-10 12:09:55 +00:00
|
|
|
}
|
2023-01-11 05:56:56 +00:00
|
|
|
}
|
|
|
|
height, ok := tt["height"]
|
|
|
|
if ok {
|
|
|
|
h, ok := height.(int)
|
2023-01-10 12:09:55 +00:00
|
|
|
if ok {
|
2023-01-11 05:56:56 +00:00
|
|
|
r.Height = h
|
2023-01-10 12:09:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|