From e54cff2274fad75b0bd7d6bd9e38055ef1fbaf6d Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 3 May 2023 14:38:06 +0100 Subject: [PATCH] Set images to public-read Defining an alternate s3 configuration set to `public: true` means we can use it selectively. It sets the objects to `acl: "public-read"` by default (read-only) and means any image tags for those assets can use direct public links in the src attribute (without hitting the ActiveStorage::Representation endpoint). The default non-public service will still be used by default for any other files on instances using s3. --- app/models/application_record.rb | 4 ++++ app/models/enterprise.rb | 6 +++--- app/models/enterprise_group.rb | 4 ++-- app/models/spree/image.rb | 2 +- config/storage.yml | 8 ++++++++ 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/app/models/application_record.rb b/app/models/application_record.rb index d36b82d6f7..6bfeb85c3a 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -10,4 +10,8 @@ class ApplicationRecord < ActiveRecord::Base include ArelHelpers::JoinAssociation self.abstract_class = true + + def self.image_service + ENV["S3_BUCKET"].present? ? :amazon_public : :local + end end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index e7b81947ae..bd813f287f 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -87,10 +87,10 @@ class Enterprise < ApplicationRecord } accepts_nested_attributes_for :custom_tab - has_one_attached :logo - has_one_attached :promo_image + has_one_attached :logo, service: image_service + has_one_attached :promo_image, service: image_service has_one_attached :terms_and_conditions - has_one_attached :white_label_logo + has_one_attached :white_label_logo, service: image_service validates :logo, processable_image: true, diff --git a/app/models/enterprise_group.rb b/app/models/enterprise_group.rb index 9616c24722..4221776554 100644 --- a/app/models/enterprise_group.rb +++ b/app/models/enterprise_group.rb @@ -25,8 +25,8 @@ class EnterpriseGroup < ApplicationRecord delegate :phone, :address1, :address2, :city, :zipcode, :state, :country, to: :address - has_one_attached :logo - has_one_attached :promo_image + has_one_attached :logo, service: image_service + has_one_attached :promo_image, service: image_service validates :logo, processable_image: true, diff --git a/app/models/spree/image.rb b/app/models/spree/image.rb index e328eea790..8bdaeb65bb 100644 --- a/app/models/spree/image.rb +++ b/app/models/spree/image.rb @@ -9,7 +9,7 @@ module Spree large: { resize_to_limit: [600, 600] }, }.freeze - has_one_attached :attachment + has_one_attached :attachment, service: image_service validates :attachment, attached: true, diff --git a/config/storage.yml b/config/storage.yml index 255c8298d9..271782041f 100644 --- a/config/storage.yml +++ b/config/storage.yml @@ -19,3 +19,11 @@ amazon: secret_access_key: <%= ENV["S3_SECRET"] %> bucket: <%= ENV["S3_BUCKET"] %> region: <%= ENV.fetch("S3_REGION", "us-east-1") %> + +amazon_public: + service: S3 + public: true + access_key_id: <%= ENV["S3_ACCESS_KEY"] %> + secret_access_key: <%= ENV["S3_SECRET"] %> + bucket: <%= ENV["S3_BUCKET"] %> + region: <%= ENV.fetch("S3_REGION", "us-east-1") %>