3

I have a bunch of jpeg files in a folder on my server, and I'm attempting to attach them to their corresponding Property instances through a rake task.

property.rb has the following code:

  has_attached_file :temp_photo,
    :styles => PropertyImage::STYLES,
    :url => "/assets/:class/:attachment/:id_partition/:style_:basename.:extension",
    :path => "#{Rails.root}/public/assets/:class/:attachment/:id_partition/:style_:basename.:extension"

I use paperclip on other models, and there are no issues whatsoever, but I get a problem when I attempt the following:

p = Property.find(id)
file = File.open(temp_file_path)
p.temp_photo = file
p.save

# => false

file.close
p.errors

# => "/tmp/stream20110524-1126-1cunv0y-0.jpg is not recognized by the 'identify' command."

The file definitely exists, and I've tried changing the permissions. Restarting the server doesn't help. The problem seems to be with using the command line, as the normal form / HTTP approach works fine. This is only a temporary set-up, so I'm looking for a working way to import a batch of files into my rails app paperclip model.

Any suggestions?

Jeriko
  • 6,547
  • 4
  • 28
  • 40
  • have a look at this [answer](http://stackoverflow.com/questions/1996102/rails-paperclip-and-passenger-is-not-recognized-by-the-identify-command) – sled May 24 '11 at 10:45
  • Nope, I've tried that already, it seemed to make no difference. As I said, it works fine when I add images over apache - the problem is in a rake task / console. – Jeriko May 24 '11 at 11:08
  • 1
    Are you sure that the image is a correct one? What happens if you manually invoke `identify /tmp/stream2011....`? Maybe some newer version will work for you - I can without problems import files with paperclip 2.4.0 – Arsen7 Sep 12 '11 at 14:56

1 Answers1

5
path = 'target_file_path'
attach_name = 'temp_photo'

p = Property.find(id)
attach = Paperclip::Attachment.new(attach_name, p, p.class.attachment_definitions[attach_name.to_suym])

file = File.open(path) 
attach.assign file
attach.save

file.close
ukitazume
  • 51
  • 1
  • 4