How do I replace an attachment in an email with the Rails mail gem? - ruby-on-rails

My program is dealing with emails which arrive as files (something.eml). In some circumstances I need to amend an attachment and then resave the file. I've been using the instructions here as a basis for my code, but there's no suggestion for trying to do exactly what I'd like. The code I have below successfully removes the original attachment and then tries to add in a new one.
#email.without_attachments!
#email.add_file(amended_version)
Unfortunately it goes wrong in two places. Firstly it seems to remove all mime parts, not just attachments. Any text/plain sections are also ditched. Secondly, if I test by reloading my amended .eml file, the attachment is no longer recognised, despite being in the file.
I've included a gist which includes the original and amended files from my current method.
Is there a better way to do this? Perhaps a way of replacing the attachment directly rather than get rid of it and adding again?

I don't know enough about mail formatting to know why this works, but it does.
I extracted just the line I was interested in from the without_attachments! method and it now seems to work fine. The non-attachment parts of the message are kept intact and the message re-reads fine. Code now reads....
#email.parts.delete_if { |p| p.attachment? }
#email.add_file(amended_version)

Related

List of System.IO.Packaging ContentType strings?

I'm updating some older code that uses System.IO.Packaging to programmatically create Excel files. It ultimately calls CreatePackage on various bits of internal state to build out the documents. CreatePackage takes a ContentType parameter, and the existing code contains a list of constants like:
Const cxl07WorksheetContentType = "application/vnd...."
I'm trying to add support for PivotTables, which require a PivotCache. I cannot find the appropriate ContentType. I thought I might be able to discern it from within the file, looking in _Rels for instance, but these are always in URI form and bear no obvious relationship to these constants.
So...
is this even required? I passed Nothing and "" but that did not work.
does anyone know where these might be defined? I looked on the mime database, and the MS website, but nothing came up on either for "pivot"
where are these even used? they do not appear in the resulting Package as far as I can see.

How to make Carrierwave add random string to the file path?

I have this kind of hot fix:
= image_tag "#{current_candidate.profile.photo_url}?stamp=#{SecureRandom.hex}"
The reason for this is that I made a "crop" feature, and when users recrop their photo - old version gets displayed from browser cache.
Anyway.
This photo_url is called in many places, and rather than making a helper & call it everywhere I thought it would be nice to configure uploader to do that.
Is that possible?
Note: filename can't be random. For other reasons.
I would imagine it has something to do with overriding photo_url method.

Test PDF content sent with ActionMailer

