Rails / Paperclip attaching via command line - ruby-on-rails

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?

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

Related

Rails, S3, Paperclip changes attachment file .epp extension to .txt

I have trouble setting my application correctly, to store attachments with .epp extension (which has file -b --mime-type = text/plain) and serve them to users with correct extenstion (it changes to .txt).
After adding to initializers:
Paperclip.options[:content_type_mappings] = {
:epp => 'text/plain'
}
I am able to upload the file to s3, without receiving the spoofed_media_type error. However, when I try to serve the file for user with:
redirect_to #job.file.expiring_url
it downloads with .txt extension. The file is saved with the following code in the job class:
...
has_attached_file :file, :s3_permissions => 'authenticated-read', :s3_headers => {"Content-Disposition" => "attachment"}
do_not_validate_attachment_file_type :file
...
self.file = File.open(file_path)
self.save!
Any ideas what could be the problem?
Update:
Actually it happens only with Chrome, Firefox downloads the file correctly, so it might be the browser issue...
I am guessing your paperclip.rb initializer looks something like this:
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
Try changing the path to include the extension like so:
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename.:extension'

Paperclip don't generate styles and original image appears as broken

I having a big issue here. I really tried, but I can't solve this problem by myself, so I hope people can help me here.
Before talk about my problem, I must say I'm using Paperclip and IMGKit in my Project, but I think the problem is with Paperclip.
I create a Rails Task to take snapshots from the home page of some sites. Sometime ago everything is working fine, but now everything goes down. I import my real database from Heroku to localhost (without any images and migrations of paperclip), run the migrations, delete all old files from 'public/system' and run my task again (to take snapshot of all websites).
So, now I have:
The paths and original images are generated, but when I try to load them in View, this just show as a broken image.
Paperclip doesn't generate the path and converted images of :styles.
Sites that don't have image, I can see my default image correctly.
ImageMagick seems to be working, I try convert some images and worked like a charm.
Let's take a look at the code. Assume that I'm running a task that will perform this task to all sites in my database. The sites are called "items" in my architecture.
Development.rb
#config/environments/development.rb
# "which convert" give me this path
Paperclip.options[:command_path] = "/usr/local/bin/"
Item.rb (model)
My task just call "object.save" of every site in the DB, so my code starts on before_save.
has_attached_file :image,
:styles => { :small => "200x147#" },
:convert_options => { :small => "-quality 75 -strip" },
:default_url => '/images/:style/bitcoin-earth.jpg'
before_save :generate_data
def generate_data
self.image = get_image(self.id, self.url_original)
end
# Take snapshot of the website
def get_image(filename, link)
kit = IMGKit.new(link.to_s, :quality => 100, :width => 1024, :height => 768)
file = Tempfile.new(["template_#{filename}", 'png'], 'tmp',
:encoding => 'ascii-8bit')
file.write(kit.to_img(:png))
file.flush
return file
end
View
<%= image_tag store.image.url %>
Gemfile
gem "paperclip"
If I try to run rake paperclip:refresh:missing_styles, the task finish very fast without any error. But if I try to run rake paperclip:refresh CLASS=Item I got:
Image Paperclip::Errors::NotIdentifiedByImageMagickError
And yes, I already search for it and I didn't found a solution for my case.
A little tip?
When I "inspect element" in my project and try to see the source of the item image, I saw:
http://localhost:3000/public/system/items/images/000/000/216/original/template_21620140109-14507-1c0yszzpng?1389305824
But if I go to my project folder, I just see a image called template_21620140109-21209-1yls03opng. Note that doesn't exist any "?1389305824" there. See the image above.
Well, I think that's it. What can be the problem? I really need solve this issue, please, help me :/
[ Edited on Jan 10, 2013 ]
Item.rb (model):
before_save :generate_data
def generate_data
file = File.open(get_image(self.id, self.url_original))
self.image = file
file.close
end
def get_image(filename, link)
kit = IMGKit.new(link.to_s, :quality => 100,
:width => 1024, :height => 768)
file = Tempfile.new(["template_#{filename}", '.png'], 'tmp',
:encoding => 'ascii-8bit')
file.write(kit.to_img(:png))
file.flush
return file
end
Now I don't have any error on console while getting images and saving on DB, but Paperclip still don't generate my :styles. When I go to log/development.log, I can see this error, but I don't know what I can do to solve:
Command :: file -b --mime 'tmp/template_24320140110-17577-80zj1c.png'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/template_24320140110-17577-80zj1c20140110-17577-mqa2q3.png[0]'
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
I think we're getting closer, please, keep helping me :)
I think your problem is here:
template_21620140109-14507-1c0yszzpng?1389305824 #-> should have .png (not a valid image)
Image
This might not be the problem, but maybe you could streamline your method to exclude the temporary file:
# Take snapshot of the website
def get_image(filename, link)
kit = IMGKit.new(link.to_s, :quality => 100, :width => 1024, :height => 768)
file = kit.to_file("system/temp/template_#{filename}")
return file
end
I think the issue is that ImageMagick is not being passed a "real" file, and consequently you're getting the unrecognized image issues

