2017-04-13 17:14:08 +00:00
|
|
|
module BlobViewer
|
|
|
|
class Base
|
|
|
|
class_attribute :partial_name, :type, :extensions, :client_side, :text_based, :switcher_icon, :switcher_title, :max_size, :absolute_max_size
|
|
|
|
|
2017-04-24 14:27:19 +00:00
|
|
|
delegate :partial_path, :rich?, :simple?, :client_side?, :server_side?, :text?, :binary?, to: :class
|
2017-04-13 17:14:08 +00:00
|
|
|
|
|
|
|
attr_reader :blob
|
2017-04-21 18:59:34 +00:00
|
|
|
attr_accessor :override_max_size
|
2017-04-13 17:14:08 +00:00
|
|
|
|
|
|
|
def initialize(blob)
|
|
|
|
@blob = blob
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.partial_path
|
|
|
|
"projects/blob/viewers/#{partial_name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.rich?
|
|
|
|
type == :rich
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.simple?
|
|
|
|
type == :simple
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.client_side?
|
|
|
|
client_side
|
|
|
|
end
|
|
|
|
|
2017-04-21 18:22:04 +00:00
|
|
|
def self.server_side?
|
2017-04-13 17:14:08 +00:00
|
|
|
!client_side?
|
|
|
|
end
|
|
|
|
|
2017-04-24 14:27:19 +00:00
|
|
|
def self.text?
|
2017-04-13 17:14:08 +00:00
|
|
|
text_based
|
|
|
|
end
|
|
|
|
|
2017-04-24 14:27:19 +00:00
|
|
|
def self.binary?
|
|
|
|
!text?
|
|
|
|
end
|
|
|
|
|
2017-04-13 17:14:08 +00:00
|
|
|
def self.can_render?(blob)
|
|
|
|
!extensions || extensions.include?(blob.extension)
|
|
|
|
end
|
|
|
|
|
2017-04-21 18:59:34 +00:00
|
|
|
def too_large?
|
|
|
|
blob.raw_size > max_size
|
2017-04-13 17:14:08 +00:00
|
|
|
end
|
|
|
|
|
2017-04-21 18:59:34 +00:00
|
|
|
def absolutely_too_large?
|
|
|
|
blob.raw_size > absolute_max_size
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_override_max_size?
|
|
|
|
too_large? && !absolutely_too_large?
|
2017-04-13 17:14:08 +00:00
|
|
|
end
|
|
|
|
|
2017-04-21 18:59:34 +00:00
|
|
|
def render_error
|
|
|
|
if override_max_size ? absolutely_too_large? : too_large?
|
2017-04-13 17:14:08 +00:00
|
|
|
:too_large
|
|
|
|
elsif server_side_but_stored_in_lfs?
|
|
|
|
:server_side_but_stored_in_lfs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare!
|
|
|
|
if server_side? && blob.project
|
|
|
|
blob.load_all_data!(blob.project.repository)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def server_side_but_stored_in_lfs?
|
2017-04-20 16:48:19 +00:00
|
|
|
server_side? && blob.valid_lfs_pointer?
|
2017-04-13 17:14:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|