Email attachment -> RMagick

I am trying to save an email attachment as a blob to the db. I’m trying
to it within our existing framework which uses RMagick to read the image
as a blob. When it does this I get a “JPEG datastream contains no image”
error. Here is my code.

// Handles receiving the email
class AssetSubmitHandler < ActionMailer::Base

def receive(email)
if email.has_attachments?
email.attachments.each do |attachment|
img = Photo.create(:original_data => attachment)
asset = Asset.create(:user_id => 2)
asset.resource = img
asset.save
end
end
end
end

//Handles saving the image
class Photo < ActiveRecord::Base
//…some code

def original_data=(file)
if file.size > 0
if file.respond_to? :read
img = Magick::Image.from_blob(file.read)[0] //this call causes
the error
else
img = Magick::Image.from_blob(file)[0]
end

  img.format = 'JPG'
  self[:original_data] = img.to_blob

  self.width = img.columns
  self.height = img.rows
end
GC.start

end

//…more code
end

Do I need to base 64 decode the attachment? I could find a library
function to do this.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs