Module: Paperclip::Storage::Cloudfiles
- Defined in:
- lib/paperclip/storage/cloudfiles.rb
Overview
Alternative Storage for Paperclip, using Rackspace’s CloudFiles storage service. All files will be saved to a specified CDN-enabled container.
Class Method Summary collapse
Instance Method Summary collapse
- #cf_base_url ⇒ Object
- #cf_connection ⇒ Object
- #cf_container ⇒ Object
- #cf_container_name ⇒ Object
-
#cf_headers ⇒ Object
Headers sent to Cloudfiles on upload.
-
#exists?(style = default_style) ⇒ Boolean
Checks wether this attachment exists in the given style.
-
#flush_deletes ⇒ Object
:nodoc:.
-
#flush_writes ⇒ Object
:nodoc:.
-
#to_file(style = default_style) ⇒ Object
Returns representation of the data of the file assigned to the given style, in the format most representative of the current storage.
Class Method Details
.extended(base) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/paperclip/storage/cloudfiles.rb', line 9 def self.extended base begin require 'cloudfiles' rescue LoadError => e e. << " (You may need to install the rackspace-cloudfiles gem)" raise e end base.instance_eval do @cf_credentials = @options[:cloudfiles][:credentials] @cf_container_name = @options[:cloudfiles][:container] @cf_base_url = @options[:cloudfiles][:base_url] end Paperclip.interpolates(:container_url) do |, style| "#{.cf_base_url}" end end |
Instance Method Details
#cf_base_url ⇒ Object
45 46 47 |
# File 'lib/paperclip/storage/cloudfiles.rb', line 45 def cf_base_url @cf_base_url ||= cf_container.cdn_url end |
#cf_connection ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/paperclip/storage/cloudfiles.rb', line 28 def cf_connection # TODO: Ensure that this works acceptably. A new CloudFiles::Connection and # container instance will be created for each attachment when the corresponding # methods are called. Those could probably be shared for all instances of the # same attachment for the process. @cf_connection ||= CloudFiles::Connection.new(@cf_credentials[:username], @cf_credentials[:api_key]) end |
#cf_container ⇒ Object
37 38 39 |
# File 'lib/paperclip/storage/cloudfiles.rb', line 37 def cf_container @cf_container ||= cf_connection.container(cf_container_name) end |
#cf_container_name ⇒ Object
41 42 43 |
# File 'lib/paperclip/storage/cloudfiles.rb', line 41 def cf_container_name @cf_container_name end |
#cf_headers ⇒ Object
Headers sent to Cloudfiles on upload. Currently used to provide a Content-Type header if one is available.
83 84 85 86 87 88 89 |
# File 'lib/paperclip/storage/cloudfiles.rb', line 83 def cf_headers #:nodoc: result = {} if content_type = instance_read(:content_type) result['Content-Type'] = content_type end result end |
#exists?(style = default_style) ⇒ Boolean
Checks wether this attachment exists in the given style.
50 51 52 53 54 55 56 |
# File 'lib/paperclip/storage/cloudfiles.rb', line 50 def exists?(style = default_style) if original_filename cf_container.object_exists?(path(style)) else false end end |
#flush_deletes ⇒ Object
:nodoc:
91 92 93 94 95 96 97 98 99 |
# File 'lib/paperclip/storage/cloudfiles.rb', line 91 def flush_deletes #:nodoc: @queued_for_delete.each do |path| begin log("deleting from cloudfiles #{path}") cf_container.delete(path) end end @queued_for_delete = [] end |
#flush_writes ⇒ Object
:nodoc:
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/paperclip/storage/cloudfiles.rb', line 68 def flush_writes #:nodoc: @queued_for_write.each do |style, file| begin log("saving to cloudfiles #{path(style)}") obj = cf_container.create_object(path(style)) obj.write(file, cf_headers) rescue StandardError => e raise end end @queued_for_write = {} end |
#to_file(style = default_style) ⇒ Object
Returns representation of the data of the file assigned to the given style, in the format most representative of the current storage.
60 61 62 63 64 65 66 |
# File 'lib/paperclip/storage/cloudfiles.rb', line 60 def to_file style = default_style return @queued_for_write[style] if @queued_for_write[style] file = Tempfile.new(path(style)) file.write(cf_container.object(path(style)).value) file.rewind return file end |