lib/rmega/nodes/node.rb in rmega-0.2.0 vs lib/rmega/nodes/node.rb in rmega-0.2.1

- old
+ new

@@ -6,11 +6,14 @@ include Traversable include Crypto attr_reader :data, :session - delegate :request, :shared_keys, :rsa_privk, :master_key, :storage, :to => :session + # Delegate to :session + [:request, :shared_keys, :rsa_privk, :master_key, :storage].each do |name| + __send__(:define_method, name) { |*args| session.__send__(name, *args) } + end TYPES = {0 => :file, 1 => :folder, 2 => :root, 3 => :inbox, 4 => :trash} def initialize(session, data) @session = session @@ -23,10 +26,34 @@ def public_handle @public_handle ||= request(a: 'l', n: handle) end + def serialize_attributes(hash) + str = "MEGA" + str << hash.to_json + str << ("\x00" * (16 - (str.size % 16))) + return str + end + + def rename(new_name) + node_key = NodeKey.load(decrypted_file_key) + + _attr = serialize_attributes(attributes.merge("n" => new_name)) + _attr = aes_cbc_encrypt(node_key.aes_key, _attr) + _attr = Utils.base64urlencode(_attr) + + resp = request(:a => "a", :attr => _attr, :key => Utils.base64urlencode(node_key.aes_key), :n => handle) + + if resp != 0 + raise("Rename failed") + else + @data['a'] = _attr + return self + end + end + def handle data['h'] end def parent_handle @@ -120,10 +147,13 @@ return if !encrypted or encrypted.empty? node_key = NodeKey.load(decrypted_file_key) encrypted = Utils.base64urldecode(encrypted) encrypted.strip! if encrypted.size % 16 != 0 # Fix possible errors json = aes_cbc_decrypt(node_key.aes_key, encrypted) - JSON.parse json.gsub(/^MEGA/, '').rstrip + # Remove invalid bytes at the end of the string + json.strip! + json.gsub!(/^MEGA\{(.+)\}.*/, '{\1}') + return JSON.parse(json) end def type TYPES[data['t']] end