Sha256: 44cf6fd030e918e187bdbcb1adff88bfaa5671395d0b18046dec4b2b06aa1986

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

module WebTranslateIt

  module Safe

    class Ftp < Sink

      protected

      def active?
        host && user
      end

      def path
        @path ||= expand(config[:ftp, :path] || config[:local, :path] || ':kind/:id')
      end

      def save
        raise 'pipe-streaming not supported for FTP.' unless @backup.path

        puts "Uploading #{host}:#{full_path} via FTP" if verbose? || dry_run?

        return if dry_run? || local_only?

        port ||= 21
        Net::FTP.open(host) do |ftp|
          ftp.connect(host, port)
          ftp.login(user, password)
          puts "Sending #{@backup.path} to #{full_path}" if verbose?
          begin
            ftp.put(@backup.path, full_path)
          rescue Net::FTPPermError
            puts "Ensuring remote path (#{path}) exists" if verbose?
          end
        end
        puts '...done' if verbose?
      end

      def cleanup
        return if local_only? || dry_run?

        return unless (keep = config[:keep, :ftp])

        puts "listing files: #{host}:#{base}*" if verbose?
        port ||= 21
        Net::FTP.open(host) do |ftp|
          ftp.connect(host, port)
          ftp.login(user, password)
          files = ftp.nlst(path)
          pattern = File.basename(base.to_s)
          files = files.select { |x| x.start_with?(pattern) }
          puts(files.collect { |x| x }) if verbose?

          files = files
                  .collect { |x| x }
                  .sort

          cleanup_with_limit(files, keep) do |f|
            file = File.join(path, f)
            puts "removing ftp file #{host}:#{file}" if dry_run? || verbose?
            ftp.delete(file) unless dry_run? || local_only?
          end
        end
      end

      def host
        config[:ftp, :host]
      end

      def user
        config[:ftp, :user]
      end

      def password
        config[:ftp, :password]
      end

      def port
        config[:ftp, :port]
      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
webtranslateit-safe-0.4.0 lib/webtranslateit/safe/ftp.rb