Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A piece of code that will help paperclip to detect if the user is browsing your app in HTTPS and serve up S3 Images over the corresponding protocol.
#create this file in app/middleware and then in config/application.rb at the beginning of the application class you need to call it "config.middleware.use "PaperclipS3UrlRewriter"
class PaperclipS3UrlRewriter
def initialize(app)
@app = app
end
def call(env)
status, headers, response = @app.call(env)
if response.is_a?(ActionController::Response) && response.request.protocol == 'https://' && headers["Content-Type"].include?("text/html")
body = response.body.gsub('http://s3.amazonaws.com', 'https://s3.amazonaws.com')
headers["Content-Length"] = body.length.to_s
[status, headers, body]
else
[status, headers, response]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment