From 0af32351a2718373536c80a336603a5d68a2768e Mon Sep 17 00:00:00 2001 From: Pavel Chipiga Date: Sat, 3 Dec 2011 02:52:19 +0200 Subject: [PATCH] Redo paperclip file upload through nginx upload module. Refs #2261 --- app/models/product.rb | 30 +++++++++++++++--------------- config/initializers/load_config.rb | 2 ++ lib/ext/paperclip.rb | 28 ++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 lib/ext/paperclip.rb diff --git a/app/models/product.rb b/app/models/product.rb index 1c9edc424..aa9abc8ca 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -16,21 +16,21 @@ class Product < ActiveRecord::Base scope :recent, order("name ASC") - attr_accessor :tmp_upload_dir - after_save lambda { FileUtils.rm_rf(tmp_upload_dir) if tmp_upload_dir and File.directory?(tmp_upload_dir) } - - def tar_with_fast=(file) - if file.present? and file.respond_to?('[]') - self.tmp_upload_dir = "#{file['filepath']}_1" - tmp_file_path = File.join tmp_upload_dir, file['original_name'] - FileUtils.mkdir_p tmp_upload_dir - FileUtils.mv file['filepath'], tmp_file_path - self.tar_without_fast = File.open tmp_file_path, 'rb' - else - self.tar_without_fast = file - end - end - alias_method_chain :tar=, :fast + # attr_accessor :tmp_upload_dir + # after_save lambda { FileUtils.rm_rf(tmp_upload_dir) if tmp_upload_dir and File.directory?(tmp_upload_dir) } + # + # def tar_with_fast=(file) + # if file.present? and file.respond_to?('[]') + # self.tmp_upload_dir = "#{file['filepath']}_1" + # tmp_file_path = File.join tmp_upload_dir, file['original_name'] + # FileUtils.mkdir_p tmp_upload_dir + # FileUtils.mv file['filepath'], tmp_file_path + # self.tar_without_fast = File.open tmp_file_path, 'rb' + # else + # self.tar_without_fast = file + # end + # end + # alias_method_chain :tar=, :fast def delete_tar @delete_tar ||= "0" diff --git a/config/initializers/load_config.rb b/config/initializers/load_config.rb index fb6d0a02a..70d1244b6 100644 --- a/config/initializers/load_config.rb +++ b/config/initializers/load_config.rb @@ -10,3 +10,5 @@ Rosa::Application.config.middleware.insert_after ::Rails::Rack::Logger, ::Grack: Rosa::Application.config.middleware.insert_before ::Grack::Handler, ::Grack::Auth # Grit::Git.git_timeout = 60 + +Dir[Rails.root.join("lib/ext/**/*.rb")].each {|f| require f} diff --git a/lib/ext/paperclip.rb b/lib/ext/paperclip.rb new file mode 100644 index 000000000..b41f9275a --- /dev/null +++ b/lib/ext/paperclip.rb @@ -0,0 +1,28 @@ +module Paperclip + class Attachment + class UploadedPath + attr_reader :original_filename, :content_type, :size, :path + def initialize(uploaded_file) + @original_filename = uploaded_file["name"].downcase + @content_type = uploaded_file["content_type"].to_s.strip + @file_size = uploaded_file["size"].to_i + @path = uploaded_file["path"] + end + + # TODO remove failed files + + def to_tempfile; self; end + + def size; @file_size; end + + def close; end + def closed?; true; end + end + + def assign_with_upload(uploaded_file) + uploaded_file = UploadedPath.new(uploaded_file) if uploaded_file.is_a?(Hash) + assign_without_upload(uploaded_file) + end + alias_method_chain :assign, :upload + end +end