lib/syncftp.rb in syncftp-0.0.1 vs lib/syncftp.rb in syncftp-0.0.2
- old
+ new
@@ -45,12 +45,16 @@
def remote_dir_exist?( dir )
path = dir.split( "/" )
find = path.pop
path = path.join( "/" )
path = "." if path == ""
+ altdir = dir
+ altdir = dir[2..-1] if dir[0,2] == "./"
- nlst( path ).include?( dir )
+ return true if dir == "."
+
+ nlst( path ).include?( find ) or nlst( path ).include?( dir ) or nlst( path ).include?( altdir )
end
#
# Net::FTP extension
#
@@ -81,10 +85,13 @@
# File extension
#
# Check if the +file+ is a binary file
#
def self.binary?( file )
+ if MIME::Types.type_for( file ).size == 0
+ return true
+ end
MIME::Types.type_for( file ).map{ |e| (e.binary?) ? e : nil }.compact.size > 0
end
end
class SyncFTP
@@ -100,13 +107,20 @@
# * +:port+ - default = 21
# * +:logfile+ - default = STDOUT
# * +:loglevel+ - default = Logger::UNKNOWN (Cool if you don't want logs)
#
def initialize(host, options = {})
- options = {:username => "anonymous", :password => nil, :logfile => STDOUT, :loglevel => Logger::UNKNOWN}.merge(options)
+ options = {
+ :username => "anonymous",
+ :password => nil,
+ :logfile => STDOUT,
+ :loglevel => Logger::UNKNOWN,
+ :catalog => :remote
+ }.merge(options)
@host, @port = host, options[:port]||21
@username, @password = options[:username], options[:password]
+ @catalog = options[:catalog]
@remote_md5s = {}
@local_md5s = {}
@log = Logger.new( options[:logfile] )
@log.level = options[:loglevel]
end
@@ -143,10 +157,19 @@
ftp.puttextfile( tmpname, remote+"/"+".syncftp" )
end
File.delete( tmpname )
end
+ def getCatalog #:nodoc
+ end
+
+ def saveCatalog #:nodoc
+ end
+
+ def catalogFileName #:nodoc
+ end
+
private
def tmpfilename #:nodoc:
tmpdir = Dir::tmpdir
basename = File.basename( $0 )
tempname = nil
@@ -173,11 +196,11 @@
yield ftp
ftp.close
end
def send_dir(ftp, local, remote) #:nodoc:
- ftp.mkdir_p(remote)
+ ftp.mkdir_p(remote) unless ftp.remote_dir_exist?(remote)
Dir.foreach(local) do |file|
next if file == "." or file == ".."
local_file = File.join( local, file )
@@ -188,15 +211,17 @@
send_dir(ftp, local_file, remote_file)
else
@local_md5s[remote_file] = Digest::MD5.hexdigest( File.open(local_file).read )
if( @local_md5s[remote_file] != @remote_md5s[remote_file] )
- @log.info "Copy #{local_file} to ftp://#{@host}:#{@port}/#{remote_file}"
-
# It's a file, we just send it
if File.binary?(local_file)
+ @log.info "Copy [Binary] #{local_file} to ftp://#{@host}:#{@port}/#{remote_file}"
+
ftp.putbinaryfile(local_file, remote_file)
else
+ @log.info "Copy [Text] #{local_file} to ftp://#{@host}:#{@port}/#{remote_file}"
+
ftp.puttextfile(local_file, remote_file)
end
else
@log.info "#{local_file} don't need to be overwritten !"
end