Created
April 14, 2009 12:24
-
-
Save maccman/95153 to your computer and use it in GitHub Desktop.
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
| module Paperclip | |
| module Storage | |
| module Better | |
| def self.extended base | |
| require 'aws/s3' | |
| base.instance_eval do | |
| @bucket = @options[:bucket] | |
| @bucket = @bucket.call(self) if @bucket.is_a?(Proc) | |
| end | |
| end | |
| def bucket_name | |
| @bucket | |
| end | |
| def s3_url(style = default_style, options = {}) | |
| AWS::S3::S3Object.url_for(path(style), bucket_name, options) | |
| end | |
| def exists?(style = default_style) | |
| AWS::S3::S3Object.exists?(path(style), bucket_name) | |
| end | |
| # Returns representation of the data of the file assigned to the given | |
| # style, in the format most representative of the current storage. | |
| def to_file(style = default_style) | |
| @queued_for_write[style] || AWS::S3Object.find(path(style), bucket_name) | |
| end | |
| alias_method :to_io, :to_file | |
| def flush_writes #:nodoc: | |
| @queued_for_write.each do |style, file| | |
| logger.info("[paperclip] saving #{path(style)}") | |
| AWS::S3::S3Object.store( | |
| path(style), | |
| file, | |
| bucket_name, | |
| :content_type => instance_read(:content_type) | |
| ) | |
| end | |
| @queued_for_write = {} | |
| end | |
| def flush_deletes #:nodoc: | |
| @queued_for_delete.each do |path| | |
| logger.info("[paperclip] deleting #{path}") | |
| AWS::S3::S3Object.delete(path, bucket_name) | |
| end | |
| @queued_for_delete = [] | |
| end | |
| end # Better | |
| end # Storage | |
| end # Paperclip |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment