Permalink
Browse files

Updates modules to respect ActiveSupport::Concern::InstanceMethods de…

…precation.
  • Loading branch information...
1 parent 08382ff commit 16cb140c2763d0eef787a4a9736482e81345cefc Rob Yurkowski committed Jan 30, 2012
Showing with 90 additions and 94 deletions.
  1. +90 −94 lib/carrierwave_direct/uploader.rb
@@ -19,117 +19,113 @@ module Uploader
end
end
- module InstanceMethods
- def direct_fog_url(options = {})
- fog_uri = CarrierWave::Storage::Fog::File.new(self, CarrierWave::Storage::Fog.new(self), nil).public_url
- if options[:with_path]
- uri = URI.parse(fog_uri)
- path = "/#{key}"
- uri.path = path
- fog_uri = uri.to_s
- end
- fog_uri
- end
-
- def guid
- UUID.generate
- end
+ def direct_fog_url(options = {})
+ fog_uri = CarrierWave::Storage::Fog::File.new(self, CarrierWave::Storage::Fog.new(self), nil).public_url
+ if options[:with_path]
+ uri = URI.parse(fog_uri)
+ path = "/#{key}"
+ uri.path = path
+ fog_uri = uri.to_s
+ end
+ fog_uri
+ end
- def key=(k)
- @key = k
- update_version_keys(:with => @key)
- end
+ def guid
+ UUID.generate
+ end
- def key
- @key ||= "#{store_dir}/#{guid}/#{FILENAME_WILDCARD}"
- end
+ def key=(k)
+ @key = k
+ update_version_keys(:with => @key)
+ end
- def has_key?
- @key.present? && !(@key =~ /#{Regexp.escape(FILENAME_WILDCARD)}\z/)
- end
+ def key
+ @key ||= "#{store_dir}/#{guid}/#{FILENAME_WILDCARD}"
+ end
- def acl
- s3_access_policy.to_s.gsub('_', '-')
- end
+ def has_key?
+ @key.present? && !(@key =~ /#{Regexp.escape(FILENAME_WILDCARD)}\z/)
+ end
- def policy(options = {})
- options[:expiration] ||= self.class.upload_expiration
- options[:max_file_size] ||= self.class.max_file_size
-
- Base64.encode64(
- {
- 'expiration' => Time.now.utc + options[:expiration],
- 'conditions' => [
- ["starts-with", "$utf8", ""],
- ["starts-with", "$key", store_dir],
- {"bucket" => fog_directory},
- {"acl" => acl},
- {"success_action_redirect" => success_action_redirect},
- ["content-length-range", 1, options[:max_file_size]]
- ]
- }.to_json
- ).gsub("\n","")
- end
+ def acl
+ s3_access_policy.to_s.gsub('_', '-')
+ end
- def signature
- Base64.encode64(
- OpenSSL::HMAC.digest(
- OpenSSL::Digest::Digest.new('sha1'),
- aws_secret_access_key, policy
- )
- ).gsub("\n","")
- end
+ def policy(options = {})
+ options[:expiration] ||= self.class.upload_expiration
+ options[:max_file_size] ||= self.class.max_file_size
+
+ Base64.encode64(
+ {
+ 'expiration' => Time.now.utc + options[:expiration],
+ 'conditions' => [
+ ["starts-with", "$utf8", ""],
+ ["starts-with", "$key", store_dir],
+ {"bucket" => fog_directory},
+ {"acl" => acl},
+ {"success_action_redirect" => success_action_redirect},
+ ["content-length-range", 1, options[:max_file_size]]
+ ]
+ }.to_json
+ ).gsub("\n","")
+ end
- def persisted?
- false
- end
+ def signature
+ Base64.encode64(
+ OpenSSL::HMAC.digest(
+ OpenSSL::Digest::Digest.new('sha1'),
+ aws_secret_access_key, policy
+ )
+ ).gsub("\n","")
+ end
- def filename
- unless has_key?
- # Use the attached models remote url to generate a new key otherwise return nil
- remote_url = model.send("remote_#{mounted_as}_url")
- remote_url ? key_from_file(remote_url.split("/").pop) : return
- end
+ def persisted?
+ false
+ end
- key_path = key.split("/")
- filename_parts = []
- filename_parts.unshift(key_path.pop)
- unique_key = key_path.pop
- filename_parts.unshift(unique_key) if unique_key
- filename_parts.join("/")
+ def filename
+ unless has_key?
+ # Use the attached models remote url to generate a new key otherwise return nil
+ remote_url = model.send("remote_#{mounted_as}_url")
+ remote_url ? key_from_file(remote_url.split("/").pop) : return
end
- def key_regexp
- /\A#{store_dir}\/[a-f\d\-]+\/.+\.#{extension_regexp}\z/
- end
+ key_path = key.split("/")
+ filename_parts = []
+ filename_parts.unshift(key_path.pop)
+ unique_key = key_path.pop
+ filename_parts.unshift(unique_key) if unique_key
+ filename_parts.join("/")
+ end
- def extension_regexp
- allowed_file_types = extension_white_list
- extension_regexp = allowed_file_types.present? && allowed_file_types.any? ? "(#{allowed_file_types.join("|")})" : "\\w+"
- end
+ def key_regexp
+ /\A#{store_dir}\/[a-f\d\-]+\/.+\.#{extension_regexp}\z/
+ end
- def url_scheme_white_list
- nil
- end
+ def extension_regexp
+ allowed_file_types = extension_white_list
+ extension_regexp = allowed_file_types.present? && allowed_file_types.any? ? "(#{allowed_file_types.join("|")})" : "\\w+"
+ end
- private
+ def url_scheme_white_list
+ nil
+ end
- def key_from_file(fname)
- new_key_parts = key.split("/")
- new_key_parts.pop
- new_key_parts << fname
- self.key = new_key_parts.join("/")
- end
+ private
- # Update the versions to use this key
- def update_version_keys(options)
- versions.each do |name, uploader|
- uploader.key = options[:with]
- end
- end
- end # InstanceMethods
+ def key_from_file(fname)
+ new_key_parts = key.split("/")
+ new_key_parts.pop
+ new_key_parts << fname
+ self.key = new_key_parts.join("/")
+ end
- private
+ # Update the versions to use this key
+ def update_version_keys(options)
+ versions.each do |name, uploader|
+ uploader.key = options[:with]
+ end
+ end
# Put the version name at the end of the filename since the guid is also stored
# e.g. guid/filename_thumb.jpg instead of CarrierWave's default: thumb_guid/filename.jpg

0 comments on commit 16cb140

Please sign in to comment.