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