0

I store my files in a document table with paperclip.My files are word,csv,xlsx and pdf. I want to read these records from database and convert them into cdata and put them in an xml tag named Attachment.

This is the out put I'm looking forward:

<Attachment ContentType="xlsx" Extension="xlsx" Description="TEST2.xlsx"><![CDATA[UEsDBBQABgAIAAAAIQBxDjkrcAEAAKAFAAATANsBW0NvbnRlbnRfVHlwZXNdLnhtbCCi1wEooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA..

and my files are like this when I query them:

#<Paperclip::Attachment:0x000000049dfe28 @name=:picture, @name_string="picture", @instance=#<Document id: 225, picture_file_name: "data", picture_content_type: "application/vnd.openxmlformats-officedocument.word...", picture_file_size: 10001, picture_updated_at: "2018-02-07 20:14:18", ece_id: 242, created_at: "2018-02-07 20:14:18", updated_at: "2018-02-07 20:14:18", xml_file_name: nil, xml_content_type: nil, xml_file_size: nil, xml_updated_at: nil, whodunnit: nil, document_type: "Attachment">, @options={:convert_options=>{}, :default_style=>:original, :default_url=>"/:attachment/:style/missing.png", :escape_url=>true, :restricted_characters=>/[&$+,\/:;=?@<>\[\]\{\}\|\\\^~%# ]/, :filename_cleaner=>nil, :hash_data=>":class/:attachment/:id/:style/:updated_at", :hash_digest=>"SHA1", :interpolator=>Paperclip::Interpolations, :only_process=>[], :path=>":rails_root/public:url", :preserve_files=>false, :processors=>[:thumbnail], :source_file_options=>{}, :storage=>:filesystem, :styles=>{}, :url=>"/system/:class/:attachment/:id_partition/:style/:filename", :url_generator=>Paperclip::UrlGenerator, :use_default_time_zone=>true, :use_timestamp=>true, :whiny=>true, :validate_media_type=>true, :check_validity_before_processing=>true}, @post_processing=true, @queued_for_delete=[], @queued_for_write={}, @errors={}, @dirty=false, @interpolator=Paperclip::Interpolations, @url_generator=#<Paperclip::UrlGenerator:0x000000049dfd10 @attachment=#<Paperclip::Attachment:0x000000049dfe28 ...>>, @source_file_options={}, @whiny=true> 

I'm using nokogiri to generate the xml tags.

Do you have any ideas? Thanks in advance

Afsanefda
  • 3,069
  • 6
  • 36
  • 76

1 Answers1

1

You can read the file content with Paperclip like this:

file_content = Paperclip.io_adapters.for(attachment.file).read

There are many ways to write a CDATA XML tag to your XML document with Nokogiri, depending on the way your are building your document, here's one from the cheat sheet:

doc.create_cdata(file_content)

However, you should encode the file content with base64 encoding, because the file content is binary, and it might have characters that are not allowed in XML documents.

So here it is:

require 'base64' # if not already required
Base64.encode64(file_content)

All together, here's a pseudo snippet:

file_content = Paperclip.io_adapters.for(attachment.file).read
doc.create_cdata(Base64.encode64(file_content))

You should also consider using Base64 element instead of CDATA, checkout this and this answers (and other on the same question).

Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82