# Faking it... import local files into attachment\_fu

**URL:** <https://www.ruby-forum.com/t/faking-it-import-local-files-into-attachment-fu/88484>\
**Category:** Rails\
**Created:** [March 11, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/faking-it-import-local-files-into-attachment-fu/88484 "2007-03-11T22:41:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcginniwa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbce88/32.png) [@mcginniwa](https://www.ruby-forum.com/u/mcginniwa)\
**Post date:** [March 11, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/faking-it-import-local-files-into-attachment-fu/88484/1 "2007-03-11T22:41:59Z")

</div>

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/](http://blog.katipo.co.nz/)

---

<div class="post-metadata">

**Author:** ![mcginniwa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbce88/32.png) [@mcginniwa](https://www.ruby-forum.com/u/mcginniwa)\
**Post date:** [March 20, 2007, 5:25am UTC](https://www.ruby-forum.com/t/faking-it-import-local-files-into-attachment-fu/88484/2 "2007-03-20T05:25:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mcginniwa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbce88/32.png) [@mcginniwa](https://www.ruby-forum.com/u/mcginniwa)\
**Post date:** [April 19, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/faking-it-import-local-files-into-attachment-fu/88484/3 "2007-04-19T18:26:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mcginniwa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbce88/32.png) [@mcginniwa](https://www.ruby-forum.com/u/mcginniwa)\
**Post date:** [April 19, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/faking-it-import-local-files-into-attachment-fu/88484/4 "2007-04-19T19:46:35Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![mcginniwa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bbce88/32.png) [@mcginniwa](https://www.ruby-forum.com/u/mcginniwa)\
**Post date:** [May 28, 2007, 1:18am UTC](https://www.ruby-forum.com/t/faking-it-import-local-files-into-attachment-fu/88484/5 "2007-05-28T01:18:12Z")

</div>

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](http://development.kete.net.nz/kete/repository/file/trunk/lib/workers/past_perfect4_importer_worker.rb)

Cheers,  
Walter
