lib/rmega/nodes/node.rb in rmega-0.0.6 vs lib/rmega/nodes/node.rb in rmega-0.1.0

- old
+ new

@@ -1,122 +1,80 @@ +require 'rmega/loggable' +require 'rmega/utils' +require 'rmega/crypto/crypto' +require 'rmega/nodes/traversable' + module Rmega - class Node - attr_reader :data, :session + module Nodes + class Node + include Loggable + include Traversable - def initialize session, data - @session = session + attr_reader :data, :session - if self.class.mega_url?(data) - @data = self.class.public_data(session, data) - @public_url = data - else + delegate :storage, :request, :to => :session + + def initialize(session, data) + @session = session @data = data end - end - def self.fabricate session, data - type_name = mega_url?(data) ? :file : type_by_number(data['t']) - node_class = Rmega.const_get("#{type_name}_node".camelize) rescue nil - node_class ||= Rmega::Node - node_class.new session, data - end + 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 - def self.types - {file: 0, folder: 1, root: 2, inbox: 3, trash: 4} - end + def public_url=(url) + @public_url = url + end - def self.type_by_number number - founded_type = types.find { |k, v| number == v } - founded_type.first if founded_type - end + def public_handle + @public_handle ||= request(a: 'l', n: handle) + end - def self.mega_url? url - !!(url.to_s =~ /^https:\/\/mega\.co\.nz\/#!.*$/i) - end + def handle + data['h'] + end - def logger - Rmega.logger - end + def parent_handle + data['p'] + end - # Member actions + def owner_key + data['k'].split(':').first + end - def public_url - return @public_url if @public_url - return nil if type != :file - b64_dec_key = Utils.a32_to_base64 decrypted_file_key[0..7] - "https://mega.co.nz/#!#{public_handle}!#{b64_dec_key}" - end + def name + return attributes['n'] if attributes + end - def trash - trash_node_public_handle = storage.trash_node.public_handle - request a: 'm', n: handle, t: trash_node_public_handle - end + def file_key + data['k'].split(':').last + end + def decrypted_file_key + if data['k'] + Crypto.decrypt_key session.master_key, Utils.base64_to_a32(file_key) + else + Utils.base64_to_a32 public_url.split('!').last + end + end - # Delegate to session - - delegate :storage, :request, :to => :session - - - # Other methods - - def self.public_data session, public_url - public_handle, key = public_url.strip.split('!')[1, 2] - session.request a: 'g', g: 1, p: public_handle - end - - def public_handle - @public_handle ||= request(a: 'l', n: handle) - end - - def handle - data['h'] - end - - def parent_handle - data['p'] - end - - def filesize - data['s'] - end - - def owner_key - data['k'].split(':').first - end - - def name - return attributes['n'] if attributes - end - - def file_key - data['k'].split(':').last - end - - def decrypted_file_key - if data['k'] - Crypto.decrypt_key session.master_key, Utils.base64_to_a32(file_key) - else - Utils.base64_to_a32 public_url.split('!').last + def can_decrypt_attributes? + !data['u'] or data['u'] == owner_key end - end - def can_decrypt_attributes? - !data['u'] or data['u'] == owner_key - end - - def attributes - @attributes ||= begin - return nil unless can_decrypt_attributes? - Crypto.decrypt_attributes decrypted_file_key, (data['a'] || data['at']) + def attributes + @attributes ||= begin + return nil unless can_decrypt_attributes? + Crypto.decrypt_attributes decrypted_file_key, (data['a'] || data['at']) + end end - end - def type - self.class.type_by_number data['t'] - end - - def delete - request a: 'd', n: handle + def type + Factory.type(data['t']) + end end end end