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 26 kB

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

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