2008-12-09 19:54:46 +03:00
|
|
|
# Redmine - project management software
|
2012-05-05 16:56:53 +04:00
|
|
|
# Copyright (C) 2006-2012 Jean-Philippe Lang
|
2007-05-26 19:42:37 +04:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-05-18 11:12:27 +04:00
|
|
|
#
|
2007-05-26 19:42:37 +04:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2011-05-18 11:12:27 +04:00
|
|
|
#
|
2007-05-26 19:42:37 +04:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
class AttachmentsController < ApplicationController
|
2012-02-23 14:01:16 +04:00
|
|
|
before_filter :find_project, :except => :upload
|
2012-07-07 17:48:07 +04:00
|
|
|
before_filter :file_readable, :read_authorize, :only => [:show, :download, :thumbnail]
|
2008-12-09 19:54:46 +03:00
|
|
|
before_filter :delete_authorize, :only => :destroy
|
2012-02-23 14:01:16 +04:00
|
|
|
before_filter :authorize_global, :only => :upload
|
2011-05-18 11:12:27 +04:00
|
|
|
|
2012-02-23 14:01:16 +04:00
|
|
|
accept_api_auth :show, :download, :upload
|
2011-05-18 11:12:27 +04:00
|
|
|
|
2008-06-08 22:26:39 +04:00
|
|
|
def show
|
2011-07-19 00:53:10 +04:00
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
if @attachment.is_diff?
|
|
|
|
@diff = File.new(@attachment.diskfile, "rb").read
|
2011-11-20 10:13:26 +04:00
|
|
|
@diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
|
|
|
|
@diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
|
2012-02-04 10:23:38 +04:00
|
|
|
# Save diff type as user preference
|
|
|
|
if User.current.logged? && @diff_type != User.current.pref[:diff_type]
|
|
|
|
User.current.pref[:diff_type] = @diff_type
|
|
|
|
User.current.preference.save
|
|
|
|
end
|
2011-07-19 00:53:10 +04:00
|
|
|
render :action => 'diff'
|
|
|
|
elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
|
|
|
|
@content = File.new(@attachment.diskfile, "rb").read
|
|
|
|
render :action => 'file'
|
|
|
|
else
|
|
|
|
download
|
|
|
|
end
|
|
|
|
}
|
|
|
|
format.api
|
2008-06-08 22:26:39 +04:00
|
|
|
end
|
|
|
|
end
|
2011-05-18 11:12:27 +04:00
|
|
|
|
2007-05-26 19:42:37 +04:00
|
|
|
def download
|
2008-12-30 16:32:51 +03:00
|
|
|
if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project)
|
|
|
|
@attachment.increment_download
|
|
|
|
end
|
2011-05-18 11:12:27 +04:00
|
|
|
|
2012-07-08 19:26:40 +04:00
|
|
|
if stale?(:etag => @attachment.digest)
|
|
|
|
# images are sent inline
|
|
|
|
send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
|
|
|
|
:type => detect_content_type(@attachment),
|
|
|
|
:disposition => (@attachment.image? ? 'inline' : 'attachment')
|
|
|
|
end
|
2007-05-26 19:42:37 +04:00
|
|
|
end
|
2011-05-18 11:12:27 +04:00
|
|
|
|
2012-07-07 17:48:07 +04:00
|
|
|
def thumbnail
|
2012-07-16 21:15:40 +04:00
|
|
|
if @attachment.thumbnailable? && thumbnail = @attachment.thumbnail(:size => params[:size])
|
2012-07-08 19:26:40 +04:00
|
|
|
if stale?(:etag => thumbnail)
|
|
|
|
send_file thumbnail,
|
|
|
|
:filename => filename_for_content_disposition(@attachment.filename),
|
|
|
|
:type => detect_content_type(@attachment),
|
|
|
|
:disposition => 'inline'
|
|
|
|
end
|
2012-07-07 17:48:07 +04:00
|
|
|
else
|
|
|
|
# No thumbnail for the attachment or thumbnail could not be created
|
|
|
|
render :nothing => true, :status => 404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-23 14:01:16 +04:00
|
|
|
def upload
|
|
|
|
# Make sure that API users get used to set this content type
|
|
|
|
# as it won't trigger Rails' automatic parsing of the request body for parameters
|
|
|
|
unless request.content_type == 'application/octet-stream'
|
|
|
|
render :nothing => true, :status => 406
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-05-08 14:11:09 +04:00
|
|
|
@attachment = Attachment.new(:file => request.raw_post)
|
2012-02-23 14:01:16 +04:00
|
|
|
@attachment.author = User.current
|
2012-10-19 01:06:35 +04:00
|
|
|
@attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
|
2012-02-23 14:01:16 +04:00
|
|
|
|
|
|
|
if @attachment.save
|
|
|
|
respond_to do |format|
|
|
|
|
format.api { render :action => 'upload', :status => :created }
|
|
|
|
end
|
|
|
|
else
|
|
|
|
respond_to do |format|
|
|
|
|
format.api { render_validation_errors(@attachment) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-09 19:54:46 +03:00
|
|
|
def destroy
|
2012-03-04 15:41:10 +04:00
|
|
|
if @attachment.container.respond_to?(:init_journal)
|
|
|
|
@attachment.container.init_journal(User.current)
|
|
|
|
end
|
2008-12-09 19:54:46 +03:00
|
|
|
# Make sure association callbacks are called
|
|
|
|
@attachment.container.attachments.delete(@attachment)
|
2012-04-06 20:53:12 +04:00
|
|
|
redirect_to_referer_or project_path(@project)
|
2008-12-09 19:54:46 +03:00
|
|
|
end
|
2011-05-18 11:12:27 +04:00
|
|
|
|
2007-05-26 19:42:37 +04:00
|
|
|
private
|
|
|
|
def find_project
|
|
|
|
@attachment = Attachment.find(params[:id])
|
2008-07-22 21:55:19 +04:00
|
|
|
# Show 404 if the filename in the url is wrong
|
|
|
|
raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
|
2007-05-26 19:42:37 +04:00
|
|
|
@project = @attachment.project
|
2008-07-22 21:20:02 +04:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render_404
|
2007-05-26 19:42:37 +04:00
|
|
|
end
|
2011-05-18 11:12:27 +04:00
|
|
|
|
2009-04-25 13:31:36 +04:00
|
|
|
# Checks that the file exists and is readable
|
|
|
|
def file_readable
|
|
|
|
@attachment.readable? ? true : render_404
|
|
|
|
end
|
2011-05-18 11:12:27 +04:00
|
|
|
|
2008-12-09 19:54:46 +03:00
|
|
|
def read_authorize
|
|
|
|
@attachment.visible? ? true : deny_access
|
|
|
|
end
|
2011-05-18 11:12:27 +04:00
|
|
|
|
2008-12-09 19:54:46 +03:00
|
|
|
def delete_authorize
|
|
|
|
@attachment.deletable? ? true : deny_access
|
|
|
|
end
|
2011-05-18 11:12:27 +04:00
|
|
|
|
2009-12-29 16:28:30 +03:00
|
|
|
def detect_content_type(attachment)
|
|
|
|
content_type = attachment.content_type
|
|
|
|
if content_type.blank?
|
|
|
|
content_type = Redmine::MimeType.of(attachment.filename)
|
|
|
|
end
|
2010-01-05 21:16:03 +03:00
|
|
|
content_type.to_s
|
2009-12-29 16:28:30 +03:00
|
|
|
end
|
2007-05-26 19:42:37 +04:00
|
|
|
end
|