Chris@0: # Redmine - project management software Chris@441: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@441: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@441: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class AttachmentsController < ApplicationController Chris@0: before_filter :find_project Chris@0: before_filter :file_readable, :read_authorize, :except => :destroy Chris@0: before_filter :delete_authorize, :only => :destroy Chris@441: Chris@0: verify :method => :post, :only => :destroy Chris@441: Chris@0: def show Chris@0: if @attachment.is_diff? Chris@0: @diff = File.new(@attachment.diskfile, "rb").read Chris@0: render :action => 'diff' Chris@0: elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte Chris@0: @content = File.new(@attachment.diskfile, "rb").read Chris@0: render :action => 'file' Chris@0: else Chris@0: download Chris@0: end Chris@0: end Chris@441: Chris@0: def download Chris@0: if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project) Chris@0: @attachment.increment_download Chris@0: end Chris@441: Chris@0: # images are sent inline Chris@0: send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), Chris@441: :type => detect_content_type(@attachment), Chris@0: :disposition => (@attachment.image? ? 'inline' : 'attachment') Chris@441: Chris@0: end Chris@441: Chris@0: def destroy Chris@0: # Make sure association callbacks are called Chris@0: @attachment.container.attachments.delete(@attachment) Chris@0: redirect_to :back Chris@0: rescue ::ActionController::RedirectBackError Chris@0: redirect_to :controller => 'projects', :action => 'show', :id => @project Chris@0: end Chris@441: Chris@0: private Chris@0: def find_project Chris@0: @attachment = Attachment.find(params[:id]) Chris@0: # Show 404 if the filename in the url is wrong Chris@0: raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename Chris@0: @project = @attachment.project Chris@0: rescue ActiveRecord::RecordNotFound Chris@0: render_404 Chris@0: end Chris@441: Chris@0: # Checks that the file exists and is readable Chris@0: def file_readable Chris@0: @attachment.readable? ? true : render_404 Chris@0: end Chris@441: Chris@0: def read_authorize Chris@0: @attachment.visible? ? true : deny_access Chris@0: end Chris@441: Chris@0: def delete_authorize Chris@0: @attachment.deletable? ? true : deny_access Chris@0: end Chris@441: Chris@0: def detect_content_type(attachment) Chris@0: content_type = attachment.content_type Chris@0: if content_type.blank? Chris@0: content_type = Redmine::MimeType.of(attachment.filename) Chris@0: end Chris@0: content_type.to_s Chris@0: end Chris@0: end