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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, :except => :upload
  19. before_filter :file_readable, :read_authorize, :only => [:show, :download, :thumbnail]
  20. before_filter :delete_authorize, :only => :destroy
  21. before_filter :authorize_global, :only => :upload
  22. accept_api_auth :show, :download, :upload
  23. def show
  24. respond_to do |format|
  25. format.html {
  26. if @attachment.is_diff?
  27. @diff = File.new(@attachment.diskfile, "rb").read
  28. @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
  29. @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
  30. # Save diff type as user preference
  31. if User.current.logged? && @diff_type != User.current.pref[:diff_type]
  32. User.current.pref[:diff_type] = @diff_type
  33. User.current.preference.save
  34. end
  35. render :action => 'diff'
  36. elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
  37. @content = File.new(@attachment.diskfile, "rb").read
  38. render :action => 'file'
  39. else
  40. download
  41. end
  42. }
  43. format.api
  44. end
  45. end
  46. def download
  47. if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project)
  48. @attachment.increment_download
  49. end
  50. if stale?(:etag => @attachment.digest)
  51. # images are sent inline
  52. send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
  53. :type => detect_content_type(@attachment),
  54. :disposition => (@attachment.image? ? 'inline' : 'attachment')
  55. end
  56. end
  57. def thumbnail
  58. if @attachment.thumbnailable? && thumbnail = @attachment.thumbnail(:size => params[:size])
  59. if stale?(:etag => thumbnail)
  60. send_file thumbnail,
  61. :filename => filename_for_content_disposition(@attachment.filename),
  62. :type => detect_content_type(@attachment),
  63. :disposition => 'inline'
  64. end
  65. else
  66. # No thumbnail for the attachment or thumbnail could not be created
  67. render :nothing => true, :status => 404
  68. end
  69. end
  70. def upload
  71. # Make sure that API users get used to set this content type
  72. # as it won't trigger Rails' automatic parsing of the request body for parameters
  73. unless request.content_type == 'application/octet-stream'
  74. render :nothing => true, :status => 406
  75. return
  76. end
  77. @attachment = Attachment.new(:file => request.raw_post)
  78. @attachment.author = User.current
  79. @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
  80. saved = @attachment.save
  81. respond_to do |format|
  82. format.js
  83. format.api {
  84. if saved
  85. render :action => 'upload', :status => :created
  86. else
  87. render_validation_errors(@attachment)
  88. end
  89. }
  90. end
  91. end
  92. def destroy
  93. if @attachment.container.respond_to?(:init_journal)
  94. @attachment.container.init_journal(User.current)
  95. end
  96. if @attachment.container
  97. # Make sure association callbacks are called
  98. @attachment.container.attachments.delete(@attachment)
  99. else
  100. @attachment.destroy
  101. end
  102. respond_to do |format|
  103. format.html { redirect_to_referer_or project_path(@project) }
  104. format.js
  105. end
  106. end
  107. private
  108. def find_project
  109. @attachment = Attachment.find(params[:id])
  110. # Show 404 if the filename in the url is wrong
  111. raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
  112. @project = @attachment.project
  113. rescue ActiveRecord::RecordNotFound
  114. render_404
  115. end
  116. # Checks that the file exists and is readable
  117. def file_readable
  118. if @attachment.readable?
  119. true
  120. else
  121. logger.error "Cannot send attachment, #{@attachment.diskfile} does not exist or is unreadable."
  122. render_404
  123. end
  124. end
  125. def read_authorize
  126. @attachment.visible? ? true : deny_access
  127. end
  128. def delete_authorize
  129. @attachment.deletable? ? true : deny_access
  130. end
  131. def detect_content_type(attachment)
  132. content_type = attachment.content_type
  133. if content_type.blank?
  134. content_type = Redmine::MimeType.of(attachment.filename)
  135. end
  136. content_type.to_s
  137. end
  138. end