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 / app / controllers / attachments_controller.rb @ 1270:b2f7f52a164d

History | View | Annotate | Download (5.15 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2012  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, :except => :upload
20
  before_filter :file_readable, :read_authorize, :only => [:show, :download, :thumbnail]
21
  before_filter :delete_authorize, :only => :destroy
22
  before_filter :authorize_global, :only => :upload
23
  before_filter :active_authorize, :only => :toggle_active
24

    
25
  include AttachmentsHelper
26
  helper :attachments
27

    
28
  accept_api_auth :show, :download, :upload
29

    
30
  def show
31
    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
    end
52
  end
53

    
54
  def download
55
    # cc: formerly this happened only if "@attachment.container.is_a?(Version)"
56
    # 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

    
61
    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
  end
68

    
69
  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

    
95
    if @attachment.save
96
      respond_to do |format|
97
        format.api { render :action => 'upload', :status => :created }
98
      end
99
    else
100
      respond_to do |format|
101
        format.api { render_validation_errors(@attachment) }
102
      end
103
    end
104
  end
105

    
106
  def destroy
107
    if @attachment.container.respond_to?(:init_journal)
108
      @attachment.container.init_journal(User.current)
109
    end
110
    # Make sure association callbacks are called
111
    @attachment.container.attachments.delete(@attachment)
112
    redirect_to_referer_or project_path(@project)
113
  end
114

    
115
  def toggle_active
116
    @attachment.active = !@attachment.active?
117
    @attachment.save!
118
    respond_to do |format|
119
      format.js
120
    end
121
  end
122

    
123
private
124
  def find_project
125
    @attachment = Attachment.find(params[:id])
126
    # Show 404 if the filename in the url is wrong
127
    raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
128
    @project = @attachment.project
129
  rescue ActiveRecord::RecordNotFound
130
    render_404
131
  end
132

    
133
  # Checks that the file exists and is readable
134
  def file_readable
135
    @attachment.readable? ? true : render_404
136
  end
137

    
138
  def read_authorize
139
    @attachment.visible? ? true : deny_access
140
  end
141

    
142
  def delete_authorize
143
    @attachment.deletable? ? true : deny_access
144
  end
145

    
146
  def active_authorize
147
    true
148
  end
149

    
150
  def detect_content_type(attachment)
151
    content_type = attachment.content_type
152
    if content_type.blank?
153
      content_type = Redmine::MimeType.of(attachment.filename)
154
    end
155
    content_type.to_s
156
  end
157
end