html view with cids

This commit is contained in:
Wojciech Todryk 2011-08-28 18:01:37 +02:00
parent 19aef7a5ed
commit cead4d9d74
4 changed files with 38 additions and 15 deletions

View file

@ -136,45 +136,56 @@ class MessagesController < ApplicationController
end end
def show def show
logger.custom('start',Time.now)
@attachments = [] @attachments = []
@render_as_text = [] @render_as_text = []
@render_as_html_idx = nil @render_as_html_idx = nil
@message = @current_user.messages.find_by_uid(params[:id]) @message = @current_user.messages.find_by_uid(params[:id])
@message.update_attributes(:unseen => false) @message.update_attributes(:unseen => false)
logger.custom('start_fetch',Time.now)
imap_message = @mailbox.fetch_body(@message.uid) imap_message = @mailbox.fetch_body(@message.uid)
logger.custom('stop_fetch',Time.now)
parts = imap_message.split(/\r\n\r\n/) parts = imap_message.split(/\r\n\r\n/)
@message_header = parts[0] @message_header = parts[0]
logger.custom('before_parse',Time.now)
@mail = Mail.new(imap_message) @mail = Mail.new(imap_message)
logger.custom('after_parse',Time.now)
if @mail.multipart? if @mail.multipart?
Attachment.fromMultiParts(@attachments,@message.id,@mail.parts) Attachment.fromMultiParts(@attachments,@message.id,@mail.parts)
else else
Attachment.fromSinglePart(@attachments,@message.id,@mail) Attachment.fromSinglePart(@attachments,@message.id,@mail)
end end
logger.custom('attach',Time.now)
for idx in 0..@attachments.size-1 for idx in 0..@attachments.size-1
@attachments[idx].isText? ? @render_as_text << @attachments[idx].decode_and_charset : @render_as_text @attachments[idx].isText? ? @render_as_text << @attachments[idx].decode_and_charset : @render_as_text
@attachments[idx].isHtml? ? @render_as_html_idx ||= idx : @render_as_html_idx @attachments[idx].isHtml? ? @render_as_html_idx ||= idx : @render_as_html_idx
end end
logger.custom('done',Time.now)
end end
def body def body
attachments = [] attachments = []
cids = []
message = @current_user.messages.find(params[:id]) message = @current_user.messages.find(params[:id])
mail = Mail.new(@mailbox.fetch_body(message.uid)) mail = Mail.new(@mailbox.fetch_body(message.uid))
if mail.multipart? if mail.multipart?
Attachment.fromMultiParts(attachments,message.id,mail.parts) Attachment.fromMultiParts(attachments,message.id,mail.parts)
else else
Attachment.fromSinglePart(attachments,message.id,mail) Attachment.fromSinglePart(attachments,message.id,mail)
end end
a = attachments[params[:idx].to_i] html = attachments[params[:idx].to_i]
@title = 'aaaaa'
@body = a.decode_and_charset @body = html.decode_and_charset
#
#header = parts[0] for idx in 0..attachments.size-1
#body = parts[1] if not attachments[idx].cid.size.zero?
#@body = "<html><head><title>ala</title><body><pre>#{header}</pre>#{mail.inspect}</body></html>" @body.gsub!(/cid:#{attachments[idx].cid}/,messages_attachment_download_path(message.uid,idx))
end
end
render 'html_view',:layout => 'html_view' render 'html_view',:layout => 'html_view'
end end

View file

@ -3,7 +3,7 @@ class Attachment
include ActiveModel::Conversion include ActiveModel::Conversion
extend ActiveModel::Naming extend ActiveModel::Naming
attr_accessor :type, :charset, :encoding, :name, :description, :content, :message_id, :idx, :boundary, :format, :multipart attr_accessor :type, :charset, :encoding, :name, :description, :content, :message_id, :idx, :boundary, :format, :multipart,:size,:cid
attr_reader :link attr_reader :link
def initialize(attributes = {}) def initialize(attributes = {})
@ -33,13 +33,22 @@ class Attachment
end end
def self.fromMultiParts(attachments,id,parts) def self.fromMultiParts(attachments,id,parts)
cid = ''
parts.each do |part| parts.each do |part|
if not part.content_id.nil?
part.content_id =~ /\<(\S+)\>/
cid = $1
else
cid = ''
end
a = Attachment.new( :message_id => id, a = Attachment.new( :message_id => id,
:description => part.content_description, :description => part.content_description,
:type => part.content_type, :type => part.content_type,
:content => part.body.raw_source, :content => part.body.raw_source,
:encoding => part.content_transfer_encoding, :encoding => part.content_transfer_encoding,
:multipart => part.multipart? :size => part.body.raw_source.size,
:multipart => part.multipart?,
:cid => cid
) )
if a.multipart? if a.multipart?
fromMultiParts(attachments,id,part.parts) fromMultiParts(attachments,id,part.parts)
@ -54,9 +63,9 @@ class Attachment
:description => part.content_description, :description => part.content_description,
:type => part.content_type, :type => part.content_type,
:encoding => part.body.encoding, :encoding => part.body.encoding,
:charset => part.body.charset, :content => part.body.raw_source,
:content => part.body.raw_source :size => part.body.raw_source.size,
:charset => part.body.charset
) )
attachments << a attachments << a
end end
@ -156,7 +165,7 @@ class Attachment
if not @charset == 'UTF-8' if not @charset == 'UTF-8'
@charset.nil? ? charset = $defaults["msg_unknown_encoding"] : charset = @charset @charset.nil? ? charset = $defaults["msg_unknown_encoding"] : charset = @charset
charseted = Iconv.iconv("UTF-8",charset,decoded) charseted = Iconv.iconv("UTF-8",charset,decoded).first
else else
charseted = decoded charseted = decoded
end end

View file

@ -40,4 +40,4 @@ mailbox_sent: INBOX.sent
mailbox_drafts: INBOX.drafts mailbox_drafts: INBOX.drafts
# array of logins which only can login to application, comment it to allow everyone to login # array of logins which only can login to application, comment it to allow everyone to login
only_can_logins: [wojciech@todryk.pl] only_can_logins: [some_login]

View file

@ -11,6 +11,9 @@
<%= attachment.encoding %> <%= attachment.encoding %>
</td> </td>
<td> <td>
<%= size_formatter(attachment.size) %>
</td>
<td>
<%= link_to t(:download), messages_attachment_download_path(attachment.message_id,attachment.idx) %> <%= link_to t(:download), messages_attachment_download_path(attachment.message_id,attachment.idx) %>
</td> </td>
<td> <td>