Created
August 30, 2011 13:05
-
-
Save cgallagher/1180829 to your computer and use it in GitHub Desktop.
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.
This file contains hidden or 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
#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