socialforge/app/controllers/zipdown_controller.rb

120 lines
3.8 KiB
Ruby
Raw Normal View History

2015-12-22 15:45:17 +08:00
#coding=utf-8
class ZipdownController < ApplicationController
#查找项目(课程)
before_filter :find_project_by_bid_id, :only => [:assort]
#检查权限
#勿删 before_filter :authorize, :only => [:assort,:download_user_homework]
SAVE_FOLDER = "#{Rails.root}/files"
2015-04-03 11:29:37 +08:00
OUTPUT_FOLDER = "#{Rails.root}/files/archiveZip"
2016-05-30 18:40:22 +08:00
MAX_PATH = 50
2017-01-06 11:30:54 +08:00
## 200M
MAX_DOWN_SIZE = 200 * 1024 * 1024
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
#一个作业下所有文件打包下载只有admin和课程老师有权限
def assort
if params[:obj_class] == "Bid"
bid = Bid.find params[:obj_id]
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"
homework = Work.find params[:obj_id]
render_403 if User.current.admin_of_contest?(homework.contest)
2017-01-06 11:30:54 +08:00
zipfile = checkfileSize(homework.contestant_works) {
zip_homework_common homework
}
else
logger.error "[ZipDown#assort] ===> #{params[:obj_class]} unKown !!"
end
respond_to do |format|
format.json {
render json: zipfile.to_json
}
2015-03-10 09:31:30 +08:00
end
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
unless homework.attachments.empty?
zipfile = zip_homework_by_user homework
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)
else
render file: 'public/no_file_found.html'
end
else
render file: 'public/file_not_found.html'
end
else
render_403
end
#rescue => e
# render file: 'public/file_not_found.html'
end
private
#通过作业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
2017-01-06 10:47:25 +08:00
include ZipService
2016-12-23 21:44:03 +08:00
2015-04-27 12:04:08 +08:00
end