Faking it... import local files into attachment_fu


I'm working on an import utility that will match the functionality of
uploading an object of a model that uses attachment_fu for
thumbnailing, etc.

Rather than uploading the file via a form, I want to populate the
params[:image][:uploaded_data] with data from a file already on the
server's file system.

So far, I've been able to copy to an instance of Tempfile via the
copy_to_temp_file method I jacked from attachment_fu and pass that as
the value of :uploaded_data, but attachment_fu is still not happy.
The Tempfile created via a multipart form and a browser has
content_type set, as well as having the path be how the object is
identified (i.e. - #<File:/tmp/file/path>).

I feel like I've either reached a dead end and I have to explore
setting size, original_filename, and content_type explicitly in the
new method parameters or that I'm just bumping up against my ignorance
and there is a better way I don't know about.


I noticed that there are mechanisms for this fake out in testing code,
perhaps I'll give them another look.

Walter McGinnis

Add this to your has_attachment class
Let me know how it works

def local_file=(local_file_path, content_type = nil)
    @@image_mime_types ||= {
                                         ".gif" => "image/gif",
                                         ".ief" => "image/ief",
                                         ".jpe" => "image/jpeg",
                                         ".jpeg" => "image/jpeg",
                                         ".jpg" => "image/jpeg",
                                         ".pbm" => "image/x-portable-
                                         ".pgm" => "image/x-portable-
                                         ".png" => "image/png",
                                         ".pnm" => "image/x-portable-
                                         ".ppm" => "image/x-portable-
                                         ".ras" => "image/cmu-raster",
                                         ".rgb" => "image/x-rgb",
                                         ".tif" => "image/tiff",
                                         ".tiff" => "image/tiff",
                                         ".xbm" => "image/x-xbitmap",
                                         ".xpm" => "image/x-xpixmap",
                                         ".xwd" => "image/x-
    content_type ||= @@image_mime_types[File.extname(local_file_path)]
    raise "Unrecognized MIME type for #{local_file_path}" unless
    self.uploaded_data = LocalUploadedFile.new(local_file_path,

# Taken from action_controller/test_process.rb
  # Essentially generates a modified Tempfile object similar to the
  # you'd get from the standard library CGI module in a multipart
  # request. This means you can use an
  # object in the params of a test request in order to simulate
  # a file upload.
  require 'tempfile'
  class LocalUploadedFile
    # The filename, *not* including the path, of the "uploaded" file
    attr_reader :original_filename

    # The content type of the "uploaded" file
    attr_reader :content_type

    def initialize(path, content_type = 'text/plain')
      raise "#{path} file does not exist" unless File.exist?(path)
      @content_type = content_type
      @original_filename = path.sub(/^.*#{File::SEPARATOR}
([^#{File::SEPARATOR}]+)$/) { $1 }
      @tempfile = Tempfile.new(@original_filename)
      FileUtils.copy_file(path, @tempfile.path)

    def path #:nodoc:

    alias local_path path

    def method_missing(method_name, *args, &block) #:nodoc:
      @tempfile.send(method_name, *args, &block)

I have a similar problem, but now I'm using file_column.

mcginniwa: did it worked properly?

Or maybe someone knows how to make it works with file_column plugin?

I don't know if it's the right way to do this, but it works:

Asset.create :temp_path => File.new("/home/bruno/wallpaper.png"),
:filename => 'wallpaper.png', :content_type => 'image/png'

Yep. That was what basically worked for me. Check out 613 through
618 in this file for actual code: