2015-12-22 15:45:17 +08:00
|
|
|
|
#coding=utf-8
|
|
|
|
|
|
2014-09-15 10:58:14 +08:00
|
|
|
|
class ZipdownController < ApplicationController
|
|
|
|
|
#查找项目(课程)
|
|
|
|
|
before_filter :find_project_by_bid_id, :only => [:assort]
|
|
|
|
|
#检查权限
|
|
|
|
|
#勿删 before_filter :authorize, :only => [:assort,:download_user_homework]
|
2017-01-06 11:30:54 +08:00
|
|
|
|
## 200M
|
|
|
|
|
MAX_DOWN_SIZE = 200 * 1024 * 1024
|
2014-09-15 10:58:14 +08:00
|
|
|
|
|
2017-01-06 14:52:47 +08:00
|
|
|
|
include ZipService
|
|
|
|
|
|
2017-01-06 11:30:54 +08:00
|
|
|
|
#统一下载功能
|
2015-03-10 09:31:30 +08:00
|
|
|
|
def download
|
2015-05-24 13:52:58 +08:00
|
|
|
|
if User.current.logged?
|
|
|
|
|
begin
|
2015-12-22 15:45:17 +08:00
|
|
|
|
if params[:base64file]
|
2016-05-30 18:35:47 +08:00
|
|
|
|
file = decode64(params[:base64file])
|
2015-12-22 15:45:17 +08:00
|
|
|
|
send_file "#{OUTPUT_FOLDER}/#{file}", :filename => filename_for_content_disposition(file), :type => detect_content_type(file)
|
|
|
|
|
else
|
|
|
|
|
send_file "#{OUTPUT_FOLDER}/#{params[:file]}", :filename => filename_for_content_disposition(params[:filename]), :type => detect_content_type(params[:file])
|
|
|
|
|
end
|
2015-05-24 13:52:58 +08:00
|
|
|
|
rescue => e
|
|
|
|
|
render file: 'public/no_file_found.html'
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
render_403
|
2015-03-10 09:31:30 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
2015-01-31 11:14:55 +08:00
|
|
|
|
|
2015-04-03 13:10:18 +08:00
|
|
|
|
#一个作业下所有文件打包下载,只有admin和课程老师有权限
|
2014-09-15 10:58:14 +08:00
|
|
|
|
def assort
|
2014-11-11 11:16:37 +08:00
|
|
|
|
if params[:obj_class] == "Bid"
|
|
|
|
|
bid = Bid.find params[:obj_id]
|
2015-04-03 13:10:18 +08:00
|
|
|
|
render_403 if User.current.allowed_to?(:as_teacher,bid.courses.first)
|
2017-01-06 11:30:54 +08:00
|
|
|
|
zipfile = checkfileSize(bid.homeworks) {
|
|
|
|
|
zip_bid bid
|
|
|
|
|
}
|
2015-05-28 10:30:10 +08:00
|
|
|
|
elsif params[:obj_class] == "HomeworkCommon"
|
|
|
|
|
homework = HomeworkCommon.find params[:obj_id]
|
|
|
|
|
render_403 if User.current.allowed_to?(:as_teacher,homework.course)
|
2017-01-06 11:30:54 +08:00
|
|
|
|
zipfile = checkfileSize(homework.student_works) {
|
|
|
|
|
zip_homework_common homework
|
|
|
|
|
}
|
2016-12-23 21:44:03 +08:00
|
|
|
|
elsif params[:obj_class] == "Work"
|
2017-01-13 11:17:21 +08:00
|
|
|
|
contest_work = Work.find params[:obj_id]
|
|
|
|
|
render_403 if User.current.admin_of_contest?(contest_work.contest)
|
|
|
|
|
zipfile = checkfileSize(contest_work.contestant_works) {
|
|
|
|
|
zip_contest_work contest_work
|
2017-01-06 11:30:54 +08:00
|
|
|
|
}
|
2014-11-11 11:16:37 +08:00
|
|
|
|
else
|
|
|
|
|
logger.error "[ZipDown#assort] ===> #{params[:obj_class]} unKown !!"
|
2014-09-15 10:58:14 +08:00
|
|
|
|
end
|
2015-03-25 13:13:09 +08:00
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.json {
|
|
|
|
|
render json: zipfile.to_json
|
|
|
|
|
}
|
2015-03-10 09:31:30 +08:00
|
|
|
|
end
|
2014-09-15 10:58:14 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#下载某一学生的作业的所有文件
|
|
|
|
|
def download_user_homework
|
|
|
|
|
homework = HomeworkAttach.find params[:homework]
|
|
|
|
|
if User.current.admin? || User.current.member_of_course?(homework.bid.courses.first)
|
|
|
|
|
if homework != nil
|
2014-11-11 11:16:37 +08:00
|
|
|
|
unless homework.attachments.empty?
|
2014-09-15 10:58:14 +08:00
|
|
|
|
zipfile = zip_homework_by_user homework
|
2015-06-10 15:36:11 +08:00
|
|
|
|
filename = ((homework.user.user_extensions.nil? || homework.user.user_extensions.student_id.nil?) ? "" : homework.user.user_extensions.student_id) +
|
|
|
|
|
"_" + homework.user.show_name +
|
|
|
|
|
"_" + homework.name + ".zip"
|
|
|
|
|
send_file zipfile.file_path, :filename => filename_for_content_disposition(filename), :type => detect_content_type(zipfile.file_path) if(zipfile)
|
2014-09-15 10:58:14 +08:00
|
|
|
|
else
|
2014-11-11 11:16:37 +08:00
|
|
|
|
render file: 'public/no_file_found.html'
|
2014-09-15 10:58:14 +08:00
|
|
|
|
end
|
|
|
|
|
else
|
2014-11-11 11:16:37 +08:00
|
|
|
|
render file: 'public/file_not_found.html'
|
2014-09-15 10:58:14 +08:00
|
|
|
|
end
|
|
|
|
|
else
|
2014-11-11 11:16:37 +08:00
|
|
|
|
render_403
|
2014-09-15 10:58:14 +08:00
|
|
|
|
end
|
2015-01-10 10:55:37 +08:00
|
|
|
|
#rescue => e
|
|
|
|
|
# render file: 'public/file_not_found.html'
|
2014-09-15 10:58:14 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
2015-01-10 10:55:37 +08:00
|
|
|
|
#通过作业Id找到项目(课程)
|
|
|
|
|
def find_project_by_bid_id
|
|
|
|
|
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
|
|
|
|
|
@project = obj.courses[0]
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-01-06 11:30:54 +08:00
|
|
|
|
def checkfileSize(works)
|
|
|
|
|
file_count = 0
|
|
|
|
|
file_size = 0
|
|
|
|
|
works.each do |work|
|
|
|
|
|
file_count += work.attachments.count
|
|
|
|
|
work.attachments.each do |attach|
|
|
|
|
|
file_size += attach.filesize
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if file_size > MAX_DOWN_SIZE
|
|
|
|
|
{err: -1, message: 'file size to large'}
|
|
|
|
|
elsif file_count > 0
|
|
|
|
|
yield if block_given?
|
|
|
|
|
else
|
|
|
|
|
{err: -2, :message => "no file"}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-04-27 12:04:08 +08:00
|
|
|
|
end
|