annotate .svn/pristine/79/79562457d0c616b9485dc60b22465abfd7eb8ae4.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 class AttachmentsController < ApplicationController
Chris@1295 19 before_filter :find_project, :except => :upload
Chris@1295 20 before_filter :file_readable, :read_authorize, :only => [:show, :download, :thumbnail]
Chris@1295 21 before_filter :delete_authorize, :only => :destroy
Chris@1295 22 before_filter :authorize_global, :only => :upload
Chris@1295 23
Chris@1295 24 accept_api_auth :show, :download, :upload
Chris@1295 25
Chris@1295 26 def show
Chris@1295 27 respond_to do |format|
Chris@1295 28 format.html {
Chris@1295 29 if @attachment.is_diff?
Chris@1295 30 @diff = File.new(@attachment.diskfile, "rb").read
Chris@1295 31 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
Chris@1295 32 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
Chris@1295 33 # Save diff type as user preference
Chris@1295 34 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
Chris@1295 35 User.current.pref[:diff_type] = @diff_type
Chris@1295 36 User.current.preference.save
Chris@1295 37 end
Chris@1295 38 render :action => 'diff'
Chris@1295 39 elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
Chris@1295 40 @content = File.new(@attachment.diskfile, "rb").read
Chris@1295 41 render :action => 'file'
Chris@1295 42 else
Chris@1295 43 download
Chris@1295 44 end
Chris@1295 45 }
Chris@1295 46 format.api
Chris@1295 47 end
Chris@1295 48 end
Chris@1295 49
Chris@1295 50 def download
Chris@1295 51 if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project)
Chris@1295 52 @attachment.increment_download
Chris@1295 53 end
Chris@1295 54
Chris@1295 55 if stale?(:etag => @attachment.digest)
Chris@1295 56 # images are sent inline
Chris@1295 57 send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
Chris@1295 58 :type => detect_content_type(@attachment),
Chris@1295 59 :disposition => (@attachment.image? ? 'inline' : 'attachment')
Chris@1295 60 end
Chris@1295 61 end
Chris@1295 62
Chris@1295 63 def thumbnail
Chris@1295 64 if @attachment.thumbnailable? && thumbnail = @attachment.thumbnail(:size => params[:size])
Chris@1295 65 if stale?(:etag => thumbnail)
Chris@1295 66 send_file thumbnail,
Chris@1295 67 :filename => filename_for_content_disposition(@attachment.filename),
Chris@1295 68 :type => detect_content_type(@attachment),
Chris@1295 69 :disposition => 'inline'
Chris@1295 70 end
Chris@1295 71 else
Chris@1295 72 # No thumbnail for the attachment or thumbnail could not be created
Chris@1295 73 render :nothing => true, :status => 404
Chris@1295 74 end
Chris@1295 75 end
Chris@1295 76
Chris@1295 77 def upload
Chris@1295 78 # Make sure that API users get used to set this content type
Chris@1295 79 # as it won't trigger Rails' automatic parsing of the request body for parameters
Chris@1295 80 unless request.content_type == 'application/octet-stream'
Chris@1295 81 render :nothing => true, :status => 406
Chris@1295 82 return
Chris@1295 83 end
Chris@1295 84
Chris@1295 85 @attachment = Attachment.new(:file => request.raw_post)
Chris@1295 86 @attachment.author = User.current
Chris@1295 87 @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
Chris@1295 88
Chris@1295 89 if @attachment.save
Chris@1295 90 respond_to do |format|
Chris@1295 91 format.api { render :action => 'upload', :status => :created }
Chris@1295 92 end
Chris@1295 93 else
Chris@1295 94 respond_to do |format|
Chris@1295 95 format.api { render_validation_errors(@attachment) }
Chris@1295 96 end
Chris@1295 97 end
Chris@1295 98 end
Chris@1295 99
Chris@1295 100 def destroy
Chris@1295 101 if @attachment.container.respond_to?(:init_journal)
Chris@1295 102 @attachment.container.init_journal(User.current)
Chris@1295 103 end
Chris@1295 104 # Make sure association callbacks are called
Chris@1295 105 @attachment.container.attachments.delete(@attachment)
Chris@1295 106 redirect_to_referer_or project_path(@project)
Chris@1295 107 end
Chris@1295 108
Chris@1295 109 private
Chris@1295 110 def find_project
Chris@1295 111 @attachment = Attachment.find(params[:id])
Chris@1295 112 # Show 404 if the filename in the url is wrong
Chris@1295 113 raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
Chris@1295 114 @project = @attachment.project
Chris@1295 115 rescue ActiveRecord::RecordNotFound
Chris@1295 116 render_404
Chris@1295 117 end
Chris@1295 118
Chris@1295 119 # Checks that the file exists and is readable
Chris@1295 120 def file_readable
Chris@1295 121 @attachment.readable? ? true : render_404
Chris@1295 122 end
Chris@1295 123
Chris@1295 124 def read_authorize
Chris@1295 125 @attachment.visible? ? true : deny_access
Chris@1295 126 end
Chris@1295 127
Chris@1295 128 def delete_authorize
Chris@1295 129 @attachment.deletable? ? true : deny_access
Chris@1295 130 end
Chris@1295 131
Chris@1295 132 def detect_content_type(attachment)
Chris@1295 133 content_type = attachment.content_type
Chris@1295 134 if content_type.blank?
Chris@1295 135 content_type = Redmine::MimeType.of(attachment.filename)
Chris@1295 136 end
Chris@1295 137 content_type.to_s
Chris@1295 138 end
Chris@1295 139 end