Paperclip::NotIdentifiedByImageMagickError image is not recognized by the 'identify' command

i´m getting this error when editing a model specifically when i delete an image associated to it and I select another:
Paperclip::NotIdentifiedByImageMagickError in Admin/packsController#update
Chrysanthemumprueba4.jpg is not recognized by the 'identify' command.
C:/Users/.../vendor/plugins/thoughtbot-paperclip-fc792c8/lib/paperclip/geometry.rb:24:in `from_file'
But when i create a new pack and I select images for it, it works ok.
I have two tables: packs and pack_images,and pack_images has the photos for the pack associated, here are the relations:
class Pack < ActiveRecord::Base
has_many :pack_images, :dependent => :destroy
end
class PackImage < ActiveRecord::Base
belongs_to :pack
attr_accessor :height, :width
has_attached_file :photo, :url => "/:attachment/:class/:id/:style_:basename.:extension", :styles => {:principal => "240x240>", :original => "400x400>", ...}
end
This is the controller's action that throws me the error:
def update
#pack = Pack.find(params[:id])
#pack.pack_products
unless params[:pack][:pack_images_attributes].nil?
params[:pack][:pack_images_attributes].count.times do |i|
unless params[:pack][:pack_images_attributes][:"#{i.to_s}"][:photo].blank?
file = params[:pack][:pack_images_attributes][:"#{i.to_s}"][:photo]
dimensions = Paperclip::Geometry.from_file(file)
#pack.pack_images[i].width = dimensions.width
#pack.pack_images[i].height = dimensions.height
end
end
end
respond_to do |format|
#pack.update_attributes(params[:pack])
format.html { redirect_to(admin_pack_path(#pack.id), :notice => 'Pack updated') }
end
I noticed that, when updating I get less parameters (only the photo's name) than when creating (photo's name,file type,width,height,etc).
I hope you can help me
Thank you very much
it used to work fine for pdf and images, tried out for an hour or so, followed everything I googled later the problem was found in my model has_attached_file :attachment,
:styles => {:original=> "125x125#"}
had to comment this line, and it worked for other attachments like docx or odt etc..
so in your case :styles => {:principal => "240x240>", :original => "400x400>"}
check out and comment.
Locate the path of the identify command like this:
$ which identify
For me the above command prints this: /usr/local/bin/identify
Add this in some initializer file:
Paperclip::Attachment.default_options[:command_path] = "/usr/local/bin"
One reason this error occurs is when you try to determine the dimensions of an image that does not exist:
Paperclip::Geometry.from_file(nil)
This command will hang and cause this error.
Try to run the "identify" command from ImageMagick on this image. It seems something with your ImageMagick install.
I had the exact same issue. Windows 8 64bit, Rails 4, ImageMagick-6.8.7-1-Q16-x64-static.exe. Do this :
In the root of your rails app (from Git Bash)
$ which identify
/c/Program Files/ImageMagick-6.8.7-Q16/./identify
Then
$ cd "/c/Program Files/ImageMagick-6.8.7-Q16"
Don't forget the quotes. Copy all executables to you /bin directory. I actually copied all these files to be certain.
$ cp * /bin
And voila paperclip works!

Frequent "No such file or directory" while saving files via Paperclip

I feel like this will end in a facepalm moment but I've been banging on my head on it for too long.
I have a Rails seed.rb file that gets all the files from a specific directory creating a new object for each file and saving the file via Paperclip:
Dir["./**/*.jpg"].each do |f|
...
p = Picture.new
File.open(f, 'r') { |photo_file| p.photo = photo_file }
p.save!
....
end
where photo is the Paperclip-assigned attribute (picture.rb):
has_attached_file :photo,
:styles => { :medium => "500x500>", :thumb => "100x100#" },
:processors => [:rotator]
My problem is after some number of files (some times 50, some times 2) the script exits with the following error:
No such file or directory - /var/folders/oD/oDq1WD11EEaXmfi8VfNvfE+++TM/-Tmp-/stream,22423,0,22423,0
/Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:1407:in `stat'
/Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:1407:in `block in fu_each_src_dest'
/Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:1423:in `fu_each_src_dest0'
/Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:1405:in `fu_each_src_dest'
/Users/patgeorge/.rvm/rubies/ruby-1.9.2-head/lib/ruby/1.9.1/fileutils.rb:504:in `mv'
/Users/patgeorge/.rvm/gems/ruby-1.9.2-head#rails3/bundler/gems/paperclip-61f74de14812cabc026967a2b2c3ca8cbd2eed69-master/lib/paperclip/storage.rb:42:in `block in flush_writes'
I thought maybe I wasn't closing the file but according to the Ruby IO docs using the block from of open will close the file.
Obviously I don't see myself having to run this often so it's not a huge problem. It's just frustrating and confusing.
I'm running Ruby 1.9.2 r28142, Rails 3.0.0.beta4, and Paperclip 2.3.3.
Additional:
Attempting Winfield's suggestion my code block now looks like this:
Dir["./**/*.jpg"].each do |f|
...
File.open(f, 'r') do |photo_file|
p = Picture.new
p.photo = photo_file
p.save!
end
...
end
Still getting the error periodically, though.
Still more info:
I noticed that when I first run my script it's able to do a large amount of files (12 or so). As I continue to run it the number decreases to where I can only do 2 at a time. I'm not sure what I'm doing to make it "reset" and process more. But I imagine that's the key.
This monkey patch solved the issue:
http://github.com/thoughtbot/paperclip/issues/issue/262/
It sure looks to me like you're closing the File handle you opened before you read it out into paperclip.
File.open() with a block opens the file, passes it to the block, and closes it after block execution. This means it's likely closed before you call p.save!
Try doing all of your Photo creation inside your file block:
File.open(f, 'r') {|photo_file| Picture.create!(:photo => photo_file) }

