Sha256: 2556a3904d10b83af686e81e86a8934253b05282ddeefaea96faa293b77bc312

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

module Imap; end

module Imap::Backup
  class Serializer; end

  class Serializer::Appender
    attr_reader :imap
    attr_reader :folder
    attr_reader :mbox

    def initialize(folder:, imap:, mbox:)
      @folder = folder
      @imap = imap
      @mbox = mbox
    end

    def run(uid:, message:, flags:)
      raise "Can't add messages without uid_validity" if !imap.uid_validity

      uid = uid.to_i
      existing = imap.get(uid)
      if existing
        Logger.logger.debug(
          "[#{folder}] message #{uid} already downloaded - skipping"
        )
        return
      end

      do_append uid, message, flags
    end

    private

    def do_append(uid, message, flags)
      mboxrd_message = Email::Mboxrd::Message.new(message)
      initial = mbox.length || 0
      mbox_appended = false
      begin
        serialized = mboxrd_message.to_serialized
        mbox.append serialized
        mbox_appended = true
        imap.append uid, serialized.length, flags
      rescue StandardError => e
        mbox.rewind(initial) if mbox_appended

        error = <<-ERROR.gsub(/^\s*/m, "")
          [#{folder}] failed to append message #{uid}:
          #{message}. #{e}:
          #{e.backtrace.join("\n")}"
        ERROR
        Logger.logger.warn error
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
imap-backup-10.0.1 lib/imap/backup/serializer/appender.rb