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
Related
I want to trimming the image of paperclip in my database.
Because I`m running my own private web service, but I forgot to add the styles when user create the image...
has_attached_file :avatar,
:storage => :s3,
:styles => { # I forgot to add this styles
:medium => "370x370#", # I forgot to add this styles
:thumbs => "120x120#" # I forgot to add this styles
}
So I want to run the rake task and trimming like 370×370# and 120×120#.
But I cant`t find the way to trimming the image after user save.
Does anyone assist me ?
The paperclip gem comes with a rake task for exactly this purpose:
rake paperclip:refresh:thumbnails
Just run that from the command line and it will take care of generating all of your reduced-size images.
If I'm adding a new version in carrierwave, what is the command line to recreate the images if I already have existing images?
What would it be for localhost and for production if I'm using Heroku?
Thanks!
Seems like you want to resize existing images.
You have to add you new version to the image uploader(taking 200*200 thumb as an example)
version :thumb do
process :resize_to_fill => [200,200]
end
Then recreate them in console (taking avatar in User model as an example):
User.all.each do |user|
user.avatar.recreate_versions!
end
UPDATE
If you want to use a custom rake task, you can create a file lib/tasks/resize_image.rake and put the following code in this file:
namespace :resize_image do
desc "RESIZE"
task :recreate => :environment do
User.all.each do |user|
user.avatar.recreate_versions!
end
end
end
And in your console use:
rake resize_image:recreate
I have just created a Rails app with a model app/models/post.rb and have written a scraper scrapers/base_scraper.rb (class BaseScraper) that collect data from the target site to the hash variable data. Now I want to insert values of data into the Post model. How to do it properly in Rails? I have heard smth about Rake but have no idea how to utilize it properly. Help me please!
Assuming that data stores just one post and that each of the key stored in the datahash are valid Post fields (column_name), you can do simply this:
Post.create(data)
If you want to launch the whole process from console, you can create a rake task under lib/tasks directory of your process with the following:
# scraper.rake
namespace :scraper do
desc "Run scraper"
task :run => :environment do
data = BaseScraper.your_collect_data_class_method
Post.create(data) if data
end
end
task :default => 'scraper:run'
And then run it from console as a rake task with rake scraper
Of course I also assume that scrapers dir is in your Rails load path.
If not, add it to your application.rbfile.
# application.rb
...
module YourApp
class Application < Rails::Application
...
config.autoload_paths += Dir["#{config.root}/scrapers/"]
...
end
end
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.
During some recent refactoring we changed how our user avatars are stored not realizing that once deployed it would affect all the existing users. So now I'm trying to write a rake task to fix this by doing something like this.
namespace :fix do
desc "Create associated ImageAttachment using data in the Users photo fields"
task :user_avatars => :environment do
class User
# Paperclip
has_attached_file :photo ... <paperclip stuff, styles etc>
end
User.all.each do |user|
i = ImageAttachment.new
i.photo_url = user.photo.url
user.image_attachments << i
end
end
end
When I try running that though I'm getting undefined method `has_attached_file' for User:Class
I'm able to do this in script/console but it seems like it can't find the paperclip plugin's methods from a rake task.
the rake task is probably not be loading the full Rails environment. You can force it to do so by doing something like this:
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
where the path leads to your environment.rb file. If this were to fix the issue, you should include it inside this task specifically, because you probably do not want all your rake tasks to include the environment by default. In fact, a rake task may not even be the best place to do what you're trying to do. You could try creating a script in the script directory as well.