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

attachments_controller.rb 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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_action :find_attachment, :only => [:show, :download, :thumbnail, :update, :destroy]
  19. before_action :find_editable_attachments, :only => [:edit_all, :update_all]
  20. before_action :file_readable, :read_authorize, :only => [:show, :download, :thumbnail]
  21. before_action :update_authorize, :only => :update
  22. before_action :delete_authorize, :only => :destroy
  23. before_action :authorize_global, :only => :upload
  24. # Disable check for same origin requests for JS files, i.e. attachments with
  25. # MIME type text/javascript.
  26. skip_after_action :verify_same_origin_request, :only => :download
  27. accept_api_auth :show, :download, :thumbnail, :upload, :update, :destroy
  28. def show
  29. respond_to do |format|
  30. format.html {
  31. if @attachment.container.respond_to?(:attachments)
  32. @attachments = @attachment.container.attachments.to_a
  33. if index = @attachments.index(@attachment)
  34. @paginator = Redmine::Pagination::Paginator.new(
  35. @attachments.size, 1, index+1
  36. )
  37. end
  38. end
  39. if @attachment.is_diff?
  40. @diff = File.read(@attachment.diskfile, :mode => "rb")
  41. @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
  42. @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
  43. # Save diff type as user preference
  44. if User.current.logged? && @diff_type != User.current.pref[:diff_type]
  45. User.current.pref[:diff_type] = @diff_type
  46. User.current.preference.save
  47. end
  48. render :action => 'diff'
  49. elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
  50. @content = File.read(@attachment.diskfile, :mode => "rb")
  51. render :action => 'file'
  52. elsif @attachment.is_image?
  53. render :action => 'image'
  54. else
  55. render :action => 'other'
  56. end
  57. }
  58. format.api
  59. end
  60. end
  61. def download
  62. if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project)
  63. @attachment.increment_download
  64. end
  65. if stale?(:etag => @attachment.digest)
  66. # images are sent inline
  67. send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
  68. :type => detect_content_type(@attachment),
  69. :disposition => disposition(@attachment)
  70. end
  71. end
  72. def thumbnail
  73. if @attachment.thumbnailable? && tbnail = @attachment.thumbnail(:size => params[:size])
  74. if stale?(:etag => tbnail)
  75. send_file tbnail,
  76. :filename => filename_for_content_disposition(@attachment.filename),
  77. :type => detect_content_type(@attachment),
  78. :disposition => 'inline'
  79. end
  80. else
  81. # No thumbnail for the attachment or thumbnail could not be created
  82. head 404
  83. end
  84. end
  85. def upload
  86. # Make sure that API users get used to set this content type
  87. # as it won't trigger Rails' automatic parsing of the request body for parameters
  88. unless request.content_type == 'application/octet-stream'
  89. head 406
  90. return
  91. end
  92. @attachment = Attachment.new(:file => request.raw_post)
  93. @attachment.author = User.current
  94. @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
  95. @attachment.content_type = params[:content_type].presence
  96. saved = @attachment.save
  97. respond_to do |format|
  98. format.js
  99. format.api {
  100. if saved
  101. render :action => 'upload', :status => :created
  102. else
  103. render_validation_errors(@attachment)
  104. end
  105. }
  106. end
  107. end
  108. # Edit all the attachments of a container
  109. def edit_all
  110. end
  111. # Update all the attachments of a container
  112. def update_all
  113. if Attachment.update_attachments(@attachments, update_all_params)
  114. redirect_back_or_default home_path
  115. return
  116. end
  117. render :action => 'edit_all'
  118. end
  119. def update
  120. @attachment.safe_attributes = params[:attachment]
  121. saved = @attachment.save
  122. respond_to do |format|
  123. format.api {
  124. if saved
  125. render_api_ok
  126. else
  127. render_validation_errors(@attachment)
  128. end
  129. }
  130. end
  131. end
  132. def destroy
  133. if @attachment.container.respond_to?(:init_journal)
  134. @attachment.container.init_journal(User.current)
  135. end
  136. if @attachment.container
  137. # Make sure association callbacks are called
  138. @attachment.container.attachments.delete(@attachment)
  139. else
  140. @attachment.destroy
  141. end
  142. respond_to do |format|
  143. format.html { redirect_to_referer_or project_path(@project) }
  144. format.js
  145. format.api { render_api_ok }
  146. end
  147. end
  148. # Returns the menu item that should be selected when viewing an attachment
  149. def current_menu_item
  150. container = @attachment.try(:container) || @container
  151. if container
  152. case container
  153. when WikiPage
  154. :wiki
  155. when Message
  156. :boards
  157. when Project, Version
  158. :files
  159. else
  160. container.class.name.pluralize.downcase.to_sym
  161. end
  162. end
  163. end
  164. private
  165. def find_attachment
  166. @attachment = Attachment.find(params[:id])
  167. # Show 404 if the filename in the url is wrong
  168. raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
  169. @project = @attachment.project
  170. rescue ActiveRecord::RecordNotFound
  171. render_404
  172. end
  173. def find_editable_attachments
  174. klass = params[:object_type].to_s.singularize.classify.constantize rescue nil
  175. unless klass && klass.reflect_on_association(:attachments)
  176. render_404
  177. return
  178. end
  179. @container = klass.find(params[:object_id])
  180. if @container.respond_to?(:visible?) && !@container.visible?
  181. render_403
  182. return
  183. end
  184. @attachments = @container.attachments.select(&:editable?)
  185. if @container.respond_to?(:project)
  186. @project = @container.project
  187. end
  188. render_404 if @attachments.empty?
  189. rescue ActiveRecord::RecordNotFound
  190. render_404
  191. end
  192. # Checks that the file exists and is readable
  193. def file_readable
  194. if @attachment.readable?
  195. true
  196. else
  197. logger.error "Cannot send attachment, #{@attachment.diskfile} does not exist or is unreadable."
  198. render_404
  199. end
  200. end
  201. def read_authorize
  202. @attachment.visible? ? true : deny_access
  203. end
  204. def update_authorize
  205. @attachment.editable? ? true : deny_access
  206. end
  207. def delete_authorize
  208. @attachment.deletable? ? true : deny_access
  209. end
  210. def detect_content_type(attachment)
  211. content_type = attachment.content_type
  212. if content_type.blank? || content_type == "application/octet-stream"
  213. content_type = Redmine::MimeType.of(attachment.filename)
  214. end
  215. content_type.to_s
  216. end
  217. def disposition(attachment)
  218. if attachment.is_pdf?
  219. 'inline'
  220. else
  221. 'attachment'
  222. end
  223. end
  224. # Returns attachments param for #update_all
  225. def update_all_params
  226. params.permit(:attachments => [:filename, :description]).require(:attachments)
  227. end
  228. end