Module: EchoUploads::Model

Defined in:
lib/echo_uploads/model.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/echo_uploads/model.rb', line 7

def self.included(base)
  base.class_eval do
    class_attribute :echo_uploads_config
    
    include ::EchoUploads::Validation
    include ::EchoUploads::PermFileSaving
    include ::EchoUploads::TempFileSaving
    
    extend ClassMethods
  end
end

Instance Method Details

#echo_uploads_dataObject



19
20
21
22
23
24
25
26
27
# File 'lib/echo_uploads/model.rb', line 19

def echo_uploads_data
  Base64.encode64(JSON.dump(self.class.echo_uploads_config.inject({}) do |hash, (attr, cfg)|
    meta = send("#{attr}_tmp_metadata")
    if meta
      hash[attr] = {'id' => meta.id, 'key' => meta.key}
    end
    hash
  end)).strip
end

#echo_uploads_data=(data) ⇒ Object

Pass in a hash that’s been encoded as JSON and then Base64.



30
31
32
33
34
35
36
37
38
# File 'lib/echo_uploads/model.rb', line 30

def echo_uploads_data=(data)
  parsed = JSON.parse Base64.decode64(data)
  parsed.each do |attr, attr_data|
    # Must verify that the metadata record is temporary. If not, an attacker could
    # pass the ID of a permanent record and change its owner.
    meta = ::EchoUploads::File.where(key: attr_data['key'], temporary: true).find(attr_data['id'])
    send("#{attr}_tmp_metadata=", meta)
  end
end

#map_metadata(attr) ⇒ Object

Helper method used internally Echo Uploads.



41
42
43
44
# File 'lib/echo_uploads/model.rb', line 41

def (attr)
  meta = send("#{attr}_metadata")
  meta ? yield(meta) : nil
end