From 94a815a9ef53f940f171fdb3af6cbf706b73d562 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Thu, 22 Oct 2020 18:16:47 +0100 Subject: [PATCH 1/4] Upgrade aws-sdk and set s3 host name using the provided url config. This will enable us to set hostnames with the s3 region defined and that will make our image upload work for all s3 regions. --- Gemfile | 2 +- Gemfile.lock | 10 +++++----- app/models/spree/image.rb | 5 +++++ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Gemfile b/Gemfile index f5e229439e..944b9b3ed1 100644 --- a/Gemfile +++ b/Gemfile @@ -58,7 +58,7 @@ gem 'kaminari', '~> 0.14.1' gem 'andand' gem 'angularjs-rails', '1.5.5' -gem 'aws-sdk', '1.11.1' # temporarily locked down due to https://github.com/aws/aws-sdk-ruby/issues/273 +gem 'aws-sdk', '1.67.0' gem 'bugsnag' gem 'db2fog' gem 'haml' diff --git a/Gemfile.lock b/Gemfile.lock index 456bb05829..0a132c0247 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -117,10 +117,11 @@ GEM awesome_nested_set (3.0.3) activerecord (>= 4.0.0, < 5) awesome_print (1.8.0) - aws-sdk (1.11.1) + aws-sdk (1.67.0) + aws-sdk-v1 (= 1.67.0) + aws-sdk-v1 (1.67.0) json (~> 1.4) - nokogiri (>= 1.4.4) - uuidtools (~> 2.1) + nokogiri (~> 1) bcrypt (3.1.13) bugsnag (6.18.0) concurrent-ruby (~> 1.0) @@ -648,7 +649,6 @@ GEM unicorn-worker-killer (0.4.4) get_process_mem (~> 0) unicorn (>= 4, < 6) - uuidtools (2.1.5) warden (1.2.7) rack (>= 1.0) webdrivers (4.2.0) @@ -688,7 +688,7 @@ DEPENDENCIES atomic awesome_nested_set (~> 3.0.0.rc.1) awesome_print - aws-sdk (= 1.11.1) + aws-sdk (= 1.67.0) bugsnag byebug (~> 11.0.0) cancan (~> 1.6.10) diff --git a/app/models/spree/image.rb b/app/models/spree/image.rb index 8af54e443b..a8ea22038a 100644 --- a/app/models/spree/image.rb +++ b/app/models/spree/image.rb @@ -61,6 +61,11 @@ module Spree set_attachment_attributes(:s3_headers, ActiveSupport::JSON.decode(Spree::Config[:s3_headers])) set_attachment_attributes(:bucket, Spree::Config[:s3_bucket]) + + # When using S3, we set the URL on property s3_host_name + set_attachment_attributes(:s3_host_name, attachment_definitions[:attachment][:url]) + set_attachment_attributes(:url, ":s3_domain_url") + # :s3_alias_url else attachment_definitions[:attachment].delete :storage end From e879aa1bed035a9bf847079b503e154723b23e2f Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Mon, 26 Oct 2020 20:27:42 +0000 Subject: [PATCH 2/4] Switch to s3_alias_url that enabble virtual host type of path --- app/models/spree/image.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/models/spree/image.rb b/app/models/spree/image.rb index a8ea22038a..57597fe378 100644 --- a/app/models/spree/image.rb +++ b/app/models/spree/image.rb @@ -62,10 +62,9 @@ module Spree ActiveSupport::JSON.decode(Spree::Config[:s3_headers])) set_attachment_attributes(:bucket, Spree::Config[:s3_bucket]) - # When using S3, we set the URL on property s3_host_name - set_attachment_attributes(:s3_host_name, attachment_definitions[:attachment][:url]) - set_attachment_attributes(:url, ":s3_domain_url") - # :s3_alias_url + # We use :s3_alias_url (virtual host url style) and set the URL on property s3_host_alias + set_attachment_attributes(:s3_host_alias, attachment_definitions[:attachment][:url]) + set_attachment_attributes(:url, ":s3_alias_url") else attachment_definitions[:attachment].delete :storage end From 88ac5dc9554f99be0d27c57f5dad50441999e988 Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Thu, 29 Oct 2020 17:44:50 +0000 Subject: [PATCH 3/4] Fix rubocop issues by refactoring code --- app/models/spree/image.rb | 30 ++++++++++++++++++------------ config/initializers/spree.rb | 2 +- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/app/models/spree/image.rb b/app/models/spree/image.rb index 57597fe378..24d20aeb93 100644 --- a/app/models/spree/image.rb +++ b/app/models/spree/image.rb @@ -50,30 +50,36 @@ module Spree false end - def self.set_attachment_attributes(attribute_name, attribute_value) + def self.set_attachment_attribute(attribute_name, attribute_value) attachment_definitions[:attachment][attribute_name] = attribute_value end - def self.set_s3_attachment_definitions + def self.set_storage_attachment_attributes if Spree::Config[:use_s3] - set_attachment_attributes(:storage, :s3) - set_attachment_attributes(:s3_credentials, s3_credentials) - set_attachment_attributes(:s3_headers, - ActiveSupport::JSON.decode(Spree::Config[:s3_headers])) - set_attachment_attributes(:bucket, Spree::Config[:s3_bucket]) - - # We use :s3_alias_url (virtual host url style) and set the URL on property s3_host_alias - set_attachment_attributes(:s3_host_alias, attachment_definitions[:attachment][:url]) - set_attachment_attributes(:url, ":s3_alias_url") + set_s3_attachment_attributes else - attachment_definitions[:attachment].delete :storage + attachment_definitions[:attachment].delete(:storage) end end + def self.set_s3_attachment_attributes + set_attachment_attribute(:storage, :s3) + set_attachment_attribute(:s3_credentials, s3_credentials) + set_attachment_attribute(:s3_headers, + ActiveSupport::JSON.decode(Spree::Config[:s3_headers])) + set_attachment_attribute(:bucket, Spree::Config[:s3_bucket]) + + # We use :s3_alias_url (virtual host url style) and set the URL on property s3_host_alias + set_attachment_attribute(:s3_host_alias, attachment_definitions[:attachment][:url]) + set_attachment_attribute(:url, ":s3_alias_url") + end + private_class_method :set_s3_attachment_attributes + def self.s3_credentials { access_key_id: Spree::Config[:s3_access_key], secret_access_key: Spree::Config[:s3_secret], bucket: Spree::Config[:s3_bucket] } end + private_class_method :s3_credentials end end diff --git a/config/initializers/spree.rb b/config/initializers/spree.rb index c91497b27a..78a1e56bc8 100644 --- a/config/initializers/spree.rb +++ b/config/initializers/spree.rb @@ -39,7 +39,7 @@ end # Attachments settings Spree::Image.set_attachment_attributes(:path, ENV['ATTACHMENT_PATH']) if ENV['ATTACHMENT_PATH'] Spree::Image.set_attachment_attributes(:url, ENV['ATTACHMENT_URL']) if ENV['ATTACHMENT_URL'] -Spree::Image.set_s3_attachment_definitions +Spree::Image.set_storage_attachment_attributes # Spree 2.0 recommends explicitly setting this here when using spree_auth_devise Spree.user_class = 'Spree::User' From a802fe29be176583fcec2bc50ff2a9ff51049c5f Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 10 Nov 2020 00:07:06 +0000 Subject: [PATCH 4/4] Fix typo in initializer --- config/initializers/spree.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/initializers/spree.rb b/config/initializers/spree.rb index 78a1e56bc8..56f2e3325c 100644 --- a/config/initializers/spree.rb +++ b/config/initializers/spree.rb @@ -37,8 +37,8 @@ Spree.config do |config| end # Attachments settings -Spree::Image.set_attachment_attributes(:path, ENV['ATTACHMENT_PATH']) if ENV['ATTACHMENT_PATH'] -Spree::Image.set_attachment_attributes(:url, ENV['ATTACHMENT_URL']) if ENV['ATTACHMENT_URL'] +Spree::Image.set_attachment_attribute(:path, ENV['ATTACHMENT_PATH']) if ENV['ATTACHMENT_PATH'] +Spree::Image.set_attachment_attribute(:url, ENV['ATTACHMENT_URL']) if ENV['ATTACHMENT_URL'] Spree::Image.set_storage_attachment_attributes # Spree 2.0 recommends explicitly setting this here when using spree_auth_devise