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 @ 1298:4f746d8966dd
History | View | Annotate | Download (5.42 KB)
| 1 | 0:513646585e45 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1295:622f24f53b42 | Chris | # Copyright (C) 2006-2013 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 | 1115:433d4f72a19b | Chris | before_filter :find_project, :except => :upload |
| 20 | before_filter :file_readable, :read_authorize, :only => [:show, :download, :thumbnail] |
||
| 21 | 0:513646585e45 | Chris | before_filter :delete_authorize, :only => :destroy |
| 22 | 1115:433d4f72a19b | Chris | before_filter :authorize_global, :only => :upload |
| 23 | 1116:bb32da3bea34 | Chris | before_filter :active_authorize, :only => :toggle_active |
| 24 | 257:3ecf99348b9f | chris | |
| 25 | 967:19884e9d5eff | chris | include AttachmentsHelper
|
| 26 | helper :attachments
|
||
| 27 | 441:cbce1fd3b1b7 | Chris | |
| 28 | 1115:433d4f72a19b | Chris | accept_api_auth :show, :download, :upload |
| 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 | 0:513646585e45 | Chris | @attachment.increment_download
|
| 59 | end
|
||
| 60 | 441:cbce1fd3b1b7 | Chris | |
| 61 | 1115:433d4f72a19b | Chris | if stale?(:etag => @attachment.digest) |
| 62 | # images are sent inline
|
||
| 63 | send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), |
||
| 64 | :type => detect_content_type(@attachment), |
||
| 65 | :disposition => (@attachment.image? ? 'inline' : 'attachment') |
||
| 66 | end
|
||
| 67 | 0:513646585e45 | Chris | end
|
| 68 | 441:cbce1fd3b1b7 | Chris | |
| 69 | 1115:433d4f72a19b | Chris | def thumbnail |
| 70 | if @attachment.thumbnailable? && thumbnail = @attachment.thumbnail(:size => params[:size]) |
||
| 71 | if stale?(:etag => thumbnail) |
||
| 72 | send_file thumbnail, |
||
| 73 | :filename => filename_for_content_disposition(@attachment.filename), |
||
| 74 | :type => detect_content_type(@attachment), |
||
| 75 | :disposition => 'inline' |
||
| 76 | end
|
||
| 77 | else
|
||
| 78 | # No thumbnail for the attachment or thumbnail could not be created
|
||
| 79 | render :nothing => true, :status => 404 |
||
| 80 | end
|
||
| 81 | end
|
||
| 82 | |||
| 83 | def upload |
||
| 84 | # Make sure that API users get used to set this content type
|
||
| 85 | # as it won't trigger Rails' automatic parsing of the request body for parameters
|
||
| 86 | unless request.content_type == 'application/octet-stream' |
||
| 87 | render :nothing => true, :status => 406 |
||
| 88 | return
|
||
| 89 | end
|
||
| 90 | |||
| 91 | @attachment = Attachment.new(:file => request.raw_post) |
||
| 92 | @attachment.author = User.current |
||
| 93 | @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16) |
||
| 94 | 1295:622f24f53b42 | Chris | saved = @attachment.save
|
| 95 | 1115:433d4f72a19b | Chris | |
| 96 | 1295:622f24f53b42 | Chris | respond_to do |format|
|
| 97 | format.js |
||
| 98 | format.api {
|
||
| 99 | if saved
|
||
| 100 | render :action => 'upload', :status => :created |
||
| 101 | else
|
||
| 102 | render_validation_errors(@attachment)
|
||
| 103 | end
|
||
| 104 | } |
||
| 105 | 1115:433d4f72a19b | Chris | end
|
| 106 | end
|
||
| 107 | |||
| 108 | 0:513646585e45 | Chris | def destroy |
| 109 | 1115:433d4f72a19b | Chris | if @attachment.container.respond_to?(:init_journal) |
| 110 | @attachment.container.init_journal(User.current) |
||
| 111 | end
|
||
| 112 | 1295:622f24f53b42 | Chris | if @attachment.container |
| 113 | # Make sure association callbacks are called
|
||
| 114 | @attachment.container.attachments.delete(@attachment) |
||
| 115 | else
|
||
| 116 | @attachment.destroy
|
||
| 117 | end
|
||
| 118 | |||
| 119 | respond_to do |format|
|
||
| 120 | format.html { redirect_to_referer_or project_path(@project) }
|
||
| 121 | format.js |
||
| 122 | end
|
||
| 123 | 0:513646585e45 | Chris | end
|
| 124 | 441:cbce1fd3b1b7 | Chris | |
| 125 | 257:3ecf99348b9f | chris | def toggle_active |
| 126 | @attachment.active = !@attachment.active? |
||
| 127 | @attachment.save!
|
||
| 128 | 1156:2f6e71e31b55 | chris | respond_to do |format|
|
| 129 | format.js |
||
| 130 | end
|
||
| 131 | 257:3ecf99348b9f | chris | end
|
| 132 | |||
| 133 | 0:513646585e45 | Chris | private |
| 134 | def find_project |
||
| 135 | @attachment = Attachment.find(params[:id]) |
||
| 136 | # Show 404 if the filename in the url is wrong
|
||
| 137 | raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename |
||
| 138 | @project = @attachment.project |
||
| 139 | rescue ActiveRecord::RecordNotFound |
||
| 140 | render_404 |
||
| 141 | end
|
||
| 142 | 441:cbce1fd3b1b7 | Chris | |
| 143 | 0:513646585e45 | Chris | # Checks that the file exists and is readable
|
| 144 | def file_readable |
||
| 145 | 1295:622f24f53b42 | Chris | if @attachment.readable? |
| 146 | true
|
||
| 147 | else
|
||
| 148 | logger.error "Cannot send attachment, #{@attachment.diskfile} does not exist or is unreadable."
|
||
| 149 | render_404 |
||
| 150 | end
|
||
| 151 | 0:513646585e45 | Chris | end
|
| 152 | 441:cbce1fd3b1b7 | Chris | |
| 153 | 0:513646585e45 | Chris | def read_authorize |
| 154 | @attachment.visible? ? true : deny_access |
||
| 155 | end
|
||
| 156 | 441:cbce1fd3b1b7 | Chris | |
| 157 | 0:513646585e45 | Chris | def delete_authorize |
| 158 | @attachment.deletable? ? true : deny_access |
||
| 159 | end
|
||
| 160 | 441:cbce1fd3b1b7 | Chris | |
| 161 | 257:3ecf99348b9f | chris | def active_authorize |
| 162 | true
|
||
| 163 | end
|
||
| 164 | |||
| 165 | 0:513646585e45 | Chris | def detect_content_type(attachment) |
| 166 | content_type = attachment.content_type |
||
| 167 | if content_type.blank?
|
||
| 168 | content_type = Redmine::MimeType.of(attachment.filename) |
||
| 169 | end
|
||
| 170 | content_type.to_s |
||
| 171 | end
|
||
| 172 | end |