Class: Rmega::Nodes::Node

Inherits:
Object
  • Object
show all
Includes:
Loggable, Traversable
Defined in:
lib/rmega/nodes/node.rb

Direct Known Subclasses

File, Folder, Inbox, Root, Trash

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Traversable

#children, #empty?, #files, #folders, #parent

Methods included from Loggable

#logger

Constructor Details

#initialize(session, data) ⇒ Node

Returns a new instance of Node.


16
17
18
19
# File 'lib/rmega/nodes/node.rb', line 16

def initialize(session, data)
  @session = session
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data


12
13
14
# File 'lib/rmega/nodes/node.rb', line 12

def data
  @data
end

#sessionObject (readonly)

Returns the value of attribute session


12
13
14
# File 'lib/rmega/nodes/node.rb', line 12

def session
  @session
end

Instance Method Details

#attributesObject


101
102
103
104
105
# File 'lib/rmega/nodes/node.rb', line 101

def attributes
  encrypted = data['a'] || data['at']
  return if !encrypted or encrypted.empty?
  Crypto.decrypt_attributes(decrypted_file_key, encrypted)
end

#decrypted_file_keyObject


89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rmega/nodes/node.rb', line 89

def decrypted_file_key
  h, shared_key = *process_shared_key

  if shared_key
    Crypto.decrypt_key(shared_key, Utils.base64_to_a32(file_keys[h]))
  elsif file_key
    Crypto.decrypt_key(session.master_key, Utils.base64_to_a32(file_key))
  else
    Utils.base64_to_a32(public_url.split('!').last)
  end
end

#file_keyObject


59
60
61
# File 'lib/rmega/nodes/node.rb', line 59

def file_key
  file_keys.values.first
end

#file_keysObject


48
49
50
51
52
53
54
55
56
57
# File 'lib/rmega/nodes/node.rb', line 48

def file_keys
  return {} unless data['k']

  pairs = data['k'].split('/')
  pairs.inject({}) do |hash, pair|
    h, k = pair.split(':')
    hash[h] = k
    hash
  end
end

#handleObject


36
37
38
# File 'lib/rmega/nodes/node.rb', line 36

def handle
  data['h']
end

#nameObject


44
45
46
# File 'lib/rmega/nodes/node.rb', line 44

def name
  attributes['n'] if attributes
end

#parent_handleObject


40
41
42
# File 'lib/rmega/nodes/node.rb', line 40

def parent_handle
  data['p']
end

#process_shared_keyObject


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rmega/nodes/node.rb', line 67

def process_shared_key
  h = (shared_keys.keys & file_keys.keys).first
  return [h, shared_keys[h]] if h

  sk = data['sk']

  return unless sk

  shared_key = if sk.size > 22
    # Decrypt sk
    sk = Utils.base64_mpi_to_bn(sk)
    sk = Rmega::Crypto::Rsa.decrypt(sk, rsa_privk).to_s(16)
    sk = '0' + sk if sk.length % 2 > 0
    Utils.str_to_a32(Utils.hexstr_to_bstr(sk)[0..15])
  else
    Crypto.decrypt_key session.master_key, Utils.base64_to_a32(data['sk'])
  end

  shared_keys[handle] = shared_key
  [handle, shared_key]
end

#public_handleObject


32
33
34
# File 'lib/rmega/nodes/node.rb', line 32

def public_handle
  @public_handle ||= request(a: 'l', n: handle)
end

#public_urlObject


21
22
23
24
25
26
# File 'lib/rmega/nodes/node.rb', line 21

def public_url
  @public_url ||= begin
    b64_dec_key = Utils.a32_to_base64(decrypted_file_key[0..7])
    "https://mega.co.nz/#!#{public_handle}!#{b64_dec_key}"
  end
end

#public_url=(url) ⇒ Object


28
29
30
# File 'lib/rmega/nodes/node.rb', line 28

def public_url=(url)
  @public_url = url
end

#shared_root?Boolean

Returns:

  • (Boolean)

63
64
65
# File 'lib/rmega/nodes/node.rb', line 63

def shared_root?
  data['su'] && data['sk'] && data['k']
end

#typeObject


107
108
109
# File 'lib/rmega/nodes/node.rb', line 107

def type
  Factory.type(data['t'])
end