diff options
author | Sean McGivern <sean@mcgivern.me.uk> | 2017-06-09 10:10:44 +0000 |
---|---|---|
committer | Sean McGivern <sean@mcgivern.me.uk> | 2017-06-09 10:10:44 +0000 |
commit | 66aad16d1aaa426237e52ef19385385566f194dc (patch) | |
tree | 47784b6a677d4f8d1fd82bc435e0ea6c9380d775 /app | |
parent | 288e8c7c42bd9507725637dcf7e5b8e0eaa5229c (diff) | |
parent | 370bc86fb007c1683495bdf4082bf5442b517895 (diff) | |
download | gitlab-ce-66aad16d1aaa426237e52ef19385385566f194dc.tar.gz |
Merge branch 'dm-blob-binaryness-change' into 'master'
Detect if blob binaryness changes after loading all data
See merge request !11981
Diffstat (limited to 'app')
-rw-r--r-- | app/models/blob.rb | 7 | ||||
-rw-r--r-- | app/models/blob_viewer/base.rb | 14 | ||||
-rw-r--r-- | app/views/projects/blob/_viewer.html.haml | 14 |
3 files changed, 22 insertions, 13 deletions
diff --git a/app/models/blob.rb b/app/models/blob.rb index 3869e79ba75..954d4e4d779 100644 --- a/app/models/blob.rb +++ b/app/models/blob.rb @@ -191,9 +191,12 @@ class Blob < SimpleDelegator rendered_as_text? && rich_viewer end + def expanded? + !!@expanded + end + def expand! - simple_viewer&.expanded = true - rich_viewer&.expanded = true + @expanded = true end private diff --git a/app/models/blob_viewer/base.rb b/app/models/blob_viewer/base.rb index d2aa33d994e..35965d01692 100644 --- a/app/models/blob_viewer/base.rb +++ b/app/models/blob_viewer/base.rb @@ -6,15 +6,15 @@ module BlobViewer self.loading_partial_name = 'loading' - delegate :partial_path, :loading_partial_path, :rich?, :simple?, :text?, :binary?, to: :class + delegate :partial_path, :loading_partial_path, :rich?, :simple?, :load_async?, :text?, :binary?, to: :class attr_reader :blob - attr_accessor :expanded delegate :project, to: :blob def initialize(blob) @blob = blob + @initially_binary = blob.binary? end def self.partial_path @@ -57,14 +57,10 @@ module BlobViewer false end - def load_async? - self.class.load_async? && render_error.nil? - end - def collapsed? return @collapsed if defined?(@collapsed) - @collapsed = !expanded && collapse_limit && blob.raw_size > collapse_limit + @collapsed = !blob.expanded? && collapse_limit && blob.raw_size > collapse_limit end def too_large? @@ -73,6 +69,10 @@ module BlobViewer @too_large = size_limit && blob.raw_size > size_limit end + def binary_detected_after_load? + !@initially_binary && blob.binary? + end + # This method is used on the server side to check whether we can attempt to # render the blob at all. Human-readable error messages are found in the # `BlobHelper#blob_render_error_reason` helper. diff --git a/app/views/projects/blob/_viewer.html.haml b/app/views/projects/blob/_viewer.html.haml index 4252f27d007..013f1c267c8 100644 --- a/app/views/projects/blob/_viewer.html.haml +++ b/app/views/projects/blob/_viewer.html.haml @@ -1,13 +1,19 @@ - hidden = local_assigns.fetch(:hidden, false) - render_error = viewer.render_error -- load_async = local_assigns.fetch(:load_async, viewer.load_async?) +- load_async = local_assigns.fetch(:load_async, viewer.load_async? && render_error.nil?) - viewer_url = local_assigns.fetch(:viewer_url) { url_for(params.merge(viewer: viewer.type, format: :json)) } if load_async .blob-viewer{ data: { type: viewer.type, url: viewer_url }, class: ('hidden' if hidden) } - - if load_async - = render viewer.loading_partial_path, viewer: viewer - - elsif render_error + - if render_error = render 'projects/blob/render_error', viewer: viewer + - elsif load_async + = render viewer.loading_partial_path, viewer: viewer - else - viewer.prepare! + + -# In the rare case where the first kilobyte of the file looks like text, + -# but the file turns out to actually be binary after loading all data, + -# we fall back on the binary Download viewer. + - viewer = BlobViewer::Download.new(viewer.blob) if viewer.binary_detected_after_load? + = render viewer.partial_path, viewer: viewer |