annotate app/controllers/attachments_controller.rb @ 1170:bfd49444c276 feature_523

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