Module: CarrierWaveDirect::Uploader

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

Defined Under Namespace

Modules: Configuration

Constant Summary collapse

FILENAME_WILDCARD =
"${filename}"

Instance Method Summary collapse

Instance Method Details

#aclObject



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

def acl
  s3_access_policy.to_s.gsub('_', '-')
end

#direct_fog_url(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/carrierwave_direct/uploader.rb', line 22

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 = URI.escape(path)
    fog_uri = uri.to_s
  end
  fog_uri
end

#extension_regexpObject



105
106
107
108
# File 'lib/carrierwave_direct/uploader.rb', line 105

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/carrierwave_direct/uploader.rb', line 86

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

  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



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

def guid
  UUID.generate
end

#has_key?Boolean

Returns:

  • (Boolean)


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

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

#keyObject



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

def key
  @key ||= "#{store_dir}/#{guid}/#{FILENAME_WILDCARD}"
end

#key=(k) ⇒ Object



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

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

#key_regexpObject



101
102
103
# File 'lib/carrierwave_direct/uploader.rb', line 101

def key_regexp
  /\A#{store_dir}\/[a-f\d\-]+\/.+\.#{extension_regexp}\z/
end

#persisted?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/carrierwave_direct/uploader.rb', line 82

def persisted?
  false
end

#policy(options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/carrierwave_direct/uploader.rb', line 54

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

#signatureObject



73
74
75
76
77
78
79
80
# File 'lib/carrierwave_direct/uploader.rb', line 73

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

#url_scheme_white_listObject



110
111
112
# File 'lib/carrierwave_direct/uploader.rb', line 110

def url_scheme_white_list
  nil
end