Faking it... import local files into attachment_fu

61 views
Skip to first unread message

mcginniwa

unread,
Mar 11, 2007, 5:36:26 PM3/11/07
to Ruby on Rails: Talk
Hello,

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.

Recommendations?

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

Cheers,
Walter McGinnis
http://blog.katipo.co.nz/

S. Robert James

unread,
Mar 20, 2007, 12:24:44 AM3/20/07
to Ruby on Rails: Talk
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-
bitmap",
".pgm" => "image/x-portable-
graymap",
".png" => "image/png",
".pnm" => "image/x-portable-
anymap",
".ppm" => "image/x-portable-
pixmap",
".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-
xwindowdump",
}.freeze
content_type ||= @@image_mime_types[File.extname(local_file_path)]
raise "Unrecognized MIME type for #{local_file_path}" unless
content_type
self.uploaded_data = LocalUploadedFile.new(local_file_path,
content_type)
end

# Taken from action_controller/test_process.rb
# Essentially generates a modified Tempfile object similar to the
object
# you'd get from the standard library CGI module in a multipart
# request. This means you can use an
ActionController::TestUploadedFile
# 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)
end

def path #:nodoc:
@tempfile.path
end

alias local_path path

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

Drogomir

unread,
Apr 19, 2007, 12:25:56 PM4/19/07
to Ruby on Rails: Talk
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?

Bruno Celeste

unread,
Apr 19, 2007, 1:45:06 PM4/19/07
to rubyonra...@googlegroups.com
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'

mcginniwa

unread,
May 27, 2007, 7:17:12 PM5/27/07
to Ruby on Rails: Talk
Yep. That was what basically worked for me. Check out 613 through
618 in this file for actual code:

http://development.kete.net.nz/kete/repository/file/trunk/lib/workers/past_perfect4_importer_worker.rb

Cheers,
Walter

Reply all
Reply to author
Forward
0 new messages