Commit ff7eb7b2 authored by Jean-Philippe Lang's avatar Jean-Philippe Lang

Auto-detect attachment content type when blank (#3782).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3258 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent b7b4e278
......@@ -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
......@@ -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
......
# 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
......
......@@ -128,7 +128,7 @@ namespace :redmine do
end
def content_type
Redmine::MimeType.of(filename) || ''
''
end
def exist?
......
......@@ -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
......
......@@ -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]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment