Paperclip.run 'jpegoptim' - problem with params - ruby-on-rails

In my model, that has_attached_file, I have the following code:
before_post_process :jpeg_optim_compression
...
def jpeg_optim_compression
path = attachment.queued_for_write[:original].path
Paperclip.run("jpegoptim", "-o --strip-all #{path}")
end
And locally it works fine giving a following output:
[paperclip] jpegoptim -o --strip-all temporary_path_to_attachment 2>/dev/null
But deployed on the staging server it raises an error:
[paperclip] jpegoptim '-o --strip-all temporary_path_to_attachment' 2>/dev/null
Paperclip::PaperclipCommandLineError (Error while running jpegoptim. Expected return code to be 0 but was 3):
It took me some time to realize that these quotations were the cause (I supposed some file access problem and others). After change to
Paperclip.run("jpegoptim -o --strip-all #{path}")
OR
Paperclip.run("jpegoptim -o --strip-all", "#{path}")
it works. Does anybody have an idea why is that?
Thanks for any suggestions!

Related

Command /bin/sh emitted errors

In xcode it is a good idea to generate documentation during build your library.. I am using headerdoc2HTML command .. to make it run with build phase I've added it to build phase script (shell script) like this:
headerdoc2html -o "outputPath" "myHeader.h"
But it always gives me an error:
Command /bin/sh emitted errors but did not return a nonzero exit code to indicate failure
Even the documentation is generated but it gives me an error .. if I remove this command every thing goes fine!
I checked with -d parameter I did not get where is the error
Any help with that? is there a way to check if the command did not return 0 then don't show error (ignore all warnings and errors)?
EDIT:
I just made sure that the command is ok and no problem with it by doing this check:
if headerdoc2html -q -j -o "outputPath" "myHeader.h"
then
echo "Documents generated successfully!"
fi
While the problem is from the command it self and don't effect the over all build phase so it is good idea to not show the warnings and errors from headerdoc2html command.
The easiest way to do that is to hide any emitted errors from it like by redirect errors to null like this:
headerdoc2html -o "outputPath" "myHeader.h" >/dev/null 2>/dev/null

How to resize already uploaded Paperclip images to s3 using reprocess? (Rails)

