Created
December 6, 2010 15:58
-
-
Save pedrodelgallego/730476 to your computer and use it in GitHub Desktop.
This class control the incoming mails, parse the information in it, extract the attached picture and upload it to amazon s3 using paperclip, A monkey patch hack is needed to solve a inconsistence between paperclip and heroku/cloudmailin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IncomingMailsController < ApplicationController | |
skip_before_filter :verify_authenticity_token | |
def create | |
message = Mail.new(params[:message]) | |
user = User.where(:email=>message.from).first | |
if !message.attachments.first.nil? | |
post = Post.new( | |
:title => message.subject, | |
:photo => parse_photo(message), | |
:user => user) | |
if post.save | |
render :text => 'success', :status => 200 | |
else | |
render :text => 'The picture was to big!', :status => 404 | |
end | |
else | |
render :text => 'Ups, You forget to attach a picture to the mail', :status => 404 | |
end | |
end | |
private | |
# We need that the file-like object that we are going to use | |
# for paperclip respond to original_filename and content_type | |
# so we create then wiht class eval as attr_accessor. | |
# | |
# Due that we are deploying in heroku we can not write to disk. | |
def parse_photo message | |
attachment = message.attachments.first | |
file = StringIO.new(attachment.decoded) | |
file.class.class_eval { attr_accessor :original_filename, :content_type } | |
file.original_filename = attachment.filename | |
file.content_type = attachment.mime_type | |
file | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment