1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2025-03-20 21:34:35 +00:00
mpw/lib/mpw/sync/ftp.rb
2016-06-30 22:02:45 +02:00

68 lines
1.9 KiB
Ruby

#!/usr/bin/ruby
# MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'i18n'
require 'net/ftp'
module MPW
class FTP
# Constructor
# @args: config -> the config
def initialize(config)
@host = config['host']
@user = config['user']
@password = config['password']
@path = config['path']
@port = config['port'].instance_of?(Integer) ? 22 : config['port']
end
# Connect to server
def connect
Net::FTP.open(@host) do |ftp|
ftp.login(@user, @password)
break
end
rescue Exception => e
raise "#{I18n.t('error.sync.connection')}\n#{e}"
end
# Get data on server
# @args: file_tmp -> the path where download the file
def get(file_tmp)
Net::FTP.open(@host) do |ftp|
ftp.login(@user, @password)
ftp.gettextfile(@path, file_tmp)
end
rescue Exception => e
raise "#{I18n.t('error.sync.download')}\n#{e}"
end
# Update the remote data
# @args: file_gpg -> the data to send on server
def update(file_gpg)
Net::FTP.open(@host) do |ftp|
ftp.login(@user, @password)
ftp.puttextfile(file_gpg, @path)
end
rescue Exception => e
raise "#{I18n.t('error.sync.upload')}\n#{e}"
end
end
end