2023-01-21 13:13:33 +00:00
|
|
|
package dao
|
2023-01-12 12:42:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-05-04 12:36:17 +00:00
|
|
|
"github.com/fthvgb1/wp-go/app/phphelper"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/logs"
|
|
|
|
"github.com/fthvgb1/wp-go/app/pkg/models"
|
|
|
|
"github.com/fthvgb1/wp-go/app/wpconfig"
|
2023-01-21 11:31:23 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
2023-01-18 15:02:59 +00:00
|
|
|
"github.com/fthvgb1/wp-go/model"
|
2023-01-12 12:42:16 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-02-06 12:50:25 +00:00
|
|
|
rr, err := model.Finds[models.PostMeta](ctx, model.Conditions(
|
|
|
|
model.Where(model.SqlBuilder{{"post_id", "in", ""}}),
|
|
|
|
model.In(slice.ToAnySlice(ids)),
|
|
|
|
))
|
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)
|
|
|
|
}
|
2023-02-06 12:50:25 +00:00
|
|
|
r[postmeta.PostId][postmeta.MetaKey] = postmeta.MetaValue
|
2023-01-12 12:42:16 +00:00
|
|
|
if postmeta.MetaKey == "_wp_attachment_metadata" {
|
2023-02-20 17:07:32 +00:00
|
|
|
metadata, err := phphelper.UnPHPSerializeToStruct[models.WpAttachmentMetadata](postmeta.MetaValue)
|
2023-01-12 12:42:16 +00:00
|
|
|
if err != nil {
|
2023-04-07 14:59:07 +00:00
|
|
|
logs.Error(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
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2023-02-14 11:47:47 +00:00
|
|
|
r = wpconfig.Thumbnail(metadata, "post-thumbnail", host, "thumbnail")
|
2023-01-17 15:18:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|