Module: CarrierWaveDirect::Uploader

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

Defined Under Namespace

Modules: Configuration, ContentType

Constant Summary collapse

FILENAME_WILDCARD =
"${filename}"

Instance Method Summary collapse

Methods included from ContentType

#content_type, #content_types

Instance Method Details

#aclObject



34
35
36
# File 'lib/carrierwave_direct/uploader.rb', line 34

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

#algorithmObject



46
47
48
# File 'lib/carrierwave_direct/uploader.rb', line 46

def algorithm
  signing_policy.algorithm
end

#clear_policy!Object



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

def clear_policy!
  signing_policy.clear!
end

#credentialObject



50
51
52
# File 'lib/carrierwave_direct/uploader.rb', line 50

def credential
  signing_policy.credential
end

#dateObject



42
43
44
# File 'lib/carrierwave_direct/uploader.rb', line 42

def date
  signing_policy.date
end

#direct_fog_hash(policy_options = {}) ⇒ Object



133
134
135
# File 'lib/carrierwave_direct/uploader.rb', line 133

def direct_fog_hash(policy_options = {})
  signing_policy.direct_fog_hash(policy_options)
end

#direct_fog_urlObject



129
130
131
# File 'lib/carrierwave_direct/uploader.rb', line 129

def direct_fog_url
  CarrierWave::Storage::Fog::File.new(self, CarrierWave::Storage::Fog.new(self), nil).public_url
end

#extension_regexpObject



103
104
105
106
# File 'lib/carrierwave_direct/uploader.rb', line 103

def extension_regexp
  allowed_file_types = extension_allowlist
  extension_regexp = allowed_file_types.present? && allowed_file_types.any? ?  "(#{allowed_file_types.join("|")})" : "\\w+"
end

#filenameObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/carrierwave_direct/uploader.rb', line 108

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")
    if remote_url
      key_from_file(CarrierWave::SanitizedFile.new(remote_url).filename)
    else
      return
    end
  end

  key_parts = key.split("/")
  filename  = key_parts.pop
  guid      = key_parts.pop

  filename_parts = []
  filename_parts << guid if guid
  filename_parts << filename
  filename_parts.join("/")
end

#has_key?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/carrierwave_direct/uploader.rb', line 95

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

#keyObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/carrierwave_direct/uploader.rb', line 78

def key
  return @key if @key.present?
  if present?
    identifier = model.send("#{mounted_as}_identifier")
    self.key = [store_dir, identifier].join("/")
  else
    guid = SecureRandom.uuid
    @key = [store_dir, guid, FILENAME_WILDCARD].join("/")
  end
  @key
end

#key=(k) ⇒ Object



90
91
92
93
# File 'lib/carrierwave_direct/uploader.rb', line 90

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

#key_regexpObject



99
100
101
# File 'lib/carrierwave_direct/uploader.rb', line 99

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

#persisted?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/carrierwave_direct/uploader.rb', line 66

def persisted?
  false
end

#policy(options = {}, &block) ⇒ Object



38
39
40
# File 'lib/carrierwave_direct/uploader.rb', line 38

def policy(options = {}, &block)
  signing_policy.policy(options, &block)
end

#regionObject

ensure that region returns something. Since sig v4 it is required in the signing key & credentials



30
31
32
# File 'lib/carrierwave_direct/uploader.rb', line 30

def region
  defined?(super) ? super : "us-east-1"
end

#signatureObject



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

def signature
  signing_policy.signature
end

#signing_policy_classObject



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

def signing_policy_class
  @signing_policy_class ||= Policies::Aws4HmacSha256
end

#signing_policy_class=(signing_policy_class) ⇒ Object



74
75
76
# File 'lib/carrierwave_direct/uploader.rb', line 74

def signing_policy_class=(signing_policy_class)
  @signing_policy_class = signing_policy_class
end

#url_scheme_white_listObject



62
63
64
# File 'lib/carrierwave_direct/uploader.rb', line 62

def url_scheme_white_list
  nil
end