From 562d9fdb4e3e7022808be3026146ee6ec460822e Mon Sep 17 00:00:00 2001 From: Prabhakar Kumar Date: Sun, 16 Feb 2014 23:56:30 +0530 Subject: [PATCH 1/2] Add put_dir and put_file with local path methods --- lib/net/dav.rb | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/lib/net/dav.rb b/lib/net/dav.rb index ea29d63..2f71895 100644 --- a/lib/net/dav.rb +++ b/lib/net/dav.rb @@ -611,6 +611,64 @@ def put(path, stream, length) res.body end + # Stores a file to a URL + # + # Example: + # dav.put(url.path, file_path) + def put(path, file_path) + return false unless File.file?(file_path) + path = @uri.merge(path).path + File.open(file_path, "rb") do |stream| + res = @handler.request_sending_stream(:put, path, stream, File.size(file_path), @headers) + return res.body + end + return false + end + + # Stores a DIR to a URL recursively + # + # Example: + # dav.put(url.path, dir_path) + def put_dir(path, dir_path) + return false unless File.directory?(dir_path) + response_arr = [] + path = @uri.merge(path).path + target_prefix = File.basename(dir_path) + puts "Target path is: #{target_prefix}" + src_prefix = File.dirname(dir_path) + puts "Source prefix is: #{src_prefix}" + dirs_to_traverse = [target_prefix] + mkdir(target_prefix) unless exists?(target_prefix) + until dirs_to_traverse.empty? + current_dir = dirs_to_traverse.shift + puts "Processing DIR: #{current_dir}" + Dir.entries("#{src_prefix}/#{current_dir}").each do |file| + next if file == "." + next if file == ".." + remote_file = "#{current_dir}/#{file}" + local_file = "#{src_prefix}/#{remote_file}" + puts "Processing File: #{local_file}" + if File.file?(local_file) + puts "Uploading file: #{file} to #{remote_file}" + response_arr << put(remote_file, File.path(local_file)) + end + if File.directory?(local_file) + # Its a DIR + puts "It is a directory" + dir = remote_file + puts "Creating DIR: #{dir}" + unless exists?(dir) + response_arr << mkdir(dir) + end + dirs_to_traverse << dir + end + end + end + return response_arr.join("\n") + end + + + # Stores the content of a string to a URL # # Example: From 9eb52694910636e4bd9044082a63ca7ddeb64f15 Mon Sep 17 00:00:00 2001 From: Prabhakar Kumar Date: Sun, 16 Feb 2014 23:56:30 +0530 Subject: [PATCH 2/2] Add put_dir and put_file with local path methods --- lib/net/dav.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/net/dav.rb b/lib/net/dav.rb index 2f71895..9279d67 100644 --- a/lib/net/dav.rb +++ b/lib/net/dav.rb @@ -611,10 +611,11 @@ def put(path, stream, length) res.body end - # Stores a file to a URL + # Stores a file specified with a local path to a URL # # Example: # dav.put(url.path, file_path) + # def put(path, file_path) return false unless File.file?(file_path) path = @uri.merge(path).path @@ -629,34 +630,35 @@ def put(path, file_path) # # Example: # dav.put(url.path, dir_path) + # def put_dir(path, dir_path) return false unless File.directory?(dir_path) response_arr = [] path = @uri.merge(path).path target_prefix = File.basename(dir_path) - puts "Target path is: #{target_prefix}" + #puts "Target Prefix is: #{target_prefix}" src_prefix = File.dirname(dir_path) - puts "Source prefix is: #{src_prefix}" + #puts "Source prefix is: #{src_prefix}" dirs_to_traverse = [target_prefix] mkdir(target_prefix) unless exists?(target_prefix) until dirs_to_traverse.empty? current_dir = dirs_to_traverse.shift - puts "Processing DIR: #{current_dir}" + #puts "Processing DIR: #{current_dir}" Dir.entries("#{src_prefix}/#{current_dir}").each do |file| next if file == "." next if file == ".." remote_file = "#{current_dir}/#{file}" local_file = "#{src_prefix}/#{remote_file}" - puts "Processing File: #{local_file}" + #puts "Processing File: #{local_file}" if File.file?(local_file) - puts "Uploading file: #{file} to #{remote_file}" + #puts "Uploading file: #{file} to #{remote_file}" response_arr << put(remote_file, File.path(local_file)) end if File.directory?(local_file) # Its a DIR - puts "It is a directory" + #puts "It is a directory" dir = remote_file - puts "Creating DIR: #{dir}" + #puts "Creating DIR: #{dir}" unless exists?(dir) response_arr << mkdir(dir) end @@ -664,11 +666,9 @@ def put_dir(path, dir_path) end end end - return response_arr.join("\n") + return response_arr.select{|x| !x.empty?}.join("\n") end - - # Stores the content of a string to a URL # # Example: