Classier solution for file uploads for Rails, Sinatra and other Ruby web frameworks
Go to file
Jonas Nicklas ba70b6d8e3 Log to a log file in specs 2009-05-10 13:12:16 +02:00
features Make version names an array 2009-04-07 22:29:35 +02:00
lib Fallback logger if none given. 2009-05-10 13:00:28 +02:00
rails_generators/uploader Now possible to specify a default in uploader 2009-05-01 18:06:38 +02:00
spec Log to a log file in specs 2009-05-10 13:12:16 +02:00
.gitignore Log to a log file in specs 2009-05-10 13:12:16 +02:00
CHANGELOG.md Changelog update 2009-05-04 23:34:19 +02:00
Generators no more more/core division, just one single gem 2009-02-16 21:49:33 +01:00
LICENSE I made this! Let the world know... :P 2009-03-15 00:31:28 +01:00
README.rdoc Added Paperclip compatibility mode 2009-05-03 01:06:33 +02:00
Rakefile bumped version to 0.2.2 2009-05-01 17:18:43 +02:00
TODO more renaming 2009-02-26 23:13:45 +01:00
carrierwave.gemspec bumped version to 0.2.2 2009-05-01 17:18:43 +02:00
cucumber.yml no more more/core division, just one single gem 2009-02-16 21:49:33 +01:00

README.rdoc

= CarrierWave

This plugin for Merb and Rails provides a simple and extremely flexible way to upload files.

== Getting Started

Install the latest stable release:

    [sudo] gem install carrierwave

Or the cutting edge development version:

    [sudo] gem install jnicklas-carrierwave --source http://gems.github.com

In Merb, add it as a dependency to your config/dependencies.rb:
    
    dependency 'carrierwave'

In Rails, add it to your environment.rb:

    config.gem "carrierwave"

== Quick Start

Start off by generating an uploader:

    merb-gen uploader Avatar

or in Rails:

    script/generate uploader Avatar

this should give you a file in:

    app/uploaders/avatar_uploader.rb

Check out this file for some hints on how you can customize your uploader. It should look something like this:

    class AvatarUploader
      include CarrierWave::Uploader

      storage :file
    end

You can use your uploader class to store and retrieve files like this:

    uploader = AvatarUploader.new
    
    uploader.store!(my_file)
    
    uploader.retrieve_from_store!('my_file.png')

CarrierWave gives you a +store+ for permanent storage, and a +cache+ for temporary storage. You can use different stores, at the moment a filesystem store and an Amazon S3 store are bundled.

Most of the time you are going to want to use CarrierWave together with an ORM. It is quite simple to mount uploaders on columns in your model, so you can simply assign files and get going:

=== ActiveRecord, DataMapper, Sequel

First require the relevant extension:

    require 'carrierwave/orm/activerecord'
    require 'carrierwave/orm/datamapper'
    require 'carrierwave/orm/sequel'

You don't need to do this if you are using Merb or Rails.

Open your model file, for ActiveRecord do something like:

    class User < ActiveRecord::Base
      mount_uploader :avatar, AvatarUploader
    end

Or for DataMapper:

    class User
      include DataMapper::Resource

      mount_uploader :avatar, AvatarUploader
    end

Or for Sequel

    class User < Sequel::Model
      mount_uploader :avatar, AvatarUploader
    end

Now you can cache files by assigning them to the attribute, they will automatically be stored when the record is saved.

    u = User.new
    u.avatar = params[:file]
    u.avatar = File.open('somewhere')
    u.save!
    u.avatar.url # => '/url/to/file.png'
    u.avatar.current_path # => 'path/to/file.png'

== Changing the storage directory

In order to change where uploaded files are put, just override the +store_dir+ method:

    class MyUploader
      include CarrierWave::Uploader

      def store_dir
        'public/my/upload/directory'
      end
    end

This works for the file storage as well as Amazon S3.

== Adding versions

Often you'll want to add different versions of the same file. The classic example is image thumbnails. There is built in support for this:

    class MyUploader
      include CarrierWave::Uploader
      include CarrierWave::RMagick
      
      process :resize => [800, 800]

      version :thumb do
        process :crop_resized => [200,200]
      end
      
    end

When this uploader is used, an uploaded image would be scaled to be no larger than 800 by 800 pixels. A version called thumb is then created, which is scaled and cropped to exactly 200 by 200 pixels. The uploader could be used like this:

    uploader = AvatarUploader.new
    uploader.store!(my_file)                              # size: 1024x768
    
    uploader.url # => '/url/to/my_file.png'               # size: 800x600
    uploader.thumb.url # => '/url/to/thumb_my_file.png'   # size: 200x200

