diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 92d60ee0..92fa01e8 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -41,7 +41,7 @@ class AttachmentsController < ApplicationController # 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 @@ -76,4 +76,12 @@ private 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 + end end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index fe1d6afa..e44b162a 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -58,6 +58,9 @@ class Attachment < ActiveRecord::Base self.filename = sanitize_filename(@temp_file.original_filename) self.disk_filename = Attachment.disk_filename(filename) self.content_type = @temp_file.content_type.to_s.chomp + if content_type.blank? + self.content_type = Redmine::MimeType.of(filename) + end self.filesize = @temp_file.size end end diff --git a/lib/redmine/mime_type.rb b/lib/redmine/mime_type.rb index 69b0760b..c85ebdcb 100644 --- a/lib/redmine/mime_type.rb +++ b/lib/redmine/mime_type.rb @@ -1,5 +1,5 @@ -# 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 @@ -36,6 +36,7 @@ module Redmine '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', @@ -43,6 +44,20 @@ module Redmine '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 diff --git a/lib/tasks/migrate_from_trac.rake b/lib/tasks/migrate_from_trac.rake index e267a340..8c6838ae 100644 --- a/lib/tasks/migrate_from_trac.rake +++ b/lib/tasks/migrate_from_trac.rake @@ -128,7 +128,7 @@ namespace :redmine do end def content_type - Redmine::MimeType.of(filename) || '' + '' end def exist? diff --git a/test/functional/attachments_controller_test.rb b/test/functional/attachments_controller_test.rb index bf57349f..b64d9c51 100644 --- a/test/functional/attachments_controller_test.rb +++ b/test/functional/attachments_controller_test.rb @@ -84,6 +84,14 @@ class AttachmentsControllerTest < ActionController::TestCase 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 diff --git a/test/unit/attachment_test.rb b/test/unit/attachment_test.rb index 7b289d5c..8c25adb4 100644 --- a/test/unit/attachment_test.rb +++ b/test/unit/attachment_test.rb @@ -38,6 +38,14 @@ class AttachmentTest < ActiveSupport::TestCase 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]