socialforge/app/controllers/test_controller.rb

56 lines
1.5 KiB
Ruby

class TestController < ApplicationController
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|
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 courselist
@courses = Project.course_entities
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