Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API uploads images via URL
Browse files Browse the repository at this point in the history
This enhancement give the images API the ability to create images from URLs.  A
simple check determines whether or not the attachment parameter is a URL.  If it
is a URL, OpenURI is used to download the image and it subsequently undergoes
the standard post upload processing (via paperclip).  If it is not a URL, then
the attachment parameter is handled in the same manner it was prior to this
commit.
calebhaye committed Mar 31, 2020
1 parent 68cad80 commit 579794a
Showing 2 changed files with 39 additions and 14 deletions.
13 changes: 12 additions & 1 deletion api/app/controllers/spree/api/images_controller.rb
Original file line number Diff line number Diff line change
@@ -15,7 +15,11 @@ def show

def create
authorize! :create, Image
@image = scope.images.create(image_params)
if valid_url? image_params[:attachment]
@image = scope.images.create(attachment: open(image_params[:attachment]))
else
@image = scope.images.create(image_params)
end
respond_with(@image, status: 201, default_template: :show)
end

@@ -44,6 +48,13 @@ def scope
Spree::Variant.find(params[:variant_id])
end
end

def valid_url?(url)
uri = URI.parse url
uri.is_a?(URI::HTTP) && !uri.host.blank?
rescue URI::InvalidURIError
false
end
end
end
end
40 changes: 27 additions & 13 deletions api/spec/requests/spree/api/images_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -17,20 +17,34 @@ module Spree

context "as an admin" do
sign_in_as_admin!
context "can upload a new image"
it "for a variant" do
expect do
post spree.api_product_images_path(product.id), params: {
image: {
attachment: upload_image('thinking-cat.jpg'),
viewable_type: 'Spree::Variant',
viewable_id: product.master.to_param
},
}
expect(response.status).to eq(201)
expect(json_response).to have_attributes(attributes)
end.to change(Image, :count).by(1)
end

it "can upload a new image for a variant" do
expect do
post spree.api_product_images_path(product.id), params: {
image: {
attachment: upload_image('thinking-cat.jpg'),
viewable_type: 'Spree::Variant',
viewable_id: product.master.to_param
},
}
expect(response.status).to eq(201)
expect(json_response).to have_attributes(attributes)
end.to change(Image, :count).by(1)
end
it "from a valid URL" do
expect do
post spree.api_product_images_path(product.id), params: {
image: {
attachment: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png',
viewable_type: 'Spree::Variant',
viewable_id: product.master.to_param
},
}
expect(response.status).to eq(201)
expect(json_response).to have_attributes(attributes)
end.to change(Image, :count).by(1)
end

context "working with an existing product image" do
let!(:product_image) { product.master.images.create!(attachment: image('thinking-cat.jpg')) }

0 comments on commit 579794a

Please sign in to comment.