Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix 403s when trying to download external images (#13282)

Damian Legawieca588009249cc
diff --git a/core/app/jobs/spree/images/save_from_url_job.rb b/core/app/jobs/spree/images/save_from_url_job.rb
index 3c472dc7ee..f29ea5a018 100644
--- a/core/app/jobs/spree/images/save_from_url_job.rb
+++ b/core/app/jobs/spree/images/save_from_url_job.rb
@@ -1,10 +1,11 @@
require 'open-uri'
+require 'openssl'
module Spree
module Images
class SaveFromUrlJob < ::Spree::BaseJob
queue_as Spree.queues.images
- retry_on ActiveRecord::RecordInvalid, OpenURI::HTTPError, wait: :polynomially_longer, attempts: 2
+ retry_on ActiveRecord::RecordInvalid, OpenURI::HTTPError, wait: :polynomially_longer, attempts: Spree::Config.images_save_from_url_job_attempts.to_i
discard_on URI::InvalidURIError
def perform(viewable_id, viewable_type, external_url, external_id = nil, position = nil)
@@ -33,7 +34,17 @@ module Spree
raise URI::InvalidURIError, "Invalid URL scheme: #{uri.scheme}. Only http and https are allowed."
end
- file = uri.open
+ file = uri.open(
+ 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
+ 'Accept' => 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
+ 'Accept-Language' => 'en-US,en;q=0.9',
+ 'Accept-Encoding' => 'gzip, deflate, br',
+ 'Cache-Control' => 'no-cache',
+ 'Pragma' => 'no-cache',
+ read_timeout: 60,
+ ssl_verify_mode: OpenSSL::SSL::VERIFY_PEER,
+ redirect: true
+ )
filename = File.basename(uri.path)
image.attachment.attach(io: file, filename: filename)
diff --git a/core/lib/spree/core/configuration.rb b/core/lib/spree/core/configuration.rb
index c70718f2a7..c010c467f1 100644
--- a/core/lib/spree/core/configuration.rb
+++ b/core/lib/spree/core/configuration.rb
@@ -45,6 +45,7 @@ module Spree
preference :expedited_exchanges, :boolean, default: false # NOTE this requires payment profiles to be supported on your gateway of choice as well as a delayed job handler to be configured with activejob. kicks off an exchange shipment upon return authorization save. charge customer if they do not return items within timely manner.
preference :expedited_exchanges_days_window, :integer, default: 14 # the amount of days the customer has to return their item after the expedited exchange is shipped in order to avoid being charged
preference :geocode_addresses, :boolean, default: true
+ preference :images_save_from_url_job_attempts, :integer, default: 5
preference :layout, :string, deprecated: 'Please use Spree::Frontend::Config[:layout] instead'
preference :logo, :string, deprecated: true
preference :mailer_logo, :string, deprecated: true