diff options
author | Louise Crow <louise.crow@gmail.com> | 2015-05-22 15:29:34 +0100 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2015-06-04 12:17:51 +0100 |
commit | a048c2d1af31006363712f0c348e3bee5ca45ac6 (patch) | |
tree | 80a4db19c93d1022185b26c88ff56f5af0c17ae4 /app | |
parent | 998b23aac1efbc246376242bbf06aec1ac865bec (diff) |
Handle unparsed email contents as binary.
I think I was wrong in a83b379fd2d676172855825d0592937b234371e2 in
assuming that all email gets properly encoded for transfer. Looking
at the mail gem load method
https://github.com/mikel/mail/blob/b159e0a542962fdd5e292a48cfffa560d7cf412e/lib/mail/mail.rb#L175a,
it reads raw email content from a file in binary mode. So this commit
makes both reading and writing the raw_email a binary mode operation
and adds a data_as_text method for displaying the data in the admin
interface that coerces it to valid utf-8.
Diffstat (limited to 'app')
-rw-r--r-- | app/models/raw_email.rb | 23 | ||||
-rw-r--r-- | app/views/admin_request/show_raw_email.html.erb | 5 |
2 files changed, 21 insertions, 7 deletions
diff --git a/app/models/raw_email.rb b/app/models/raw_email.rb index 21a53f493..175868b7a 100644 --- a/app/models/raw_email.rb +++ b/app/models/raw_email.rb @@ -39,16 +39,27 @@ class RawEmail < ActiveRecord::Base end def data=(d) - if !File.exists?(self.directory) - FileUtils.mkdir_p self.directory + FileUtils.mkdir_p(directory) unless File.exists?(directory) + File.atomic_write(filepath) do |file| + file.binmode + file.write(d) end - File.atomic_write(self.filepath) { |file| - file.write d - } end def data - File.open(self.filepath, "r").read + File.open(filepath, "rb").read + end + + def data_as_text + text = data + if text.respond_to?(:encoding) + text = text.encode("UTF-8", :invalid => :replace, + :undef => :replace, + :replace => "") + else + text = Iconv.conv('UTF-8//IGNORE', 'UTF-8', text) + end + text end def destroy_file_representation! diff --git a/app/views/admin_request/show_raw_email.html.erb b/app/views/admin_request/show_raw_email.html.erb index da22b6069..045989df6 100644 --- a/app/views/admin_request/show_raw_email.html.erb +++ b/app/views/admin_request/show_raw_email.html.erb @@ -59,5 +59,8 @@ <p><%= link_to "Download", admin_request_download_raw_email_path(@raw_email) %></p> -<pre><%=h(@raw_email.data).gsub(/\n/, '<br>').html_safe %></pre> +<h2>Preview</h2> + +For an exact rendering of this email, use the "Download" link. +<pre><%=h(@raw_email.data_as_text).gsub(/\n/, '<br>').html_safe %></pre> |