# images are sent inline
send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
- :type => @attachment.content_type,
+ :type => detect_content_type(@attachment),
:disposition => (@attachment.image? ? 'inline' : 'attachment')
end
def delete_authorize
@attachment.deletable? ? true : deny_access
end
+
+ def detect_content_type(attachment)
+ content_type = attachment.content_type
+ if content_type.blank?
+ content_type = Redmine::MimeType.of(attachment.filename)
+ end
+ content_type.to_s
+ end
end
-# redMine - project management software
-# Copyright (C) 2006-2007 Jean-Philippe Lang
+# Redmine - project management software
+# Copyright (C) 2006-2009 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
'text/x-sh' => 'sh',
'text/xml' => 'xml,xsd,mxml',
'text/yaml' => 'yml,yaml',
+ 'text/csv' => 'csv',
'image/gif' => 'gif',
'image/jpeg' => 'jpg,jpeg,jpe',
'image/png' => 'png',
'image/x-ms-bmp' => 'bmp',
'image/x-xpixmap' => 'xpm',
'application/pdf' => 'pdf',
+ 'application/rtf' => 'rtf',
+ 'application/msword' => 'doc',
+ 'application/vnd.ms-excel' => 'xls',
+ 'application/vnd.ms-powerpoint' => 'ppt,pps',
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
+ 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
+ 'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => 'ppsx',
+ 'application/vnd.oasis.opendocument.spreadsheet' => 'ods',
+ 'application/vnd.oasis.opendocument.text' => 'odt',
+ 'application/vnd.oasis.opendocument.presentation' => 'odp',
+ 'application/x-7z-compressed' => '7z',
+ 'application/x-rar-compressed' => 'rar',
+ 'application/x-tar' => 'tar',
'application/zip' => 'zip',
'application/x-gzip' => 'gz',
}.freeze
assert_equal 'application/x-ruby', @response.content_type
end
+ def test_download_should_assign_content_type_if_blank
+ Attachment.find(4).update_attribute(:content_type, '')
+
+ get :download, :id => 4
+ assert_response :success
+ assert_equal 'text/x-ruby', @response.content_type
+ end
+
def test_download_missing_file
get :download, :id => 2
assert_response 404
assert File.exist?(a.diskfile)
end
+ def test_create_should_auto_assign_content_type
+ a = Attachment.new(:container => Issue.find(1),
+ :file => uploaded_test_file("testfile.txt", ""),
+ :author => User.find(1))
+ assert a.save
+ assert_equal 'text/plain', a.content_type
+ end
+
def test_diskfilename
assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]