zip down controller created
This commit is contained in:
parent
d43bf49f4c
commit
84d3e40c41
|
@ -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.
|
|
@ -0,0 +1,4 @@
|
|||
/*
|
||||
Place all the styles related to the matching controller here.
|
||||
They will automatically be included in application.css.
|
||||
*/
|
|
@ -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
|
|
@ -0,0 +1,2 @@
|
|||
module ZipdownHelper
|
||||
end
|
|
@ -14,7 +14,7 @@
|
|||
</div>
|
||||
<% 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? %>
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
require 'test_helper'
|
||||
|
||||
class ZipdownControllerTest < ActionController::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
|
@ -0,0 +1,4 @@
|
|||
require 'test_helper'
|
||||
|
||||
class ZipdownHelperTest < ActionView::TestCase
|
||||
end
|
Loading…
Reference in New Issue