package mediafiller import ( "encoding/json" "math/rand" "service/api/proto/gateway/proto" "service/dbstruct" "service/library/logger" "time" "github.com/gin-gonic/gin" ) var TestMidMap = map[int64]bool{ 161: true, 1: true, } func init() { rand.Seed(time.Now().Unix()) } var defaultMediaFiller *MediaFiller type GetImageByIdsFunc func(ctx *gin.Context, ids []int64) ([]*dbstruct.Image, error) type GetVideoByIdsFunc func(ctx *gin.Context, ids []int64) ([]*dbstruct.Video, error) type MediaFiller struct { fileServerDomainName string //文件服务器域名 getImageByIds GetImageByIdsFunc //获取图片数据源 getVideoByIds GetVideoByIdsFunc //获取视频数据源 } func InitDefaultMediaFiller() { defaultMediaFiller = &MediaFiller{} } func SetGetImageByIdsFunc(getImageByIdsFunc GetImageByIdsFunc) { defaultMediaFiller.getImageByIds = getImageByIdsFunc } func SetGetVideoByIdsFunc(getVideoByIdsFunc GetVideoByIdsFunc) { defaultMediaFiller.getVideoByIds = getVideoByIdsFunc } func SetFileServerDomainName(fileServerDomainName string) { defaultMediaFiller.fileServerDomainName = fileServerDomainName } func (p *MediaFiller) GetFileServerDomain() string { return defaultMediaFiller.fileServerDomainName } func (p *MediaFiller) GetInternalFileServerDomain() string { return p.GetFileServerDomain() } func FillEntity(ctx *gin.Context, entity MediaFillable) error { baseReq := proto.BaseReq{} var bodyBytes []byte body, ok := ctx.Get(gin.BodyBytesKey) if ok { bodyBytes = body.([]byte) } _ = json.Unmarshal(bodyBytes, &baseReq) if TestMidMap[baseReq.Mid] { logger.Info("FillTest req: %v", string(bodyBytes)) } videoIds := entity.GetVideoIds() videoMap, err := getVideoMapByIds(ctx, videoIds) if err != nil { logger.Error("mediafiller component getVideoMapByIds failed : %v", err) return err } imageIds := entity.GetImageIds() // 获取视频封面 for _, v := range videoMap { imageIds = append(imageIds, v.CoverId) } imageMap, err := getImageMapByIds(ctx, imageIds) if err != nil { logger.Error("mediafiller component getImageMapByIds failed : %v", err) return err } images := make([]*dbstruct.ToCImage, 0) videos := make([]*dbstruct.ToCVideo, 0) for _, imageId := range imageIds { if image, ok := imageMap[imageId]; ok { images = append(images, transToCImage(image)) } } for _, videoId := range videoIds { if video, ok := videoMap[videoId]; ok { if TestMidMap[baseReq.Mid] { videos = append(videos, transToCVideoTest(video, imageMap[video.CoverId])) } else { videos = append(videos, transToCVideo(video, imageMap[video.CoverId])) } } } entity.SetImages(images) entity.SetVideos(videos) return nil } func FillList(ctx *gin.Context, list []MediaFillable) error { baseReq := proto.BaseReq{} var bodyBytes []byte body, ok := ctx.Get(gin.BodyBytesKey) if ok { bodyBytes = body.([]byte) } _ = json.Unmarshal(bodyBytes, &baseReq) if TestMidMap[baseReq.Mid] { logger.Info("FillTest req: %v", string(bodyBytes)) } imageIds := make([]int64, 0) videoIds := make([]int64, 0) for _, v := range list { imageIds = append(imageIds, v.GetImageIds()...) videoIds = append(videoIds, v.GetVideoIds()...) } videoMap, err := getVideoMapByIds(ctx, videoIds) if err != nil { logger.Error("mediafiller component getVideoMapByIds failed : %v", err) return err } // 获取视频封面 for _, v := range videoMap { imageIds = append(imageIds, v.CoverId) } imageMap, err := getImageMapByIds(ctx, imageIds) if err != nil { logger.Error("mediafiller component getImageMapByIds failed : %v", err) return err } for _, v := range list { images := make([]*dbstruct.ToCImage, 0) videos := make([]*dbstruct.ToCVideo, 0) for _, imageId := range v.GetImageIds() { if image, ok := imageMap[imageId]; ok { images = append(images, transToCImage(image)) } } for _, videoId := range v.GetVideoIds() { if video, ok := videoMap[videoId]; ok { if TestMidMap[baseReq.Mid] { videos = append(videos, transToCVideoTest(video, imageMap[video.CoverId])) } else { videos = append(videos, transToCVideo(video, imageMap[video.CoverId])) } } } v.SetImages(images) v.SetVideos(videos) } return nil } func FillListInternal(ctx *gin.Context, list []MediaFillable) error { imageIds := make([]int64, 0) videoIds := make([]int64, 0) for _, v := range list { imageIds = append(imageIds, v.GetImageIds()...) videoIds = append(videoIds, v.GetVideoIds()...) } videoMap, err := getVideoMapByIds(ctx, videoIds) if err != nil { logger.Error("mediafiller component getVideoMapByIds failed : %v", err) return err } // 获取视频封面 for _, v := range videoMap { imageIds = append(imageIds, v.CoverId) } imageMap, err := getImageMapByIds(ctx, imageIds) if err != nil { logger.Error("mediafiller component getImageMapByIds failed : %v", err) return err } for _, v := range list { images := make([]*dbstruct.ToCImage, 0) videos := make([]*dbstruct.ToCVideo, 0) for _, imageId := range v.GetImageIds() { if image, ok := imageMap[imageId]; ok { images = append(images, transToCImageInternal(image)) } } for _, videoId := range v.GetVideoIds() { if video, ok := videoMap[videoId]; ok { videos = append(videos, transToCVideoInternal(video, imageMap[video.CoverId])) } } v.SetImages(images) v.SetVideos(videos) } return nil } func GetCoverIds(ctx *gin.Context, list []MediaFillable) ([]int64, error) { coverIds := make([]int64, 0) videoIds := make([]int64, 0) for _, v := range list { videoIds = append(videoIds, v.GetVideoIds()...) } videoMap, err := getVideoMapByIds(ctx, videoIds) if err != nil { logger.Error("mediafiller component getVideoMapByIds failed : %v", err) return make([]int64, 0), err } // 获取视频封面 for _, v := range videoMap { coverIds = append(coverIds, v.CoverId) } return coverIds, nil }