Class: SelfSDK::Chat::FileObject

Inherits:
Object
  • Object
show all
Defined in:
lib/chat/file_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, url) ⇒ FileObject

Returns a new instance of FileObject.



11
12
13
14
# File 'lib/chat/file_object.rb', line 11

def initialize(token, url)
  @token = token
  @url = url
end

Instance Attribute Details

#ciphertextObject

Returns the value of attribute ciphertext.



9
10
11
# File 'lib/chat/file_object.rb', line 9

def ciphertext
  @ciphertext
end

#contentObject

Returns the value of attribute content.



9
10
11
# File 'lib/chat/file_object.rb', line 9

def content
  @content
end

#keyObject

Returns the value of attribute key.



9
10
11
# File 'lib/chat/file_object.rb', line 9

def key
  @key
end

Returns the value of attribute link.



9
10
11
# File 'lib/chat/file_object.rb', line 9

def link
  @link
end

#mimeObject

Returns the value of attribute mime.



9
10
11
# File 'lib/chat/file_object.rb', line 9

def mime
  @mime
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/chat/file_object.rb', line 9

def name
  @name
end

#nonceObject

Returns the value of attribute nonce.



9
10
11
# File 'lib/chat/file_object.rb', line 9

def nonce
  @nonce
end

Instance Method Details

#build_from_data(name, data, mime, opts = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/chat/file_object.rb', line 16

def build_from_data(name, data, mime, opts = {})
  @key = SelfCrypto::Util.aead_xchacha20poly1305_ietf_keygen
  @nonce = SelfCrypto::Util.aead_xchacha20poly1305_ietf_nonce
  @content = data
  @name = name
  @mime = mime

  # encrypt the object
  @ciphertext = SelfCrypto::Util.aead_xchacha20poly1305_ietf_encrypt(@key, @nonce, @content)

  # Upload
  remote_object = upload(ciphertext)
  public_url = opts[:public_url] || @url
  @link = "#{public_url}/v1/objects/#{remote_object["id"]}"
  @expires = remote_object["expires"]

  self
end

#build_from_object(input) ⇒ Object

Incoming objects



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/chat/file_object.rb', line 36

def build_from_object(input)
  # Download from CDN
  ciphertext = ""
  link = input[:link]
  5.times do
    begin
      ciphertext = URI.open(link, "Authorization" => "Bearer #{@token}").read
      break
    rescue => e
      SelfSDK.logger.info "error fetching #{input[:link]} : #{e.message}"
      link = link.sub("localhost:8080", "api:8080")
      sleep 1
    end
  end

  if ciphertext.empty?
    SelfSDK.logger.warn "unable to process incoming object"
    return
  end

  @content = ciphertext
  @key = nil
  @nonce = nil
  if input.key?(:key) && !input[:key].empty?
    # Decrypt
    composed_key = extract_key(input[:key])
    @key =   composed_key[:key]
    @nonce = composed_key[:nonce]

    @content = SelfCrypto::Util.aead_xchacha20poly1305_ietf_decrypt(@key, @nonce, ciphertext)
  end

  @name =  input[:name]
  @link =  input[:link]
  @mime =  input[:mime]
  @expires = input[:expires]

  self
end

#save(path) ⇒ Object



86
87
88
# File 'lib/chat/file_object.rb', line 86

def save(path)
  File.open(path, 'wb') { |file| file.write(@content) }
end

#to_payloadObject



76
77
78
79
80
81
82
83
84
# File 'lib/chat/file_object.rb', line 76

def to_payload
  {
    name: @name,
    link: @link,
    key: build_key(@key, @nonce),
    mime: @mime,
    expires: @expires
  }
end