Writing a new plugin for handling email attachments

I’m trying to create a plugin to handle Mail gem’s attachment.

I get the content of the mail attachment by calling the attachment#decoded. This returns me a string.

I’m thinking of writing the content to a tempfile(with the filename, content type, and all) and going with the usual workflow of creating an attachment(the activerecord workflow where file is assigned when creating a record, and shrine uploads it).

The question is, is this a good way to deal with something like this?

Hi, just wrap the file content string in a StringIO on assignment and you should be good to go.

photo.image = StringIO.new("file content")

If you want to assign specific metadata, you can use Attacher#assign:

photo.image_attacher.assign(StringIO.new("file content"), metadata: { "filename" => "image.jpg", "mime_type" => "image/jpeg" })

Thank you very much Janko! This is what I was considering to do. I was weighing it versus using data_uri.

I think I will go with this method. It feels to me more straightforward than the data_uri way.