Last active
February 2, 2022 13:24
-
-
Save cgarvis/8925326 to your computer and use it in GitHub Desktop.
Image Proxy in Rails
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
require 'base64' | |
require 'net/http' | |
class ImageProxyController < ActionController::Base | |
def get | |
url = URI.parse(Base64.decode64(params[:url])) | |
image = Net::HTTP.get_response(url) | |
send_data image.body, type: image.content_type, disposition: 'inline' | |
end | |
end |
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
require 'base64' | |
module ImageProxyHelper | |
def proxy_image_tag(source, options = nil | |
options[:src] = "/image_proxy/#{Base64.encode64(source)}" | |
tag("img", options) | |
end | |
end |
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
require 'net/http' | |
class ProfileController < ActionController::Base | |
def image | |
if params['id'] == 'john' | |
url = URI.parse('https://pbs.twimg.com/profile_images/1979976243/potato_300.jpg') | |
elsif params['id'] == 'cgarvis' | |
url = URI.parse('https://pbs.twimg.com/profile_images/1809634620/twitter-profile.png') | |
end | |
unless url == nil | |
image = Net::HTTP.get_response(url) | |
send_data image.body, type: image.content_type, disposition: 'inline' | |
else | |
send_file 'default_image_path.jpg' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment