]> source.dussan.org Git - redmine.git/commitdiff
Attachment preview does not work for some source files such as JavaScript and Go...
authorGo MAEDA <maeda@farend.jp>
Sat, 4 May 2019 09:32:05 +0000 (09:32 +0000)
committerGo MAEDA <maeda@farend.jp>
Sat, 4 May 2019 09:32:05 +0000 (09:32 +0000)
Patch by Go MAEDA with the help of Stephan Wenzel's contribution.

git-svn-id: http://svn.redmine.org/redmine/trunk@18122 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/attachment.rb
lib/redmine/syntax_highlighting.rb
test/fixtures/files/hello.js [new file with mode: 0644]
test/unit/attachment_test.rb
test/unit/lib/redmine/syntax_highlighting/rouge_test.rb

index 45560d0dc19c3cc04f6558d410c8501e880c8e94..362ac1fde2b8af7676b8bd200704591fcbd6b7e6 100644 (file)
@@ -237,7 +237,7 @@ class Attachment < ActiveRecord::Base
   end
 
   def is_text?
-    Redmine::MimeType.is_type?('text', filename)
+    Redmine::MimeType.is_type?('text', filename) || Redmine::SyntaxHighlighting.filename_supported?(filename)
   end
 
   def is_image?
index 6e9aa02a7c879bbb69d35440913de531837048ca..42985317abff95a4af00a3dbd224f29ae2ff4ebc 100644 (file)
@@ -52,6 +52,14 @@ module Redmine
       rescue
         false
       end
+
+      def filename_supported?(filename)
+        if highlighter.respond_to? :filename_supported?
+          highlighter.filename_supported? filename
+        else
+          false
+        end
+      end
     end
 
     module Rouge
@@ -101,7 +109,11 @@ module Redmine
         def language_supported?(language)
           find_lexer(language.to_s.downcase) ? true : false
         end
-        
+
+        def filename_supported?(filename)
+          !::Rouge::Lexer.guesses(:filename => filename).empty?
+        end
+
         private
         # Alias names used by CodeRay and not supported by Rouge
         LANG_ALIASES = {
diff --git a/test/fixtures/files/hello.js b/test/fixtures/files/hello.js
new file mode 100644 (file)
index 0000000..c0380dc
--- /dev/null
@@ -0,0 +1 @@
+document.write('Hello, World!');
index 1ed1a719e07f4c2da2a292f85afd83a0b8419bc2..384ee1fd7765aa30a97b8c384c1787689c3f7385 100644 (file)
@@ -502,4 +502,21 @@ class AttachmentTest < ActiveSupport::TestCase
     puts '(ImageMagick convert not available)'
   end
 
+  def test_is_text
+    js_attachment = Attachment.new(
+      :container => Issue.find(1),
+      :file => uploaded_test_file('hello.js', 'application/javascript'),
+      :author => User.find(1))
+
+    to_test = {
+      js_attachment => true,               # hello.js (application/javascript)
+      attachments(:attachments_003) => false, # logo.gif (image/gif)
+      attachments(:attachments_004) => true,  # source.rb (application/x-ruby)
+      attachments(:attachments_015) => true,  # private.diff (text/x-diff)
+      attachments(:attachments_016) => false, # testfile.png (image/png)
+    }
+    to_test.each do |attachment, expected|
+      assert_equal expected, attachment.is_text?, attachment.inspect
+    end
+  end
 end
index 0bee260e185af1b71e07df94a86584a63200b3d0..862c3afca36a76a09a8495ebed8e6c1bc1f1ed72 100644 (file)
 require File.expand_path('../../../../../test_helper', __FILE__)
 
 class Redmine::SyntaxHighlighting::RougeTest < ActiveSupport::TestCase
+  def test_filename_supported
+    to_test = {
+      'application.js' => true,
+      'Gemfile' => true,
+      'AUTOEXEC.BAT' => false,  # Rouge does not support BAT files
+      'HELLO.C' => true
+    }
+    to_test.each do |filename, expected|
+      assert_equal expected, Redmine::SyntaxHighlighting::Rouge.filename_supported?(filename)
+    end
+  end
+
   def test_highlight_by_filename_should_distinguish_perl_and_prolog
     raw_perl = <<'RAW_PERL'
 #!/usr/bin/perl