Skip to content

Instantly share code, notes, and snippets.

@TorvaldsDB
Last active November 26, 2019 09:30
Show Gist options
  • Save TorvaldsDB/2cabb645e8676e6513eee16b8d1e2b30 to your computer and use it in GitHub Desktop.
Save TorvaldsDB/2cabb645e8676e6513eee16b8d1e2b30 to your computer and use it in GitHub Desktop.
How to validate whether an image url is valid or not
module ImgageValidate
module_function
def url_exist?(url)
uri = URI.parse(url)
Net::HTTP.start(uri.host, uri.port) do |http|
Net::HTTP.start(uri.host, uri.port) do |http|
response = http.head(uri.path)
case response
when Net::HTTPSuccess, Net::HTTPRedirection
case response.content_type
when "image/png", "image/gif", "image/jpeg", "image/jpg"
true
else
false
end
else
false
end
end
end
end
end
ImgageValidate.url_exist?("http://s3.amazonaws.com/podknife-production/uploads/podcast/uploaded_cover_img/462/thumb_512-1.jpg")
# => false
https://github.com/carrierwaveuploader/carrierwave/wiki/how-to:-validate-the-url-for-remote-uploads
Or
MiniMagick::Image.open("http://s3.amazonaws.com/podknife-production/uploads/podcast/uploaded_cover_img/462/thumb_512-1.jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment