Attach non image files in Rails with paperclip

19 05 2009

Content moved to http://awesomeful.net/posts/33-attach-non-image-files-in-rails-with-paperclip

Advertisements

Actions

Information

7 responses

2 06 2009
bkidd

So what if I want to store text/html from an object in memory as an html file using paperclip?

2 06 2009
H

I don’t understand. If you have an object in memory, why would you want to use paperclip to store it? Paperclip is excellent at helping you upload files, and then helps you do the bookkeeping on the server side by associating those files with your models. From the looks of it, you already have an object in memory and so all you need to do is persist it on the filesystem somehow. This could either be on your database (using ActiveRecord or other ORM) or as a file (using the File class)
Maybe you could explain a little better what it is you want to do?

2 06 2009
bkidd

Yeah, we’re storing html in the DB currently, but it’s becoming a size issue, so…I need to store the html that is currently going in a text field in mysql to a file with id partitioning as we will have millions and millions of records before too long. I think using the id of the row would be good as a file name, so the file would probably have to save to the file system as an after_save. The file contents wouldn’t need to be mutable or searchable, but they will be rendered back in a view. Just thought that something like paperclip would help with this. Suggestions?

2 06 2009
H

I think that paperclip is an overkill for this, but I would definitely study it’s source code. And to be honest, you seem to have it mapped out already. An after_save sounds good to me, so does IDs for file names. Have you considered storing this in S3? The one last suggestion would be to go for either page, action or fragment caching depending on your needs, just to reduce the I/O burden.
Good luck!

2 06 2009
bkidd

Yeah, we’re looking at AS3 as a possibility. Mainly I’m just trying to determine the best way to go about taking the object data and writing it to a file. I’ve been browsing FileStore and Pstore. I haven’t actually done this before. We use memcached for a few things in the app right now.

2 06 2009
H

Here’s a quick crack at saving the file, untested.

some_html = 'Awesome! This is a ....'
file_name = base_path + id + '.htm'
File.open(file_name, 'w') do |file|
  some_html.each_line do |line|
    file << line
  end
end
FileUtils.chmod(0644, file_name) #just to make sure

Definitely take a look at these if you haven't already:
http://railslab.newrelic.com/scaling-rails
http://rightaws.rubyforge.org/right_aws_gem_doc/

Cheers

20 08 2009
Tyler

Thanks so much for this post! Just what I needed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: