Skip to content
This repository has been archived by the owner on Nov 28, 2023. It is now read-only.

put_dir and put_file methods #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions lib/net/dav.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down