Flex_image and bulk import

I’m using the excellent flex_image plugin for a new project and have
hit a bit of a road block.

I need to import a large number of images and so have been looking at
an alternative to just using a simple html form to upload the images
one at a time.

something like…

fp = open(‘one.jpg’,‘rb’)
file = fp.read
p = Picture.create(:data=>file)

gives an error…

FlexImage::Model::InvalidImage: Uploaded file contains no binary
data. Be sure that {:multipart => true} is set on your form.
from ./script/…/config/…/config/…/vendor/plugins/flex_image/
lib/flex_image/model.rb:153:in `data=’

Anyone have an idea how I can get round this?

Can you use url to the images you want to import? Flex image is
supporting that otherwise you can hack the code for local file system.

For urls;

Picture.create(:data => “http://localhost/import/1.jpg”)

Gokhan A.
www.sylow.net
kevin evans wrote:

I’m using the excellent flex_image plugin for a new project and have
hit a bit of a road block.

I need to import a large number of images and so have been looking at
an alternative to just using a simple html form to upload the images
one at a time.

something like…

fp = open(‘one.jpg’,‘rb’)
file = fp.read
p = Picture.create(:data=>file)

gives an error…

FlexImage::Model::InvalidImage: Uploaded file contains no binary
data. Be sure that {:multipart => true} is set on your form.
from ./script/…/config/…/config/…/vendor/plugins/flex_image/
lib/flex_image/model.rb:153:in `data=’

Anyone have an idea how I can get round this?

This seems to work fine in the batch processor I’m using with FlexImage:

@photo = Photo.new()

@photo.data = File.new("/path/to/image/#{image}")

@photo.save

Where image is just the filename of the image. Took a lot of trial and
error
to get to that point, but seems to work fine (this is a little longhand
because this is just small subset of the batch … I’ve also got a lot
of
IPTC/EXIF fiddling in there).

Thanks for reminding me, I forgot I adjusted FlexImage to handle this
(sorry).

In flex_image/lib/flex_image/model.rb , there’s a line (156) in the
data=(file) method that looks like this:

  if file.size > 0

The problem I ran into was the one you ran into, file.size doesn’t work
with
File.new (apparently) … however, file.stat.size does.

So, I added this above that line:

  begin
    size = file.stat.size
  rescue
    size = file.size
  end

and replaced that line with:

  if size > 0

The whole thing:

  begin
    size = file.stat.size
  rescue
    size = file.size
  end

  if size > 0

Sorry again that I forgot to mention that. And yes, there’s probably a
more
elegant way than fiddling directly in the FlexImage code. In fact,
there’s
probably a more elegant way to handle this in general (feel free to fill
me
in).

Chris,

That works great - thanks again.

Kevin

There is an easier way. Just wrap your data in a StringIO object:

file = open(filename, ‘rb’).read
Photo.create :data => StringIO.new(file)

Thanks for the replies guys,

Chris,

I tried your method under irb and get…

@picture = Picture.new()
=> #<Picture:0x3dfa310 @new_record=true, @invalid_image=false,
@attributes={“caption”=>nil, “data”=>nil}>

@picture.caption = “A test”
=> “A test”

@picture.data = File.new("/Users/kwe/Desktop/two.jpg")
NoMethodError: undefined method size' for #<File:/Users/kwe/Desktop/ two.jpg> from ./script/../config/../config/../vendor/plugins/flex_image/ lib/flex_image/model.rb:156:indata=’
from (irb):3

I’m not that familiar with ruby, my File class does not seem to have
a .size method !?!?

ie.

@f = File.new(’/Users/kwe/Desktop/two.jpg’)
=> #<File:/Users/kwe/Desktop/two.jpg>

@f.size
NoMethodError: undefined method `size’ for #<File:/Users/kwe/Desktop/
two.jpg>
from (irb):11

@f.stat
=> #<File::Stat dev=0xe000002, ino=1008872, mode=0100644, nlink=1,
uid=501, gid=501, rdev=0x0, size=305935, blksize=4096, blocks=600,
atime=Tue Jun 05 16:17:54 +0100 2007, mtime=Thu Apr 26 16:35:18 +0100
2007, ctime=Tue Jun 05 16:17:46 +0100 2007>

Any ideas?

thanks
Kevin