2018-08-03 03:15:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-04-13 13:14:08 -04:00
|
|
|
module BlobViewer
|
|
|
|
class Base
|
2019-08-31 15:57:00 -04:00
|
|
|
PARTIAL_PATH_PREFIX = 'projects/blob/viewers'
|
2017-04-13 13:14:08 -04:00
|
|
|
|
2017-05-26 19:27:30 -04:00
|
|
|
class_attribute :partial_name, :loading_partial_name, :type, :extensions, :file_types, :load_async, :binary, :switcher_icon, :switcher_title, :collapse_limit, :size_limit
|
2017-05-08 19:50:23 -04:00
|
|
|
|
|
|
|
self.loading_partial_name = 'loading'
|
|
|
|
|
2017-06-07 17:07:57 -04:00
|
|
|
delegate :partial_path, :loading_partial_path, :rich?, :simple?, :load_async?, :text?, :binary?, to: :class
|
2017-04-13 13:14:08 -04:00
|
|
|
|
|
|
|
attr_reader :blob
|
|
|
|
|
2017-05-17 14:00:13 -04:00
|
|
|
delegate :project, to: :blob
|
|
|
|
|
2017-04-13 13:14:08 -04:00
|
|
|
def initialize(blob)
|
|
|
|
@blob = blob
|
2018-12-13 12:49:05 -05:00
|
|
|
@initially_binary = blob.binary_in_repo?
|
2017-04-13 13:14:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.partial_path
|
2017-05-08 19:50:23 -04:00
|
|
|
File.join(PARTIAL_PATH_PREFIX, partial_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.loading_partial_path
|
|
|
|
File.join(PARTIAL_PATH_PREFIX, loading_partial_name)
|
2017-04-13 13:14:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.rich?
|
|
|
|
type == :rich
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.simple?
|
|
|
|
type == :simple
|
|
|
|
end
|
|
|
|
|
2017-05-08 19:50:23 -04:00
|
|
|
def self.auxiliary?
|
|
|
|
type == :auxiliary
|
|
|
|
end
|
|
|
|
|
2017-05-12 15:43:06 -04:00
|
|
|
def self.load_async?
|
|
|
|
load_async
|
2017-04-13 13:14:08 -04:00
|
|
|
end
|
|
|
|
|
2017-04-24 13:51:56 -04:00
|
|
|
def self.binary?
|
|
|
|
binary
|
2017-04-13 13:14:08 -04:00
|
|
|
end
|
|
|
|
|
2017-04-24 13:51:56 -04:00
|
|
|
def self.text?
|
|
|
|
!binary?
|
2017-04-24 10:27:19 -04:00
|
|
|
end
|
|
|
|
|
2017-05-08 19:50:23 -04:00
|
|
|
def self.can_render?(blob, verify_binary: true)
|
2018-12-13 12:49:05 -05:00
|
|
|
return false if verify_binary && binary? != blob.binary_in_repo?
|
2017-05-08 19:50:23 -04:00
|
|
|
return true if extensions&.include?(blob.extension)
|
2017-06-06 17:26:31 -04:00
|
|
|
return true if file_types&.include?(blob.file_type)
|
2017-05-08 19:50:23 -04:00
|
|
|
|
|
|
|
false
|
2017-04-13 13:14:08 -04:00
|
|
|
end
|
|
|
|
|
2017-05-26 19:27:30 -04:00
|
|
|
def collapsed?
|
|
|
|
return @collapsed if defined?(@collapsed)
|
2017-04-21 14:59:34 -04:00
|
|
|
|
2017-06-07 17:07:57 -04:00
|
|
|
@collapsed = !blob.expanded? && collapse_limit && blob.raw_size > collapse_limit
|
2017-05-15 11:03:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def too_large?
|
2017-05-26 19:27:30 -04:00
|
|
|
return @too_large if defined?(@too_large)
|
|
|
|
|
|
|
|
@too_large = size_limit && blob.raw_size > size_limit
|
2017-04-13 13:14:08 -04:00
|
|
|
end
|
|
|
|
|
2017-06-07 17:07:57 -04:00
|
|
|
def binary_detected_after_load?
|
2018-12-13 12:49:05 -05:00
|
|
|
!@initially_binary && blob.binary_in_repo?
|
2017-06-07 17:07:57 -04:00
|
|
|
end
|
|
|
|
|
2017-04-26 16:29:12 -04:00
|
|
|
# This method is used on the server side to check whether we can attempt to
|
2017-04-27 09:42:32 -04:00
|
|
|
# render the blob at all. Human-readable error messages are found in the
|
2017-04-26 16:29:12 -04:00
|
|
|
# `BlobHelper#blob_render_error_reason` helper.
|
|
|
|
#
|
|
|
|
# This method does not and should not load the entire blob contents into
|
|
|
|
# memory, and should not be overridden to do so in order to validate the
|
|
|
|
# format of the blob.
|
|
|
|
#
|
|
|
|
# Prefer to implement a client-side viewer, where the JS component loads the
|
2017-08-03 08:29:35 -04:00
|
|
|
# binary from `blob_raw_path` and does its own format validation and error
|
2017-04-26 16:29:12 -04:00
|
|
|
# rendering, especially for potentially large binary formats.
|
2017-04-21 14:59:34 -04:00
|
|
|
def render_error
|
2017-05-15 11:03:34 -04:00
|
|
|
if too_large?
|
2017-05-12 15:43:06 -04:00
|
|
|
:too_large
|
2017-05-26 19:27:30 -04:00
|
|
|
elsif collapsed?
|
|
|
|
:collapsed
|
2017-05-12 15:43:06 -04:00
|
|
|
end
|
2017-04-13 13:14:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def prepare!
|
2017-05-08 19:50:23 -04:00
|
|
|
# To be overridden by subclasses
|
2017-04-13 13:14:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|