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.

attachments_controller.rb 31 kB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
10 years ago
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
11 years ago
​
​
​
​
​
​
​
​
​
10 years ago
​
​
10 years ago
​
​
10 years ago
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
10 years ago
​
​
​
​
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. #encoding: utf-8
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2013 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class AttachmentsController < ApplicationController
  19. layout "users_base"
  20. before_filter :verify_authenticity_token, only: [:uploa]
  21. before_filter :find_project, :only => [:show, :download, :thumbnail, :destroy, :delete_homework]#, :except => [:upload, :autocomplete]
  22. before_filter :file_readable, :read_authorize, :only => [:show, :thumbnail]#Modified by young
  23. before_filter :delete_authorize, :only => [:destroy]
  24. before_filter :authorize_global, :only => [:upload]
  25. before_filter :authorize_attachment_download1, :only => [:download]
  26. before_filter :has_login
  27. #before_filter :login_without_softapplication, only: [:download]
  28. accept_api_auth :show, :download, :upload
  29. require 'iconv'
  30. include AttachmentsHelper
  31. include ApplicationHelper
  32. def show
  33. respond_to do |format|
  34. format.html {
  35. if @attachment.is_diff?
  36. @diff = File.new(@attachment.diskfile, "rb").read
  37. @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
  38. @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
  39. # Save diff type as user preference
  40. if User.current.logged? && @diff_type != User.current.pref[:diff_type]
  41. User.current.pref[:diff_type] = @diff_type
  42. User.current.preference.save
  43. end
  44. render :action => 'diff'
  45. elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
  46. @content = File.new(@attachment.diskfile, "rb").read
  47. # 编码为非 UTF-8先进行间接转码
  48. # 部分unicode编码不直接支持转为 UTF-8
  49. # modify by nwb
  50. if @content.encoding.name != 'UTF-8'
  51. @content = @content.force_encoding('GBK')
  52. @content = @content.encode('UTF-8')
  53. end
  54. render :action => 'file'
  55. else
  56. download
  57. end
  58. }
  59. format.api
  60. end
  61. rescue Encoding::InvalidByteSequenceError => e
  62. render :action => 'file'
  63. end
  64. def pdf?(file)
  65. file.downcase.end_with?(".pdf")
  66. end
  67. def direct_download
  68. @attachment.increment_download
  69. file_type = detect_content_type(@attachment)
  70. send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
  71. :type => file_type,
  72. :disposition => 'attachment' #inline can open in browser
  73. end
  74. def direct_download_history
  75. @attachment_history = AttachmentHistory.find(params[:id])
  76. @attachment_history.increment_download
  77. send_file @attachment_history.diskfile_history, :filename => filename_for_content_disposition(@attachment_history.filename),
  78. :type => detect_content_type(@attachment_history),
  79. :disposition => 'attachment' #inline can open in browser
  80. end
  81. def download_history
  82. @attachment_history = AttachmentHistory.find(params[:id])
  83. candown = attachment_history_candown @attachment_history
  84. if candown || User.current.admin? || User.current.id == @attachment_history.author_id
  85. if stale?(:etag => @attachment_history.digest)
  86. if params[:preview] == 'true'
  87. convered_file = @attachment_history.diskfile_history
  88. #如果本身不是pdf文件,则先寻找是不是已转换化,如果没有则转化
  89. unless pdf?(convered_file)
  90. convered_file = File.join(Rails.root, "files", "convered_office", @attachment.disk_filename + ".pdf")
  91. unless File.exist?(convered_file)
  92. office = Trustie::Utils::Office.new(@attachment_history.diskfile)
  93. office.conver(convered_file)
  94. end
  95. end
  96. if File.exist?(convered_file) && pdf?(convered_file)
  97. send_file convered_file, :type => 'application/pdf; charset=utf-8', :disposition => 'inline'
  98. else
  99. direct_download_history
  100. end
  101. else
  102. # 记录用户行为
  103. record_user_actions(params[:id])
  104. # 直接下载历史版本
  105. direct_download_history
  106. end
  107. end
  108. else
  109. render_403 :message => :notice_not_authorized
  110. end
  111. rescue => e
  112. redirect_to "http://" + (Setting.host_name.to_s) +"/file_not_found.html"
  113. end
  114. def record_user_actions id
  115. if params[:action] == "download_history"
  116. UserActions.create(:action_id => id, :action_type => "AttachmentHistory", :user_id => User.current.id) unless id.nil?
  117. elsif params[:action] == "download"
  118. UserActions.create(:action_id => id, :action_type => "Attachment", :user_id => User.current.id) unless id.nil?
  119. end
  120. end
  121. def download
  122. # modify by nwb
  123. # 下载添加权限设置
  124. candown = attachment_candown @attachment
  125. if candown || User.current.admin? || User.current.id == @attachment.author_id
  126. if stale?(:etag => @attachment.digest)
  127. if params[:preview] == 'true'
  128. convered_file = @attachment.diskfile
  129. #如果本身不是pdf文件,则先寻找是不是已转换化,如果没有则转化
  130. unless pdf?(convered_file)
  131. convered_file = File.join(Rails.root, "files", "convered_office", @attachment.disk_filename + ".pdf")
  132. unless File.exist?(convered_file)
  133. office = Trustie::Utils::Office.new(@attachment.diskfile)
  134. office.conver(convered_file)
  135. end
  136. end
  137. if File.exist?(convered_file) && pdf?(convered_file)
  138. send_file convered_file, :type => 'application/pdf; charset=utf-8', :disposition => 'inline'
  139. else
  140. direct_download
  141. end
  142. else
  143. # 记录用户行为
  144. record_user_actions(params[:id])
  145. direct_download
  146. end
  147. end
  148. else
  149. render_403 :message => :notice_not_authorized
  150. end
  151. rescue => e
  152. redirect_to "http://" + (Setting.host_name.to_s) +"/file_not_found.html"
  153. end
  154. #更新资源文件类型
  155. def updateType
  156. @attachment = Attachment.find(params[:attachmentid])
  157. if @attachment != nil
  158. @attachment.attachtype = params[:newtype]
  159. @attachment.save
  160. render :text =>'success'
  161. else
  162. render :text=>'error'
  163. end
  164. end
  165. # 更新文件密级
  166. def updateFileDense
  167. @attachment = Attachment.find(params[:attachmentid])
  168. if @attachment != nil
  169. filedense = params[:newtype].to_s
  170. # d = Iconv.conv("unicodebig","utf-8",filedense)
  171. if filedense == "%E5%85%AC%E5%BC%80" #l(:field_is_public)
  172. @attachment.is_public = 1
  173. else
  174. @attachment.is_public = 0
  175. end
  176. @attachment.save
  177. @newfiledense = filedense
  178. end
  179. respond_to do |format|
  180. format.js
  181. end
  182. end
  183. def update_file_dense
  184. @attachment = Attachment.find(params[:attachmentid])
  185. if @attachment != nil
  186. filedense = params[:newtype].to_s
  187. if filedense == "1"
  188. @attachment.is_public = 1
  189. else
  190. @attachment.is_public = 0
  191. end
  192. @attachment.save
  193. @newfiledense = filedense
  194. end
  195. if @attachment.container_type == "Project" || @attachment.container_type == "Course"
  196. tip_attachment_update
  197. end
  198. respond_to do |format|
  199. format.js
  200. end
  201. end
  202. def tip_attachment_update
  203. if params[:course_id]
  204. @tip_all_attachments = Attachment.where(:container_type => "Course", :container_id => params[:course_id])
  205. @tip_all_public_attachments = Attachment.where(:container_type => "Course", :container_id => params[:course_id], :is_public => 1)
  206. @tip_all_private_attachments = Attachment.where(:container_type => "Course", :container_id => params[:course_id], :is_public => 0)
  207. @course = Course.find(params[:course_id])
  208. elsif params[:project_id]
  209. @tip_all_attachments = Attachment.where(:container_type => "Project", :container_id => params[:project_id])
  210. @tip_all_public_attachments = Attachment.where(:container_type => "Project", :container_id => params[:project_id], :is_public => 1)
  211. @tip_all_private_attachments = Attachment.where(:container_type => "Project", :container_id => params[:project_id], :is_public => 0)
  212. @project = Project.find(params[:project_id])
  213. end
  214. @tag_name = params[:tag_name]
  215. @other = params[:other]
  216. unless @tag_name.blank?
  217. if @other
  218. if @project
  219. @tip_all_attachments = @tip_all_attachments.select{|attachment| !attachment.tag_list.include?('软件版本') && !attachment.tag_list.include?('文档') && !attachment.tag_list.include?('代码') && !attachment.tag_list.include?('媒体') && !attachment.tag_list.include?('论文') }
  220. @tip_all_public_attachments = @tip_all_public_attachments.select{|attachment| !attachment.tag_list.include?('软件版本') && !attachment.tag_list.include?('文档') && !attachment.tag_list.include?('代码') && !attachment.tag_list.include?('媒体') && !attachment.tag_list.include?('论文') }
  221. @tip_all_private_attachments = @tip_all_private_attachments.select{|attachment| !attachment.tag_list.include?('软件版本') && !attachment.tag_list.include?('文档') && !attachment.tag_list.include?('代码') && !attachment.tag_list.include?('媒体') && !attachment.tag_list.include?('论文') }
  222. elsif @course
  223. @tip_all_attachments = @tip_all_attachments.select{|attachment| !attachment.tag_list.include?('课件') && !attachment.tag_list.include?('软件') && !attachment.tag_list.include?('媒体') && !attachment.tag_list.include?('代码') && !attachment.tag_list.include?('论文') }
  224. @tip_all_public_attachments = @tip_all_public_attachments.select{|attachment| !attachment.tag_list.include?('课件') && !attachment.tag_list.include?('软件') && !attachment.tag_list.include?('媒体') && !attachment.tag_list.include?('代码') && !attachment.tag_list.include?('论文') }
  225. @tip_all_private_attachments = @tip_all_private_attachments.select{|attachment| !attachment.tag_list.include?('课件') && !attachment.tag_list.include?('软件') && !attachment.tag_list.include?('媒体') && !attachment.tag_list.include?('代码') && !attachment.tag_list.include?('论文') }
  226. end
  227. else
  228. @tip_all_attachments = @tip_all_attachments.select{|attachment| attachment.tag_list.include?(@tag_name)}
  229. @tip_all_public_attachments = @tip_all_public_attachments.select{|attachment| attachment.tag_list.include?(@tag_name)}
  230. @tip_all_private_attachments = @tip_all_private_attachments.select{|attachment| attachment.tag_list.include?(@tag_name)}
  231. end
  232. end
  233. @tip_all_attachments = @tip_all_attachments.count
  234. @tip_all_public_attachments = @tip_all_public_attachments.count
  235. @tip_all_private_attachments = @tip_all_private_attachments.count
  236. end
  237. def thumbnail
  238. if @attachment.thumbnailable? && thumbnail = @attachment.thumbnail(:size => params[:size])
  239. if stale?(:etag => thumbnail)
  240. send_file thumbnail,
  241. :filename => filename_for_content_disposition(@attachment.filename),
  242. :type => detect_content_type(@attachment),
  243. :disposition => 'inline'
  244. end
  245. else
  246. # No thumbnail for the attachment or thumbnail could not be created
  247. render :nothing => true, :status => 404
  248. end
  249. end
  250. def upload
  251. # Make sure that API users get used to set this content type
  252. # as it won't trigger Rails' automatic parsing of the request body for parameters
  253. unless request.content_type == 'application/octet-stream'
  254. render :nothing => true, :status => 406
  255. return
  256. end
  257. @attachment = Attachment.new(:file => request.raw_post)
  258. @attachment.author = User.current
  259. if !params[:project].nil?
  260. @attachment.container_type = 'Project'
  261. @attachment.container_id = params[:project].split("?")[0]
  262. end
  263. @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
  264. saved = @attachment.save
  265. respond_to do |format|
  266. format.js
  267. format.api {
  268. if saved
  269. render :action => 'upload', :status => :created
  270. else
  271. render_validation_errors(@attachment)
  272. end
  273. }
  274. end
  275. end
  276. def upload_attachment_version
  277. @flag = false
  278. Attachment.transaction do
  279. @old_attachment = Attachment.find params[:old_attachment_id]
  280. #取出当前上传的文件
  281. @attachment = Attachment.find(params[:attachments ].first[1][:attachment_id])
  282. #将需要修改的记录保存到历史记录
  283. @history = AttachmentHistory.new
  284. @history.attributes = @old_attachment.attributes.dup.except("id")
  285. @history.attachment_id = params[:old_attachment_id]
  286. #需要更新版本号,需要拿到原来该文件最大的历史版本号
  287. @old_history = @old_attachment.attachment_histories.reorder('version desc').first
  288. @history.version = @old_history.nil? ? 1 : @old_history.version + 1
  289. @history.save #历史记录保存完毕
  290. #将最新保存的记录 数据替换到 需要修改的文件记录
  291. @old_attachment.attributes = @attachment.attributes.dup.except("id","container_id","container_type","is_public","downloads", "quotes")
  292. # 如果附件描述被修改,则保存附件
  293. unless params[:description] == @attachment.description
  294. @old_attachment.description = params[:description]
  295. end
  296. @old_attachment.save
  297. #删除当前记录
  298. @attachment.delete
  299. @flag = true
  300. end
  301. # tip_attachment_update
  302. respond_to do |format|
  303. format.js
  304. end
  305. end
  306. # prams[:type] => history 历史版本
  307. def destroy
  308. if params[:type] == "history"
  309. begin
  310. AttachmentHistory.find(params[:history_id]).destroy
  311. @attachment = Attachment.find(params[:id])
  312. @is_history = true
  313. @is_history_destroy = false
  314. @attachment_histories = @attachment.attachment_histories
  315. rescue Exception => e
  316. puts e
  317. end
  318. elsif params[:type] == "history_delete"
  319. begin
  320. AttachmentHistory.find(params[:history_id]).destroy
  321. @attachment = Attachment.find(params[:id])
  322. @is_history_delete = true
  323. @is_history_destroy = false
  324. @attachment_histories = @attachment.attachment_histories
  325. @attachment_histories_count = @attachment_histories.count
  326. rescue Exception => e
  327. puts e
  328. end
  329. else
  330. @history = params[:history]
  331. if @attachment.container.respond_to?(:init_journal)
  332. @attachment.container.init_journal(User.current)
  333. end
  334. if @attachment.container
  335. if @attachment.container_type == "Issue"
  336. @attachment.destroy
  337. else
  338. @attachment.container.attachments.delete(@attachment)
  339. end
  340. else
  341. @attachment.destroy
  342. end
  343. end
  344. respond_to do |format|
  345. if !@attachment.container.nil? &&
  346. (@attachment.container.is_a?(Course) || ((@attachment.container.has_attribute?(:course) || @attachment.container.has_attribute?(:course_id) ) &&
  347. @attachment.container.course ) || ((@attachment.container.has_attribute?(:board) || @attachment.container.has_attribute?(:board_id)) &&
  348. @attachment.container.board && @attachment.container.board.course ) || @attachment.container.is_a?(StudentWorksScore) || @attachment.container.is_a?(HomeworkCommon) ||
  349. @attachment.container.is_a?(StudentWork))
  350. if @attachment.container.is_a?(News)
  351. format.html { redirect_to_referer_or news_path(@attachment.container) }
  352. elsif @attachment.container.is_a?(StudentWorksScore)
  353. @is_destroy = true unless params[:attachment_id] #根据ID删除页面对应的数据,js刷新页面
  354. format.js
  355. elsif @attachment.container.is_a?(HomeworkCommon)
  356. @is_destroy = true unless params[:attachment_id] #根据ID删除页面对应的数据,js刷新页面
  357. format.js
  358. elsif @attachment.container.is_a?(StudentWork)
  359. @is_destroy = true unless params[:attachment_id] #根据ID删除页面对应的数据,js刷新页面
  360. format.js
  361. elsif @attachment.container.is_a?(Message)
  362. format.html { redirect_to_referer_or new_board_message_path(@attachment.container) }
  363. elsif @attachment.container.is_a?(BlogComment)
  364. format.html { redirect_to_referer_or user_blog_blog_comment_path(:user_id=>@attachment.container.author.id,:blog_id=>@attachment.container.blog_id,:id=>@attachment.container.id)}
  365. elsif @course.nil?
  366. format.html { redirect_to_referer_or forum_memo_path(@attachment.container.forum, @attachment.container) }
  367. else
  368. format.html { redirect_to_referer_or course_path(@course) }
  369. end
  370. elsif !@attachment.container.nil? && @attachment.container.is_a?(Softapplication)
  371. format.html { redirect_to_referer_or softapplications_path(@attachment.container) }
  372. elsif !@attachment.container.nil? && @attachment.container.is_a?(Bid)
  373. format.html { redirect_to_referer_or respond_path(@attachment.container) }
  374. elsif !@attachment.container.nil? && @attachment.container.is_a?(PhoneAppVersion)
  375. format.html { redirect_to_referer_or mobile_version_path }
  376. elsif !@attachment.container.nil? && @attachment.container.is_a?(OrgSubfield)
  377. format.html {redirect_to_referer_or org_subfield_files_path(@attachment.container)}
  378. elsif !@attachment.container.nil? && @attachment.container.is_a?(OrgDocumentComment)
  379. format.html {redirect_to_referer_or org_document_comment_path(@attachment.container)}
  380. else
  381. if @project.nil?
  382. format.html { redirect_to_referer_or forum_memo_path(@attachment.container.forum, @attachment.container) }
  383. else
  384. format.html { redirect_to_referer_or project_path(@project) }
  385. end
  386. end
  387. format.js
  388. end
  389. end
  390. def delete_homework
  391. @bid = @attachment.container.bid
  392. # Make sure association callbacks are called
  393. container = @attachment.container
  394. @attachment.container.attachments.delete(@attachment)
  395. #if container.attachments.empty?
  396. #container.delete
  397. #end
  398. respond_to do |format|
  399. format.html { redirect_to_referer_or respond_path(@bid) }
  400. format.js
  401. end
  402. end
  403. #删除竞赛作品的附件
  404. def delete_softapplications
  405. @attachment = Attachment.find params[:id]
  406. @softapplication = @attachment.container if @attachment!=nil
  407. @attachment.container.attachments.delete(@attachment) if @attachment!=nil
  408. respond_to do |format|
  409. format.html { redirect_to_referer_or edit_softapplication_path(@softapplication) }
  410. #format.js
  411. end
  412. end
  413. def autocomplete
  414. # modify by nwb
  415. if params[:project_id]
  416. @project = Project.find_by_id(params[:project_id])
  417. elsif params[:course_id]
  418. @course = Course.find_by_id(params[:course_id])
  419. end
  420. respond_to do |format|
  421. format.js
  422. end
  423. end
  424. def add_exist_file_to_project
  425. classname = params[:class_name]
  426. class_id = params[:class_id]
  427. attachments = params[:attachment][:attach]
  428. obj = Object.const_get(classname).find_by_id(class_id)
  429. attachments.collect do |attach_id|
  430. ori = Attachment.find_by_id(attach_id)
  431. next if ori.blank?
  432. attach_copied_obj = ori.copy
  433. attach_copied_obj.tag_list.add(ori.tag_list) # tag关联
  434. attach_copied_obj.container = obj
  435. attach_copied_obj.created_on = Time.now
  436. attach_copied_obj.author_id = User.current.id
  437. if attach_copied_obj.attachtype == nil
  438. attach_copied_obj.attachtype = 1
  439. end
  440. @obj = obj
  441. @save_flag = attach_copied_obj.save
  442. @save_message = attach_copied_obj.errors.full_messages
  443. end
  444. respond_to do |format|
  445. format.js
  446. end
  447. rescue NoMethodError
  448. @save_flag = false
  449. @save_message = [] << l(:error_attachment_empty)
  450. respond_to do |format|
  451. format.js
  452. end
  453. end
  454. def add_exist_file_to_course
  455. class_id = params[:class_id]
  456. attachments = params[:attachment][:attach]
  457. obj = Course.find_by_id(class_id)
  458. attachments.collect do |attach_id|
  459. ori = Attachment.find_by_id(attach_id)
  460. next if ori.blank?
  461. attach_copied_obj = ori.copy
  462. attach_copied_obj.tag_list.add(ori.tag_list) # tag关联
  463. attach_copied_obj.container = obj
  464. attach_copied_obj.created_on = Time.now
  465. attach_copied_obj.author_id = User.current.id
  466. if attach_copied_obj.attachtype == nil
  467. attach_copied_obj.attachtype = 4
  468. end
  469. @obj = obj
  470. @save_flag = attach_copied_obj.save
  471. @save_message = attach_copied_obj.errors.full_messages
  472. end
  473. respond_to do |format|
  474. format.js
  475. end
  476. rescue NoMethodError
  477. @save_flag = false
  478. @save_message = [] << l(:error_attachment_empty)
  479. respond_to do |format|
  480. format.js
  481. end
  482. end
  483. def add_exist_file_to_projects
  484. file = Attachment.find(params[:file_id])
  485. projects = params[:projects][:project]
  486. @message = ""
  487. projects.each do |project|
  488. c = Project.find(project);
  489. if project_contains_attachment?(c,file)
  490. if @message && @message == ""
  491. @message += l(:label_project_prompt) + c.name + l(:label_contain_resource) + file.filename + l(:label_quote_resource_failed)
  492. next
  493. else
  494. @message += "<br/>" + l(:label_project_prompt) + c.name + l(:label_contain_resource) + file.filename + l(:label_quote_resource_failed)
  495. next
  496. end
  497. end
  498. attach_copied_obj = file.copy
  499. attach_copied_obj.tag_list.add(file.tag_list) # tag关联
  500. attach_copied_obj.container = c
  501. attach_copied_obj.created_on = Time.now
  502. attach_copied_obj.author_id = User.current.id
  503. attach_copied_obj.copy_from = file.copy_from.nil? ? file.id : file.copy_from
  504. if attach_copied_obj.attachtype == nil
  505. attach_copied_obj.attachtype = 4
  506. end
  507. @obj = c
  508. @save_flag = attach_copied_obj.save
  509. @save_message = attach_copied_obj.errors.full_messages
  510. update_quotes attach_copied_obj
  511. end
  512. respond_to do |format|
  513. format.js
  514. end
  515. rescue NoMethodError
  516. @save_flag = false
  517. @save_message = [] << l(:label_project_empty_select)
  518. respond_to do |format|
  519. format.js
  520. end
  521. end
  522. def add_exist_file_to_courses
  523. file = Attachment.find(params[:file_id])
  524. courses = params[:courses][:course]
  525. @message = ""
  526. courses.each do |course|
  527. c = Course.find(course);
  528. if course_contains_attachment?(c,file)
  529. if @message && @message == ""
  530. @message += l(:label_course_prompt) + c.name + l(:label_contain_resource) + file.filename + l(:label_quote_resource_failed)
  531. next
  532. else
  533. @message += "<br/>" + l(:label_course_prompt) + c.name + l(:label_contain_resource) + file.filename + l(:label_quote_resource_failed)
  534. next
  535. end
  536. end
  537. attach_copied_obj = file.copy
  538. attach_copied_obj.tag_list.add(file.tag_list) # tag关联
  539. attach_copied_obj.container = c
  540. attach_copied_obj.created_on = Time.now
  541. attach_copied_obj.author_id = User.current.id
  542. attach_copied_obj.copy_from = file.copy_from.nil? ? file.id : file.copy_from
  543. if attach_copied_obj.attachtype == nil
  544. attach_copied_obj.attachtype = 4
  545. end
  546. @obj = c
  547. @save_flag = attach_copied_obj.save
  548. @save_message = attach_copied_obj.errors.full_messages
  549. update_quotes attach_copied_obj
  550. end
  551. respond_to do |format|
  552. format.js
  553. end
  554. rescue NoMethodError
  555. @save_flag = false
  556. @save_message = [] << l(:label_course_empty_select)
  557. respond_to do |format|
  558. format.js
  559. end
  560. end
  561. def add_exist_file_to_org_subfield
  562. file = Attachment.find(params[:file_id])
  563. org_subfields = params[:org_subfields][:org_subfield]
  564. @message = ""
  565. org_subfields.each do |org_subfield|
  566. s = OrgSubfield.find(org_subfield)
  567. if s.attachments.include?file
  568. if @message && @message == ""
  569. @message += l(:label_resource_subfield_prompt) + c.name + l(:label_contain_resource) + file.filename + l(:label_quote_resource_failed)
  570. next
  571. else
  572. @message += "<br/>" + l(:label_resource_subfield_prompt) + c.name + l(:label_contain_resource) + file.filename + l(:label_quote_resource_failed)
  573. next
  574. end
  575. end
  576. attach_copied_obj = file.copy
  577. attach_copied_obj.tag_list.add(file.tag_list) # tag关联
  578. attach_copied_obj.container = s
  579. attach_copied_obj.created_on = Time.now
  580. attach_copied_obj.author_id = User.current.id
  581. attach_copied_obj.copy_from = file.copy_from.nil? ? file.id : file.copy_from
  582. if attach_copied_obj.attachtype == nil
  583. attach_copied_obj.attachtype = 4
  584. end
  585. @obj = s
  586. @save_flag = attach_copied_obj.save
  587. @save_message = attach_copied_obj.errors.full_messages
  588. update_quotes attach_copied_obj
  589. end
  590. respond_to do |format|
  591. format.js
  592. end
  593. rescue NoMethodError
  594. @save_flag = false
  595. @save_message = [] << l(:label_resource_subfield_empty_select)
  596. respond_to do |format|
  597. format.js
  598. end
  599. end
  600. def update_quotes attachment
  601. if attachment.copy_from
  602. attachments = Attachment.find_by_sql("select * from attachments where copy_from = #{attachment.copy_from} or id = #{attachment.copy_from}")
  603. else
  604. attachments = Attachment.find_by_sql("select * from attachments where copy_from = #{attachment.id} or id = #{attachment.copy_from}")
  605. end
  606. attachment.quotes = get_qute_number attachment
  607. attachment.save
  608. attachments.each do |att|
  609. att.quotes = attachment.quotes
  610. att.save
  611. end
  612. end
  613. #找到文件的所有的历史版本
  614. def attachment_versions
  615. @history = params[:history]
  616. @attachment = Attachment.find(params[:id])
  617. @attachment_histories = @attachment.attachment_histories
  618. respond_to do |format|
  619. format.js
  620. end
  621. end
  622. def attachment_versions_delete
  623. @attachment = Attachment.find(params[:id])
  624. @attachment_histories = @attachment.attachment_histories
  625. @attachment_histories_count = @attachment_histories.count
  626. respond_to do |format|
  627. format.js
  628. end
  629. end
  630. #找到文件的所有的历史版本及当前版本
  631. def attachment_history_download
  632. @attachment = Attachment.find(params[:id])
  633. @attachment_histories = @attachment.attachment_histories
  634. respond_to do |format|
  635. format.js
  636. end
  637. end
  638. private
  639. def find_project
  640. @attachment = Attachment.find(params[:id])
  641. # Show 404 if the filename in the url is wrong
  642. # modify by nwb
  643. raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
  644. if @attachment.container_type == 'Course'
  645. @course = @attachment.course
  646. elsif !@attachment.container.nil? && (@attachment.container.has_attribute?(:course) || @attachment.container.has_attribute?(:course_id)) && @attachment.container.course
  647. @course = @attachment.container.course
  648. elsif !@attachment.container.nil? && ((@attachment.container.has_attribute?(:board) || @attachment.container.has_attribute?(:board_id)) && @attachment.container.board &&
  649. @attachment.container.board.course)
  650. @course = @attachment.container.board.course
  651. else
  652. unless @attachment.container_type == 'Syllabus' || @attachment.container_type == 'Bid' || @attachment.container_type == 'Organization' || @attachment.container_type == 'HomeworkAttach' || @attachment.container_type == 'Memo' || @attachment.container_type == 'Softapplication' || @attachment.container_type == 'PhoneAppVersion' || @attachment.container_type == 'StudentWorksScore'|| @attachment.container_type == 'StudentWork'
  653. @project = @attachment.project
  654. end
  655. end
  656. rescue ActiveRecord::RecordNotFound
  657. render_404
  658. end
  659. # Checks that the file exists and is readable
  660. def file_readable
  661. if @attachment.readable?
  662. true
  663. else
  664. logger.error "Cannot send attachment, #{@attachment.diskfile} does not exist or is unreadable."
  665. render_404
  666. end
  667. end
  668. def read_authorize
  669. if @attachment.container_type == "HomeworkAttach" || @attachment.container_type == 'Bid'
  670. true
  671. #User.current.allowed_to?(:view_homework_attaches, @attachment.project) ? true : deny_access
  672. else
  673. @attachment.visible? ? true : deny_access
  674. end
  675. end
  676. def delete_authorize
  677. @attachment.deletable? ? true : deny_access
  678. end
  679. def detect_content_type(attachment)
  680. content_type = attachment.content_type
  681. if content_type.blank?
  682. content_type = Redmine::MimeType.of(attachment.filename)
  683. end
  684. content_type.to_s
  685. end
  686. def login_without_softapplication
  687. referer = request.headers['Referer']
  688. require_login unless referer =~ /softapplication/ || @attachment.container_type == "Memo"
  689. end
  690. def renderTag
  691. @attachmentNew = Attachment.find(params[:attchmentId])
  692. respond_to do |format|
  693. format.js
  694. end
  695. end
  696. def has_login
  697. unless (@attachment && @attachment.container_type == "Organization").nil?
  698. unless (@attachment && @attachment.container_type == "PhoneAppVersion").nil?
  699. render_403 if (!User.current.logged? && !(params[:type] && params[:type] == "wechat")) && !(@attachment.container_type == 'OrgSubfield' && @attachment.container.organization.allow_guest_download) && !(@attachment.container_type == 'OrgDocumentComment' && @attachment.container.organization.allow_guest_download)
  700. end
  701. end
  702. end
  703. end

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