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

View file

@ -3,7 +3,7 @@ class Attachment
include ActiveModel::Conversion
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
def initialize(attributes = {})
@ -33,13 +33,22 @@ class Attachment
end
def self.fromMultiParts(attachments,id,parts)
cid = ''
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,
:description => part.content_description,
:type => part.content_type,
:content => part.body.raw_source,
:encoding => part.content_transfer_encoding,
:multipart => part.multipart?
:size => part.body.raw_source.size,
:multipart => part.multipart?,
:cid => cid
)
if a.multipart?
fromMultiParts(attachments,id,part.parts)
@ -54,9 +63,9 @@ class Attachment
:description => part.content_description,
:type => part.content_type,
: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
end
@ -156,7 +165,7 @@ class Attachment
if not @charset == 'UTF-8'
@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
charseted = decoded
end

View file

@ -40,4 +40,4 @@ mailbox_sent: INBOX.sent
mailbox_drafts: INBOX.drafts
# 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 %>
</td>
<td>
<%= size_formatter(attachment.size) %>
</td>
<td>
<%= link_to t(:download), messages_attachment_download_path(attachment.message_id,attachment.idx) %>
</td>
<td>