I'm trying to test specific content inside a pdf document which is being generated via the prawn gem and attached to an ActionMailer email. I'm using RSpec and Capybara for testing.
I've managed to test the filename like this
expect(ActionMailer::Base.deliveries[0].filename).to eq("my_file.pdf")
I thought that I've read somewhere that I have to test the pdf itself like this but it doesn't work.
`expect(ActionMailer::Base.deliveries[0].body.encoded).to have_content(user.first_name)``
When running the test, I get the following error:
Failure/Error: expect(ActionMailer::Base.deliveries[0].body.encoded).to have_content(user.first_name)
expected to find text "John" in "JVBERi0xLjQKJf////8KMSAwIG9iago8PCAvQ3JlYXRvciA8ZmVmZjAwNTAw MDcyMDA2MTAwNzcwMDZlPgovUHJvZHVjZXIgPGZlZmYwMDUwMDA3MjAwNjEw MDc3MDA2ZT4KPj4KZW5kb2JqCjIgMCBvYmoKPDwgL1R5cGUgL0NhdGFsb2cK L1BhZ2VzIDMgMCBSCj4+CmVuZG9iagozIDAgb2JqCjw8IC9UeXBlIC9QYWdl cwovQ291bnQgMQovS2lkcyBbNSAwIFJdCj4+CmVuZG9iago0IDAgb2JqCjw8 IC9MZW5ndGggMzE2NAo+PgpzdHJlYW0KcQpxCi9UcjEgZ3MKMjA2LjAwMCA2 NTYuMDAwIDIwMC4wMDAgMTAwLjAwMCByZQpTClEKCnEKNDAwLjAwMCAwIDAg NTAuMDAwIDEwNi4wMDAgNzA2LjAwMCBjbQovSTEgRG8KUQpxCi9UcjEgZ3MK MjA2LjAwMCA2NTYuMDAwIDIwMC4wMDAgMTAwLjAwMCByZQpTClEKCkJUCjIx OC40ODI1NTg1OTM3NSA2NDIuODk2IFRkCi9GMS4wIDE4IFRmCls8NTQ+IDEx MC44Mzk4NDM3NSA8NjU2OTZjNmU2MTY4NmQ2NTYyNjU3Mzc0OGE3NDY5Njc3 NTZlNjc+XSBUSgpFVAoKCkJUCjM2IDYyMy4zMjk5OTk5OTk5OTk5IFRkCkVU CgoKQlQKMzYgNjAzLjc2Mzk5OTk5OTk5OTkgVGQKRVQKCgpCVAoyNDguODUy
This text continues a long time.
Maybe it's just me, but this doesn't exactly look "testable" to me. Does someone know to do this? Thanks!
Firstly, your PDF-generation code should really be separate from your mailer code, and should have its own tests separate from your mailer tests. Please do this first.
Once you've separated your PDF-generation code and tests you can generate your PDF and test its content using the pdf-inspector gem, which is helpfully maintained by the same folks who make Prawn. Then in your mailer tests you can simply check whether the file is attached, using something like this.
P.S. The reason the email content looks garbled (JVBERi0xLjQ...) is that email attachments are (usually) Base64-encoded. But even if you decoded it, you might not be able to search the PDF content for a plaintext string without a library like pdf-inspector because it might be compressed (I don't know if Prawn does this by default). But really, your PDF code and tests and your email code and tests should be completely separate.

How to download image from url and display in view

I am trying to download an image and displaying it in a view in rails.
The reason why I want to download it is because the url contains some api-keys which I am not very fond of giving away.
The solution I have tried thus far is the following:
#Model.rb file
def getUrlMethod
someUrlToAPNGfile = "whatever.png"
file = Tempfile.new(['imageprependname', '.png'], :encoding => "ascii-8bit")
file.write(open(data).read)
return "#{Rails.application.config.action_mailer.default_url_options[:host]}#{file.path}"
end
#This seems to be downloading the image just fine. However the url that is returned does not point to a legal place
Under development I get this URL for the picture: localhost:3000/var/folders/18/94qgts592sq_yq45fnthpzxh0000gn/T/imageprependname20130827-97433-10esqxh.png
That image link does not point anywhere useful.
My theories to what might be wrong is:
The tempfile is deleted before the user can request it
The url points to the wrong place
The url is not a legal route in the routes file
A am currently not aware of any way to fix either of these. Any help?
By the way: I do not need to store the picture after I have displayed it, as it will be changing constantly from the source.
I can think of two options:
First, embed the image directly in the HTML documents, see
http://www.techerator.com/2011/12/how-to-embed-images-directly-into-your-html/
http://webcodertools.com/imagetobase64converter
Second, in the HTML documents, write the image tag as usual:
<img src="/remote_images/show/whatever.png" alt="whatever" />
Then you create a RemoteImages controller to process the requests for images. In the action show, the images will be downloaded and returned with send_data.
You don't have to manage temporary files with both of these options.
You can save the file anywhere in the public folder of the rails application. The right path would be something like this #{Rails.root}/public/myimages/<image_name>.png and then you can refer to it with a URL like this http://localhost:3000/myimages/<image_name>.png. Hope this will help.

file_field_tag : what was the original file name?

My site allows users to upload csv files for processing. It all works
fine, but on the response I'd like to report something like "Your file
abc.csv processed OK".
Unfortunately I cannot seem to find the actual original file name in
the params, even though Firebug tells me it's part of the post.
Any tips?
Thanks....
Try using debug on the results of your form.
http://guides.rubyonrails.org/debugging_rails_applications.html#debug
As Jarrod mentions in the comments above. Use params[:file].original_filename
Funny thing is, my form has two file upload tags (file1 and file2). One comes in as a ActionController::UploadedTempfile and the other ActionController::UploadedStringIO.
This may be a rails bug but it doesn't matter to me as both have the original_filename method.

Resources