Module: MailReport::Archive

Defined in:
lib/mailreport/archive.rb

Overview

Attachment wrapping per RFC 7489 §7.2.1.1 / RFC 8460 §3. Detected from magic bytes, not the filename.

Constant Summary collapse

GZIP =
"\x1f\x8b".b
ZIP =
"PK".b
MAX_SIZE =
25 * 1024 * 1024
CHUNK_SIZE =
64 * 1024
STORED =
0
UNREADABLE =
[
  ZipKit::FileReader::ReadError, ZipKit::FileReader::MissingEOCD,
  ZipKit::FileReader::UnsupportedFeature, Zlib::Error
].freeze

Class Method Summary collapse

Class Method Details

.open(bytes, max_size: MAX_SIZE, name: nil) ⇒ Object

name prefers a zip entry whose filename matches (e.g. /.xml\z/i).



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mailreport/archive.rb', line 23

def open(bytes, max_size: MAX_SIZE, name: nil)
  bytes = bytes.to_s.b

  if bytes.empty?
    nil
  elsif gzipped?(bytes)
    inflated(bytes, max_size)
  elsif zipped?(bytes)
    unzipped(bytes, max_size, name)
  elsif bytes.bytesize <= max_size
    bytes
  end
rescue *UNREADABLE
  nil
end