Skip to content

Commit

Permalink
Use original images instead of imgix (#1541)
Browse files Browse the repository at this point in the history
* Fix docker compose syntax

* Make imgix optional, and return GIFMedia for gifs

* Update tests
  • Loading branch information
radazen authored Oct 24, 2024
1 parent ae1dc8c commit 839ffb3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -548,30 +548,30 @@ abi-gen:

# Miscellaneous stuff
docker-start-clean: docker-build
docker-compose up -d
docker compose up -d

docker-build: docker-stop
docker-compose build
docker compose build

docker-start: docker-stop
docker-compose up -d
docker compose up -d

docker-stop:
docker-compose down
docker compose down

format-graphql:
yarn install;
yarn prettier --write graphql/schema/schema.graphql
yarn prettier --write graphql/testdata/operations.graphql;

start-local-graphql-gateway:
docker-compose -f docker/graphql-gateway/docker-compose.yml up --build -d graphql-gateway-local
docker compose -f docker/graphql-gateway/docker-compose.yml up --build -d graphql-gateway-local

start-dev-graphql-gateway:
docker-compose -f docker/graphql-gateway/docker-compose.yml up --build -d graphql-gateway-dev
docker compose -f docker/graphql-gateway/docker-compose.yml up --build -d graphql-gateway-dev

start-prod-graphql-gateway:
docker-compose -f docker/graphql-gateway/docker-compose.yml up --build -d graphql-gateway-prod
docker compose -f docker/graphql-gateway/docker-compose.yml up --build -d graphql-gateway-prod

# Listing targets as dependencies doesn't pull in target-specific secrets, so we need to
# invoke $(MAKE) here to read appropriate secrets for each target.
Expand All @@ -580,13 +580,13 @@ start-sql-proxy:
$(MAKE) start-prod-sql-proxy

start-dev-sql-proxy:
docker-compose -f docker/cloud_sql_proxy/docker-compose.yml up -d cloud-sql-proxy-dev
docker compose -f docker/cloud_sql_proxy/docker-compose.yml up -d cloud-sql-proxy-dev

start-prod-sql-proxy:
docker-compose -f docker/cloud_sql_proxy/docker-compose.yml up -d cloud-sql-proxy-prod
docker compose -f docker/cloud_sql_proxy/docker-compose.yml up -d cloud-sql-proxy-prod

stop-sql-proxy:
docker-compose -f docker/cloud_sql_proxy/docker-compose.yml down
docker compose -f docker/cloud_sql_proxy/docker-compose.yml down

migrate-local-coredb:
go run cmd/migrate/main.go
Expand Down
2 changes: 1 addition & 1 deletion graphql/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ func testSyncShouldProcessMedia(t *testing.T) {
response, err := syncTokensMutation(ctx, c, []Chain{ChainEthereum}, nil)

tokens := assertSyncedTokens(t, response, err, 1)
media := waitForSynced[*syncTokensMutationSyncTokensSyncTokensPayloadViewerUserGalleryUserTokensTokenMediaVideoMedia](*tokens[0].Media)
media := waitForSynced[*syncTokensMutationSyncTokensSyncTokensPayloadViewerUserGalleryUserTokensTokenMediaGIFMedia](*tokens[0].Media)
assert.Equal(t, string(persist.MediaTypeGIF), *media.MediaType)
assert.NotEmpty(t, *media.MediaURL)
})
Expand Down
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 839ffb3

Please sign in to comment.