annotate .svn/pristine/64/643e94db4aed32471bbf1bd1d5bd4f887d610b4b.svn-base @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 class AttachmentsController < ApplicationController
Chris@909 19 before_filter :find_project
Chris@909 20 before_filter :file_readable, :read_authorize, :except => :destroy
Chris@909 21 before_filter :delete_authorize, :only => :destroy
Chris@909 22
Chris@909 23 accept_api_auth :show, :download
Chris@909 24
Chris@909 25 def show
Chris@909 26 respond_to do |format|
Chris@909 27 format.html {
Chris@909 28 if @attachment.is_diff?
Chris@909 29 @diff = File.new(@attachment.diskfile, "rb").read
Chris@909 30 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
Chris@909 31 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
Chris@909 32 # Save diff type as user preference
Chris@909 33 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
Chris@909 34 User.current.pref[:diff_type] = @diff_type
Chris@909 35 User.current.preference.save
Chris@909 36 end
Chris@909 37 render :action => 'diff'
Chris@909 38 elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
Chris@909 39 @content = File.new(@attachment.diskfile, "rb").read
Chris@909 40 render :action => 'file'
Chris@909 41 else
Chris@909 42 download
Chris@909 43 end
Chris@909 44 }
Chris@909 45 format.api
Chris@909 46 end
Chris@909 47 end
Chris@909 48
Chris@909 49 def download
Chris@909 50 if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project)
Chris@909 51 @attachment.increment_download
Chris@909 52 end
Chris@909 53
Chris@909 54 # images are sent inline
Chris@909 55 send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
Chris@909 56 :type => detect_content_type(@attachment),
Chris@909 57 :disposition => (@attachment.image? ? 'inline' : 'attachment')
Chris@909 58
Chris@909 59 end
Chris@909 60
Chris@909 61 verify :method => :delete, :only => :destroy
Chris@909 62 def destroy
Chris@909 63 # Make sure association callbacks are called
Chris@909 64 @attachment.container.attachments.delete(@attachment)
Chris@909 65 redirect_to :back
Chris@909 66 rescue ::ActionController::RedirectBackError
Chris@909 67 redirect_to :controller => 'projects', :action => 'show', :id => @project
Chris@909 68 end
Chris@909 69
Chris@909 70 private
Chris@909 71 def find_project
Chris@909 72 @attachment = Attachment.find(params[:id])
Chris@909 73 # Show 404 if the filename in the url is wrong
Chris@909 74 raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
Chris@909 75 @project = @attachment.project
Chris@909 76 rescue ActiveRecord::RecordNotFound
Chris@909 77 render_404
Chris@909 78 end
Chris@909 79
Chris@909 80 # Checks that the file exists and is readable
Chris@909 81 def file_readable
Chris@909 82 @attachment.readable? ? true : render_404
Chris@909 83 end
Chris@909 84
Chris@909 85 def read_authorize
Chris@909 86 @attachment.visible? ? true : deny_access
Chris@909 87 end
Chris@909 88
Chris@909 89 def delete_authorize
Chris@909 90 @attachment.deletable? ? true : deny_access
Chris@909 91 end
Chris@909 92
Chris@909 93 def detect_content_type(attachment)
Chris@909 94 content_type = attachment.content_type
Chris@909 95 if content_type.blank?
Chris@909 96 content_type = Redmine::MimeType.of(attachment.filename)
Chris@909 97 end
Chris@909 98 content_type.to_s
Chris@909 99 end
Chris@909 100 end