55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/libis/format/tool/msg_to_pdf.rb', line 55
def msg_to_pdf(msg, target, target_format, pdf_options, root_msg: true)
outdir = File.dirname(target)
FileUtils.mkdir_p(outdir)
body = msg.properties.body_html
body ||= HTML_WRAPPER_TEMPLATE % msg.properties.body
begin
body.encode!('UTF-8', universal_newline: true)
rescue Encoding::InvalidByteSequenceError, Encoding::UndefinedConversionError
begin
body.force_encoding('ISO-8859-1').encode!('UTF-8', universal_newline: true)
rescue Encoding::InvalidByteSequenceError, Encoding::UndefinedConversionError => e
@warnings << "#{e.class}: #{e.message}"
body.encode!('UTF-8', universal_newline: true, invalid: :replace, undef: :replace)
end
end
= {}
hdr_html = ''
%w[From To Cc Subject Date].each do |key|
value = find_hdr(msg., key)
if value
[key.downcase.to_sym] = value
hdr_html += hdr_html(key, value)
end
end
[:date].each do |key|
next unless [key]
[key] = DateTime.parse([key]).to_time.localtime.iso8601
end
unless hdr_html.empty?
if body =~ %r{</head>}
body.gsub!(%r{</head>}, "#{HEADER_STYLE}</head>")
elsif body =~ %r{<head/>}
body.gsub!(%r{<head/>}, "<head>#{HEADER_STYLE}</head>")
else
body.gsub!(/<body/, "<head>#{HEADER_STYLE}</head><body")
end
body.gsub!(/<body[^>]*>/) { |m| "#{m}#{HEADER_TABLE_TEMPLATE % hdr_html}" }
end
attachments = msg.attachments
used_files = []
body.gsub!(IMG_CID_PLAIN_REGEX) do |_match|
data = get_attachment_data(attachments, ::Regexp.last_match(1))
if data
used_files << ::Regexp.last_match(1)
"<img src=\"data:#{data[:mime_type]};base64,#{data[:base64]}\"/>"
else
'<img src=""/>'
end
end
body.gsub!(IMG_CID_HTML_REGEX) do |_match|
data = get_attachment_data(attachments, ::Regexp.last_match(1))
if data
used_files << ::Regexp.last_match(1)
"data:#{data[:mime_type]};base64,#{data[:base64]}"
else
''
end
end
files = []
if target_format == :PDF
pdf_options = {
page_size: 'A4',
margin_top: '10mm',
margin_bottom: '10mm',
margin_left: '10mm',
margin_right: '10mm',
dpi: 300
}.merge pdf_options
subject = find_hdr(msg., 'Subject')
kit = PDFKit.new(body, title: (subject || 'message'), **pdf_options)
pdf = kit.to_pdf
File.open(target, 'wb') { |f| f.write(pdf) }
else
File.open(target, 'wb') { |f| f.write(body) }
end
files << target if File.exist?(target)
outdir = File.join(outdir, "#{File.basename(target)}.attachments")
digits = ((attachments.count + 1) / 10) + 1
i = 1
attachments.delete_if { |a| a.properties.attachment_hidden }.each do |a|
prefix = "#{format('%0*d', digits, i)}-"
if (sub_msg = a.instance_variable_get(:@embedded_msg))
subject = a.properties[:display_name] || sub_msg.subject || ''
file = File.join(outdir, "#{prefix}#{subject}.msg.#{target_format.to_s.downcase}")
result = msg_to_pdf(sub_msg, file, target_format, pdf_options, root_msg: false)
if (e = result[:error])
raise e
end
files += result[:files]
elsif a.filename
next if used_files.include?(a.filename)
file = File.join(outdir, "#{prefix}#{a.filename}")
FileUtils.mkdir_p(File.dirname(file))
File.open(file, 'wb') { |f| a.save(f) }
files << file
else
@warnings << "Attachment #{a.properties[:display_name]} cannot be extracted"
next
end
i += 1
end
if root_msg
p = Pathname(File.dirname(files.first))
files[1..].each do |f|
([:attachments] ||= []) << Pathname.new(f).relative_path_from(p).to_s
end
end
{
command: { status: 0 },
files:,
headers:,
warnings: @warnings
}
rescue Exception => e
raise unless root_msg
msg.close
{
command: { status: -1 },
files: [],
headers: {},
errors: [
{
error: e.message,
error_class: e.class.name,
error_trace: e.backtrace
}
],
warnings: @warnings
}
end
|