Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Images not being replaced #105

Closed
jptosso opened this issue May 7, 2018 · 5 comments
Closed

Images not being replaced #105

jptosso opened this issue May 7, 2018 · 5 comments
Labels

Comments

@jptosso
Copy link

jptosso commented May 7, 2018

Hello

I have a document where I try to replace an image without success. The sourcecode to add the image is:

      v.vulnerability_files.where("file_content_type LIKE 'image/%'").each_with_index do |img, ii|
        fname = "/tmp/image-#{SecureRandom.urlsafe_base64}." + img.file.original_filename.split(".")[-1]
        img.file.copy_to_local_file(:original, fname)
        media.push(Sablon.content(:image, fname))
      end

While the document structure is this:
screen shot 2018-05-07 at 10 52 06 am

I hope we can find a solution.

Thank you

@stadelmanma
Copy link
Collaborator

stadelmanma commented May 12, 2018

Hi @asdfuken do you get any kind of error? Or is your placeholder image simply replicated for each item in your media array?

Otherwise your code looks fine to my eyes, assuming the media array you push items to gets added into the context in such a way that vulnerability.media accesses it. The other thing I would check is if you are using the most recent release of sablon or at least one recent enough to support images.

Perhaps you could attach a copy of the problem template where you have removed everything from it except the media loop where you are having issues and I can try to reproduce the problem locally. Assuming that after you remove everything the problem still occurs, if it doesn't I'd try and backtrack what change you made that fixed the problem.

@jptosso
Copy link
Author

jptosso commented Aug 20, 2018

Hello, I have updated my code as following:

    @vulnerabilities.each_with_index do |v, i|
      data = {
..........
        'media': []
        }
      media = []
      v.files_blobs.where("content_type LIKE 'image/%'").each_with_index do |img, ii|
        fname = "/tmp/image-#{SecureRandom.urlsafe_base64}" 
        File.open(fname, 'wb') do |file|
           file.write(img.download)
        end     
        content = StringIO.new(IO.binread(fname))
        mediadata = Sablon.content(:image, content, filename: "#{SecureRandom.urlsafe_base64}.png")
        media.push({
          :image => mediadata,
          :caption => ""
          })
      end
      data[:media] = media
      vulnerabilities.push(data)
    end
......


    context = {
.......
      vulnerabilities: vulnerabilities,
    }
    template.render_to_file File.expand_path(outfile), context

And for the word document:
capture

This time I have a stack error:

NoMethodError: undefined method `next_element' for nil:NilClass
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/processor/document/blocks.rb:58:in `body'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/processor/document/blocks.rb:97:in `replace'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/operations.rb:128:in `evaluate'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/processor/document.rb:75:in `block in manipulate'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/processor/document.rb:74:in `each'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/processor/document.rb:74:in `manipulate'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/processor/document.rb:61:in `process'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/processor/document/blocks.rb:37:in `process'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/operations.rb:22:in `block in evaluate'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/operations.rb:20:in `each'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/operations.rb:20:in `flat_map'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/operations.rb:20:in `evaluate'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/processor/document.rb:75:in `block in manipulate'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/processor/document.rb:74:in `each'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/processor/document.rb:74:in `manipulate'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/processor/document.rb:61:in `process'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/processor/document/blocks.rb:37:in `process'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/operations.rb:22:in `block in evaluate'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/operations.rb:20:in `each'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/operations.rb:20:in `flat_map'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/operations.rb:20:in `evaluate'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/processor/document.rb:75:in `block in manipulate'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/processor/document.rb:74:in `each'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/processor/document.rb:74:in `manipulate'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/processor/document.rb:61:in `process'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/template.rb:75:in `block (2 levels) in process'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/template.rb:75:in `each'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/template.rb:75:in `block in process'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/template.rb:72:in `each'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/template.rb:72:in `process'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/template.rb:61:in `render'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/template.rb:49:in `render_to_string'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/template.rb:43:in `block in render_to_file'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/template.rb:42:in `open'
	from /usr/local/rvm/gems/ruby-2.4.1/gems/sablon-0.3.0/lib/sablon/template.rb:42:in `render_to_file'
	from /home/ec2-user/environment/ehmanager/app/controllers/ehm/projects_controller.rb:286:in `report

Documento:
error_demo.docx

@stadelmanma
Copy link
Collaborator

Going back through old issues and I noticed this appears to be very similar to #109

@stadelmanma
Copy link
Collaborator

After doing some investigating I think this is due to the fact I have an each block and its contents all within a single paragraph and then those children get wrapped in a "tmp" node instead of whatever parent node type was being used before. This was discovered from a WordML insertion bug that happened when an entire conditional appeared within a single paragraph.

@stadelmanma
Copy link
Collaborator

Closing this as the root of the problem appears to be the same as #109.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants