Commit a2f4650f authored by Douwe Maan's avatar Douwe Maan

Add BlobViewer::Base#binary? method

parent ec19703a
...@@ -70,7 +70,11 @@ class Blob < SimpleDelegator ...@@ -70,7 +70,11 @@ class Blob < SimpleDelegator
def raw_binary? def raw_binary?
if valid_lfs_pointer? if valid_lfs_pointer?
!rich_viewer&.text_based? if rich_viewer
rich_viewer.binary?
else
true
end
else else
binary? binary?
end end
...@@ -96,22 +100,6 @@ class Blob < SimpleDelegator ...@@ -96,22 +100,6 @@ class Blob < SimpleDelegator
lfs_pointer? && !project.lfs_enabled? lfs_pointer? && !project.lfs_enabled?
end end
def simple_viewer_class
if empty?
BlobViewer::Empty
elsif raw_binary?
BlobViewer::Download
else # text
BlobViewer::Text
end
end
def rich_viewer_class
return if invalid_lfs_pointer? || empty?
rich_viewers_classes.find { |viewer_class| viewer_class.can_render?(self) }
end
def simple_viewer def simple_viewer
@simple_viewer ||= simple_viewer_class.new(self) @simple_viewer ||= simple_viewer_class.new(self)
end end
...@@ -123,7 +111,7 @@ class Blob < SimpleDelegator ...@@ -123,7 +111,7 @@ class Blob < SimpleDelegator
end end
def rendered_as_text?(ignore_errors: true) def rendered_as_text?(ignore_errors: true)
simple_viewer.is_a?(BlobViewer::Text) && (ignore_errors || simple_viewer.render_error.nil?) simple_viewer.text? && (ignore_errors || simple_viewer.render_error.nil?)
end end
def show_viewer_switcher? def show_viewer_switcher?
...@@ -137,13 +125,29 @@ class Blob < SimpleDelegator ...@@ -137,13 +125,29 @@ class Blob < SimpleDelegator
private private
def simple_viewer_class
if empty?
BlobViewer::Empty
elsif raw_binary?
BlobViewer::Download
else # text
BlobViewer::Text
end
end
def rich_viewers_classes def rich_viewers_classes
if valid_lfs_pointer? if valid_lfs_pointer?
RICH_VIEWERS RICH_VIEWERS
elsif binary? elsif binary?
RICH_VIEWERS.reject(&:text_based?) RICH_VIEWERS.select(&:binary?)
else # text else # text
RICH_VIEWERS.select(&:text_based?) RICH_VIEWERS.select(&:text?)
end end
end end
def rich_viewer_class
return if invalid_lfs_pointer? || empty?
rich_viewers_classes.find { |viewer_class| viewer_class.can_render?(self) }
end
end end
...@@ -2,7 +2,7 @@ module BlobViewer ...@@ -2,7 +2,7 @@ module BlobViewer
class Base class Base
class_attribute :partial_name, :type, :extensions, :client_side, :text_based, :switcher_icon, :switcher_title, :max_size, :absolute_max_size class_attribute :partial_name, :type, :extensions, :client_side, :text_based, :switcher_icon, :switcher_title, :max_size, :absolute_max_size
delegate :partial_path, :rich?, :simple?, :client_side?, :server_side?, :text_based?, to: :class delegate :partial_path, :rich?, :simple?, :client_side?, :server_side?, :text?, :binary?, to: :class
attr_reader :blob attr_reader :blob
attr_accessor :override_max_size attr_accessor :override_max_size
...@@ -31,10 +31,14 @@ module BlobViewer ...@@ -31,10 +31,14 @@ module BlobViewer
!client_side? !client_side?
end end
def self.text_based? def self.text?
text_based text_based
end end
def self.binary?
!text?
end
def self.can_render?(blob) def self.can_render?(blob)
!extensions || extensions.include?(blob.extension) !extensions || extensions.include?(blob.extension)
end end
......
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