Class: SelfSDK::Chat::FileObject
- Inherits:
-
Object
- Object
- SelfSDK::Chat::FileObject
- Defined in:
- lib/chat/file_object.rb
Instance Attribute Summary collapse
-
#ciphertext ⇒ Object
Returns the value of attribute ciphertext.
-
#content ⇒ Object
Returns the value of attribute content.
-
#key ⇒ Object
Returns the value of attribute key.
-
#link ⇒ Object
Returns the value of attribute link.
-
#mime ⇒ Object
Returns the value of attribute mime.
-
#name ⇒ Object
Returns the value of attribute name.
-
#nonce ⇒ Object
Returns the value of attribute nonce.
Instance Method Summary collapse
- #build_from_data(name, data, mime, opts = {}) ⇒ Object
-
#build_from_object(input) ⇒ Object
Incoming objects.
-
#initialize(token, url) ⇒ FileObject
constructor
A new instance of FileObject.
- #save(path) ⇒ Object
- #to_payload ⇒ Object
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
#ciphertext ⇒ Object
Returns the value of attribute ciphertext.
9 10 11 |
# File 'lib/chat/file_object.rb', line 9 def ciphertext @ciphertext end |
#content ⇒ Object
Returns the value of attribute content.
9 10 11 |
# File 'lib/chat/file_object.rb', line 9 def content @content end |
#key ⇒ Object
Returns the value of attribute key.
9 10 11 |
# File 'lib/chat/file_object.rb', line 9 def key @key end |
#link ⇒ Object
Returns the value of attribute link.
9 10 11 |
# File 'lib/chat/file_object.rb', line 9 def link @link end |
#mime ⇒ Object
Returns the value of attribute mime.
9 10 11 |
# File 'lib/chat/file_object.rb', line 9 def mime @mime end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/chat/file_object.rb', line 9 def name @name end |
#nonce ⇒ Object
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.}" 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
88 89 90 |
# File 'lib/chat/file_object.rb', line 88 def save(path) File.open(path, 'wb') { |file| file.write(@content) } end |
#to_payload ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/chat/file_object.rb', line 76 def to_payload k = build_key(@key, @nonce) { name: @name, link: @link, key: k, mime: @mime, expires: @expires, public: (k == "") } end |