Forked to store files in a database table
Ruby
Pull request Compare This branch is 7 commits ahead, 1015 commits behind thoughtbot:master.
Latest commit 89d5362 Apr 1, 2013 @patshaughnessy Merge pull request #9 from reinteractive-open/master
Added setup/usage to readme
Permalink
Failed to load latest commit information.
cucumber shoulda macro and webrat step for testing paperclip on s3 Aug 24, 2009
features Reapplied database storage changes Dec 23, 2011
gemfiles Use AWS::SDK instead of AWS::S3 Dec 12, 2011
generators/paperclip Clean up whitespace Jun 7, 2010
lib Reapplied database storage changes Dec 23, 2011
rails Revert removal of rails/init.rb for rails2 compatibility. Jun 30, 2011
shoulda_macros Fix problem with the newer version of Factory Girl Sep 4, 2011
test Fix test for Ruby 1.9.3 Dec 23, 2011
.gitignore
.travis.yml Add basic integration test using Cucumber Sep 7, 2011
Appraisals Add basic integration test using Cucumber Sep 7, 2011
CONTRIBUTING.md Add a document describing how to contribute. Jul 22, 2011
Gemfile Use Bundler gem conventions. Nov 12, 2011
Gemfile.lock Update Cucumber dependency Dec 12, 2011
LICENSE Moved the License into its own file Apr 18, 2008
README.md Update README.md with usage example Oct 7, 2012
Rakefile bundler/gem_tasks is easier. Nov 12, 2011
init.rb change plugin initialization to rails3 style Oct 19, 2010
paperclip.gemspec Update Cucumber dependency Dec 12, 2011

README.md

Paperclip forked to store files in a database table

This is a fork of Thoughtbot’s fantastic Paperclip gem that supports saving and fetching of file data to/from an RDBMS BLOB column.

Saving file data in a BLOB is a REALLY BAD IDEA - so DON’T USE this fork of Paperclip, unless you are working with a legacy, non-Rails database and you have existing BLOB data you have to work with.

Usage is identical to the file system storage version, except in your model specify the :storage => :database storage option; for example:

has_attached_file :avatar, :storage => :database

The file will be stored in a column called [attachment name]_file (e.g. avatar_file) by default. To specify a different column name, use :column, like this:

has_attached_file :avatar, :storage => :database, :column => 'avatar_data'

If you have defined different styles, these files will be stored in additional columns called [attachment name]_[style name]_file (e.g. avatar_thumb_file) by default.

To specify different column names for styles, use :column in the style definition, like this:

has_attached_file :avatar,
  :storage => :database,
  :styles => { 
    :medium => {:geometry => "300x300>", :column => 'medium_file'},
    :thumb =>  {:geometry => "100x100>", :column => 'thumb_file'}
  }

If you need to create the BLOB columns (remember you should only be using this with a legacy database!!) ...you can use migrations like this:

add_column :users, :avatar_file,        :binary, :limit => 2.gigabytes
add_column :users, :avatar_medium_file, :binary, :limit => 1.gigabyte
add_column :users, :avatar_thumb_file,  :binary, :limit => 200.megabytes

Note the "limit" option (when used for MySQL) will create a BLOB of the appropriate size (up to 4.gigabytes will create a LONGBLOB).

To avoid performance problems loading all of the BLOB columns every time you access your ActiveRecord object, a class method is provided on your model called select_without_file_columns_for. This is set to a :select scope hash that will instruct ActiveRecord::Base.find to load all of the columns except the BLOB/file data columns.

You can specify this as a default scope:

default_scope select_without_file_columns_for(:avatar)

By default, attachment URLs will be set to this pattern:

/:relative_root/:attachment/:id/:class?style=:style

Example (assuming the relative root is null):

/users/23/avatars?style=original

And attachments paths will be nil, since they are not stored on the file system. If you need to create a copy of a file attachment on the file system, for example if you need to process it for some reason, use code similar to this:

begin
  temp_file = Tempfile.open("#{user.avatar_file_name}_#{style}")
  temp_file << user.avatar.file_contents(style)

  ... do something with the temp file ...

ensure
  temp_file.close
end

You'll need to download file attachments through a controller. To do that you can use this utility method to generate an avatars action for example:

downloads_files_for :user, :avatar

Or you can write a download method manually if there are security, logging or other requirements. If you prefer a different URL for downloading files you can specify that in the model; e.g.:

 has_attached_file :avatar, :storage => :database, :url => '/users/show_avatar/:id/:style'

Remember to add a route for the download to the controller which will handle downloads, if necessary.

 resources :users do
   get :avatars
 end

Quick Start

 bundle exec rails g paperclip User avatar

Edit that migration and add:

 add_column :users, :avatar_file

Test in console

 u = User.first
 u.avatar = File.open('some-image.jpg','rb')
 u.save