From e9496e65949b9458c9f6f5e3ec573432066671e2 Mon Sep 17 00:00:00 2001 From: Ezra Smith Date: Thu, 24 Oct 2024 12:49:13 -0400 Subject: [PATCH] Make imgix optional, and return GIFMedia for gifs --- graphql/resolver/schema.resolvers.helpers.go | 29 ++++---------------- service/mediamapper/mediamapper.go | 22 +++++++++++++++ 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/graphql/resolver/schema.resolvers.helpers.go b/graphql/resolver/schema.resolvers.helpers.go index 81470923d..66cd1593a 100644 --- a/graphql/resolver/schema.resolvers.helpers.go +++ b/graphql/resolver/schema.resolvers.helpers.go @@ -2527,34 +2527,15 @@ func getFallbackMedia(ctx context.Context, media persist.FallbackMedia) *model.F } } -// getGIFMedia returns VideoMedia because we convert GIFs to videos. -func getGIFMedia(ctx context.Context, tokenMedia db.TokenMedia, fallbackMedia *model.FallbackMedia, darkMode persist.DarkMode) model.VideoMedia { +func getGIFMedia(ctx context.Context, tokenMedia db.TokenMedia, fallbackMedia *model.FallbackMedia, darkMode persist.DarkMode) model.GIFMedia { url := remapLargeImageUrls(tokenMedia.Media.MediaURL.String()) - options := make([]mediamapper.Option, 2) - options[0] = mediamapper.WithFormatVideo() - - // GIFs support transparency, but MP4s don't, so we need to set a background color for the MP4 - // that will look transparent. - if darkMode == persist.DarkModeEnabled { - options[1] = mediamapper.WithBackgroundColor(darkModeMP4BackgroundColor) - } else { - options[1] = mediamapper.WithBackgroundColor(lightModeMP4BackgroundColor) - } - - mm := mediamapper.For(ctx) - videoUrls := model.VideoURLSet{ - Raw: util.ToPointer(mm.GetLargeImageUrl(url, options...)), - Small: util.ToPointer(mm.GetSmallImageUrl(url, options...)), - Medium: util.ToPointer(mm.GetMediumImageUrl(url, options...)), - Large: util.ToPointer(mm.GetLargeImageUrl(url, options...)), - } - - return model.VideoMedia{ - PreviewURLs: previewURLsFromTokenMedia(ctx, tokenMedia, mediamapper.WithStaticImage()), + return model.GIFMedia{ + PreviewURLs: previewURLsFromTokenMedia(ctx, tokenMedia), + StaticPreviewURLs: previewURLsFromTokenMedia(ctx, tokenMedia, mediamapper.WithStaticImage()), MediaURL: util.ToPointer(tokenMedia.Media.MediaURL.String()), MediaType: (*string)(&tokenMedia.Media.MediaType), - ContentRenderURLs: &videoUrls, + ContentRenderURL: &url, Dimensions: mediaToDimensions(tokenMedia.Media.Dimensions), FallbackMedia: fallbackMedia, } diff --git a/service/mediamapper/mediamapper.go b/service/mediamapper/mediamapper.go index e0ded1d75..eeef6b850 100644 --- a/service/mediamapper/mediamapper.go +++ b/service/mediamapper/mediamapper.go @@ -23,6 +23,8 @@ const contextKey = "mediamapper.instance" const assetDomain = "assets.gallery.so" +const imgixEnabled = false + const ( thumbnailWidth = 64 smallWidth = 204 @@ -104,6 +106,10 @@ func getDefaultParams() []imgix.IxParam { } func (u *MediaMapper) buildPreviewImageUrl(sourceUrl string, width int, params []imgix.IxParam, options ...Option) string { + if !imgixEnabled { + return setGoogleWidthParams(sourceUrl, width) + } + if sourceUrl == "" { return sourceUrl } @@ -115,6 +121,10 @@ func (u *MediaMapper) buildPreviewImageUrl(sourceUrl string, width int, params [ } func (u *MediaMapper) buildSrcSet(sourceUrl string, params []imgix.IxParam, options ...Option) string { + if !imgixEnabled { + return sourceUrl + } + if sourceUrl == "" { return sourceUrl } @@ -201,6 +211,10 @@ func (u *MediaMapper) GetSrcSet(sourceUrl string, options ...Option) string { } func (u *MediaMapper) GetBlurhash(sourceUrl string) *string { + if !imgixEnabled { + return nil + } + url := u.urlBuilder.CreateURL(sourceUrl, imgix.Param("fm", "blurhash")) req, err := http.NewRequestWithContext(context.Background(), "GET", url, bytes.NewBuffer([]byte{})) @@ -229,6 +243,10 @@ func (u *MediaMapper) GetBlurhash(sourceUrl string) *string { } func (u *MediaMapper) GetAspectRatio(sourceUrl string) *float64 { + if !imgixEnabled { + return nil + } + url := u.urlBuilder.CreateURL(sourceUrl, buildParams(getDefaultParams(), imgix.Param("fm", "json"))...) rawResponse, err := http.Get(url) @@ -264,6 +282,10 @@ func (u *MediaMapper) GetAspectRatio(sourceUrl string) *float64 { } func PurgeImage(ctx context.Context, u string) error { + if !imgixEnabled { + return nil + } + // '{ "data": { "attributes": { "url": "" }, "type": "purges" } }' body := map[string]interface{}{ "data": map[string]interface{}{