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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2012 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]
  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. # images are sent inline
  51. send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
  52. :type => detect_content_type(@attachment),
  53. :disposition => (@attachment.image? ? 'inline' : 'attachment')
  54. end
  55. def upload
  56. # Make sure that API users get used to set this content type
  57. # as it won't trigger Rails' automatic parsing of the request body for parameters
  58. unless request.content_type == 'application/octet-stream'
  59. render :nothing => true, :status => 406
  60. return
  61. end
  62. @attachment = Attachment.new(:file => request.body)
  63. @attachment.author = User.current
  64. @attachment.filename = Redmine::Utils.random_hex(16)
  65. if @attachment.save
  66. respond_to do |format|
  67. format.api { render :action => 'upload', :status => :created }
  68. end
  69. else
  70. respond_to do |format|
  71. format.api { render_validation_errors(@attachment) }
  72. end
  73. end
  74. end
  75. def destroy
  76. if @attachment.container.respond_to?(:init_journal)
  77. @attachment.container.init_journal(User.current)
  78. end
  79. # Make sure association callbacks are called
  80. @attachment.container.attachments.delete(@attachment)
  81. redirect_to_referer_or project_path(@project)
  82. end
  83. private
  84. def find_project
  85. @attachment = Attachment.find(params[:id])
  86. # Show 404 if the filename in the url is wrong
  87. raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
  88. @project = @attachment.project
  89. rescue ActiveRecord::RecordNotFound
  90. render_404
  91. end
  92. # Checks that the file exists and is readable
  93. def file_readable
  94. @attachment.readable? ? true : render_404
  95. end
  96. def read_authorize
  97. @attachment.visible? ? true : deny_access
  98. end
  99. def delete_authorize
  100. @attachment.deletable? ? true : deny_access
  101. end
  102. def detect_content_type(attachment)
  103. content_type = attachment.content_type
  104. if content_type.blank?
  105. content_type = Redmine::MimeType.of(attachment.filename)
  106. end
  107. content_type.to_s
  108. end
  109. end