From 93048a1a4b89405a9a5e510c2828d332a4f41f1c Mon Sep 17 00:00:00 2001 From: lwl0608 Date: Fri, 8 Mar 2024 15:49:32 +0800 Subject: [PATCH] cdn test --- library/mediafiller/mediafiller.go | 14 ++++++++++++++ library/mediafiller/mediafillerhelper.go | 6 +++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/library/mediafiller/mediafiller.go b/library/mediafiller/mediafiller.go index b79b7958..4cd34f71 100644 --- a/library/mediafiller/mediafiller.go +++ b/library/mediafiller/mediafiller.go @@ -1,12 +1,18 @@ 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) @@ -34,6 +40,14 @@ func SetFileServerDomainName(fileServerDomainName string) { defaultMediaFiller.fileServerDomainName = fileServerDomainName } +func (p *MediaFiller) GetFileServerDomain() string { + // cdn测试 + if rand.Intn(100) < 20 { + return "https://filecdn01.tiefen.fun/" + } + return defaultMediaFiller.fileServerDomainName +} + func FillEntity(ctx *gin.Context, entity MediaFillable) error { videoIds := entity.GetVideoIds() videoMap, err := getVideoMapByIds(ctx, videoIds) diff --git a/library/mediafiller/mediafillerhelper.go b/library/mediafiller/mediafillerhelper.go index 6650e905..d43f1d43 100644 --- a/library/mediafiller/mediafillerhelper.go +++ b/library/mediafiller/mediafillerhelper.go @@ -55,7 +55,7 @@ func transToCImage(image *dbstruct.Image) *dbstruct.ToCImage { W: image.W, H: image.H, Fmt: image.Fmt, - Urls: []string{defaultMediaFiller.fileServerDomainName + imgSrcId}, + Urls: []string{defaultMediaFiller.GetFileServerDomain() + imgSrcId}, } } @@ -68,14 +68,14 @@ func transToCVideo(video *dbstruct.Video, coverImg *dbstruct.Image) *dbstruct.To Id: video.Id, Dur: video.Dur, CoverUrls: []string{}, - Urls: []string{defaultMediaFiller.fileServerDomainName + video.SrcId}, + Urls: []string{defaultMediaFiller.GetFileServerDomain() + video.SrcId}, } if coverImg != nil { ret.CoverW = coverImg.W ret.CoverH = coverImg.H ret.CoverFmt = coverImg.Fmt imgSrcId := coverImg.SelectMinSizeOssId() - ret.CoverUrls = []string{defaultMediaFiller.fileServerDomainName + imgSrcId} + ret.CoverUrls = []string{defaultMediaFiller.GetFileServerDomain() + imgSrcId} } return ret }