Last active
November 26, 2019 09:30
-
-
Save TorvaldsDB/2cabb645e8676e6513eee16b8d1e2b30 to your computer and use it in GitHub Desktop.
How to validate whether an image url is valid or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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