service/library/mediafiller/mediafiller.go

187 lines
4.7 KiB
Go

package mediafiller
import (
"math/rand"
"service/dbstruct"
"service/library/logger"
"time"
"github.com/gin-gonic/gin"
)
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 "https://filecdnhw01.tiefen.fun/"
//if rand.Intn(100) < 80 {
// return "https://filecdnhw01.tiefen.fun/"
//}
return "https://filecdn01.tiefen.fun/"
//return defaultMediaFiller.fileServerDomainName
}
func (p *MediaFiller) GetInternalFileServerDomain() string {
return "https://wishpal-ironfan-media.oss-cn-hangzhou.aliyuncs.com/"
}
func FillEntity(ctx *gin.Context, entity MediaFillable) error {
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 {
videos = append(videos, transToCVideo(video, imageMap[video.CoverId]))
}
}
entity.SetImages(images)
entity.SetVideos(videos)
return nil
}
func FillList(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, transToCImage(image))
}
}
for _, videoId := range v.GetVideoIds() {
if video, ok := videoMap[videoId]; ok {
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
}