lib/rmega/storage.rb in rmega-0.1.7 vs lib/rmega/storage.rb in rmega-0.2.0
- old
+ new
@@ -1,10 +1,10 @@
-require 'rmega/nodes/factory'
-
module Rmega
class Storage
+ include NotInspectable
include Loggable
+ include Crypto
attr_reader :session
delegate :master_key, :shared_keys, to: :session
@@ -18,50 +18,54 @@
def total_space
quota['mstrg']
end
+ def stats
+ stats_hash = {files: 0, size: 0}
+
+ nodes.each do |n|
+ next unless n.type == :file
+ stats_hash[:files] += 1
+ stats_hash[:size] += n.filesize
+ end
+
+ return stats_hash
+ end
+
def quota
session.request(a: 'uq', strg: 1)
end
+ def nodes=(list)
+ @nodes = list
+ end
+
def nodes
+ return @nodes if @nodes
+
results = session.request(a: 'f', c: 1)
- store_shared_keys(results['ok'] || [])
+ (results['ok'] || []).each do |hash|
+ shared_keys[hash['h']] ||= aes_ecb_decrypt(master_key, Utils.base64urldecode(hash['k']))
+ end
(results['f'] || []).map do |node_data|
node = Nodes::Factory.build(session, node_data)
node.process_shared_key if node.shared_root?
node
end
end
- def folders
- list = nodes
- root_handle = list.find { |node| node.type == :root }.handle
- list.select do |node|
- node.shared_root? || (node.type == :folder && node.parent_handle == root_handle)
- end
+ def shared
+ nodes.select { |node| node.shared_root? }
end
def trash
@trash ||= nodes.find { |n| n.type == :trash }
end
def root
@root ||= nodes.find { |n| n.type == :root }
- end
-
- def download(public_url, path)
- Nodes::Factory.build_from_url(session, public_url).download(path)
- end
-
- private
-
- def store_shared_keys(list)
- list.each do |hash|
- shared_keys[hash['h']] = Crypto.decrypt_key(master_key, Utils.base64_to_a32(hash['k']))
- end
end
end
end