attachment_fu facilitates file uploads in Ruby on Rails. There are a few storage options for the actual file data, but the plugin always at a minimum stores metadata for each file in the database.
-There are three storage options for files uploaded through attachment_fu:
+There are four storage options for files uploaded through attachment_fu:
File system
Database file
Amazon S3
+ Rackspace (Mosso) Cloud Files
-Each method of storage many options associated with it that will be covered in the following section. Something to note, however, is that the Amazon S3 storage requires you to modify config/amazon_s3.yml and the Database file storage requires an extra table.
+Each method of storage many options associated with it that will be covered in the following section. Something to note, however, is that the Amazon S3 storage requires you to modify config/amazon_s3.yml, the Rackspace Cloud Files storage requires you to modify config/rackspace_cloudfiles.yml, and the Database file storage requires an extra table.
attachment_fu models
# This option need only be included if you want thumbnailing.
:thumbnail_class # Set which model class to use for thumbnails.
# This current attachment class is used by default.
- :path_prefix # path to store the uploaded files.
- # Uses public/#{table_name} by default for the filesystem, and just #{table_name} for the S3 backend.
+ :path_prefix # Path to store the uploaded files in.
+ # Uses public/#{table_name} by default for the filesystem, and just #{table_name} for the S3 and Cloud Files backend.
# Setting this sets the :storage to :file_system.
+ :partition # Whether to partiton files in directories like /0000/0001/image.jpg. Default is true. Only applicable to the :file_system backend.
:storage # Specifies the storage system to use..
- # Defaults to :db_file. Options are :file_system, :db_file, and :s3.
+ # Defaults to :db_file. Options are :file_system, :db_file, :s3, and :cloud_files.
+ :cloudfront # If using S3 for storage, this option allows for serving the files via Amazon CloudFront.
+ # Defaults to false.
:processor # Sets the image processor to use for resizing of the attached image.
# Options include ImageScience, Rmagick, and MiniMagick. Default is whatever is installed.
+ :uuid_primary_key # If your model's primary key is a 128-bit UUID in hexadecimal format, then set this to true.
+ :association_options # attachment_fu automatically defines associations with thumbnails with has_many and belongs_to. If there are any additional options that you want to pass to these methods, then specify them here.
Examples:
has_attachment :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }
has_attachment :storage => :file_system, :path_prefix => 'public/files'
has_attachment :storage => :file_system, :path_prefix => 'public/files',
- :content_type => :image, :resize_to => [50,50]
+ :content_type => :image, :resize_to => [50,50], :partition => false
has_attachment :storage => :file_system, :path_prefix => 'public/files',
:thumbnails => { :thumb => [50, 50], :geometry => 'x50' }
has_attachment :storage => :s3
+ has_attachment :store => :s3, :cloudfront => true
+ has_attachment :storage => :cloud_files
validates_as_attachment
This method prevents files outside of the valid range (:min_size to :max_size, or the :size range) from being saved. It does not however, halt the upload of such files. They will be uploaded into memory regardless of size before validation.
Example:
<%= form.file_field :uploaded_data %>
-Displaying uploaded images is made easy by the public_filename method of the ActiveRecord attachment objects using file system and s3 storage.
+Displaying uploaded images is made easy by the public_filename method of the ActiveRecord attachment objects using file system, s3, and Cloud Files storage.
public_filename(thumbnail = nil)
Returns the public path to the file. If a thumbnail prefix is specified it will return the public file path to the corresponding thumbnail.
render :action => :new
end
end
+
+attachement_fu scripting
+====================================
+
+You may wish to import a large number of images or attachments.
+The following example shows how to upload a file from a script.
+
+#!/usr/bin/env ./script/runner
+
+# required to use ActionController::TestUploadedFile
+require 'action_controller'
+require 'action_controller/test_process.rb'
+
+path = "./public/images/x.jpg"
+
+# mimetype is a string like "image/jpeg". One way to get the mimetype for a given file on a UNIX system
+# mimetype = `file -ib #{path}`.gsub(/\n/,"")
+
+mimetype = "image/jpeg"
+
+# This will "upload" the file at path and create the new model.
+@attachable = AttachmentMetadataModel.new(:uploaded_data => ActionController::TestUploadedFile.new(path, mimetype))
+@attachable.save