lib/paperclip/storage/fog.rb in kt-paperclip-4.4.0 vs lib/paperclip/storage/fog.rb in kt-paperclip-5.4.0

- old
+ new

@@ -46,21 +46,21 @@ e.message << " (You may need to install the fog gem)" raise e end unless defined?(Fog) base.instance_eval do - unless @options[:url].to_s.match(/\A:fog.*url\Z/) + unless @options[:url].to_s.match(/\A:fog.*url\z/) @options[:path] = @options[:path].gsub(/:url/, @options[:url]).gsub(/\A:rails_root\/public\/system\//, '') @options[:url] = ':fog_public_url' end Paperclip.interpolates(:fog_public_url) do |attachment, style| attachment.public_url(style) end unless Paperclip::Interpolations.respond_to? :fog_public_url end end - AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX = /\A(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}\Z))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]\Z/ + AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX = /\A(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}\z))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]\z/ def exists?(style = default_style) if original_filename !!directory.files.head(path(style)) else @@ -84,15 +84,18 @@ end end end def fog_public(style = default_style) - if @options.has_key?(:fog_public) - if @options[:fog_public].respond_to?(:has_key?) && @options[:fog_public].has_key?(style) - @options[:fog_public][style] + if @options.key?(:fog_public) + value = @options[:fog_public] + if value.respond_to?(:key?) && value.key?(style) + value[style] + elsif value.respond_to?(:call) + value.call(self) else - @options[:fog_public] + value end else true end end @@ -169,21 +172,22 @@ def copy_to_local_file(style, local_dest_path) log("copying #{path(style)} to local file #{local_dest_path}") ::File.open(local_dest_path, 'wb') do |local_file| file = directory.files.get(path(style)) + return false unless file local_file.write(file.body) end rescue ::Fog::Errors::Error => e warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}") false end private def convert_time(time) - if time.is_a?(Fixnum) + if time.is_a?(Integer) time = Time.now + time end time end @@ -194,14 +198,14 @@ (@options[:fog_host] =~ /%d/) ? @options[:fog_host] % (path(style).hash % 4) : @options[:fog_host] end end def host_name_for_directory - if @options[:fog_directory].to_s =~ Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX - "#{@options[:fog_directory]}.s3.amazonaws.com" + if directory_name.to_s =~ Fog::AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX + "#{directory_name}.s3.amazonaws.com" else - "s3.amazonaws.com/#{@options[:fog_directory]}" + "s3.amazonaws.com/#{directory_name}" end end def find_credentials(creds) case creds @@ -223,16 +227,18 @@ def connection @connection ||= ::Fog::Storage.new(fog_credentials) end def directory - dir = if @options[:fog_directory].respond_to?(:call) + @directory ||= connection.directories.new(key: directory_name) + end + + def directory_name + if @options[:fog_directory].respond_to?(:call) @options[:fog_directory].call(self) else @options[:fog_directory] end - - @directory ||= connection.directories.new(:key => dir) end def scheme @scheme ||= fog_credentials[:scheme] || 'https' end