diff --git a/lib/net/dav.rb b/lib/net/dav.rb index ea29d63..9279d67 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 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 + 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 Prefix 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.select{|x| !x.empty?}.join("\n") + end + # Stores the content of a string to a URL # # Example: