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 / 79 / 79562457d0c616b9485dc60b22465abfd7eb8ae4.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (4.76 KB)

1 1296:038ba2d95de8 Chris
# 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
24
  accept_api_auth :show, :download, :upload
25
26
  def show
27
    respond_to do |format|
28
      format.html {
29
        if @attachment.is_diff?
30
          @diff = File.new(@attachment.diskfile, "rb").read
31
          @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
32
          @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
33
          # Save diff type as user preference
34
          if User.current.logged? && @diff_type != User.current.pref[:diff_type]
35
            User.current.pref[:diff_type] = @diff_type
36
            User.current.preference.save
37
          end
38
          render :action => 'diff'
39
        elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
40
          @content = File.new(@attachment.diskfile, "rb").read
41
          render :action => 'file'
42
        else
43
          download
44
        end
45
      }
46
      format.api
47
    end
48
  end
49
50
  def download
51
    if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project)
52
      @attachment.increment_download
53
    end
54
55
    if stale?(:etag => @attachment.digest)
56
      # images are sent inline
57
      send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
58
                                      :type => detect_content_type(@attachment),
59
                                      :disposition => (@attachment.image? ? 'inline' : 'attachment')
60
    end
61
  end
62
63
  def thumbnail
64
    if @attachment.thumbnailable? && thumbnail = @attachment.thumbnail(:size => params[:size])
65
      if stale?(:etag => thumbnail)
66
        send_file thumbnail,
67
          :filename => filename_for_content_disposition(@attachment.filename),
68
          :type => detect_content_type(@attachment),
69
          :disposition => 'inline'
70
      end
71
    else
72
      # No thumbnail for the attachment or thumbnail could not be created
73
      render :nothing => true, :status => 404
74
    end
75
  end
76
77
  def upload
78
    # Make sure that API users get used to set this content type
79
    # as it won't trigger Rails' automatic parsing of the request body for parameters
80
    unless request.content_type == 'application/octet-stream'
81
      render :nothing => true, :status => 406
82
      return
83
    end
84
85
    @attachment = Attachment.new(:file => request.raw_post)
86
    @attachment.author = User.current
87
    @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
88
89
    if @attachment.save
90
      respond_to do |format|
91
        format.api { render :action => 'upload', :status => :created }
92
      end
93
    else
94
      respond_to do |format|
95
        format.api { render_validation_errors(@attachment) }
96
      end
97
    end
98
  end
99
100
  def destroy
101
    if @attachment.container.respond_to?(:init_journal)
102
      @attachment.container.init_journal(User.current)
103
    end
104
    # Make sure association callbacks are called
105
    @attachment.container.attachments.delete(@attachment)
106
    redirect_to_referer_or project_path(@project)
107
  end
108
109
private
110
  def find_project
111
    @attachment = Attachment.find(params[:id])
112
    # Show 404 if the filename in the url is wrong
113
    raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
114
    @project = @attachment.project
115
  rescue ActiveRecord::RecordNotFound
116
    render_404
117
  end
118
119
  # Checks that the file exists and is readable
120
  def file_readable
121
    @attachment.readable? ? true : render_404
122
  end
123
124
  def read_authorize
125
    @attachment.visible? ? true : deny_access
126
  end
127
128
  def delete_authorize
129
    @attachment.deletable? ? true : deny_access
130
  end
131
132
  def detect_content_type(attachment)
133
    content_type = attachment.content_type
134
    if content_type.blank?
135
      content_type = Redmine::MimeType.of(attachment.filename)
136
    end
137
    content_type.to_s
138
  end
139
end