To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 64 / 643e94db4aed32471bbf1bd1d5bd4f887d610b4b.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (3.42 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
  before_filter :find_project
20
  before_filter :file_readable, :read_authorize, :except => :destroy
21
  before_filter :delete_authorize, :only => :destroy
22

    
23
  accept_api_auth :show, :download
24

    
25
  def show
26
    respond_to do |format|
27
      format.html {
28
        if @attachment.is_diff?
29
          @diff = File.new(@attachment.diskfile, "rb").read
30
          @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
31
          @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
32
          # Save diff type as user preference
33
          if User.current.logged? && @diff_type != User.current.pref[:diff_type]
34
            User.current.pref[:diff_type] = @diff_type
35
            User.current.preference.save
36
          end
37
          render :action => 'diff'
38
        elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
39
          @content = File.new(@attachment.diskfile, "rb").read
40
          render :action => 'file'
41
        else
42
          download
43
        end
44
      }
45
      format.api
46
    end
47
  end
48

    
49
  def download
50
    if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project)
51
      @attachment.increment_download
52
    end
53

    
54
    # images are sent inline
55
    send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
56
                                    :type => detect_content_type(@attachment),
57
                                    :disposition => (@attachment.image? ? 'inline' : 'attachment')
58

    
59
  end
60

    
61
  verify :method => :delete, :only => :destroy
62
  def destroy
63
    # Make sure association callbacks are called
64
    @attachment.container.attachments.delete(@attachment)
65
    redirect_to :back
66
  rescue ::ActionController::RedirectBackError
67
    redirect_to :controller => 'projects', :action => 'show', :id => @project
68
  end
69

    
70
private
71
  def find_project
72
    @attachment = Attachment.find(params[:id])
73
    # Show 404 if the filename in the url is wrong
74
    raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
75
    @project = @attachment.project
76
  rescue ActiveRecord::RecordNotFound
77
    render_404
78
  end
79

    
80
  # Checks that the file exists and is readable
81
  def file_readable
82
    @attachment.readable? ? true : render_404
83
  end
84

    
85
  def read_authorize
86
    @attachment.visible? ? true : deny_access
87
  end
88

    
89
  def delete_authorize
90
    @attachment.deletable? ? true : deny_access
91
  end
92

    
93
  def detect_content_type(attachment)
94
    content_type = attachment.content_type
95
    if content_type.blank?
96
      content_type = Redmine::MimeType.of(attachment.filename)
97
    end
98
    content_type.to_s
99
  end
100
end