Last active
November 8, 2022 15:09
-
-
Save mhgbrown/c0e193d8f900753edfb8b1d735f1023a to your computer and use it in GitHub Desktop.
Proxy images through a Rails controller
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
Rails.application.routes.draw do | |
get '/image_proxy/:url', to: 'image_proxy#get' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks. This works great!