Skip to content

Instantly share code, notes, and snippets.

@cgallagher
Created August 30, 2011 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgallagher/1180829 to your computer and use it in GitHub Desktop.
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.
#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