trustieforge/app/controllers/test_controller.rb

59 lines
1.6 KiB
Ruby
Raw Normal View History

2013-08-12 17:30:31 +08:00
class TestController < ApplicationController
2014-04-22 14:53:25 +08:00
helper :UserScore
2014-05-23 17:07:58 +08:00
layout 'bootstrap_base'
2013-08-12 17:30:31 +08:00
2014-04-14 17:36:23 +08:00
def zip
homeworks_attach_path = []
homework_id = params[:homework_id]
bid = Bid.find_by_id(homework_id)
bid.homeworks.each do |homeattach|
homeattach.attachments.each do |attach|
2014-04-15 14:33:19 +08:00
length = attach.storage_path.length
homeworks_attach_path << attach.diskfile.to_s.slice((length+1)..-1)
2014-04-14 17:36:23 +08:00
end
2013-08-12 17:30:31 +08:00
end
2014-04-14 17:36:23 +08:00
@paths = homeworks_attach_path
zipfile = ziping homeworks_attach_path
send_file zipfile, :filename => bid.name,
:type => detect_content_type(zipfile)
2014-04-15 21:13:56 +08:00
rescue Errno::ENOENT => e
logger.error "[Errno::ENOENT] ===> #{e}"
2014-04-14 17:36:23 +08:00
end
def courselist
@courses = Project.course_entities
end
def ziping files_path
2014-04-15 21:13:56 +08:00
ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE')
2014-04-14 21:59:32 +08:00
folder = "#{Rails.root}/files"
2014-04-14 17:36:23 +08:00
input_filename = files_path
2014-04-15 14:42:00 +08:00
zipfile_name = "#{Rails.root}/tmp/archiveZip/archive_#{Time.now.to_i}.zip"
2014-04-15 21:13:56 +08:00
2014-04-16 10:42:49 +08:00
Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name))
2014-04-14 17:36:23 +08:00
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
input_filename.each do |filename|
2014-04-15 21:13:56 +08:00
zipfile.add(ic.iconv(filename_to_real(File.basename(filename))), folder + '/' + filename)
2014-04-14 17:36:23 +08:00
end
2014-04-15 14:33:19 +08:00
zipfile.get_output_stream("ReadMe"){ |os|
2014-04-14 21:59:32 +08:00
os.write "Homeworks"
2014-04-14 17:36:23 +08:00
}
end
zipfile_name
end
def detect_content_type(name)
content_type = Redmine::MimeType.of(name)
content_type.to_s
end
2014-04-15 21:13:56 +08:00
def filename_to_real name
attach = Attachment.find_by_disk_filename(name)
attach.filename
end
2013-08-12 17:30:31 +08:00
end