module Dbox::Utils
Public Instance Methods
calculate_hash(filepath)
click to toggle source
# File lib/dbox/utils.rb, line 93 def calculate_hash(filepath) begin Digest::MD5.file(filepath).to_s rescue Errno::EISDIR nil rescue Errno::ENOENT nil end end
case_insensitive_difference(a, b)
click to toggle source
# File lib/dbox/utils.rb, line 84 def case_insensitive_difference(a, b) b = b.map(&:downcase).sort a.reject {|s| b.include?(s.downcase) } end
case_insensitive_equal(a, b)
click to toggle source
# File lib/dbox/utils.rb, line 89 def case_insensitive_equal(a, b) a && b && a.downcase == b.downcase end
case_insensitive_join(path, *rest)
click to toggle source
# File lib/dbox/utils.rb, line 75 def case_insensitive_join(path, *rest) if rest.length == 0 case_insensitive_resolve(path) else rest = rest.map {|s| s.split(File::SEPARATOR) }.flatten case_insensitive_join(File.join(case_insensitive_resolve(path), rest[0]), *rest[1..-1]) end end
case_insensitive_resolve(path)
click to toggle source
# File lib/dbox/utils.rb, line 62 def case_insensitive_resolve(path) if File.exists?(path) path else matches = Dir.glob(path, File::FNM_CASEFOLD) case matches.size when 0 then path when 1 then matches.first else raise(RuntimeError, "Oops, you have multiple files with the same case. Please delete one of them, as Dropbox is case insensitive. (#{matches.join(', ')})") end end end
find_nonconflicting_path(filepath)
click to toggle source
# File lib/dbox/utils.rb, line 103 def find_nonconflicting_path(filepath) proposed = filepath while File.exists?(case_insensitive_resolve(proposed)) dir, p = File.split(proposed) p = p.sub(/^(.*?)( \((\d+)\))?(\..*?)?$/) { "#{$1} (#{$3 ? $3.to_i + 1 : 1})#{$4}" } proposed = File.join(dir, p) end proposed end
local_to_relative_path(path)
click to toggle source
assumes local_path is defined
# File lib/dbox/utils.rb, line 27 def local_to_relative_path(path) if path =~ /^#{local_path}\/?(.*)$/i $1 else raise BadPath, "Not a local path: #{path}" end end
parse_time(t)
click to toggle source
# File lib/dbox/utils.rb, line 17 def parse_time(t) case t when Time t when String Time.parse(t) end end
relative_to_local_path(path)
click to toggle source
assumes local_path is defined
# File lib/dbox/utils.rb, line 45 def relative_to_local_path(path) if path && path.length > 0 case_insensitive_join(local_path, path) else case_insensitive_resolve(local_path) end end
relative_to_remote_path(path)
click to toggle source
assumes remote_path is defined
# File lib/dbox/utils.rb, line 54 def relative_to_remote_path(path) if path && path.length > 0 File.join(remote_path, path) else remote_path end end
remote_to_relative_path(path)
click to toggle source
assumes remote_path is defined
# File lib/dbox/utils.rb, line 36 def remote_to_relative_path(path) if path =~ /^#{remote_path}\/?(.*)$/i $1 else raise BadPath, "Not a remote path: #{path}" end end
time_to_s(t)
click to toggle source
# File lib/dbox/utils.rb, line 7 def time_to_s(t) case t when Time # matches dropbox time format t.utc.strftime("%a, %d %b %Y %H:%M:%S +0000") when String t end end
times_equal?(t1, t2)
click to toggle source
# File lib/dbox/utils.rb, line 3 def times_equal?(t1, t2) time_to_s(t1) == time_to_s(t2) end