service/library/mediafiller/mediafiller.go

187 lines
4.7 KiB
Go
Raw Normal View History

2023-12-21 22:17:40 +08:00
package mediafiller
import (
2024-03-08 15:49:32 +08:00
"math/rand"
2023-12-21 22:17:40 +08:00
"service/dbstruct"
"service/library/logger"
2024-03-08 15:49:32 +08:00
"time"
2023-12-21 22:17:40 +08:00
"github.com/gin-gonic/gin"
)
2024-03-08 15:49:32 +08:00
func init() {
rand.Seed(time.Now().Unix())
}
2023-12-21 22:17:40 +08:00
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
}
2024-03-08 15:49:32 +08:00
func (p *MediaFiller) GetFileServerDomain() string {
2024-05-10 14:33:13 +08:00
return "https://filecdnhw01.tiefen.fun/"
//if rand.Intn(100) < 80 {
// return "https://filecdnhw01.tiefen.fun/"
//}
//return "https://filecdn01.tiefen.fun/"
2024-05-06 17:36:16 +08:00
//return defaultMediaFiller.fileServerDomainName
2024-03-08 15:49:32 +08:00
}
2024-05-15 10:16:57 +08:00
func (p *MediaFiller) GetInternalFileServerDomain() string {
return "https://wishpal-ironfan-media.oss-cn-hangzhou.aliyuncs.com/"
}
2023-12-21 22:17:40 +08:00
func FillEntity(ctx *gin.Context, entity MediaFillable) error {
videoIds := entity.GetVideoIds()
2023-12-30 12:43:58 +08:00
videoMap, err := getVideoMapByIds(ctx, videoIds)
2023-12-21 22:17:40 +08:00
if err != nil {
2023-12-30 12:43:58 +08:00
logger.Error("mediafiller component getVideoMapByIds failed : %v", err)
2023-12-21 22:17:40 +08:00
return err
}
2023-12-30 12:43:58 +08:00
imageIds := entity.GetImageIds()
// 获取视频封面
for _, v := range videoMap {
imageIds = append(imageIds, v.CoverId)
}
imageMap, err := getImageMapByIds(ctx, imageIds)
2023-12-21 22:17:40 +08:00
if err != nil {
2023-12-30 12:43:58 +08:00
logger.Error("mediafiller component getImageMapByIds failed : %v", err)
2023-12-21 22:17:40 +08:00
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 {
2023-12-30 12:43:58 +08:00
videos = append(videos, transToCVideo(video, imageMap[video.CoverId]))
2023-12-21 22:17:40 +08:00
}
}
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()...)
}
2023-12-30 12:43:58 +08:00
videoMap, err := getVideoMapByIds(ctx, videoIds)
2023-12-21 22:17:40 +08:00
if err != nil {
2023-12-30 12:43:58 +08:00
logger.Error("mediafiller component getVideoMapByIds failed : %v", err)
2023-12-21 22:17:40 +08:00
return err
}
2023-12-30 12:43:58 +08:00
// 获取视频封面
for _, v := range videoMap {
imageIds = append(imageIds, v.CoverId)
}
imageMap, err := getImageMapByIds(ctx, imageIds)
2023-12-21 22:17:40 +08:00
if err != nil {
2023-12-30 12:43:58 +08:00
logger.Error("mediafiller component getImageMapByIds failed : %v", err)
2023-12-21 22:17:40 +08:00
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 {
2023-12-30 12:43:58 +08:00
videos = append(videos, transToCVideo(video, imageMap[video.CoverId]))
2023-12-21 22:17:40 +08:00
}
}
v.SetImages(images)
v.SetVideos(videos)
}
return nil
}
2024-05-15 10:16:57 +08:00
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
}