OSDN Git Service

Auto-detect attachment content type when blank (#3782).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Tue, 29 Dec 2009 13:28:30 +0000 (13:28 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Tue, 29 Dec 2009 13:28:30 +0000 (13:28 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3258 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/attachments_controller.rb
app/models/attachment.rb
lib/redmine/mime_type.rb
lib/tasks/migrate_from_trac.rake
test/functional/attachments_controller_test.rb
test/unit/attachment_test.rb

index 92d60ee..92fa01e 100644 (file)
@@ -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
index fe1d6af..e44b162 100644 (file)
@@ -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
index 69b0760..c85ebdc 100644 (file)
@@ -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
index e267a34..8c6838a 100644 (file)
@@ -128,7 +128,7 @@ namespace :redmine do
         end
 
         def content_type
-          Redmine::MimeType.of(filename) || ''
+          ''
         end
 
         def exist?
index bf57349..b64d9c5 100644 (file)
@@ -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
index 7b289d5..8c25adb 100644 (file)
@@ -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]