|
- #coding=utf-8
-
- class ZipdownController < ApplicationController
- #查找项目(课程)
- before_filter :find_project_by_bid_id, :only => [:assort]
- #检查权限
- #勿删 before_filter :authorize, :only => [:assort,:download_user_homework]
- ## 200M
- MAX_DOWN_SIZE = 200 * 1024 * 1024
-
- include ZipService
-
- #统一下载功能
- def download
- if User.current.logged?
- begin
- if params[:base64file]
- file = decode64(params[:base64file])
- 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
- rescue => e
- render file: 'public/no_file_found.html'
- end
- else
- render_403
- end
- end
-
- #一个作业下所有文件打包下载,只有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)
- zipfile = checkfileSize(bid.homeworks) {
- zip_bid bid
- }
- elsif params[:obj_class] == "HomeworkCommon"
- homework = HomeworkCommon.find params[:obj_id]
- render_403 if User.current.allowed_to?(:as_teacher,homework.course)
- zipfile = checkfileSize(homework.student_works) {
- zip_homework_common homework
- }
- elsif params[:obj_class] == "Work"
- 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
- }
- else
- logger.error "[ZipDown#assort] ===> #{params[:obj_class]} unKown !!"
- end
- respond_to do |format|
- format.json {
- render json: zipfile.to_json
- }
- 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
-
- 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
-
- end
|