Redo paperclip file upload through nginx upload module. Refs #2261

This commit is contained in:
Pavel Chipiga 2011-12-03 02:52:19 +02:00
parent a9a2059cae
commit 0af32351a2
3 changed files with 45 additions and 15 deletions

View File

@ -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"

View File

@ -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}

28
lib/ext/paperclip.rb Normal file
View File

@ -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