From 84d3e40c4100cfb0ebce85e050798855aae10856 Mon Sep 17 00:00:00 2001 From: yanxd Date: Wed, 16 Apr 2014 11:26:30 +0800 Subject: [PATCH] zip down controller created --- app/assets/javascripts/zipdown.js | 2 + app/assets/stylesheets/zipdown.css | 4 ++ app/controllers/zipdown_controller.rb | 64 ++++++++++++++++++++++ app/helpers/zipdown_helper.rb | 2 + app/views/bids/_homework_list.html.erb | 2 +- config/routes.rb | 3 + test/functional/zipdown_controller_test.rb | 7 +++ test/unit/helpers/zipdown_helper_test.rb | 4 ++ 8 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/zipdown.js create mode 100644 app/assets/stylesheets/zipdown.css create mode 100644 app/controllers/zipdown_controller.rb create mode 100644 app/helpers/zipdown_helper.rb create mode 100644 test/functional/zipdown_controller_test.rb create mode 100644 test/unit/helpers/zipdown_helper_test.rb diff --git a/app/assets/javascripts/zipdown.js b/app/assets/javascripts/zipdown.js new file mode 100644 index 00000000..dee720fa --- /dev/null +++ b/app/assets/javascripts/zipdown.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/stylesheets/zipdown.css b/app/assets/stylesheets/zipdown.css new file mode 100644 index 00000000..afad32db --- /dev/null +++ b/app/assets/stylesheets/zipdown.css @@ -0,0 +1,4 @@ +/* + Place all the styles related to the matching controller here. + They will automatically be included in application.css. +*/ diff --git a/app/controllers/zipdown_controller.rb b/app/controllers/zipdown_controller.rb new file mode 100644 index 00000000..059e1189 --- /dev/null +++ b/app/controllers/zipdown_controller.rb @@ -0,0 +1,64 @@ +class ZipdownController < ApplicationController + def assort + obj_class = params[:obj_class] + obj_id = params[:obj_id] + obj = obj_class.constantize.find(obj_id) + case obj.class.to_s.to_sym + when :Bid + zip obj + else + logger.error "[ZipDown#assort] ===> #{obj.class.to_s.to_sym} unKown !!" + end + + rescue NameError,ActiveRecord::RecordNotFound => e + logger.error "[ZipDown] ===> #{e}" + end + + private + + def zip bid + # Todo: User Access Controll + homeworks_attach_path = [] + bid.homeworks.each do |homeattach| + homeattach.attachments.each do |attach| + length = attach.storage_path.length + homeworks_attach_path << attach.diskfile.to_s.slice((length+1)..-1) + end + end + @paths = homeworks_attach_path + zipfile = ziping homeworks_attach_path + send_file zipfile, :filename => bid.name, + :type => detect_content_type(zipfile) + rescue Errno::ENOENT => e + logger.error "[Errno::ENOENT] ===> #{e}" + + end + + def ziping files_path + ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE') + folder = "#{Rails.root}/files" + input_filename = files_path + zipfile_name = "#{Rails.root}/tmp/archiveZip/archive_#{Time.now.to_i}.zip" + + Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name)) + Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| + input_filename.each do |filename| + zipfile.add(ic.iconv(filename_to_real(File.basename(filename))), folder + '/' + filename) + end + zipfile.get_output_stream("ReadMe"){ |os| + os.write "Homeworks" + } + end + zipfile_name + end + + def detect_content_type(name) + content_type = Redmine::MimeType.of(name) + content_type.to_s + end + + def filename_to_real name + attach = Attachment.find_by_disk_filename(name) + attach.filename + end +end diff --git a/app/helpers/zipdown_helper.rb b/app/helpers/zipdown_helper.rb new file mode 100644 index 00000000..d0b9a488 --- /dev/null +++ b/app/helpers/zipdown_helper.rb @@ -0,0 +1,2 @@ +module ZipdownHelper +end diff --git a/app/views/bids/_homework_list.html.erb b/app/views/bids/_homework_list.html.erb index 1c122bfb..ad2bc1c0 100644 --- a/app/views/bids/_homework_list.html.erb +++ b/app/views/bids/_homework_list.html.erb @@ -14,7 +14,7 @@ <% end %> <% display_id = im_watching_student_id? @bid%> -<%= link_to "作业打包下载", test_zip_path(homework_id: @bid), remote: true, class: "button_submit button_submit_font_white", style: "margin: 5px 10px;display: inline-block;" if( User.current.admin? || +<%= link_to "作业打包下载", zipdown_assort_path(obj_class: @bid.class, obj_id: @bid), remote: false, class: "button_submit button_submit_font_white", style: "margin: 5px 10px;display: inline-block;" if( User.current.admin? || !(User.current.roles_for_project(@bid.courses.first).map(&:id) & ([7,9, 10])).empty? ) && Rails.env.development? %> diff --git a/config/routes.rb b/config/routes.rb index 0b57b8d6..8e7a2501 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,6 +16,9 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. RedmineApp::Application.routes.draw do + namespace :zipdown do + match 'assort' + end namespace :test do match 'courselist' match 'zip' diff --git a/test/functional/zipdown_controller_test.rb b/test/functional/zipdown_controller_test.rb new file mode 100644 index 00000000..f74dcc1c --- /dev/null +++ b/test/functional/zipdown_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ZipdownControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/unit/helpers/zipdown_helper_test.rb b/test/unit/helpers/zipdown_helper_test.rb new file mode 100644 index 00000000..ca30cb36 --- /dev/null +++ b/test/unit/helpers/zipdown_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class ZipdownHelperTest < ActionView::TestCase +end