Rails Paperclip Processor: shell command failing - ruby-on-rails

I've got a problem with a custom paperclip processor within my rails app. I upload a soundfile and the processor creates an waveform image via a shell command (provided by this gem)
I'm running RoR 3.2.7 / Ruby 1.9.3 on Ubuntu 12.04 (production environment). My model with paperclip attachment looks like the following:
# encoding: utf-8
class Track < ActiveRecord::Base
has_attached_file :original_file,
:styles => { :waveform_image => { :waveform => true } },
:processors => [:sound_processor],
:storage => :s3,
:s3_credentials => "#{Rails.root.to_s}/config/s3.yml",
:url => ":s3_eu_url",
:path => "sounds/:id/:five_digit_id_:basename_:style.:extension"
end
The corresponding Paperclip Processor:
class Paperclip::SoundProcessor < Paperclip::Processor
def initialize file, options = {}, attachment = nil
# cut for brevity
end
def make
src = #file
dst = Tempfile.new([#basename, #current_format])
dst.binmode
if #waveform
cmd = "waveform #{File.expand_path(src.path)} #{File.expand_path(dst.path)}"
Paperclip.log(cmd)
out = `#{cmd}`
dst = File.open "#{File.expand_path(dst.path)}"
end
dst
end
end
When the command
waveform #{File.expand_path(src.path)} #{File.expand_path(dst.path)}
is getting executed on the production machine (Ubuntu 12.04), the following error comes up:
/usr/bin/env: ruby: No such file or directory
However the usr/bin/env is a file instead of an directory. Since there is no ruby executable, how can I pass the right location when executing the shell command?
PS: On my development machine (OSX), on usr/bin/env is a copy of my rails app directory. It's working like a charm in development. I appreciate your help!

The error message is indicating that the ruby executable was not found. It is not related to the code that you have included in the question as of now.
The error is coming from the script file that includes the waveform #{File.expand_path(src.path)} #{File.expand_path(dst.path)} line.
Check out these answers to the issue:
- https://stackoverflow.com/a/6126419/429758
- https://stackoverflow.com/a/5241638/429758
- https://stackoverflow.com/a/5241593/429758
- https://stackoverflow.com/a/1064319/429758
They should give you some idea as to why the ruby executable is not being found on your production environment.

Related

crontab in rails 4 not executing correctly

I was developing a project(its an Intranet project for a company to check employees is working or not) in rails 4. In that I want to take the screen shots of the users logged in every 1 minute and save to server. I created methods for taking screen shot.And could save to the database.But problem is occurring in the cron tab creation. Its not executing at all.
For creating crontab I used gem 'whenever', :require => false in my gem file. After that I wheneverize .it, So I got schedule.rb file.
My schedule.rb file
set :environment, 'development'
every 1.minutes do
runner "MyModel.cron_screenshot"
end
I want to check in development mode thats why I set environment to 'development'
MyModel is my model file contains this code
class MyModel < ActiveRecord::Base
has_attached_file :file_avatar, :default_url => "/files/:style/missing.jpg",
:styles => {
:thumb => "100x100#",
:small => "150x150>",
:medium => "500x500"
}
do_not_validate_attachment_file_type :file_avatar
def self.cron_screenshot
puts pwd()
result = %x{scrot}
create
end
def create
f = File.open(Rails.root.join('screenshot.png'), 'rb')
model = MyModel.new
model.file_avatar = f
model.save
end
end
I am using Ubuntu 13.04 as my operating system. When I entered command crontab -l to check I got the output as
# Begin Whenever generated tasks for: store
* * * * * /bin/bash -l -c 'cd /home/project/screenshot/image && bin/rails runner -e development '\''MyModel.cron_screenshot'\'''
# End Whenever generated tasks for: store
After one minute nothing is happening. What went wrong here?

Paperclip Errno::EACCES (Permission denied - /system)

My production environment is : ruby 1.9.2-p320 , rails 3.2.7, paperclip 3.1.4, mysql, Ubuntu 8.10 x86 64bit.
I have a Errno:EACCES Permission denied /system error when i try to upload a file with paperclip. Useless to say that locally this doesn't happen.
I checked the public directory permissions and it's 775, the public/system permission is 777 as well as all it's inner directory. The tmp directory permission is : 775 too.
Moreover the user used to deploy the application is www-data:root
The model's attachment is set like this :
has_attached_file :fichier,
:path => "/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
I can't find out why i get this error. Anyone has got an idea ?
Thanks
Your code DOES NOT try to save the uploaded file in:
/path/to/app/public/system/:attachment/:id/:style/:filename
but in:
/system/:attachment/:id/:style/:filename
Try this instead:
has_attached_file :fichier,
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"

Reading file attached with paperclip -- No such file or directory

Why am I getting no such file or directory when I use a symbol in my path? Paperclip shows :rails_root/public/system/:class/:attachment/:id_partition/:style/:filename as the default path. The file is in public/system/attachments/email.txt, but I want to be able to add a file with any name.
has_attached_file :email,
:url => "public/system/attachments/:filename",
:path => "public/system/attachments/:filename"
def read_original_header
#original_email = File.read("public/system/attachments/:filename")
end
No such file or directory - public/system/attachments/:filename
Your issue is that the 'symbol' interpolation is a Paperclip convention, this convention is not shared by the standard Ruby File class.

Rails 3 app + paperclip gem + production mode = download of empty files

I've installed the paperclip gem for a Rails 3 application. Everything works fine in development mode. However, when running in production mode, if I upload a file and then try to download it again, it downloads a file with the correct name and extension, but it is an empty file. When looking on the server, the file does get uploaded and is in the correct directory. (I have an "uploads" folder in my application root.)
Anyone had this happen?
My model:
# app/models/document.rb
class Document < ActiveRecord::Base
belongs_to :kase
has_attached_file :document, :path => (Rails.root + "uploads/:class/:kase_id/:id").to_s, :url => ":class/:id"
validates_attachment_presence :document
validates_attachment_content_type :document, :content_type => [
'application/pdf',
'image/png',
'image/jpeg',
'image/pjpeg',
'text/plain'
]
end
My controller:
# app/controllers/documents_controller.rb
class DocumentsController < ApplicationController
respond_to :html
before_filter :initialize_kase # Sets the #kase instance
def show
#document = #kase.documents.find(params[:id])
send_file #document.document.path, :filename => #document.document_file_name, :content_type => #document.document_content_type
end
end
And my initializer (setting the :kase_id placeholder used in has_attached_file above:
# config/initializers/paperclip.rb
Paperclip.interpolates('kase_id') do |attachment, style|
"kases/#{attachment.instance.kase.id.to_s}"
end
I should probably mention, too, that I am accessing this as a nested controller (/kases/XX/documents/XX). Not sure if that has an effect or not...
If you are using Apache and Passenger, (possibly other servers as well) and have the line:
config.action_dispatch.x_sendfile_header = "X-Sendfile"
in your production.rb env file, then you have two options:
Install the apache module mod-xsendfile
Comment out that line and let Rails send the files instead of Apache, like it does in development mode.
Are you carrying over the uploads directory each time you deploy your app to production? Assuming that you're using capistrano (or similar) for deployment, each time you deploy you might be creating a new uploads directory in the newly-deployed release directory. In that case, the previously-uploaded files are residing in older deployed releases (if you didn't delete those) and would no longer be accessible to your app.
You want to create e.g. shared/uploads directory that is symlinked into your app on each deploy.

How can I make Paperclip process files from a server directory?

I want to run Paperclip on all files in a directory on the server. Basically, I would like to allow users to FTP some files to my webserver, then I can manually run a rake task to have Paperclip process all of the files (resize the images, update the database, etc).
How can I do this?
I'm not sure if I understood your question - are you asking to run the rake task remotely or how to import images?
In the later case there is an answer.
First you need some Model to keep the images and maybe some other data, something like this:
class Picture < ActiveRecord::Base
has_attached_file :image, :styles => {
:thumb => "100x100>",
:big => "500x500>"
}
end
You can create simple rake task in your lib/tasks folder (you should name the file with .rake extension)
namespace :import do
desc "import all images from SOURCE_DIR folder"
task :images => :environment do
# get all images from given folder
Dir.glob(File.join(ENV["SOURCE_DIR"], "*")) do |file_path|
# create new model for every picture found and save it to db
open(file_path) do |f|
pict = Picture.new(:name => File.basename(file_path),
:image => f)
# a side affect of saving is that paperclip transformation will
# happen
pict.save!
end
# Move processed image somewhere else or just remove it. It is
# necessary as there is a risk of "double import"
#FileUtils.mv(file_path, "....")
#FileUtils.rm(file_path)
end
end
end
Then you can call manually rake task from the console providing SOURCE_DIR parameter that will be the folder on the server (it can be real folder or mounted remote)
rake import:images SOURCE_DIR=~/my_images/to/be/imported
If you are planning to run this automatically I'd recommend you to go for Resque Scheduler gem.
Update: To keep things simple I've deliberately omitted exception handling

Resources