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/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
S. Robert James
2007-Mar-20 04:24 UTC
Re: Faking it... import local files into attachment_fu
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
On Mar 11, 5:36 pm, "mcginniwa"
<walter.mcgin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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 McGinnishttp://blog.katipo.co.nz/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bruno Celeste
2007-Apr-19 17:45 UTC
Re: Faking it... import local files into attachment_fu
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''
On 4/19/07, Drogomir <drogus-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> 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?
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---