class Photocopier::FTP

Public Class Methods

new(options = {}) click to toggle source

rubocop:disable Lint/MissingSuper

# File lib/photocopier/ftp.rb, line 4
def initialize(options = {})
  @options = options
end

Public Instance Methods

delete(remote_path) click to toggle source
# File lib/photocopier/ftp.rb, line 21
def delete(remote_path)
  session.delete(remote_path)
end
get(remote_path, file_path = nil) click to toggle source
# File lib/photocopier/ftp.rb, line 13
def get(remote_path, file_path = nil)
  session.get remote_path, file_path
end
get_directory(remote_path, local_path, exclude = []) click to toggle source
# File lib/photocopier/ftp.rb, line 25
def get_directory(remote_path, local_path, exclude = [])
  FileUtils.mkdir_p(local_path)
  lftp(local_path, remote_path, false, exclude, options[:port])
end
inferred_port() click to toggle source
# File lib/photocopier/ftp.rb, line 34
def inferred_port
  if options[:port].nil? && options[:scheme] == 'sftp'
    22
  elsif options[:port].nil?
    21
  else
    options[:port]
  end
end
options() click to toggle source

rubocop:enable Lint/MissingSuper

# File lib/photocopier/ftp.rb, line 9
def options
  @options.clone
end
put_directory(local_path, remote_path, exclude = []) click to toggle source
# File lib/photocopier/ftp.rb, line 30
def put_directory(local_path, remote_path, exclude = [])
  lftp(local_path, remote_path, true, exclude, options[:port])
end
put_file(file_path, remote_path) click to toggle source
# File lib/photocopier/ftp.rb, line 17
def put_file(file_path, remote_path)
  session.put_file file_path, remote_path
end

Private Instance Methods

lftp(local, remote, reverse, exclude, port = nil) click to toggle source
# File lib/photocopier/ftp.rb, line 50
def lftp(local, remote, reverse, exclude, port = nil)
  remote = Shellwords.escape(remote)
  local = Shellwords.escape(local)
  command = [
    'set ftp:list-options -a',
    "set ftp:passive-mode #{options[:passive] || 'false'}",
    'set cmd:fail-exit true',
    "open -p #{port || inferred_port} #{remote_ftp_url}",
    "find -d 1 #{remote} || mkdir -p #{remote}",
    "lcd #{local}",
    "cd #{remote}",
    lftp_mirror_arguments(reverse, exclude)
  ].join('; ')

  run "lftp -c '#{command}'"
end
lftp_mirror_arguments(reverse, exclude = []) click to toggle source
# File lib/photocopier/ftp.rb, line 79
def lftp_mirror_arguments(reverse, exclude = [])
  mirror = 'mirror --delete --use-cache --verbose' \
           ' --no-perms --allow-suid --no-umask --parallel=5'
  mirror << ' --reverse --dereference' if reverse
  exclude.each do |glob|
    mirror << " --exclude-glob #{glob}" # NOTE: do not use Shellwords.escape here
  end
  mirror
end
remote_ftp_url() click to toggle source
# File lib/photocopier/ftp.rb, line 67
def remote_ftp_url
  url = options[:scheme].dup.presence || 'ftp'
  url << '://'
  if options[:user].present?
    url << CGI.escape(options[:user])
    url << ":#{CGI.escape(options[:password])}" if options[:password].present?
    url << '@'
  end
  url << options[:host]
  url
end
session() click to toggle source
# File lib/photocopier/ftp.rb, line 46
def session
  @session ||= Session.new(options)
end