Skip to content

Commit

Permalink
Make imgix optional, and return GIFMedia for gifs
Browse files Browse the repository at this point in the history
  • Loading branch information
radazen committed Oct 24, 2024
1 parent 76aeefe commit e9496e6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
29 changes: 5 additions & 24 deletions graphql/resolver/schema.resolvers.helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
22 changes: 22 additions & 0 deletions service/mediamapper/mediamapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const contextKey = "mediamapper.instance"

const assetDomain = "assets.gallery.so"

const imgixEnabled = false

const (
thumbnailWidth = 64
smallWidth = 204
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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{}))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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": "<url-to-purge>" }, "type": "purges" } }'
body := map[string]interface{}{
"data": map[string]interface{}{
Expand Down

0 comments on commit e9496e6

Please sign in to comment.