diff --git a/lib/onebox/mixins/git_blob_onebox.rb b/lib/onebox/mixins/git_blob_onebox.rb index 0c9b2bda128..ff72256f516 100644 --- a/lib/onebox/mixins/git_blob_onebox.rb +++ b/lib/onebox/mixins/git_blob_onebox.rb @@ -169,6 +169,12 @@ module Onebox else contents = URI.parse(self.raw_template(m)).open(read_timeout: timeout).read + if contents.encoding == Encoding::BINARY + @raw = nil + @binary = true + return + end + contents_lines = contents.lines #get contents lines contents_lines_size = contents_lines.size #get number of lines @@ -211,6 +217,7 @@ module Onebox # as *side effects* of the `raw` method! They must all appear # AFTER the call to `raw`! Don't get bitten by this like I did! content: raw, + binary: @binary, lang: "lang-#{@lang}", lines: @selected_lines_array , has_lines: !@selected_lines_array.nil?, diff --git a/lib/onebox/templates/githubblob.mustache b/lib/onebox/templates/githubblob.mustache index 810c9ce6622..bfffc2555b7 100644 --- a/lib/onebox/templates/githubblob.mustache +++ b/lib/onebox/templates/githubblob.mustache @@ -1,53 +1,59 @@

{{title}}

-{{^has_lines}} - {{#model_file}} - - {{/model_file}} +{{^binary}} + {{^has_lines}} + {{#model_file}} + + {{/model_file}} - {{^model_file}} -
{{content}}
- {{/model_file}} -{{/has_lines}} + {{^model_file}} +
{{content}}
+ {{/model_file}} + {{/has_lines}} -{{#has_lines}} - {{! This is a template comment | Sample rules for this box - - }} + } + pre.onebox code li.selected{ + background-color:#cfc + } + + }} -
-    
-      
    - {{#lines}} - {{data}} - {{/lines}} -
-
-
-{{/has_lines}} +
+      
+        
    + {{#lines}} + {{data}} + {{/lines}} +
+
+
+ {{/has_lines}} +{{/binary}} + +{{#binary}} + This file is binary. show original +{{/binary}} {{#truncated}} This file has been truncated. show original diff --git a/spec/lib/onebox/engine/github_blob_onebox_spec.rb b/spec/lib/onebox/engine/github_blob_onebox_spec.rb index 610f6b7d86f..9e813e6cc97 100644 --- a/spec/lib/onebox/engine/github_blob_onebox_spec.rb +++ b/spec/lib/onebox/engine/github_blob_onebox_spec.rb @@ -5,6 +5,7 @@ require "rails_helper" describe Onebox::Engine::GithubBlobOnebox do before do @link = "https://github.com/discourse/onebox/blob/master/lib/onebox/engine/github_blob_onebox.rb" + @uri = URI.parse(@link) stub_request(:get, "https://raw.githubusercontent.com/discourse/onebox/master/lib/onebox/engine/github_blob_onebox.rb") .to_return(status: 200, body: onebox_response(described_class.onebox_name)) end @@ -20,5 +21,16 @@ describe Onebox::Engine::GithubBlobOnebox do it "includes blob contents" do expect(html).to include("module Oneboxer") end + + it "does not include blob contents if it is binary" do + # stub_request if the response must be binary (ASCII-8BIT) + uri = mock('object') + uri.stubs(:open).returns(File.open("#{Rails.root}/spec/fixtures/pdf/small.pdf", "rb")) + URI.stubs(:parse).with(@link).returns(@uri) + URI.stubs(:parse).with('https://raw.githubusercontent.com/discourse/onebox/master/lib/onebox/engine/github_blob_onebox.rb').returns(uri) + + expect(html).not_to include('/Pages') + expect(html).to include('This file is binary.') + end end end