Sha256: a073f4960a1aacb5d5dbff27afb8c9defde92dc5a1a290739b4d28e390d44441

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

module Imap::Backup
  class Migrator
    attr_reader :folder
    attr_reader :reset
    attr_reader :serializer

    def initialize(serializer, folder, reset: false)
      @folder = folder
      @reset = reset
      @serializer = serializer
    end

    def run
      count = serializer.uids.count
      folder.create
      ensure_destination_empty!

      Logger.logger.debug "[#{folder.name}] #{count} to migrate"
      serializer.each_message(serializer.uids).with_index do |(uid, message), i|
        next if message.nil?

        log_prefix = "[#{folder.name}] uid: #{uid} (#{i + 1}/#{count}) -"
        Logger.logger.debug(
          "#{log_prefix} #{message.supplied_body.size} bytes"
        )

        begin
          folder.append(message)
        rescue StandardError => e
          Logger.logger.warn "#{log_prefix} append error: #{e}"
        end
      end
    end

    private

    def ensure_destination_empty!
      if reset
        folder.clear
      else
        fail_if_destination_not_empty!
      end
    end

    def fail_if_destination_not_empty!
      return if folder.uids.empty?

      raise <<~ERROR
        The destination folder '#{folder.name}' is not empty.
        Pass the --reset flag if you want to clear existing emails from destination
        folders before uploading.
      ERROR
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
imap-backup-6.3.0 lib/imap/backup/migrator.rb
imap-backup-6.2.1 lib/imap/backup/migrator.rb