lib/rapidshare-ext/api.rb in rapidshare-ext-0.0.3 vs lib/rapidshare-ext/api.rb in rapidshare-ext-0.0.4

- old
+ new

@@ -245,16 +245,22 @@ # @param [Hash] params # <tt>:force</tt>:: # Invalidate cached tree, default: false # After each call of this method the generated tree will be saved as cache # to avoid unnecessary queries to be performed fpr a future calls + # <tt>:validate</tt>:: + # Validate tree after it has been generated, default: true # <tt>:consistent</tt>:: # Delete all found orphans, default: false + # Ignored if :validate is set to false def folders_hierarchy(params = {}) force_load = params.delete :force from_folder_path = path_trim(params.delete(:from) || '/') remove_orphans = params.delete(:consistent) + perform_validation = params.delete(:validate) + perform_validation = true if perform_validation.nil? + remove_orphans = false unless perform_validation if @tree && !force_load if from_folder_path.empty? return @tree else @@ -282,24 +288,25 @@ end # Kill orphans remove_orphans! if remove_orphans - # Validate folder tree consistency @tree.each_pair do |folder_id, data| - parent_id = data[:parent] - if !parent_id.zero? && @tree[parent_id].nil? + @tree[folder_id][:path] = folder_path folder_id + end - error = "There is no parent folder with id ##{data[:parent]} for the folder \"#{data[:name]}\" [#{folder_id}]" - raise error + if perform_validation + # Validate folder tree consistency + @tree.each_pair do |folder_id, data| + parent_id = data[:parent] + if !parent_id.zero? && @tree[parent_id].nil? + error = "Directory tree consistency error. Parent folder ##{data[:parent]} for the folder \"#{data[:path]}\" [#{folder_id}] could not be found" + raise error + end end end - @tree.each_pair do |folder_id, data| - @tree[folder_id][:path] = folder_path folder_id - end - @tree = slice_tree @tree, :from => from_folder_path unless from_folder_path.empty? @tree end # Build tree relative to a specified folder @@ -356,46 +363,52 @@ # Orphan folder is a folder with non existing parent (yes, it's possible) # # Example: # move_orphans :to => "/" def move_orphans(params = {}) - new_folder = folder_id params[:to].to_s - orphans = detect_gaps.join(',') - if orphans.any? - moverealfolder :realfolder => orphans.join(','), :newparent => new_folder + new_folder = path_trim(params.delete(:to) || '/') + gaps = detect_gaps + + if gaps.any? + params = { + :realfolder => gaps.join(','), + :newparent => new_folder + }.merge params + moverealfolder params end end # Returns gap list between folders # See #gap? for example def detect_gaps - @tree = folders_hierarchy - @tree.keep_if do |folder_id, data| + @tree = folders_hierarchy :validate => false + @tree.dup.keep_if do |folder_id, data| gap? folder_id # This is wrong end.keys end # The name speaks for itself # WARNING!!! All data will be lost!!! # Use it carefully def erase_all_data! - @tree = folders_hierarchy + @tree = folders_hierarchy! :validate => false @tree.keys.each do |folder_id| delrealfolder :realfolder => folder_id end folders_hierarchy! end # Check if folder with given id placed on the bottom of folder hierarchy def root_folder?(folder_id) - @tree = folders_hierarchy + @tree = folders_hierarchy :validate => false + return false if @tree[folder_id].nil? @tree[folder_id][:parent].zero? end # Check if the given folder has no parent def gap?(folder_id) - @tree = folders_hierarchy + @tree = folders_hierarchy :validate => false parent_id = @tree[folder_id][:parent] @tree[parent_id].nil? end # Check if folder has any gaps in it hierarchy @@ -404,11 +417,12 @@ # ROOT # `-a <- if we remove just this folder then the folder "c" and "b" will become orphans # `-b # `-c def orphan?(folder_id) - @tree = folders_hierarchy + @tree = folders_hierarchy :validate => false + return false if @tree[folder_id].nil? parent_id = @tree[folder_id][:parent] return false if root_folder? folder_id return true if gap? folder_id orphan?(parent_id) end @@ -416,12 +430,15 @@ # Translate folder ID to a human readable path # # api.folder_path(123) # -> "foo/bar/baz" def folder_path(folder_id) @tree = folders_hierarchy - parent_id = @tree[folder_id][:parent] - path = (folder_path(parent_id) if parent_id.nonzero?).to_s + ('/' if parent_id.nonzero?).to_s + @tree[folder_id][:name] + + folder_data = @tree[folder_id] || {:parent => 0, :name => "<undefined>", :path => "<undefined>"} + + parent_id = folder_data[:parent] + path = (folder_path(parent_id) if parent_id.nonzero?).to_s + ('/' if parent_id.nonzero?).to_s + folder_data[:name] parent_id.zero? ? "/#{path}" : path end # Get folder ID by path # @@ -454,10 +471,10 @@ # :comment, # :md5hex, # :licids, # :sentby # } - # See http://images.rapidshare.com/apidoc.txt for more details + # See the http://images.rapidshare.com/apidoc.txt for more details def file_info(file_path, params = {}) folder_path = File.dirname file_path file_name = File.basename file_path folder_id = folder_id folder_path \ No newline at end of file