acts_as_attachment - on server copy?

Hi all,

I am using aaa for uploads (Upload model) of assets to a dir below public. I also need to save files, into the Upload model, which come as attachments by email. Is there a way I can ask aaa to save the attachment as if it was uploaded? TIA,

bakki

bakki,

you should be loop through the email's attachments and create new Uploads like this:

for attachment in email.attachments   Upload.create! attachment end

# note: this is untested code, but should point you in the right direction.

Have a look at this article regarding Action Mailer's attachments:

http://wiki.rubyonrails.org/rails/pages/HowToReceiveEmailsWithActionMailer

Thank you for your reply Sam, I don't have my action mailer receive part working yet and I will try this when its done. However the Upload model has

acts_as_attachment :storage => :file_system, :file_system_path => 'uploads'   validates_as_attachment

I was wondering if acts_as_attachment can take an attachment passed to the model vs. the uploaded file which it normally gets from cgi param. I thought the validates_as_attachment call back might not work if the attachment was passed to the model as you suggest. I'll have to look the aaa code over to see if this doesn't work.

-bakki

here's a post on how to upload files stored locally:

http://www.railsweenie.com/forums/2/topics/1013

Hello,

I am not sure if I understand correctly, but you can pass a regular file (on disk) to the model conviently using TestUploadedFile (see http://api.rubyonrails.org/classes/ActionController/TestUploadedFile.html#M000278) like this:

  Photo.create( :uploaded_data => ActionController::TestUploadedFile.new(file, 'image/jpeg') )

See Parked at Loopia for more complete code I use for importing whole directory of photos into application.

(You can even use this in script/console or script/runner, thus automating the importing process.)

Cheers,

Karel

Hi Karel,

This actually should work for me. I was looking at the code in acts_as_attachment and it has the following code which handles uploaded files.

      # This method handles the uploaded file object. If you set the field name to uploaded_data, you don't need       # any special code in your controller.

Hello,

yes, that's it precisely. I guess acts_as_attachment has `TestUploadedFile` somehow encapsulated, but I couldn't get it to work, don't recall precisely why. I recently used `TestUploadedFile` to import loads of legacy data into ported application with *great* results.

Glad to help, cheers,

Karel