Module: CarrierWaveDirect::Uploader

Extended by:
ActiveSupport::Concern
Includes:
ContentType, DirectUrl
Defined in:
lib/carrierwave_direct.rb,
lib/carrierwave_direct/uploader.rb,
lib/carrierwave_direct/uploader/direct_url.rb,
lib/carrierwave_direct/uploader/content_type.rb,
lib/carrierwave_direct/uploader/configuration.rb

Defined Under Namespace

Modules: Configuration, ContentType, DirectUrl

Constant Summary collapse

FILENAME_WILDCARD =
"${filename}"

Instance Method Summary collapse

Methods included from DirectUrl

#direct_fog_url

Methods included from ContentType

#content_type, #content_types

Instance Method Details

#aclObject



28
29
30
# File 'lib/carrierwave_direct/uploader.rb', line 28

def acl
  fog_public ? 'public-read' : 'private'
end

#clear_policy!Object



40
41
42
# File 'lib/carrierwave_direct/uploader.rb', line 40

def clear_policy!
  @policy = nil
end

#extension_regexpObject



88
89
90
91
# File 'lib/carrierwave_direct/uploader.rb', line 88

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

#filenameObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/carrierwave_direct/uploader.rb', line 93

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(CarrierWave::SanitizedFile.new(remote_url).filename) : return
  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

#guidObject



76
77
78
# File 'lib/carrierwave_direct/uploader.rb', line 76

def guid
  UUIDTools::UUID.random_create
end

#has_key?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/carrierwave_direct/uploader.rb', line 80

def has_key?
  key !~ /#{Regexp.escape(FILENAME_WILDCARD)}\z/
end

#keyObject



61
62
63
64
65
66
67
68
69
# File 'lib/carrierwave_direct/uploader.rb', line 61

def key
  return @key if @key.present?
  if present?
    self.key = decoded_key # explicitly set key
  else
    @key = "#{store_dir}/#{guid}/#{FILENAME_WILDCARD}"
  end
  @key
end

#key=(k) ⇒ Object



71
72
73
74
# File 'lib/carrierwave_direct/uploader.rb', line 71

def key=(k)
  @key = k
  update_version_keys(:with => @key)
end

#key_regexpObject



84
85
86
# File 'lib/carrierwave_direct/uploader.rb', line 84

def key_regexp
  /\A(#{store_dir}|#{cache_dir})\/[a-f\d\-]+\/.+\.(?i)#{extension_regexp}(?-i)\z/
end

#persisted?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/carrierwave_direct/uploader.rb', line 57

def persisted?
  false
end

#policy(options = {}) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/carrierwave_direct/uploader.rb', line 32

def policy(options = {})
  options[:expiration] ||= upload_expiration
  options[:min_file_size] ||= min_file_size
  options[:max_file_size] ||= max_file_size

  @policy ||= generate_policy(options)
end

#signatureObject



44
45
46
47
48
49
50
51
# File 'lib/carrierwave_direct/uploader.rb', line 44

def signature
  Base64.encode64(
    OpenSSL::HMAC.digest(
      OpenSSL::Digest.new('sha1'),
      aws_secret_access_key, policy
    )
  ).gsub("\n","")
end

#url_scheme_white_listObject



53
54
55
# File 'lib/carrierwave_direct/uploader.rb', line 53

def url_scheme_white_list
  nil
end