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 8.0KB

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