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 @ 969:e632838adb35
History | View | Annotate | Download (3.79 KB)
| 1 | 0:513646585e45 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 441:cbce1fd3b1b7 | Chris | # Copyright (C) 2006-2011 Jean-Philippe Lang
|
| 3 | 0:513646585e45 | Chris | #
|
| 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 | 441:cbce1fd3b1b7 | Chris | #
|
| 9 | 0:513646585e45 | Chris | # 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 | 441:cbce1fd3b1b7 | Chris | #
|
| 14 | 0:513646585e45 | Chris | # 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 | 257:3ecf99348b9f | chris | |
| 20 | 967:19884e9d5eff | chris | include AttachmentsHelper
|
| 21 | helper :attachments
|
||
| 22 | |||
| 23 | 0:513646585e45 | Chris | before_filter :find_project
|
| 24 | before_filter :file_readable, :read_authorize, :except => :destroy |
||
| 25 | before_filter :delete_authorize, :only => :destroy |
||
| 26 | 257:3ecf99348b9f | chris | before_filter :active_authorize, :only => :toggle_active |
| 27 | 441:cbce1fd3b1b7 | Chris | |
| 28 | 909:cbb26bc654de | Chris | accept_api_auth :show, :download |
| 29 | 441:cbce1fd3b1b7 | Chris | |
| 30 | 0:513646585e45 | Chris | def show |
| 31 | 909:cbb26bc654de | Chris | 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 | 0:513646585e45 | Chris | end
|
| 52 | end
|
||
| 53 | 441:cbce1fd3b1b7 | Chris | |
| 54 | 0:513646585e45 | Chris | def download |
| 55 | 957:35782bf2eb58 | Chris | # cc: formerly this happened only if "@attachment.container.is_a?(Version)"
|
| 56 | 967:19884e9d5eff | chris | # 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 | 441:cbce1fd3b1b7 | Chris | |
| 61 | 0:513646585e45 | Chris | # images are sent inline
|
| 62 | send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), |
||
| 63 | 441:cbce1fd3b1b7 | Chris | :type => detect_content_type(@attachment), |
| 64 | 0:513646585e45 | Chris | :disposition => (@attachment.image? ? 'inline' : 'attachment') |
| 65 | 441:cbce1fd3b1b7 | Chris | |
| 66 | 0:513646585e45 | Chris | end
|
| 67 | 441:cbce1fd3b1b7 | Chris | |
| 68 | 909:cbb26bc654de | Chris | verify :method => :delete, :only => :destroy |
| 69 | 0:513646585e45 | Chris | 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 | 441:cbce1fd3b1b7 | Chris | |
| 77 | 257:3ecf99348b9f | chris | def toggle_active |
| 78 | @attachment.active = !@attachment.active? |
||
| 79 | @attachment.save!
|
||
| 80 | render :layout => false |
||
| 81 | end
|
||
| 82 | |||
| 83 | 0:513646585e45 | Chris | 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 | 441:cbce1fd3b1b7 | Chris | |
| 93 | 0:513646585e45 | Chris | # Checks that the file exists and is readable
|
| 94 | def file_readable |
||
| 95 | @attachment.readable? ? true : render_404 |
||
| 96 | end
|
||
| 97 | 441:cbce1fd3b1b7 | Chris | |
| 98 | 0:513646585e45 | Chris | def read_authorize |
| 99 | @attachment.visible? ? true : deny_access |
||
| 100 | end
|
||
| 101 | 441:cbce1fd3b1b7 | Chris | |
| 102 | 0:513646585e45 | Chris | def delete_authorize |
| 103 | @attachment.deletable? ? true : deny_access |
||
| 104 | end
|
||
| 105 | 441:cbce1fd3b1b7 | Chris | |
| 106 | 257:3ecf99348b9f | chris | def active_authorize |
| 107 | true
|
||
| 108 | end
|
||
| 109 | |||
| 110 | 0:513646585e45 | Chris | 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 |