annotate .svn/pristine/3e/3e62f1e51cbbdbddb3c1ccc8b354945e6ef528ac.svn-base @ 1516:b450a9d58aed redmine-2.4

Update to Redmine SVN revision 13356 on 2.4-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:28:31 +0100
parents e248c7af89ec
children
rev   line source
Chris@1494 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1494 3 #
Chris@1494 4 # This program is free software; you can redistribute it and/or
Chris@1494 5 # modify it under the terms of the GNU General Public License
Chris@1494 6 # as published by the Free Software Foundation; either version 2
Chris@1494 7 # of the License, or (at your option) any later version.
Chris@1494 8 #
Chris@1494 9 # This program is distributed in the hope that it will be useful,
Chris@1494 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1494 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1494 12 # GNU General Public License for more details.
Chris@1494 13 #
Chris@1494 14 # You should have received a copy of the GNU General Public License
Chris@1494 15 # along with this program; if not, write to the Free Software
Chris@1494 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1494 17
Chris@1494 18 class AttachmentsController < ApplicationController
Chris@1494 19 before_filter :find_project, :except => :upload
Chris@1494 20 before_filter :file_readable, :read_authorize, :only => [:show, :download, :thumbnail]
Chris@1494 21 before_filter :delete_authorize, :only => :destroy
Chris@1494 22 before_filter :authorize_global, :only => :upload
Chris@1494 23
Chris@1494 24 accept_api_auth :show, :download, :upload
Chris@1494 25
Chris@1494 26 def show
Chris@1494 27 respond_to do |format|
Chris@1494 28 format.html {
Chris@1494 29 if @attachment.is_diff?
Chris@1494 30 @diff = File.new(@attachment.diskfile, "rb").read
Chris@1494 31 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
Chris@1494 32 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
Chris@1494 33 # Save diff type as user preference
Chris@1494 34 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
Chris@1494 35 User.current.pref[:diff_type] = @diff_type
Chris@1494 36 User.current.preference.save
Chris@1494 37 end
Chris@1494 38 render :action => 'diff'
Chris@1494 39 elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
Chris@1494 40 @content = File.new(@attachment.diskfile, "rb").read
Chris@1494 41 render :action => 'file'
Chris@1494 42 else
Chris@1494 43 download
Chris@1494 44 end
Chris@1494 45 }
Chris@1494 46 format.api
Chris@1494 47 end
Chris@1494 48 end
Chris@1494 49
Chris@1494 50 def download
Chris@1494 51 if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project)
Chris@1494 52 @attachment.increment_download
Chris@1494 53 end
Chris@1494 54
Chris@1494 55 if stale?(:etag => @attachment.digest)
Chris@1494 56 # images are sent inline
Chris@1494 57 send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
Chris@1494 58 :type => detect_content_type(@attachment),
Chris@1494 59 :disposition => (@attachment.image? ? 'inline' : 'attachment')
Chris@1494 60 end
Chris@1494 61 end
Chris@1494 62
Chris@1494 63 def thumbnail
Chris@1494 64 if @attachment.thumbnailable? && thumbnail = @attachment.thumbnail(:size => params[:size])
Chris@1494 65 if stale?(:etag => thumbnail)
Chris@1494 66 send_file thumbnail,
Chris@1494 67 :filename => filename_for_content_disposition(@attachment.filename),
Chris@1494 68 :type => detect_content_type(@attachment),
Chris@1494 69 :disposition => 'inline'
Chris@1494 70 end
Chris@1494 71 else
Chris@1494 72 # No thumbnail for the attachment or thumbnail could not be created
Chris@1494 73 render :nothing => true, :status => 404
Chris@1494 74 end
Chris@1494 75 end
Chris@1494 76
Chris@1494 77 def upload
Chris@1494 78 # Make sure that API users get used to set this content type
Chris@1494 79 # as it won't trigger Rails' automatic parsing of the request body for parameters
Chris@1494 80 unless request.content_type == 'application/octet-stream'
Chris@1494 81 render :nothing => true, :status => 406
Chris@1494 82 return
Chris@1494 83 end
Chris@1494 84
Chris@1494 85 @attachment = Attachment.new(:file => request.raw_post)
Chris@1494 86 @attachment.author = User.current
Chris@1494 87 @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
Chris@1494 88 saved = @attachment.save
Chris@1494 89
Chris@1494 90 respond_to do |format|
Chris@1494 91 format.js
Chris@1494 92 format.api {
Chris@1494 93 if saved
Chris@1494 94 render :action => 'upload', :status => :created
Chris@1494 95 else
Chris@1494 96 render_validation_errors(@attachment)
Chris@1494 97 end
Chris@1494 98 }
Chris@1494 99 end
Chris@1494 100 end
Chris@1494 101
Chris@1494 102 def destroy
Chris@1494 103 if @attachment.container.respond_to?(:init_journal)
Chris@1494 104 @attachment.container.init_journal(User.current)
Chris@1494 105 end
Chris@1494 106 if @attachment.container
Chris@1494 107 # Make sure association callbacks are called
Chris@1494 108 @attachment.container.attachments.delete(@attachment)
Chris@1494 109 else
Chris@1494 110 @attachment.destroy
Chris@1494 111 end
Chris@1494 112
Chris@1494 113 respond_to do |format|
Chris@1494 114 format.html { redirect_to_referer_or project_path(@project) }
Chris@1494 115 format.js
Chris@1494 116 end
Chris@1494 117 end
Chris@1494 118
Chris@1494 119 private
Chris@1494 120 def find_project
Chris@1494 121 @attachment = Attachment.find(params[:id])
Chris@1494 122 # Show 404 if the filename in the url is wrong
Chris@1494 123 raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
Chris@1494 124 @project = @attachment.project
Chris@1494 125 rescue ActiveRecord::RecordNotFound
Chris@1494 126 render_404
Chris@1494 127 end
Chris@1494 128
Chris@1494 129 # Checks that the file exists and is readable
Chris@1494 130 def file_readable
Chris@1494 131 if @attachment.readable?
Chris@1494 132 true
Chris@1494 133 else
Chris@1494 134 logger.error "Cannot send attachment, #{@attachment.diskfile} does not exist or is unreadable."
Chris@1494 135 render_404
Chris@1494 136 end
Chris@1494 137 end
Chris@1494 138
Chris@1494 139 def read_authorize
Chris@1494 140 @attachment.visible? ? true : deny_access
Chris@1494 141 end
Chris@1494 142
Chris@1494 143 def delete_authorize
Chris@1494 144 @attachment.deletable? ? true : deny_access
Chris@1494 145 end
Chris@1494 146
Chris@1494 147 def detect_content_type(attachment)
Chris@1494 148 content_type = attachment.content_type
Chris@1494 149 if content_type.blank?
Chris@1494 150 content_type = Redmine::MimeType.of(attachment.filename)
Chris@1494 151 end
Chris@1494 152 content_type.to_s
Chris@1494 153 end
Chris@1494 154 end