One important thing to remember is that process is called *before* versions are created. This can cut down on processing cost.

It is possible to nest versions within versions:

    class MyUploader
      include CarrierWave::Uploader

      version :animal do
        version :human
        version :monkey
        version :llama
      end
    end

== Making uploads work across form redisplays

Often you'll notice that uploaded files disappear when a validation
fails. CarrierWave has a feature that makes it easy to remember the
uploaded file even in that case. Suppose your +user+ model has an uploader mounted on +avatar+ file, just add a hidden field called +avatar_cache+.
In Rails, this would look like this:

    <% form_for @user do |f| %>
      <p>
        <label>My Avatar</label>
        <%= f.file_field :avatar %>
        <%= f.hidden_field :avatar_cache %>
      </p>
    <% end %>

It might be a good idea to show th user that a file has been uploaded,
in the case of images, a small thumbnail would be a good indicator:

    <% form_for @user do |f| %>
      <p>
        <label>My Avatar</label>
        <%= image_tag(@user.avatar.url) if @user.avatar %>
        <%= f.file_field :avatar %>
        <%= f.hidden_field :avatar_cache %>
      </p>
    <% end %>

NOTE: this feature currently requires write access to your filesystem. If write access is unavailable (e.g. Heroku) you will not be able to upload files. You can prevent CarrierWave from writing to the file system by setting `CarrierWave.config[:cache_to_cache_dir] = false`. This will however break redisplays of forms.

== Providing a default path

In many cases, especially when working with images, it might be a good idea to provide a default path, a fallback in case no file has been uploaded. You can do this easily by overriding the +default_path+ method in your uploader:

    class MyUploader
      include CarrierWave::Uploader

      def default_path
        "images/fallback/" + [version_name, "default.png"].compact.join('_')
      end
    end

== Using Amazon S3

You'll need to configure a bucket, access id and secret key like this:

    CarrierWave.config[:s3][:access_key_id] = 'xxxxxx'
    CarrierWave.config[:s3][:secret_access_key] = 'xxxxxx'
    CarrierWave.config[:s3][:bucket] = 'name_of_bucket'

Do this in an initializer in Rails, and in a +before_app_loads+ block in Merb.

And then in your uploader, set the storage to :s3

    class AvatarUploader
      include CarrierWave::Uploader

      storage :s3
    end

That's it! You can still use the +CarrierWave::Uploader#url+ method to return the url to the file on Amazon S3

== Using RMagick

If you're uploading images, you'll probably want to manipulate them in some way, you might want to create thumbnail images for example. CarrierWave comes with a small library to make manipulating images with RMagick easier, you'll need to include it in your Uploader:

    class AvatarUploader
      include CarrierWave::Uploader
      include CarrierWave::RMagick
    end

The RMagick module gives you a few methods, like +CarrierWave::RMagick#crop_resized+ which manipulate the image file in some way. You can set a +process+ callback, which will call that method any time a file is uploaded.

    class AvatarUploader
      include CarrierWave::Uploader
      include CarrierWave::RMagick
      
      process :crop_resized => [200, 200]
      process :convert => 'png'
      
      def filename
        super + '.png'
      end
    end

Check out the manipulate! method, which makes it easy for you to write your own manipulation methods.

== Using ImageScience

ImageScience works the same way as RMagick.

    class AvatarUploader
      include CarrierWave::Uploader
      include CarrierWave::ImageScience
      
      process :crop_resized => [200, 200]
    end

== Migrating

If you are using Paperclip, you can use the provided compatibility module:

    class AvatarUploader
      include CarrierWave::Uploader
      include CarrierWave::Compatibility::Paperclip
    end

See the documentation for +Paperclip::Compatibility::Paperclip+ for more detaills.

Be sure to use mount_on to specify the correct column:

    mount_uploader :avatar, AvatarUploader, :mount_on => :avatar_file_name

Unfortunately AttachmentFoo differs too much in philosophy for there to be a sensible compatibility mode. Patches for migrating from other solutions will be happily accepted.

== Documentation

Full rdoc documentation is {available at Rubyforge}[http://carrierwave.rubyforge.org/].

== Read the source

CarrierWave is still young, but most of it is pretty well documented. It is also extensively specced, and there are cucumber features for some common use cases. Just dig in and look at the source for more in-depth explanation of what things are doing.