Skip to content

Commit 306d590

Browse files
committed
Lets add the ability to parse and understand multipart messages and store all attachments in gridfs and simply call it as an image (we could check for images or other types later)
1 parent 2f77bd0 commit 306d590

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

email_cms.rb

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,52 @@
1515
haml :post
1616
end
1717

18+
get '/delete' do
19+
Post.destroy_all
20+
'Deleted all posts'
21+
end
22+
1823
post '/incoming_message/' do
1924
message = Mail.new(params[:message])
2025

21-
@post = Post.create!(:title => message.subject, :body => message.body.decoded)
26+
if message.multipart?
27+
attachments = []
28+
body = ""
29+
30+
message.parts.each do |part|
31+
if part.content_type =~ /^text\/plain/i
32+
body << part.body.decoded
33+
elsif part.attachment?
34+
@grid ||= Mongo::Grid::new(Mongoid.database)
35+
file = @grid.put(part.body.decoded)
36+
attachments << Attachment.new(:grid_id => file)
37+
end
38+
end
39+
40+
@post = Post.create!(:title => message.subject, :body => body, :attachments => attachments)
41+
else
42+
@post = Post.create!(:title => message.subject, :body => message.body.decoded)
43+
end
44+
2245
haml :post
2346
end
2447

48+
get '/images/:id' do
49+
grid = Mongo::Grid::new(Mongoid.database)
50+
grid.get(BSON::ObjectID.from_string(params[:id])).read
51+
end
52+
2553
class Post
2654
include Mongoid::Document
2755
field :title
2856
field :body
57+
embeds_many :attachments
58+
end
59+
60+
class Attachment
61+
include Mongoid::Document
62+
63+
field :grid_id
2964
end
3065

3166
__END__
@@ -45,7 +80,8 @@ class Post
4580
%a{:href => "/#{post.id}"}= post.title
4681
#content
4782
= post.body
48-
83+
- for attachment in post.attachments do
84+
%img{:src => "/images/#{attachment.grid_id}"}
4985
@@post
5086
%h1= @post.title
5187
#content

0 commit comments

Comments
 (0)