mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Merge pull request #8829 from mkllnk/8824-patch-aws-sdk
Fix image uploads by patching aws-sdk
This commit is contained in:
20
config/initializers/aws_sdk.rb
Normal file
20
config/initializers/aws_sdk.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
# frozen_string_literal: false
|
||||
|
||||
if AWS::VERSION == "1.67.0"
|
||||
module AWS
|
||||
module Core
|
||||
module Signers
|
||||
# @api private
|
||||
class S3
|
||||
module URI
|
||||
def self.escape(string)
|
||||
::URI::RFC2396_Parser.new.escape(string)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
Rails.logger.warn "The aws-sdk patch needs updating or removing."
|
||||
end
|
||||
72
spec/models/spree/image_spec.rb
Normal file
72
spec/models/spree/image_spec.rb
Normal file
@@ -0,0 +1,72 @@
|
||||
# frozen_string_literal: false
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
module Spree
|
||||
describe Image do
|
||||
include FileHelper
|
||||
|
||||
let(:file) { Rack::Test::UploadedFile.new(black_logo_file, 'image/png') }
|
||||
let(:product) { create(:product) }
|
||||
|
||||
describe "using local storage" do
|
||||
it "stores a new image" do
|
||||
image = Spree::Image.create!(
|
||||
attachment: file,
|
||||
viewable: product.master,
|
||||
)
|
||||
|
||||
attachment = image.attachment
|
||||
expect(attachment.exists?).to eq true
|
||||
expect(attachment.file?).to eq true
|
||||
expect(attachment.url).to match %r"^/spree/products/[0-9]+/product/logo-black\.png\?[0-9]+$"
|
||||
end
|
||||
end
|
||||
|
||||
describe "using AWS S3" do
|
||||
let(:s3_config) {
|
||||
{
|
||||
url: ":s3_alias_url",
|
||||
storage: :s3,
|
||||
s3_credentials: {
|
||||
access_key_id: "A...A",
|
||||
secret_access_key: "H...H",
|
||||
},
|
||||
s3_headers: { "Cache-Control" => "max-age=31557600" },
|
||||
bucket: "ofn",
|
||||
s3_protocol: "https",
|
||||
s3_host_alias: "ofn.s3.us-east-1.amazonaws.com",
|
||||
|
||||
# This is for easier testing:
|
||||
path: "/:id/:style/:basename.:extension",
|
||||
}
|
||||
}
|
||||
|
||||
before do
|
||||
attachment_definition = Spree::Image.attachment_definitions[:attachment]
|
||||
allow(Spree::Image).to receive(:attachment_definitions).and_return(
|
||||
attachment: attachment_definition.merge(s3_config)
|
||||
)
|
||||
end
|
||||
|
||||
it "saves a new image when none is present" do
|
||||
upload_pattern = %r"^https://ofn.s3.amazonaws.com/[0-9]+/(original|mini|small|product|large)/logo-black.png$"
|
||||
download_pattern = %r"^https://ofn.s3.amazonaws.com/[0-9]+/product/logo-black.png$"
|
||||
public_url_pattern = %r"^https://ofn.s3.us-east-1.amazonaws.com/[0-9]+/product/logo-black.png\?[0-9]+$"
|
||||
|
||||
stub_request(:put, upload_pattern).to_return(status: 200, body: "", headers: {})
|
||||
stub_request(:head, download_pattern).to_return(status: 200, body: "", headers: {})
|
||||
|
||||
image = Spree::Image.create!(
|
||||
attachment: file,
|
||||
viewable: product.master,
|
||||
)
|
||||
|
||||
attachment = image.attachment
|
||||
expect(attachment.exists?).to eq true
|
||||
expect(attachment.file?).to eq true
|
||||
expect(attachment.url).to match public_url_pattern
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user