Skip to content

Commit 7703c5b

Browse files
committed
feat: add logo upload 4 organizations
1 parent cc9e1dd commit 7703c5b

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

app/controllers/api/v1/organizations_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ def upload_logo
3939

4040
service = S3UploadService.new
4141
result = service.upload(file, prefix: "orgs/#{@organization.id}/logo")
42-
logo_url = service.signed_url(result[:key], expires_in: 365 * 24 * 3600)
42+
logo_url = service.public_url(result[:key])
4343

44-
# Store the key so we can regenerate the URL later; expose via signed URL now
4544
@organization.update!(logo_url: logo_url)
4645

4746
render json: {

app/services/s3_upload_service.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,30 @@ def upload(file, prefix: 'support')
5656
# Generate a pre-signed GET URL for a stored object
5757
#
5858
# @param key [String] the S3 object key
59-
# @param expires_in [Integer] expiry in seconds
59+
# @param expires_in [Integer] expiry in seconds (max 604800 for AWS S3 Signature V4)
6060
# @return [String] signed URL
6161
def signed_url(key, expires_in: SIGNED_URL_EXPIRY)
62+
# AWS S3 Signature V4 caps at 7 days; clamp to be safe
63+
capped = [expires_in, 604_800].min
6264
signer = Aws::S3::Presigner.new(client: @client)
63-
signer.presigned_url(:get_object, bucket: @bucket, key: key, expires_in: expires_in)
65+
signer.presigned_url(:get_object, bucket: @bucket, key: key, expires_in: capped)
6466
rescue StandardError => e
6567
Rails.logger.error("[S3UploadService] Failed to generate signed URL for #{key}: #{e.message}")
6668
nil
6769
end
6870

71+
# Build a permanent public URL for the object (requires the bucket to allow public access).
72+
# Supabase format: {project_base}/storage/v1/object/public/{bucket}/{key}
73+
#
74+
# @param key [String] the S3 object key
75+
# @return [String] public URL
76+
def public_url(key)
77+
# Strip the S3 path suffix to get the project base URL
78+
# SUPABASE_S3_ENDPOINT = https://xxx.storage.supabase.co/storage/v1/s3
79+
base = ENV.fetch('SUPABASE_S3_ENDPOINT').sub(%r{/s3\z}, '')
80+
"#{base}/object/public/#{@bucket}/#{key}"
81+
end
82+
6983
private
7084

7185
def validate!(file)

0 commit comments

Comments
 (0)