Skip to content

Commit

Permalink
added upload extractor and updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Tenner committed Mar 14, 2009
1 parent ccfcd1e commit 651098e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
51 changes: 49 additions & 2 deletions README.textile
@@ -1,6 +1,6 @@
h1. What is Num-fu?

Num-fu is a plugin built for "Woobius":http://www.woobius.com, which handles uploads coming via "nginx's upload module":http://www.grid.net.ru/nginx/upload.en.html. More info on that upload module "here":http://brainspl.at/articles/2008/07/20/nginx-upload-module.
Num-fu is a plugin built for "Woobius":http://www.woobius.com, which handles uploads coming via "nginx's upload module":http://www.grid.net.ru/nginx/upload.en.html. More info on that upload module "here":http://brainspl.at/articles/2008/07/20/nginx-upload-module and "here":http://www.motionstandingstill.com/nginx-upload-awesomeness/2008-08-13/.

It's worth noting that this is not a straightforward adaptation of AttachmentFu to the Nginx Upload Module. I've picked out the functionality that we are actually using, and much of AttachmentFu has been left aside. This plugin has perhaps 5% of the functionality of Attachment-Fu, and no intention to build in things like image processing, S3 support, thumbnails, etc.

Expand All @@ -12,7 +12,7 @@ h1. How can I use this?

h2. Usage:

p. Your model should look something like this:
Your model should look something like this:

<pre>
class FileReference < ActiveRecord::Base
Expand All @@ -21,6 +21,53 @@ class FileReference < ActiveRecord::Base
end
</pre>

In your controller, add an "extract_upload" call before processing the upload, to ensure that your code works whether or not nginx is active:

<pre>
def upload
extract_upload :file_reference
FileReference.create(params[:file_reference])
end
</pre>

And add the following to your nginx config:

<pre>
location /upload
{
# Pass altered request body to this location
# NTW: This seems to be ignored and the above location is used instead
upload_pass /dummy;

# Store files to this directory
# The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
upload_store /tmp/uploads 1;

# Allow uploaded files to be read by everyone
upload_store_access user:rw group:rw all:rw;

# Set specified fields in request body
# this puts the orginal filename, new path+filename and content type in the requests params
upload_set_form_field $upload_field_name.name "$upload_file_name";
upload_set_form_field $upload_field_name.content_type "$upload_content_type";
upload_set_form_field $upload_field_name.path "$upload_tmp_path";

upload_pass_form_field "^_session_id$";
upload_pass_form_field "^folder[id]$";
}

# dummy location that needs to be defined. :-(
location /dummy
{
}
</pre>

Worth noting (from "here":http://www.motionstandingstill.com/nginx-upload-awesomeness/2008-08-13/):

* You have to explicitly tell nginx what fields to pass, if there are additional fields, on top of the file itself (and there likely will be things like session id)
* The upload_pass command is ignored in v2.04 of the module, but you still have to include it
* The location that you post to has to be of a direct route, off root - not /controller/action

h2. Schema setup

You will need the following fields set up on your FileReference schema:
Expand Down
2 changes: 1 addition & 1 deletion init.rb
@@ -1,2 +1,2 @@

ActiveRecord::Base.send(:extend, NumFu::ActMethods)
ActiveRecord::Base.send(:extend, NumFu::ActMethods)
14 changes: 9 additions & 5 deletions lib/num_fu.rb
@@ -1,7 +1,8 @@
module NumFu
@@tempfile_path = File.join(RAILS_ROOT, 'tmp', 'attachment_fu')
mattr_writer :tempfile_path

@@tempfile_path = File.join(RAILS_ROOT, 'tmp', 'attachment_fu')
@@use_nginx_upload_module = false
mattr_writer :tempfile_path,
:use_nginx_upload_module
module ActMethods
def has_attachment(options = {})
options[:min_size] ||= 1
Expand Down Expand Up @@ -54,8 +55,6 @@ def save_to_storage
FileUtils.mkdir_p(File.dirname(full_filename))
File.cp(@temp_path, full_filename)
File.chmod(attachment_options[:chmod] || 0744, full_filename)
else
RAILS_DEFAULT_LOGGER.debug "========= no temp path? #{@temp_path}"
end
end

Expand Down Expand Up @@ -104,4 +103,9 @@ def set_size_from_temp_path
self.size = File.size(@temp_path)
end
end

class FakeUploadData
attr_accessor :filename, :content_type, :path, :original_filename
end

end
17 changes: 17 additions & 0 deletions lib/num_fu/upload_extractor.rb
@@ -0,0 +1,17 @@
module NumFu
module UploadExtractor

# Determine whether the upload is via Nginx Upload Module or not, and if so, transmogrify it to be
# in a standard form that can be passed on to InstanceMethods#uploaded_data=
def extract_upload(name="file")
if params["#{name}.path"]
fake_upload_data = FakeUploadData.new
fake_upload_data.path = params["#{name}.path"]
fake_upload_data.original_filename = params["#{name}.original_filename"]
fake_upload_data.content_type = params["#{name}.content_type"]
params["#{name}"][:uploaded_data] = fake_upload_data
end
end

end
end

0 comments on commit 651098e

Please sign in to comment.