Merge branch 'develop' into szzh

dev_repository_hjq
sw 11 years ago
commit 21db854adb

@ -1,140 +1,140 @@
require 'zip' # require 'zip'
class ZipdownController < ApplicationController # class ZipdownController < ApplicationController
#查找项目(课程) # #查找项目(课程)
before_filter :find_project_by_bid_id, :only => [:assort] # before_filter :find_project_by_bid_id, :only => [:assort]
#检查权限 # #检查权限
#勿删 before_filter :authorize, :only => [:assort,:download_user_homework] # #勿删 before_filter :authorize, :only => [:assort,:download_user_homework]
SAVE_FOLDER = "#{Rails.root}/files" # SAVE_FOLDER = "#{Rails.root}/files"
OUTPUT_FOLDER = "#{Rails.root}/tmp/archiveZip" # OUTPUT_FOLDER = "#{Rails.root}/tmp/archiveZip"
#
#
def assort # def assort
if params[:obj_class] == "Bid" # if params[:obj_class] == "Bid"
bid = Bid.find params[:obj_id] # bid = Bid.find params[:obj_id]
file_count = 0 # file_count = 0
bid.homeworks.map { |homework| file_count += homework.attachments.count} # bid.homeworks.map { |homework| file_count += homework.attachments.count}
if file_count > 0 # if file_count > 0
zipfile = zip_bid bid # zipfile = zip_bid bid
else # else
render file: 'public/no_file_found.html' # render file: 'public/no_file_found.html'
end # end
else # else
logger.error "[ZipDown#assort] ===> #{params[:obj_class]} unKown !!" # logger.error "[ZipDown#assort] ===> #{params[:obj_class]} unKown !!"
end # end
send_file zipfile, :filename => bid.name + ".zip", :type => detect_content_type(zipfile) if zipfile # send_file zipfile, :filename => bid.name + ".zip", :type => detect_content_type(zipfile) if zipfile
#
#rescue Exception => e # #rescue Exception => e
# # render file: 'public/no_file_found.html'
# 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
# send_file zipfile, :filename => ((homework.user.user_extensions.nil? || homework.user.user_extensions.student_id.nil?) ? "" : homework.user.user_extensions.student_id) +
# "_" + (homework.user.lastname.nil? ? "" : homework.user.lastname) + (homework.user.firstname.nil? ? "" : homework.user.firstname) +
# "_" + homework.name + ".zip", :type => detect_content_type(zipfile) if(zipfile)
# else
# render file: 'public/no_file_found.html' # render file: 'public/no_file_found.html'
end # end
# else
#下载某一学生的作业的所有文件
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
send_file zipfile, :filename => ((homework.user.user_extensions.nil? || homework.user.user_extensions.student_id.nil?) ? "" : homework.user.user_extensions.student_id) +
"_" + (homework.user.lastname.nil? ? "" : homework.user.lastname) + (homework.user.firstname.nil? ? "" : homework.user.firstname) +
"_" + homework.name + ".zip", :type => detect_content_type(zipfile) 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' # render file: 'public/file_not_found.html'
end # end
# else
private # render_403
# end
#通过作业Id找到项目课程 # #rescue => e
def find_project_by_bid_id # # render file: 'public/file_not_found.html'
obj_class = params[:obj_class] # end
obj_id = params[:obj_id] #
obj = obj_class.constantize.find(obj_id) # private
case obj.class.to_s.to_sym #
when :Bid # #通过作业Id找到项目课程
@project = obj.courses[0] # def find_project_by_bid_id
end # obj_class = params[:obj_class]
end # obj_id = params[:obj_id]
# obj = obj_class.constantize.find(obj_id)
def zip_bid(bid) # case obj.class.to_s.to_sym
# Todo: User Access Controll # when :Bid
bid_homework_path = [] # @project = obj.courses[0]
bid.homeworks.each do |homeattach| # end
unless homeattach.attachments.empty? # end
bid_homework_path << zip_homework_by_user(homeattach) #
end # def zip_bid(bid)
end # # Todo: User Access Controll
zipping "#{Time.now.to_i}_#{bid.name}.zip", bid_homework_path, OUTPUT_FOLDER # bid_homework_path = []
end # bid.homeworks.each do |homeattach|
# unless homeattach.attachments.empty?
def zip_homework_by_user(homeattach) # bid_homework_path << zip_homework_by_user(homeattach)
homeworks_attach_path = [] # end
not_exist_file = [] # end
# 需要将所有homework.attachments遍历加入zip # zipping "#{Time.now.to_i}_#{bid.name}.zip", bid_homework_path, OUTPUT_FOLDER
# 并且返回zip路径 # end
homeattach.attachments.each do |attach| #
if File.exist?(attach.diskfile) # def zip_homework_by_user(homeattach)
homeworks_attach_path << attach.diskfile # homeworks_attach_path = []
else # not_exist_file = []
not_exist_file << attach.filename # # 需要将所有homework.attachments遍历加入zip
end # # 并且返回zip路径
end # homeattach.attachments.each do |attach|
zipping("#{homeattach.user.lastname}#{homeattach.user.firstname}_#{((homeattach.user.user_extensions.nil? || homeattach.user.user_extensions.student_id.nil?) ? "" : homeattach.user.user_extensions.student_id)}_#{Time.now.to_i.to_s}.zip", homeworks_attach_path, OUTPUT_FOLDER, true, not_exist_file) # if File.exist?(attach.diskfile)
end # homeworks_attach_path << attach.diskfile
# else
# not_exist_file << attach.filename
def zipping(zip_name_refer, files_paths, output_path, is_attachment=false, not_exist_file=[]) # end
# 输入待打包的文件列表已经打包文件定位到ouput_path # end
ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE') # zipping("#{homeattach.user.lastname}#{homeattach.user.firstname}_#{((homeattach.user.user_extensions.nil? || homeattach.user.user_extensions.student_id.nil?) ? "" : homeattach.user.user_extensions.student_id)}_#{Time.now.to_i.to_s}.zip", homeworks_attach_path, OUTPUT_FOLDER, true, not_exist_file)
input_filename = files_paths # end
#
rename_zipfile = zip_name_refer ||= "#{Time.now.to_i.to_s}.zip" #
zipfile_name = "#{output_path}/#{rename_zipfile}" # def zipping(zip_name_refer, files_paths, output_path, is_attachment=false, not_exist_file=[])
# # 输入待打包的文件列表已经打包文件定位到ouput_path
Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name)) # ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE')
# input_filename = files_paths
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| #
input_filename.each do |filename| # rename_zipfile = zip_name_refer ||= "#{Time.now.to_i.to_s}.zip"
flag = true # zipfile_name = "#{output_path}/#{rename_zipfile}"
index = 1 #
rename_file = ic.iconv( (File.basename(filename)) ).to_s # Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name))
rename_file = ic.iconv( filename_to_real( File.basename(filename))).to_s if is_attachment #
# Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
begin # input_filename.each do |filename|
zipfile.add(rename_file, filename) # flag = true
flag = false # index = 1
rescue Exception => e # rename_file = ic.iconv( (File.basename(filename)) ).to_s
zipfile.get_output_stream('FILE_NOTICE.txt') do |os| # rename_file = ic.iconv( filename_to_real( File.basename(filename))).to_s if is_attachment
os.write l(:label_file_exist) #
end # begin
next # zipfile.add(rename_file, filename)
end # flag = false
end # rescue Exception => e
unless not_exist_file.empty? # zipfile.get_output_stream('FILE_NOTICE.txt') do |os|
zipfile.get_output_stream('FILE_LOST.txt') do |os| # os.write l(:label_file_exist)
os.write l(:label_file_lost) + not_exist_file.join(',').to_s # end
end # next
end # end
end # end
zipfile_name # unless not_exist_file.empty?
#rescue Errno => e # zipfile.get_output_stream('FILE_LOST.txt') do |os|
# logger.error "[zipdown#zipping] ===> #{e}" # os.write l(:label_file_lost) + not_exist_file.join(',').to_s
# @error = e # end
end # end
def detect_content_type(name) # end
content_type = Redmine::MimeType.of(name) # zipfile_name
content_type.to_s # #rescue Errno => e
end # # logger.error "[zipdown#zipping] ===> #{e}"
# # @error = e
def filename_to_real(name) # end
attach = Attachment.find_by_disk_filename(name) # def detect_content_type(name)
attach.filename # content_type = Redmine::MimeType.of(name)
end # content_type.to_s
end # end
#
# def filename_to_real(name)
# attach = Attachment.find_by_disk_filename(name)
# attach.filename
# end
# end

