Class: MailDude::AttachmentLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_dude/attachment_locator.rb

Defined Under Namespace

Classes: Attachment

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ AttachmentLocator

Returns a new instance of AttachmentLocator.



45
46
47
# File 'lib/mail_dude/attachment_locator.rb', line 45

def initialize(message)
  @message = message
end

Class Method Details

.attachment_part?(part) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/mail_dude/attachment_locator.rb', line 6

def attachment_part?(part)
  !part.multipart? && (part.attachment? || part.filename.present? || inline_renderable_part?(part))
end

.content_disposition(part) ⇒ Object



10
11
12
# File 'lib/mail_dude/attachment_locator.rb', line 10

def content_disposition(part)
  part.content_disposition.to_s.split(';').first.to_s.downcase
end

.inline_renderable_part?(part) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/mail_dude/attachment_locator.rb', line 14

def inline_renderable_part?(part)
  inline_part?(part) || part.content_id.present?
end

Instance Method Details

#attachmentsObject



49
50
51
52
53
54
55
56
# File 'lib/mail_dude/attachment_locator.rb', line 49

def attachments
  return [] unless MailDude.configuration.capture_attachments

  attachment_parts.each_with_index.map do |part, index|
    id = "a#{index}"
    Attachment.new(id: id, part: part, metadata: (part, id))
  end
end

#attachments_countObject



58
59
60
# File 'lib/mail_dude/attachment_locator.rb', line 58

def attachments_count
  attachment_parts.length
end

#attachments_present?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/mail_dude/attachment_locator.rb', line 62

def attachments_present?
  attachments_count.positive?
end

#find(attachment_id) ⇒ Object



66
67
68
69
70
71
# File 'lib/mail_dude/attachment_locator.rb', line 66

def find(attachment_id)
  raise AttachmentNotFoundError, 'Attachment not found' unless attachment_id.to_s.match?(/\Aa\d+\z/)

  attachments.find { |attachment| attachment.id == attachment_id.to_s } ||
    raise(AttachmentNotFoundError, 'Attachment not found')
end

#find_inline_by_cid(content_id) ⇒ Object



73
74
75
76
77
78
# File 'lib/mail_dude/attachment_locator.rb', line 73

def find_inline_by_cid(content_id)
  normalized = normalize_content_id(content_id)
  return nil if normalized.blank?

  attachments.find { |attachment| attachment.['content_id'] == normalized }
end

#normalize_content_id(content_id) ⇒ Object



80
81
82
# File 'lib/mail_dude/attachment_locator.rb', line 80

def normalize_content_id(content_id)
  content_id.to_s.delete_prefix('cid:').delete_prefix('<').delete_suffix('>').strip
end

#sanitize_filename(filename, fallback:) ⇒ Object



84
85
86
87
88
# File 'lib/mail_dude/attachment_locator.rb', line 84

def sanitize_filename(filename, fallback:)
  sanitized = filename.to_s.encode('UTF-8', invalid: :replace, undef: :replace)
  sanitized = sanitized.gsub(%r{[/\\\0[:cntrl:]]}, '_').strip
  sanitized.presence || fallback
end