scene=3 is not a valid IO object (it doesn't respond to #eof?, #rewind, #close)

211 views
Skip to first unread message

shoga...@gmail.com

unread,
Dec 13, 2016, 1:52:08 PM12/13/16
to Shrine




Upload pdf and convert all pages to images one by one and store against one db record.


Here is code.


require 'RMagick'

class PdfUploader < Shrine
  include ImageProcessing::MiniMagick

  plugin :activerecord
  plugin :determine_mime_type
  plugin :logging, logger: Rails.logger
  plugin :remove_attachment
  plugin :store_dimensions
  plugin :validation_helpers
  plugin :versions, names: [:original,:pngs]
  plugin :add_metadata
  plugin :processing

  add_metadata :pages do |io, context|
    PDF::Reader.new(io.path).page_count
  end
  
  Attacher.validate do
    validate_max_size 50.megabytes, message: 'is too large (max is 2 MB)'
    validate_mime_type_inclusion ['application/pdf']
  end

 def process(io, context)
    case context[:phase]
      when :store
        pngs = if io.mime_type == 'application/pdf'
          tempPdf = io.download
          pdf = Magick::ImageList.new(tempPdf.path)
          pdf.each_with_index do |page_img, i|              
            path = Rails.root.join('tmp',"#{i}_pdf_page.png").to_s
            image = page_img.write path 
          end
        end
        # raise pngs
        { original: io,pngs: pngs}
    end
  end

end

Please help me whats wrong?
Thanks

Janko Marohnić

unread,
Dec 13, 2016, 9:57:06 PM12/13/16
to shoga...@gmail.com, Shrine
This means that a file you passed to Shrine (one of the version objects) is not a valid IO object. It looks like you passed an array RMagick objects.

The simplest way is to just `File.open(path_to_file)`, and pass that opened File object as a version. You can probably just call #path on the RMagick object to get thr path.

Kind regards,
Janko
--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine...@googlegroups.com.
To post to this group, send email to ruby-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/4b887883-9c62-4a9b-aec5-8d5c7d24dfc9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

shoga...@gmail.com

unread,
Dec 17, 2016, 12:17:35 AM12/17/16
to Shrine, shoga...@gmail.com
Thanks for you valuable reply.

Actually i wan to store multiple images in a single record in json form and keep original PDF document as well.here is versioning [:original,:pngs]

and process method.

 def process(io, context)
    case context[:phase]
      when :store
        pngs = if io.mime_type == 'application/pdf'
 
 pngs = []
images_array = ['1.jpg','2.jpg','3.jpg']

image_path_is =  Rails.root.join('lib','tasks',images_array.sample).to_s
pngs1  = File.open(image_path_is)

image_path_is =  Rails.root.join('lib','tasks',images_array.sample).to_s
pngs2  = File.open(image_path_is)

pngs.push(pngs1)
pngs.push(pngs2)

        end
          { original: io,pngs: pngs}
    end
  end

my question is how to pass converted images to shrine to update json data properly?

Thanks

shoga...@gmail.com

unread,
Dec 19, 2016, 2:08:17 AM12/19/16
to Shrine, shoga...@gmail.com
Hi friends,
Please help me on above post i have read so many posts over the internet and read documentation but could not grasp the idea .
May be i am new to ruby and rails environment that is why some things are not understandable to me.
Warmly Thanks.

shoga...@gmail.com

unread,
Dec 20, 2016, 8:06:15 AM12/20/16
to Shrine, shoga...@gmail.com

Hi,
Can any one answer in this group?
Thanks

Janko Marohnić

unread,
Dec 20, 2016, 8:33:30 AM12/20/16
to shoga...@gmail.com, Shrine
Sorry, I've been traveling a lot lately, and haven't managed to find time to answer.

Shrine currently doesn't support storing an array of files, though that support might get added in the future. At the moment you could just create version for each png in collection, and add the index number to the name:

  versions = {original: original}

  pngs.each_with_index do |png, idx|
    versions[:"png_#{idx}"] = png
  end

  versions

That way you end up with the following versions:

  :original
  :png_0
  :png_1
  :png_2
  ...

Kind regards,
Janko

To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine+unsubscribe@googlegroups.com.

To post to this group, send email to ruby-...@googlegroups.com.

shoga...@gmail.com

unread,
Dec 21, 2016, 7:01:02 AM12/21/16
to Shrine, shoga...@gmail.com


Hi Janko

Thanks for answering.I know  you are be busy but i was expecting from other group fellows.

Now this problem was resolved with new error.

I think its coming from version plugin.

Here is quick video 
https://www.dropbox.com/s/55j690y1z8brun5/1.mkv?dl=0

here is code
https://www.dropbox.com/s/n7x21onosqybowa/shrine-pratice.zip?dl=0

Hope you never mind to help :) 

Thanks  Janko
Janko


Janko Marohnić

unread,
Dec 21, 2016, 7:48:40 AM12/21/16
to shoga...@gmail.com, Shrine
The problem is that you've created nested versions:

{
  original: ...,
  pngs: {
    png_0: ...,
    png_1: ...,
    png_2: ...,
    ...
  }
}

Shrine doesn't support that (at least not at the moment), you need to assign all versions on the same level of the hash:

{
  original: ...,
  png_0: ...,
  png_1: ...,
  png_2: ...,
  ...
}

Kind regards,
Janko

To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine+unsubscribe@googlegroups.com.

To post to this group, send email to ruby-...@googlegroups.com.

shoga...@gmail.com

unread,
Dec 21, 2016, 8:34:22 AM12/21/16
to Shrine, shoga...@gmail.com
Hi janko

Thanks alot bro - fixed - you are great.

Thanks
Zeeshan
Reply all
Reply to author
Forward
0 new messages