Zip up all Paperclip attachments stored on S3

Paperclip is a great upload plugin for Rails. Storing uploads on the local filesystem or Amazon S3 seems to work well. I'd just assume store files on the localhost, but the use of S3 is required for this app as it will be hosted on Heroku.
How would I go about getting all of my uploads/attachments from S3 in a single zipped download?
Getting a zip of files from the local filesystem seems straight forward. It's getting the files from S3 that has me puzzled. I think it may have something to do with the way that rubyzip handles files referenced by URL. I've tried various approaches but can't seem to avoid errors.
format.zip {
registrations_with_attachments = Registration.find_by_sql('SELECT * FROM registrations WHERE abstract_file_name NOT LIKE ""')
headers['Cache-Control'] = 'no-cache'
tmp_filename = "#{RAILS_ROOT}/tmp/tmp_zip_" <<
Time.now.to_f.to_s <<
".zip"
# rubyzip gem version 0.9.1
# rdoc http://rubyzip.sourceforge.net/
Zip::ZipFile.open(tmp_filename, Zip::ZipFile::CREATE) do |zip|
#get all of the attachments
# attempt to get files stored on S3
# FAIL
registrations_with_attachments.each { |e| zip.add("abstracts/#{e.abstract.original_filename}", e.abstract.url(:original, false)) }
# => No such file or directory - http://s3.amazonaws.com/bucket/original/abstract.txt
# Should note that these files in S3 bucket are publicly accessible. No ACL.
# works with local storage. Thanks to Henrik Nyh
# registrations_with_attachments.each { |e| zip.add("abstracts/#{e.abstract.original_filename}", e.abstract.path(:original)) }
end
send_data(File.open(tmp_filename, "rb+").read, :type => 'application/zip', :disposition => 'attachment', :filename => tmp_filename.to_s)
File.delete tmp_filename
}
You almost certainly want to use e.abstract.to_file.path instead of e.abstract.url(...).
See:
Paperclip::Storage::S3::to_file (should return a TempFile)
TempFile::path
UPDATE
From the changelog:
New in 3.0.1:
API CHANGE: #to_file has been removed. Use the #copy_to_local_file method instead.
#vlard's solution is ok. However I've run into some issues with the to_file. It creates a tempfile and the garbage collector deletes (sometimes) the file before it was added to the zip file. Therefor, I'm getting random Errno::ENOENT: No such file or directory errors.
So I'm using the following code now (I've kept the initial code variables names for consistency with the initial question)
format.zip {
registrations_with_attachments = Registration.find_by_sql('SELECT * FROM registrations WHERE abstract_file_name NOT LIKE ""')
headers['Cache-Control'] = 'no-cache'
#please note that using nanoseconds option in strftime reduces the risks concerning the situation where 2 or more users initiate the download in the same time
tmp_filename = "#{RAILS_ROOT}/tmp/tmp_zip_" <<
Time.now.strftime('%Y-%m-%d-%H%M%S-%N').to_s <<
".zip"
# rubyzip gem version 0.9.4
zip = Zip::ZipFile.open(tmp_filename, Zip::ZipFile::CREATE)
zip.close
registrations_with_attachments.each { |e|
file_to_add = e.file.to_file
zip = Zip::ZipFile.open(tmp_filename)
zip.add("abstracts/#{e.abstract.original_filename}", file_to_add.path)
zip.close
puts "added #{file_to_add.path} to #{tmp_filename}" #force garbage collector to keep the file_to_add until after the file has been added to zip
}
send_data(File.open(tmp_filename, "rb+").read, :type => 'application/zip', :disposition => 'attachment', :filename => tmp_filename.to_s)
File.delete tmp_filename
}

Resources