I'm using a mail server that makes JSON requests to my app to notify about incoming mail. When processing attachments, it gives me a raw:

    a1.name
    # => e.g. 'sample.pdf'
    a1.type
    # => e.g. 'application/pdf'
    a1.content
    # => this is the raw content provided by Mandrill, and will be base64-encoded if not plain text
    # e.g. 'JVBERi0xLjMKJcTl8uXrp/Og0MTGCjQgMCBvY ... (etc)'
    a1.decoded_content
    # => this is the content decoded by Mandrill::Rails, ready to be written as a File or whatever
    # e.g. '%PDF-1.3\n%\xC4\xE5 ... (etc)'

What I'm trying to do is turn this into some sort of Rails 'file' object that's suitable to process with Paperclip.

Any thoughts how best to approach this?

Many thanks, Chris.

有帮助吗?

解决方案

I just solved this, pieced this together from other answers on SO.

file = StringIO.new(a1.decoded_content)
file.class.class_eval { attr_accessor :original_filename, :content_type}
file.original_filename = a1.name
file.content_type = a1.type
@d = Document.create #Document is my model that has a paperclip attachment called doc
@d.doc = file
@d.save

Good luck!

Ben

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top