Skip to content

Commit

Permalink
fix: make image transformation feature optional (#2967)
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge authored Dec 11, 2024
1 parent 8cac0be commit f1b57bc
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
8 changes: 5 additions & 3 deletions internal/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ func run(p utils.Program, ctx context.Context, fsys afero.Fs, excludedContainers

var started []string
var isStorageEnabled = utils.Config.Storage.Enabled && !isContainerExcluded(utils.Config.Storage.Image, excluded)
var isImgProxyEnabled = utils.Config.Storage.ImageTransformation != nil &&
utils.Config.Storage.ImageTransformation.Enabled && !isContainerExcluded(utils.Config.Storage.ImgProxyImage, excluded)
p.Send(utils.StatusMsg("Starting containers..."))

// Start Logflare
Expand Down Expand Up @@ -833,7 +835,7 @@ EOF
// TODO: https://github.com/supabase/storage-api/issues/55
"STORAGE_S3_REGION=" + utils.Config.Storage.S3Credentials.Region,
"GLOBAL_S3_BUCKET=stub",
fmt.Sprintf("ENABLE_IMAGE_TRANSFORMATION=%t", utils.Config.Storage.ImageTransformation.Enabled),
fmt.Sprintf("ENABLE_IMAGE_TRANSFORMATION=%t", isImgProxyEnabled),
fmt.Sprintf("IMGPROXY_URL=http://%s:5001", utils.ImgProxyId),
"TUS_URL_PATH=/storage/v1/upload/resumable",
"S3_PROTOCOL_ACCESS_KEY_ID=" + utils.Config.Storage.S3Credentials.AccessKeyId,
Expand Down Expand Up @@ -872,11 +874,11 @@ EOF
}

// Start Storage ImgProxy.
if isStorageEnabled && utils.Config.Storage.ImageTransformation.Enabled && !isContainerExcluded(utils.Config.Storage.ImageTransformation.Image, excluded) {
if isStorageEnabled && isImgProxyEnabled {
if _, err := utils.DockerStart(
ctx,
container.Config{
Image: utils.Config.Storage.ImageTransformation.Image,
Image: utils.Config.Storage.ImgProxyImage,
Env: []string{
"IMGPROXY_BIND=:5001",
"IMGPROXY_LOCAL_FILESYSTEM_ROOT=/",
Expand Down
2 changes: 1 addition & 1 deletion internal/start/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestDatabaseStart(t *testing.T) {
utils.StorageId = "test-storage"
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.Storage.Image), utils.StorageId)
utils.ImgProxyId = "test-imgproxy"
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.Storage.ImageTransformation.Image), utils.ImgProxyId)
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.Storage.ImgProxyImage), utils.ImgProxyId)
utils.EdgeRuntimeId = "test-edge-runtime"
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.EdgeRuntime.Image), utils.EdgeRuntimeId)
utils.PgmetaId = "test-pgmeta"
Expand Down
6 changes: 2 additions & 4 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,15 +300,13 @@ func NewConfig(editors ...ConfigEditor) config {
SecretKeyBase: "EAx3IQ/wRG1v47ZD4NE4/9RzBI8Jmil3x0yhcW4V2NHBP6c2iPIzwjofi2Ep4HIG",
},
Storage: storage{
Image: storageImage,
Image: storageImage,
ImgProxyImage: imageProxyImage,
S3Credentials: storageS3Credentials{
AccessKeyId: "625729a08b95bf1b7ff351a663f3a23c",
SecretAccessKey: "850181e4652dd023b7a98c58ae0d2d34bd487ee0cc3254aed6eda37307425907",
Region: "local",
},
ImageTransformation: imageTransformation{
Image: imageProxyImage,
},
},
Auth: auth{
Image: gotrueImage,
Expand Down
25 changes: 18 additions & 7 deletions pkg/config/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ type (
storage struct {
Enabled bool `toml:"enabled"`
Image string `toml:"-"`
ImgProxyImage string `toml:"-"`
FileSizeLimit sizeInBytes `toml:"file_size_limit"`
ImageTransformation *imageTransformation `toml:"image_transformation"`
S3Credentials storageS3Credentials `toml:"-"`
ImageTransformation imageTransformation `toml:"image_transformation"`
Buckets BucketConfig `toml:"buckets"`
}

imageTransformation struct {
Enabled bool `toml:"enabled"`
Image string `toml:"-"`
Enabled bool `toml:"enabled"`
}

storageS3Credentials struct {
Expand All @@ -38,15 +38,26 @@ type (
)

func (s *storage) ToUpdateStorageConfigBody() v1API.UpdateStorageConfigBody {
body := v1API.UpdateStorageConfigBody{Features: &v1API.StorageFeatures{}}
body.FileSizeLimit = cast.Ptr(int64(s.FileSizeLimit))
body.Features.ImageTransformation.Enabled = s.ImageTransformation.Enabled
body := v1API.UpdateStorageConfigBody{
FileSizeLimit: cast.Ptr(int64(s.FileSizeLimit)),
}
// When local config is not set, we assume platform defaults should not change
if s.ImageTransformation != nil {
body.Features = &v1API.StorageFeatures{
ImageTransformation: v1API.StorageFeatureImageTransformation{
Enabled: s.ImageTransformation.Enabled,
},
}
}
return body
}

func (s *storage) FromRemoteStorageConfig(remoteConfig v1API.StorageConfigResponse) {
s.FileSizeLimit = sizeInBytes(remoteConfig.FileSizeLimit)
s.ImageTransformation.Enabled = remoteConfig.Features.ImageTransformation.Enabled
// When local config is not set, we assume platform defaults should not change
if s.ImageTransformation != nil {
s.ImageTransformation.Enabled = remoteConfig.Features.ImageTransformation.Enabled
}
}

func (s *storage) DiffWithRemote(remoteConfig v1API.StorageConfigResponse) ([]byte, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/templates/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ enabled = true
file_size_limit = "50MiB"

# Image transformation API is available to Supabase Pro plan.
[storage.image_transformation]
enabled = false
# [storage.image_transformation]
# enabled = true

# Uncomment to configure local storage buckets
# [storage.buckets.images]
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func TestUpdateRemoteConfig(t *testing.T) {
Storage: storage{
Enabled: true,
FileSizeLimit: 100,
ImageTransformation: imageTransformation{
ImageTransformation: &imageTransformation{
Enabled: true,
},
},
Expand Down

0 comments on commit f1b57bc

Please sign in to comment.