Skip to content

Commit

Permalink
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).  After
processing the image, the other attributes (besides :attachment) are updated on
the resultant Spree::Image.

If it is not a URL: the attachment parameter is handled in the same manner
it was prior to this commit.

API documentation added.

• Avoid unnecessary host check in valid_url?
• Use static image from solidus repo, to avoid external dependency
  • Loading branch information
calebhaye committed Apr 15, 2020
1 parent 68cad80 commit 3415f2b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
14 changes: 13 additions & 1 deletion api/app/controllers/spree/api/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ def show

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

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

def valid_url?(url)
uri = URI.parse url
uri.is_a? URI::HTTP
rescue URI::InvalidURIError
false
end
end
end
end
1 change: 1 addition & 0 deletions api/openapi/api.oas2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5885,6 +5885,7 @@ definitions:
type: string
attachment:
type: string
description: 'This field can be used to pass an image URL. When used in this manner, the image undergoes the standard post upload processing via paperclip.'
position:
type: integer
viewable_type:
Expand Down
16 changes: 16 additions & 0 deletions api/spec/requests/spree/api/images_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ module Spree
end.to change(Image, :count).by(1)
end

it 'can upload a new image from a valid URL' do
expect do
post spree.api_product_images_path(product.id), params: {
image: {
attachment: 'https://github.com/solidusio/brand/raw/1827e7afb7ebcf5a1fc9cf7bf6cf9d277183ef11/PNG/solidus-logo-dark.png',
viewable_type: 'Spree::Variant',
viewable_id: product.master.to_param,
alt: 'just a test'
},
}
expect(response.status).to eq(201)
expect(json_response).to have_attributes(attributes)
expect(json_response[:alt]).to eq('just a test')
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')) }

Expand Down

0 comments on commit 3415f2b

Please sign in to comment.