Class: Uniword::Mhtml::MimePart

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/uniword/mhtml/mime_part.rb

Overview

Base class for MIME parts in an MHTML document.

Each MIME part has headers (Content-Type, Content-Location, Content-Transfer-Encoding) and a body (raw content).

Subclasses: HtmlPart, XmlPart, ImagePart, FileListPart, HeaderPart

Direct Known Subclasses

HeaderFooterPart, HtmlPart, ImagePart, ThemePart, XmlPart

Instance Method Summary collapse

Instance Method Details

#decoded_contentObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/uniword/mhtml/mime_part.rb', line 20

def decoded_content
  case content_transfer_encoding&.downcase
  when "base64"
    Base64.strict_decode64(raw_content.gsub(/\s+/, ""))
  when "quoted-printable"
    # Work in BINARY encoding to avoid gsub encoding conflicts,
    # then force UTF-8 at the end. Use .b to get BINARY encoding
    # without altering bytes.
    content = raw_content.dup.force_encoding("BINARY")
    content
      .gsub(/=\r?\n/, "")
      .gsub(/=([0-9A-Fa-f]{2})/) { [::Regexp.last_match(1).hex].pack("C") }
      .force_encoding("UTF-8")
  else
    raw_content
  end
end

#decoded_content=(value) ⇒ Object



38
39
40
# File 'lib/uniword/mhtml/mime_part.rb', line 38

def decoded_content=(value)
  self.raw_content = value
end

#filenameObject



42
43
44
45
46
47
48
49
# File 'lib/uniword/mhtml/mime_part.rb', line 42

def filename
  return nil unless content_location

  location = content_location.sub(/^cid:/i, "")
  if (match = location.match(%r{([^/\\]+\.[a-z0-9]+)$}i))
    match[1]
  end
end

#html_content?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/uniword/mhtml/mime_part.rb', line 59

def html_content?
  content_type.to_s.include?("text/html")
end

#image_content?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/uniword/mhtml/mime_part.rb', line 63

def image_content?
  content_type.to_s.start_with?("image/")
end

#text_content?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/uniword/mhtml/mime_part.rb', line 51

def text_content?
  content_type.to_s.start_with?("text/")
end

#theme_content?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/uniword/mhtml/mime_part.rb', line 67

def theme_content?
  content_type.to_s.include?("officetheme")
end

#xml_content?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/uniword/mhtml/mime_part.rb', line 55

def xml_content?
  content_type.to_s.match?(%r{text/xml|application/xml})
end