annotate .svn/pristine/79/79562457d0c616b9485dc60b22465abfd7eb8ae4.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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