I have a model:
class PropertyImage < ActiveRecord::Base
has_attached_file :picture,
storage: :s3,
s3_credentials: CONFIG['s3'],
s3_protocol: (Rails.env.development? ? "http": "https"),
styles: {
thumb: '100x100>',
large: '633x460>',
medium: '301x240>'
}
end
I'm using Rails (4.0.1), cocaine (0.5.4) and paperclip (3.5.4).
I want to migrate old images (with no thumb, large and medium) and resize each, so I created a rake script:
namespace :migrate_images do
desc "Resize Images in PropertyImage"
task start_migration: :environment do
PropertyImage.not_migrated.find_each do |pi|
ImageConverter.perform(pi)
end
end
end
And ImageConverter class:
class ImageConverter
def self.perform(pi)
begin
pi.picture.reprocess!
pi.update_attributes!({migrated: true})
puts "PropertyImage [#{pi.id}] has been migrated."
rescue Exception => e
puts "PropertyImage [#{pi.id}] has an error. #{e}"
end
end
end
Now when I run the script I keep getting the following error:
⇒ bundle exec rake migrate_images:start_migration
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
PropertyImage [11] has an error. Validation failed: Picture Paperclip::Errors::NotIdentifiedByImageMagickError, Picture Paperclip::Errors::NotIdentifiedByImageMagickError, Picture Paperclip::Errors::NotIdentifiedByImageMagickError
PropertyImage [12] has an error. Validation failed: Picture Paperclip::Errors::NotIdentifiedByImageMagickError, Picture Paperclip::Errors::NotIdentifiedByImageMagickError, Picture Paperclip::Errors::NotIdentifiedByImageMagickError
Please note that I read similar articles and added the following to the end of application.rb:
Paperclip.options[:command_path] = "/usr/local/bin/identify"
And I made sure I have imagemagick installed:
⇒ brew install imagemagick
Warning: You have an outdated version of /usr/bin/install_name_tool installed.
This will cause binary package installations to fail.
This can happen if you install osx-gcc-installer or RailsInstaller.
To restore it, you must reinstall OS X or restore the binary from
the OS packages.
Warning: imagemagick-6.8.9-7 already installed
Any help would be highly appreciated.
Please note if I try to reproduce the problem using rails console:
>> p = Property.find(93746)
>> p.property_images.each do |pi|
?> pi.picture.reprocess!
>> end
PropertyImage Load (1.4ms) SELECT "property_images".* FROM "property_images" WHERE "property_images"."invalid_image" = 'f' AND "property_images"."property_id" = $1 ORDER BY "property_images"."apartment_main" DESC [["property_id", 93746]]
[paperclip] copying /property_images/pictures/000/325/476/original/722952f7-4cd4-47bb-bdcf-c7411c9d6021.jpg to local file /var/folders/g8/3v37gc0x16b313mvf7464qtc0000gn/T/dfe10026af3ac1b8cc5c94f00437092020141027-6352-12gmkmc.jpg
[AWS S3 200 1.212736 0 retries] get_object(:bucket_name=>"my_bucket_development",:key=>"property_images/pictures/000/325/476/original/722952f7-4cd4-47bb-bdcf-c7411c9d6021.jpg")
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/g8/3v37gc0x16b313mvf7464qtc0000gn/T/dfe10026af3ac1b8cc5c94f00437092020141027-6352-12gmkmc.jpg[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/g8/3v37gc0x16b313mvf7464qtc0000gn/T/dfe10026af3ac1b8cc5c94f00437092020141027-6352-12gmkmc.jpg[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/g8/3v37gc0x16b313mvf7464qtc0000gn/T/dfe10026af3ac1b8cc5c94f00437092020141027-6352-12gmkmc.jpg[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
[paperclip] saving /property_images/pictures/000/325/476/original/722952f7-4cd4-47bb-bdcf-c7411c9d6021.jpg
[AWS S3 200 0.293473 0 retries] put_object(:acl=>:public_read,:bucket_name=>"my_bucket_development",:content_length=>0,:content_type=>"image/jpeg",:data=>Paperclip::AttachmentAdapter: 722952f7-4cd4-47bb-bdcf-c7411c9d6021.jpg,:key=>"property_images/pictures/000/325/476/original/722952f7-4cd4-47bb-bdcf-c7411c9d6021.jpg")
(0.2ms) BEGIN
(0.2ms) ROLLBACK
Make sure that the format you are trying to upload is supported by Imagemagick on the server you are using.
To make sure of this, use this command:
convert -list format
If the format you are trying to upload with Paperclip is not in the list, you need to install the libraries needed and then reinstall/recompile Imagemagick.
Here is given an example of how to do that on a Unix machine. To install the jpg library, it is used the command:
yum install libjpeg libjpeg-devel
How to install the library and reinstall/recompile Imagemagick depends on which OS you use and on which image format you need.

wkhtmltopdf attempting to load from http rather than file

Here's an odd little problem that's led me to post my first question on SO. I am using wkhtmltopdf to convert an HTML document to a PDF as part of a Rails app. To do so, I am rendering the Rails web page to a static HTML file in a temp directory, copying a static header, footer and images to the same temp directory, then executing wkhtmltopdf using "system".
This works perfectly in Development and Test environments. In my Staging env, it does not. I suspected permissions at first, but the first couple of parts of that process (creating the HTML static files and copying them to the directory) are working. I can run wkhtmltopdf from the command line in that temp directory and get the expected outcome. Finally, I ran wkhtmltopdf via both "system" and backticks through the Rails console in staging environment, and here's what I get as output:
> `wkhtmltopdf --footer-html tmp/invoices/footer.html --header-html tmp/invoices/header.html -s Letter -L 0in -R 0in -T 0.5in -B 1in tmp/invoices/test.html tmp/invoices/this.pdf`
Loading pages (1/6)
QPainter::begin(): Returned false ] 10%
Error: Unable to write to destination
Error: Failed loading page http://tmp/invoices/test.html (sometimes it will work just to ignore this error with --load-error-handling ignore) => ""
Notice that last bit. I'm pointing to local files, but it's looking for them via http. OK, I think, maybe I need to be explicit and feed it the file:// protocol so it doesn't look for http. So I try this:
> system("wkhtmltopdf --footer-html file://Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/footer.html --header-html file://Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/header.html -s Letter -L 0in -R 0in -T 0.5in -B 1in file://Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/test.html file://Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/this.pdf")
Loading pages (1/6)
Error: Failed loading page file://library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/test.html (sometimes it will work just to ignore this error with --load-error-handling ignore)
=> false
Notice that this one fails with a lowercase "l" on Library. What the heck? (And no, it doesn't get any better with the recommendation to ignore the error with that switch.)
Any ideas? Is there a Rails or Ruby setting that would cause system commands to get rewritten? Is there an option I can add to wkhtmltopdf to make sure it loads from local file? I'm quite baffled. Thanks!
I have had success when using the absolute file path (notice the extra slash after the file://)
wkhtmltopdf --footer-html file:///Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/footer.html --header-html file:///Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/header.html -s Letter -L 0in -R 0in -T 0.5in -B 1in file:///Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/test.html file:///Library/Server/Web/Data/Sites/intranet-staging/current/tmp/invoices/this.pdf
This is the same on windows
Unix path
file:///absolute/path/to/file
Windows path
file:///C:/absolute/path/to/file
In last 0.11 whicked-pdf i found one bug
Example
C:\Ruby193\lib\ruby\gems\1.9.1\gems\wicked_pdf-0.11.0\lib>wicked_pdf.rb
Line 198 I change from:
options[hf][:html][:url] = "file://#{tf.path}" to options[hf][:html][:url] = "file:///#{tf.path}" - (change // to ///)
After change whicked-pdf again worked.
Take a look at the wicked_pdf gem.
You can add a PDF mime type and then whatever page you want pdf'd, just tack on a .pdf to the URL.
I am using this in prod and it works quite well.
No need to call wkhtmltopdf directly.

Prevent Ghostscript from writing errors to standard output

I'm using Ghostscript to rasterize the first page of a PDF file to JPEG. To avoid creating tempfiles, the PDF data is piped into Ghoscripts's stdin and the JPEG is "drained" on stdout. This pipeline works like a charm until GS receives invalid PDF data: Instead of reporting all error messages on stderr as I would have expected, it still writes some of the messages to stdout instead.
To reproduce:
$ echo "Not a PDF" >test.txt
$ /usr/bin/gs -q -sDEVICE=jpeg -dBATCH -dNOPAUSE -dFirstPage=1 -dLastPage=1 \
-r300 -sOutputFile=- - < test.txt 2>/dev/null
Error: /undefined in Not
Operand stack:
Execution stack:
...
Note the 2>/dev/null above does not suppress the error messages. Ghostscript's documentation already warned that writing to stdout requires the -q flag to suppress messages on stdout, but I still seem to be missing something here.
If you want to really silence Ghostscript, modify your command line like this:
/usr/bin/gs -q \
-sstdout=%stderr \
-sDEVICE=jpeg \
-dBATCH \
-dNOPAUSE \
-dLastPage=1 \
-r300 \
-sOutputFile=- \
- < test.txt 2>/dev/null
The addition of -sstdout=%stderr allows Postscript stdout to be redirected, while still allowing drivers to write to stdout. (That patch is in Ghostscript since ~2001, Sept 22.)

Problem when trying to upload image from Flex to PaperClip with creating thumbnail

I'm trying to upload an image from Flex to Rails. It uploads fine if I just want to upload the original but when I tried to add Thumbnail I got the following error:
[paperclip] identify '-format' '%wx%h' '/var/folders/RH/RHekFGKME9uDJbX4d4DG3E+++TI/-Tmp-/stream,23830,0.jpeg[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::NotIdentifiedByImageMagickError: /var/folders/RH/RHekFGKME9uDJbX4d4DG3E+++TI/-Tmp-/stream,23830,0.jpeg is not recognized by the 'identify' command.>
[paperclip] identify '-format' '%wx%h' '/var/folders/RH/RHekFGKME9uDJbX4d4DG3E+++TI/-Tmp-/stream,23830,0.jpeg[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::NotIdentifiedByImageMagickError: /var/folders/RH/RHekFGKME9uDJbX4d4DG3E+++TI/-Tmp-/stream,23830,0.jpeg is not recognized by the 'identify' command.>
and I noticed for some reason in the DB that when I upload a file from Flex to Rails it puts the photo_content_type is application/octet-stream shouldn't be jpeg or something? I thought that might be the problem.
Do you know how to resolve this?
For the photo_content_type -- flash is known to not send the correct mime-type, so I recommend you use mimetype-fu to get the real mime-type
If that doesn't solve your problem comment here and include if this is your development environment and if you're on a mac. It sounds like your environment can't find the rmagick binary. Follow these instructions
/config/intializers/paperclip.rb
Paperclip.options[:image_magick_path] = ‘/opt/local/bin/identify’
Alertnative, this has worked for a colleague:
unless RAILS_ENV == "production"
Paperclip.options[:command_path] = "/opt/local/bin"
end
On my mac, it's in /opt/local/bin/identify ... if you're on a mac, type in "which identify" to find your binary.

Resources