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

13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
12 years ago
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
12 years ago
12 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
13 years ago
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
13 years ago
​
​
​
​
​
​
​
​
​
​
​
​
12 years ago
​
12 years ago
12 years ago
12 years ago
​
​
​
​
​
​
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. before_filter :find_project, :only => [:show, :download, :thumbnail, :destroy, :delete_homework]#, :except => [:upload, :autocomplete]
  19. before_filter :file_readable, :read_authorize, :only => [:show, :thumbnail]#Modified by young
  20. before_filter :delete_authorize, :only => :destroy
  21. before_filter :authorize_global, :only => :upload
  22. before_filter :login_without_softapplication, only: [:download]
  23. accept_api_auth :show, :download, :upload
  24. def show
  25. respond_to do |format|
  26. format.html {
  27. if @attachment.is_diff?
  28. @diff = File.new(@attachment.diskfile, "rb").read
  29. @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
  30. @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
  31. # Save diff type as user preference
  32. if User.current.logged? && @diff_type != User.current.pref[:diff_type]
  33. User.current.pref[:diff_type] = @diff_type
  34. User.current.preference.save
  35. end
  36. render :action => 'diff'
  37. elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
  38. @content = File.new(@attachment.diskfile, "rb").read
  39. render :action => 'file'
  40. else
  41. download
  42. end
  43. }
  44. format.api
  45. end
  46. end
  47. def download
  48. if true || @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project)
  49. @attachment.increment_download
  50. end
  51. if stale?(:etag => @attachment.digest)
  52. # images are sent inline
  53. send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
  54. :type => detect_content_type(@attachment),
  55. :disposition => (@attachment.image? ? 'inline' : 'attachment')
  56. end
  57. end
  58. #更新资源文件类型
  59. def updateType
  60. @attachment = Attachment.find(params[:attachmentid])
  61. if @attachment != nil
  62. @attachment.attachtype = params[:newtype]
  63. @attachment.save
  64. render :text =>'success'
  65. else
  66. render :text=>'error'
  67. end
  68. end
  69. def thumbnail
  70. if @attachment.thumbnailable? && thumbnail = @attachment.thumbnail(:size => params[:size])
  71. if stale?(:etag => thumbnail)
  72. send_file thumbnail,
  73. :filename => filename_for_content_disposition(@attachment.filename),
  74. :type => detect_content_type(@attachment),
  75. :disposition => 'inline'
  76. end
  77. else
  78. # No thumbnail for the attachment or thumbnail could not be created
  79. render :nothing => true, :status => 404
  80. end
  81. end
  82. def upload
  83. # Make sure that API users get used to set this content type
  84. # as it won't trigger Rails' automatic parsing of the request body for parameters
  85. unless request.content_type == 'application/octet-stream'
  86. render :nothing => true, :status => 406
  87. return
  88. end
  89. @attachment = Attachment.new(:file => request.raw_post)
  90. @attachment.author = User.current
  91. @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
  92. saved = @attachment.save
  93. respond_to do |format|
  94. format.js
  95. format.api {
  96. if saved
  97. render :action => 'upload', :status => :created
  98. else
  99. render_validation_errors(@attachment)
  100. end
  101. }
  102. end
  103. end
  104. def destroy
  105. if @attachment.container.respond_to?(:init_journal)
  106. @attachment.container.init_journal(User.current)
  107. end
  108. if @attachment.container
  109. # Make sure association callbacks are called
  110. @attachment.container.attachments.delete(@attachment)
  111. else
  112. @attachment.destroy
  113. end
  114. respond_to do |format|
  115. # modify by nwb
  116. if @attachment.container_type == 'Course'
  117. if @course.nil?
  118. format.html { redirect_to_referer_or forum_memo_path(@attachment.container.forum, @attachment.container) }
  119. else
  120. format.html { redirect_to_referer_or course_path(@course) }
  121. end
  122. else
  123. if @project.nil?
  124. format.html { redirect_to_referer_or forum_memo_path(@attachment.container.forum, @attachment.container) }
  125. else
  126. format.html { redirect_to_referer_or project_path(@project) }
  127. end
  128. end
  129. format.js
  130. end
  131. end
  132. def delete_homework
  133. @bid = @attachment.container.bid
  134. # Make sure association callbacks are called
  135. container = @attachment.container
  136. @attachment.container.attachments.delete(@attachment)
  137. #if container.attachments.empty?
  138. #container.delete
  139. #end
  140. respond_to do |format|
  141. format.html { redirect_to_referer_or respond_path(@bid) }
  142. format.js
  143. end
  144. end
  145. def autocomplete
  146. @project = Project.find_by_id(params[:project_id])
  147. respond_to do |format|
  148. format.js
  149. end
  150. end
  151. def add_exist_file_to_project
  152. classname = params[:class_name]
  153. class_id = params[:class_id]
  154. attachments = params[:attachment][:attach]
  155. obj = Object.const_get(classname).find_by_id(class_id)
  156. attachments.collect do |attach_id|
  157. ori = Attachment.find_by_id(attach_id)
  158. next if ori.blank?
  159. attach_copied_obj = ori.copy
  160. attach_copied_obj.tag_list.add(ori.tag_list) # tag关联
  161. attach_copied_obj.container = obj
  162. attach_copied_obj.created_on = Time.now
  163. attach_copied_obj.author_id = User.current.id
  164. @obj = obj
  165. @save_flag = attach_copied_obj.save
  166. @save_message = attach_copied_obj.errors.full_messages
  167. end
  168. respond_to do |format|
  169. format.js
  170. end
  171. rescue NoMethodError
  172. @save_flag = false
  173. @save_message = [] << l(:error_attachment_empty)
  174. respond_to do |format|
  175. format.js
  176. end
  177. end
  178. private
  179. def find_project
  180. @attachment = Attachment.find(params[:id])
  181. # Show 404 if the filename in the url is wrong
  182. # modify by nwb
  183. raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
  184. if @attachment.container_type == 'Course'
  185. @course = @attachment.course
  186. else
  187. unless @attachment.container_type == 'Bid' || @attachment.container_type == 'HomeworkAttach' || @attachment.container_type == 'Memo' || @attachment.container_type == 'Softapplication'
  188. @project = @attachment.project
  189. end
  190. end
  191. rescue ActiveRecord::RecordNotFound
  192. render_404
  193. end
  194. # Checks that the file exists and is readable
  195. def file_readable
  196. if @attachment.readable?
  197. true
  198. else
  199. logger.error "Cannot send attachment, #{@attachment.diskfile} does not exist or is unreadable."
  200. render_404
  201. end
  202. end
  203. def read_authorize
  204. if @attachment.container_type == "HomeworkAttach" || @attachment.container_type == 'Bid'
  205. true
  206. #User.current.allowed_to?(:view_homework_attaches, @attachment.project) ? true : deny_access
  207. else
  208. @attachment.visible? ? true : deny_access
  209. end
  210. end
  211. def delete_authorize
  212. @attachment.deletable? ? true : deny_access
  213. end
  214. def detect_content_type(attachment)
  215. content_type = attachment.content_type
  216. if content_type.blank?
  217. content_type = Redmine::MimeType.of(attachment.filename)
  218. end
  219. content_type.to_s
  220. end
  221. def login_without_softapplication
  222. referer = request.headers['Referer']
  223. require_login unless referer =~ /softapplication/
  224. end
  225. def renderTag
  226. @attachmentNew = Attachment.find(params[:attchmentId])
  227. respond_to do |format|
  228. format.js
  229. end
  230. end
  231. end

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