wp-go/internal/pkg/dao/postmeta.go

99 lines
2.7 KiB
Go
Raw Normal View History

2023-01-12 12:42:16 +00:00
package common
import (
"context"
2023-01-14 13:12:26 +00:00
"fmt"
"github.com/fthvgb1/wp-go/helper"
2023-01-21 11:31:23 +00:00
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/model"
2023-01-12 12:42:16 +00:00
"strconv"
2023-01-14 13:12:26 +00:00
"strings"
2023-01-12 12:42:16 +00:00
)
func GetPostMetaByPostIds(args ...any) (r map[uint64]map[string]any, err error) {
r = make(map[uint64]map[string]any)
ctx := args[0].(context.Context)
ids := args[1].([]uint64)
2023-01-14 13:12:26 +00:00
rr, err := model.Find[models.Postmeta](ctx, model.SqlBuilder{
2023-01-12 12:42:16 +00:00
{"post_id", "in", ""},
2023-01-21 11:31:23 +00:00
}, "*", "", nil, nil, nil, 0, slice.Map(ids, helper.ToAny[uint64]))
2023-01-12 12:42:16 +00:00
if err != nil {
return
}
for _, postmeta := range rr {
if _, ok := r[postmeta.PostId]; !ok {
r[postmeta.PostId] = make(map[string]any)
}
if postmeta.MetaKey == "_wp_attachment_metadata" {
2023-01-14 13:12:26 +00:00
metadata, err := models.AttachmentMetadata(postmeta.MetaValue)
2023-01-12 12:42:16 +00:00
if err != nil {
2023-01-14 13:12:26 +00:00
logs.ErrPrintln(err, "解析postmeta失败", postmeta.MetaId, postmeta.MetaValue)
2023-01-12 12:42:16 +00:00
continue
}
2023-01-14 13:12:26 +00:00
r[postmeta.PostId][postmeta.MetaKey] = metadata
2023-01-12 12:42:16 +00:00
} else {
r[postmeta.PostId][postmeta.MetaKey] = postmeta.MetaValue
}
}
return
}
2023-01-14 13:12:26 +00:00
func ToPostThumb(c context.Context, meta map[string]any, host string) (r models.PostThumbnail) {
2023-01-12 12:42:16 +00:00
if meta != nil {
m, ok := meta["_thumbnail_id"]
2023-01-17 15:18:31 +00:00
if !ok {
return
}
id, err := strconv.ParseUint(m.(string), 10, 64)
if err != nil {
return
}
mx, err := GetPostMetaByPostIds(c, []uint64{id})
if err != nil || mx == nil {
return
}
mm, ok := mx[id]
if !ok || mm == nil {
return
}
x, ok := mm["_wp_attachment_metadata"]
2023-01-12 12:42:16 +00:00
if ok {
2023-01-17 15:18:31 +00:00
metadata, ok := x.(models.WpAttachmentMetadata)
if ok {
r = thumbnail(metadata, "post-thumbnail", host)
}
}
}
return
}
func thumbnail(metadata models.WpAttachmentMetadata, thumbType, host string) (r models.PostThumbnail) {
if _, ok := metadata.Sizes[thumbType]; ok {
r.Path = fmt.Sprintf("%s/wp-content/uploads/%s", host, metadata.File)
r.Width = metadata.Sizes[thumbType].Width
r.Height = metadata.Sizes[thumbType].Height
up := strings.Split(metadata.File, "/")
2023-01-21 11:31:23 +00:00
r.Srcset = strings.Join(maps.FilterToSlice[string](metadata.Sizes, func(s string, size models.MetaDataFileSize) (r string, ok bool) {
2023-01-17 15:18:31 +00:00
up[2] = size.File
if s == "post-thumbnail" {
return
2023-01-12 12:42:16 +00:00
}
2023-01-17 15:18:31 +00:00
r = fmt.Sprintf("%s/wp-content/uploads/%s %dw", host, strings.Join(up, "/"), size.Width)
ok = true
return
}), ", ")
r.Sizes = fmt.Sprintf("(max-width: %dpx) 100vw, %dpx", r.Width, r.Width)
if r.Width >= 740 && r.Width < 767 {
r.Sizes = "(max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px"
} else if r.Width >= 767 {
r.Sizes = "(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px"
2023-01-12 12:42:16 +00:00
}
}
return
}