You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

zipdown_controller.rb 5.1 kB

​
12 years ago
​
​
​
​
​
​
​
​
​
​
12 years ago
​
​
​
​
​
​
​
​
​
​
​
12 years ago
​
​
12 years ago
​
​
​
​
​
12 years ago
​
​
​
​
​
​
​
​
​
​
12 years ago
​
12 years ago
​
​
​
​
​
12 years ago
12 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 years ago
​
​
12 years ago
12 years ago
​
​
12 years ago
​
​
12 years ago
​
​
12 years ago
​
​
​
​
​
​
​
12 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. class ZipdownController < ApplicationController
  2. #查找项目(课程)
  3. before_filter :find_project_by_bid_id, :only => [:assort]
  4. #检查权限
  5. #勿删 before_filter :authorize, :only => [:assort,:download_user_homework]
  6. SAVE_FOLDER = "#{Rails.root}/files"
  7. OUTPUT_FOLDER = "#{Rails.root}/tmp/archiveZip"
  8. #通过作业Id找到项目(课程)
  9. def find_project_by_bid_id
  10. obj_class = params[:obj_class]
  11. obj_id = params[:obj_id]
  12. obj = obj_class.constantize.find(obj_id)
  13. case obj.class.to_s.to_sym
  14. when :Bid
  15. @project = obj.courses[0]
  16. end
  17. end
  18. def assort
  19. obj_class = params[:obj_class]
  20. obj_id = params[:obj_id]
  21. obj = obj_class.constantize.find(obj_id)
  22. zipfile = nil
  23. case obj.class.to_s.to_sym
  24. when :Bid
  25. zipfile = zip_bid obj
  26. else
  27. logger.error "[ZipDown#assort] ===> #{obj.class.to_s.to_sym} unKown !!"
  28. end
  29. send_file zipfile, :filename => obj.name, :type => detect_content_type(zipfile) if zipfile
  30. #rescue NameError, ActiveRecord::RecordNotFound => e
  31. #logger.error "[ZipDown] ===> #{e}"
  32. #@error = e
  33. end
  34. #下载某一学生的作业的所有文件
  35. def download_user_homework
  36. homework = HomeworkAttach.find params[:homework]
  37. if homework != nil && (User.current.admin? || User.current.member_of_course?(homework.bid.courses.first))
  38. zipfile = zip_homework_by_user homework
  39. send_file zipfile, :filename => homework.name, :type => detect_content_type(zipfile) if zipfile
  40. else
  41. render_403 :message => :notice_not_authorized
  42. end
  43. end
  44. private
  45. def zip_user_bid(bid,user_id)
  46. # Todo: User Access Controll
  47. homeattaches = bid.homeworks.where("user_id = ?",user_id)
  48. # 得到每一个人所有文件打包的zip文件
  49. # 并将每一个人的zip打包为一个并返回路径
  50. user_zip_paths = homeattaches.map do |homeattach|
  51. zip_homework_by_user homeattach
  52. end
  53. #zipping "#{Time.now.to_i}_#{bid.name}.zip", user_zip_paths, OUTPUT_FOLDER
  54. user_zip_paths
  55. end
  56. def zip_bid(bid)
  57. # Todo: User Access Controll
  58. homeattaches = bid.homeworks
  59. # 得到每一个人所有文件打包的zip文件
  60. # 并将每一个人的zip打包为一个并返回路径
  61. user_zip_paths = homeattaches.map do |homeattach|
  62. zip_homework_by_user homeattach
  63. end
  64. zipping "#{Time.now.to_i}_#{bid.name}.zip", user_zip_paths, OUTPUT_FOLDER
  65. #@paths = homeworks_attach_path
  66. #zipfile = ziping homeworks_attach_path
  67. #send_file zipfile, :filename => bid.name,
  68. # :type => detect_content_type(zipfile)
  69. #rescue Errno::ENOENT => e
  70. # logger.error "[Errno::ENOENT] ===> #{e}"
  71. # @error = e
  72. end
  73. def zip_homework_by_user(homeattach)
  74. homeworks_attach_path = []
  75. # 需要将所有homework.attachments遍历加入zip
  76. # 并且返回zip路径
  77. user_attaches_paths = homeattach.attachments.each do |attach|
  78. #length = attach.storage_path.length
  79. homeworks_attach_path << attach.diskfile#.to_s.slice((length+1)..-1)
  80. end
  81. zipping "#{homeattach.user.name.to_s}.zip", homeworks_attach_path, OUTPUT_FOLDER, true
  82. #user_attaches_paths
  83. end
  84. def zipping(zip_name_refer, files_paths, output_path, is_attachment=false)
  85. # 输入待打包的文件列表,已经打包文件定位到ouput_path
  86. ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE')
  87. input_filename = files_paths
  88. rename_zipfile = zip_name_refer ||= "archive_#{Time.now.to_i}.zip"
  89. zipfile_name = "#{output_path}/#{rename_zipfile}"
  90. Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name))
  91. Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
  92. input_filename.each do |filename|
  93. rename_file = Time.now.to_i.to_s+ ic.iconv( (File.basename(filename)) ).to_s
  94. rename_file = ic.iconv( filename_to_real( File.basename(filename))).to_s if is_attachment
  95. zipfile.add(rename_file, filename)
  96. end
  97. zipfile.get_output_stream('ReadMe') do |os|
  98. os.write 'Homeworks'
  99. end
  100. end
  101. zipfile_name
  102. rescue Errno => e
  103. logger.error "[zipdown#zipping] ===> #{e}"
  104. @error = e
  105. end
  106. #def ziping files_path
  107. # ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE')
  108. # folder = SaveFolder
  109. # input_filename = files_path
  110. # zipfile_name = "#{OutputFolder}/archive_#{Time.now.to_i}.zip"
  111. #
  112. # Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name))
  113. # Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
  114. # input_filename.each do |filename|
  115. # zipfile.add(ic.iconv(filename_to_real(File.basename(filename))), folder + '/' + filename)
  116. # end
  117. # zipfile.get_output_stream("ReadMe") { |os|
  118. # os.write "Homeworks"
  119. # }
  120. # end
  121. # zipfile_name
  122. #rescue Errno => e
  123. # logger.error "[zipdown#zipping] ===> #{e}"
  124. # @error = e
  125. #end
  126. def detect_content_type(name)
  127. content_type = Redmine::MimeType.of(name)
  128. content_type.to_s
  129. end
  130. def filename_to_real(name)
  131. attach = Attachment.find_by_disk_filename(name)
  132. attach.filename
  133. end
  134. end

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...