Class: MailDude::AttachmentLocator
- Inherits:
-
Object
- Object
- MailDude::AttachmentLocator
- Defined in:
- lib/mail_dude/attachment_locator.rb
Defined Under Namespace
Classes: Attachment
Class Method Summary collapse
- .attachment_part?(part) ⇒ Boolean
- .content_disposition(part) ⇒ Object
- .inline_renderable_part?(part) ⇒ Boolean
Instance Method Summary collapse
- #attachments ⇒ Object
- #attachments_count ⇒ Object
- #attachments_present? ⇒ Boolean
- #find(attachment_id) ⇒ Object
- #find_inline_by_cid(content_id) ⇒ Object
-
#initialize(message) ⇒ AttachmentLocator
constructor
A new instance of AttachmentLocator.
- #normalize_content_id(content_id) ⇒ Object
- #sanitize_filename(filename, fallback:) ⇒ Object
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 = end |
Class Method Details
.attachment_part?(part) ⇒ Boolean
6 7 8 |
# File 'lib/mail_dude/attachment_locator.rb', line 6 def (part) !part.multipart? && (part. || 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
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
#attachments ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/mail_dude/attachment_locator.rb', line 49 def return [] unless MailDude.configuration. .each_with_index.map do |part, index| id = "a#{index}" Attachment.new(id: id, part: part, metadata: (part, id)) end end |
#attachments_count ⇒ Object
58 59 60 |
# File 'lib/mail_dude/attachment_locator.rb', line 58 def .length end |
#attachments_present? ⇒ Boolean
62 63 64 |
# File 'lib/mail_dude/attachment_locator.rb', line 62 def .positive? end |
#find(attachment_id) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/mail_dude/attachment_locator.rb', line 66 def find() raise AttachmentNotFoundError, 'Attachment not found' unless .to_s.match?(/\Aa\d+\z/) .find { || .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? .find { || .['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 |