##// END OF EJS Templates
Auto-detect attachment content type when blank (#3782)....
Jean-Philippe Lang -
r3144:ff7eb7b23a6a
parent child
Show More
@@ -41,7 +41,7 class AttachmentsController < ApplicationController
41
41
42 # images are sent inline
42 # images are sent inline
43 send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
43 send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
44 :type => @attachment.content_type,
44 :type => detect_content_type(@attachment),
45 :disposition => (@attachment.image? ? 'inline' : 'attachment')
45 :disposition => (@attachment.image? ? 'inline' : 'attachment')
46
46
47 end
47 end
@@ -76,4 +76,12 private
76 def delete_authorize
76 def delete_authorize
77 @attachment.deletable? ? true : deny_access
77 @attachment.deletable? ? true : deny_access
78 end
78 end
79
80 def detect_content_type(attachment)
81 content_type = attachment.content_type
82 if content_type.blank?
83 content_type = Redmine::MimeType.of(attachment.filename)
84 end
85 content_type
86 end
79 end
87 end
@@ -58,6 +58,9 class Attachment < ActiveRecord::Base
58 self.filename = sanitize_filename(@temp_file.original_filename)
58 self.filename = sanitize_filename(@temp_file.original_filename)
59 self.disk_filename = Attachment.disk_filename(filename)
59 self.disk_filename = Attachment.disk_filename(filename)
60 self.content_type = @temp_file.content_type.to_s.chomp
60 self.content_type = @temp_file.content_type.to_s.chomp
61 if content_type.blank?
62 self.content_type = Redmine::MimeType.of(filename)
63 end
61 self.filesize = @temp_file.size
64 self.filesize = @temp_file.size
62 end
65 end
63 end
66 end
@@ -1,5 +1,5
1 # redMine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
@@ -36,6 +36,7 module Redmine
36 'text/x-sh' => 'sh',
36 'text/x-sh' => 'sh',
37 'text/xml' => 'xml,xsd,mxml',
37 'text/xml' => 'xml,xsd,mxml',
38 'text/yaml' => 'yml,yaml',
38 'text/yaml' => 'yml,yaml',
39 'text/csv' => 'csv',
39 'image/gif' => 'gif',
40 'image/gif' => 'gif',
40 'image/jpeg' => 'jpg,jpeg,jpe',
41 'image/jpeg' => 'jpg,jpeg,jpe',
41 'image/png' => 'png',
42 'image/png' => 'png',
@@ -43,6 +44,20 module Redmine
43 'image/x-ms-bmp' => 'bmp',
44 'image/x-ms-bmp' => 'bmp',
44 'image/x-xpixmap' => 'xpm',
45 'image/x-xpixmap' => 'xpm',
45 'application/pdf' => 'pdf',
46 'application/pdf' => 'pdf',
47 'application/rtf' => 'rtf',
48 'application/msword' => 'doc',
49 'application/vnd.ms-excel' => 'xls',
50 'application/vnd.ms-powerpoint' => 'ppt,pps',
51 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
52 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
53 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
54 'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => 'ppsx',
55 'application/vnd.oasis.opendocument.spreadsheet' => 'ods',
56 'application/vnd.oasis.opendocument.text' => 'odt',
57 'application/vnd.oasis.opendocument.presentation' => 'odp',
58 'application/x-7z-compressed' => '7z',
59 'application/x-rar-compressed' => 'rar',
60 'application/x-tar' => 'tar',
46 'application/zip' => 'zip',
61 'application/zip' => 'zip',
47 'application/x-gzip' => 'gz',
62 'application/x-gzip' => 'gz',
48 }.freeze
63 }.freeze
@@ -128,7 +128,7 namespace :redmine do
128 end
128 end
129
129
130 def content_type
130 def content_type
131 Redmine::MimeType.of(filename) || ''
131 ''
132 end
132 end
133
133
134 def exist?
134 def exist?
@@ -84,6 +84,14 class AttachmentsControllerTest < ActionController::TestCase
84 assert_equal 'application/x-ruby', @response.content_type
84 assert_equal 'application/x-ruby', @response.content_type
85 end
85 end
86
86
87 def test_download_should_assign_content_type_if_blank
88 Attachment.find(4).update_attribute(:content_type, '')
89
90 get :download, :id => 4
91 assert_response :success
92 assert_equal 'text/x-ruby', @response.content_type
93 end
94
87 def test_download_missing_file
95 def test_download_missing_file
88 get :download, :id => 2
96 get :download, :id => 2
89 assert_response 404
97 assert_response 404
@@ -38,6 +38,14 class AttachmentTest < ActiveSupport::TestCase
38 assert File.exist?(a.diskfile)
38 assert File.exist?(a.diskfile)
39 end
39 end
40
40
41 def test_create_should_auto_assign_content_type
42 a = Attachment.new(:container => Issue.find(1),
43 :file => uploaded_test_file("testfile.txt", ""),
44 :author => User.find(1))
45 assert a.save
46 assert_equal 'text/plain', a.content_type
47 end
48
41 def test_diskfilename
49 def test_diskfilename
42 assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
50 assert Attachment.disk_filename("test_file.txt") =~ /^\d{12}_test_file.txt$/
43 assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
51 assert_equal 'test_file.txt', Attachment.disk_filename("test_file.txt")[13..-1]
General Comments 0
You need to be logged in to leave comments. Login now