@ -23,7 +23,7 @@
(<span id="jours_count" class="c_red f_12"><%= @jours_count %></span>) (<span id="jours_count" class="c_red f_12"><%= @jours_count %></span>)
</li> </li>
<li> <li>
<%= link_to "作品打包下载", zipdown_assort_path(obj_class: @bid.class, obj_id: @bid), class: "tb_all" unless @bid.homeworks.empty? %> <%#= link_to "作品打包下载", zipdown_assort_path(obj_class: @bid.class, obj_id: @bid), class: "tb_all" unless @bid.homeworks.empty? %>
</li> </li>
</ul> </ul>
<% else %> <% else %>

@ -26,7 +26,7 @@
</span> </span>
</li> </li>
<li class="wdown"> <li class="wdown">
<%= link_to "(#{homework.attachments.count.to_s}个附件)", zipdown_download_user_homework_path(:homework => homework)%> <%#= link_to "(#{homework.attachments.count.to_s}个附件)", "javascript:"%>
</li> </li>
<li class="wscore"> <li class="wscore">
<% unless is_student_batch_homework %> <% unless is_student_batch_homework %>

@ -102,10 +102,10 @@ RedmineApp::Application.routes.draw do
mount SeemsRateable::Engine => '/rateable', :as => :rateable mount SeemsRateable::Engine => '/rateable', :as => :rateable
namespace :zipdown do # namespace :zipdown do
match 'assort' # match 'assort'
match 'download_user_homework', :as => :download_user_homework # match 'download_user_homework', :as => :download_user_homework
end # end
namespace :test do namespace :test do
match 'courselist' match 'courselist'
match 'zip' match 'zip'

@ -38,7 +38,7 @@ a:hover.tb_all{ background:#eaeaea; text-decoration:none;}
.pic_head a{ text-align:center; width:42px; overflow:hidden;text-overflow:ellipsis; white-space:nowrap;} .pic_head a{ text-align:center; width:42px; overflow:hidden;text-overflow:ellipsis; white-space:nowrap;}
.pic_head img{ border:1px solid #fff;} .pic_head img{ border:1px solid #fff;}
.pic_head img:hover{border:1px solid #15bccf;} .pic_head img:hover{border:1px solid #15bccf;}
.dis ul li.wname a{ width:260px; font-size:14px; color:#595959; padding:15px 0 0 0px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;} .dis ul li.wname a{ width:360px; font-size:14px; color:#595959; padding:15px 0 0 0px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;}
.dis ul li.wdown a{padding-top:22px; color:#3d7ec2; margin-right:35px;} .dis ul li.wdown a{padding-top:22px; color:#3d7ec2; margin-right:35px;}
.wscore{ padding-top:22px; color:#888888; width:96px;} .wscore{ padding-top:22px; color:#888888; width:96px;}
.dis ul li.wping{margin-left:12px; } .dis ul li.wping{margin-left:12px; }

Loading…
Cancel
Save