1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2025-02-18 17:10:04 +00:00

fix ftp sync for mpw 3.0

This commit is contained in:
nishiki 2016-05-15 23:23:26 +02:00
parent 84ff0e8ae2
commit 47430ce611

View file

@ -2,80 +2,53 @@
# author: nishiki # author: nishiki
# mail: nishiki@yaegashi.fr # mail: nishiki@yaegashi.fr
require 'rubygems'
require 'i18n' require 'i18n'
require 'net/ftp' require 'net/ftp'
module MPW module MPW
class FTP class FTP
attr_accessor :error_msg
attr_accessor :enable
# Constructor # Constructor
# @args: host -> the server host # @args: config -> the config
# port -> ther connection port def initialize(config)
# gpg_key -> the gpg key @host = config['host']
# password -> the remote password @user = config['user']
# suffix -> the suffix file @password = config['password']
def initialize(host, user, password, path, port=nil) @path = config['path']
@error_msg = nil @port = config['port'].instance_of?(Integer) ? 22 : config['port']
@enable = false
@host = host
@user = user
@password = password
@path = path
@port = port.instance_of?(Integer) ? 21 : port
end end
# Connect to server # Connect to server
# @rtrn: false if the connection fail
def connect def connect
Net::FTP.open(@host) do |ftp| Net::FTP.open(@host) do |ftp|
ftp.login(@user, @password) ftp.login(@user, @password)
@enable = true break
end end
rescue Exception => e rescue Exception => e
@error_msg = "#{I18n.t('error.sync.connection')}\n#{e}" raise "#{I18n.t('error.sync.connection')}\n#{e}"
@enable = false
else
return @enable
end end
# Get data on server # Get data on server
# @args: gpg_password -> the gpg password # @args: file_tmp -> the path where download the file
# @rtrn: nil if nothing data or error
def get(file_tmp) def get(file_tmp)
return false if not @enable
Net::FTP.open(@host) do |ftp| Net::FTP.open(@host) do |ftp|
ftp.login(@user, @password) ftp.login(@user, @password)
ftp.gettextfile(@path, file_tmp) ftp.gettextfile(@path, file_tmp)
end end
return true
rescue Exception => e rescue Exception => e
@error_msg = "#{I18n.t('error.sync.download')}\n#{e}" raise "#{I18n.t('error.sync.download')}\n#{e}"
return false
end end
# Update the remote data # Update the remote data
# @args: data -> the data to send on server # @args: file_gpg -> the data to send on server
# @rtrn: false if there is a problem
def update(file_gpg) def update(file_gpg)
return true if not @enable
Net::FTP.open(@host) do |ftp| Net::FTP.open(@host) do |ftp|
ftp.login(@user, @password) ftp.login(@user, @password)
ftp.puttextfile(file_gpg, @path) ftp.puttextfile(file_gpg, @path)
end end
return true
rescue Exception => e rescue Exception => e
@error_msg = "#{I18n.t('error.sync.upload')}\n#{e}" raise "#{I18n.t('error.sync.upload')}\n#{e}"
return false
end
end end
end end
end