annotate app/controllers/.svn/text-base/attachments_controller.rb.svn-base @ 1172:60d42b9850d2 bug_367

Close obsolete branch bug_367
author Chris Cannam
date Fri, 03 Feb 2012 15:20:50 +0000
parents cbce1fd3b1b7
children
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@0 19 before_filter :find_project
Chris@0 20 before_filter :file_readable, :read_authorize, :except => :destroy
Chris@0 21 before_filter :delete_authorize, :only => :destroy
Chris@441 22
Chris@0 23 verify :method => :post, :only => :destroy
Chris@441 24
Chris@0 25 def show
Chris@0 26 if @attachment.is_diff?
Chris@0 27 @diff = File.new(@attachment.diskfile, "rb").read
Chris@0 28 render :action => 'diff'
Chris@0 29 elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
Chris@0 30 @content = File.new(@attachment.diskfile, "rb").read
Chris@0 31 render :action => 'file'
Chris@0 32 else
Chris@0 33 download
Chris@0 34 end
Chris@0 35 end
Chris@441 36
Chris@0 37 def download
Chris@0 38 if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project)
Chris@0 39 @attachment.increment_download
Chris@0 40 end
Chris@441 41
Chris@0 42 # images are sent inline
Chris@0 43 send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
Chris@441 44 :type => detect_content_type(@attachment),
Chris@0 45 :disposition => (@attachment.image? ? 'inline' : 'attachment')
Chris@441 46
Chris@0 47 end
Chris@441 48
Chris@0 49 def destroy
Chris@0 50 # Make sure association callbacks are called
Chris@0 51 @attachment.container.attachments.delete(@attachment)
Chris@0 52 redirect_to :back
Chris@0 53 rescue ::ActionController::RedirectBackError
Chris@0 54 redirect_to :controller => 'projects', :action => 'show', :id => @project
Chris@0 55 end
Chris@441 56
Chris@0 57 private
Chris@0 58 def find_project
Chris@0 59 @attachment = Attachment.find(params[:id])
Chris@0 60 # Show 404 if the filename in the url is wrong
Chris@0 61 raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
Chris@0 62 @project = @attachment.project
Chris@0 63 rescue ActiveRecord::RecordNotFound
Chris@0 64 render_404
Chris@0 65 end
Chris@441 66
Chris@0 67 # Checks that the file exists and is readable
Chris@0 68 def file_readable
Chris@0 69 @attachment.readable? ? true : render_404
Chris@0 70 end
Chris@441 71
Chris@0 72 def read_authorize
Chris@0 73 @attachment.visible? ? true : deny_access
Chris@0 74 end
Chris@441 75
Chris@0 76 def delete_authorize
Chris@0 77 @attachment.deletable? ? true : deny_access
Chris@0 78 end
Chris@441 79
Chris@0 80 def detect_content_type(attachment)
Chris@0 81 content_type = attachment.content_type
Chris@0 82 if content_type.blank?
Chris@0 83 content_type = Redmine::MimeType.of(attachment.filename)
Chris@0 84 end
Chris@0 85 content_type.to_s
Chris@0 86 end
Chris@0 87 end