2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module BlobViewer
|
|
|
|
class Base
|
2017-09-10 17:25:29 +05:30
|
|
|
PARTIAL_PATH_PREFIX = 'projects/blob/viewers'.freeze
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
class_attribute :partial_name, :loading_partial_name, :type, :extensions, :file_types, :load_async, :binary, :switcher_icon, :switcher_title, :collapse_limit, :size_limit
|
|
|
|
|
|
|
|
self.loading_partial_name = 'loading'
|
|
|
|
|
|
|
|
delegate :partial_path, :loading_partial_path, :rich?, :simple?, :load_async?, :text?, :binary?, to: :class
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
attr_reader :blob
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
delegate :project, to: :blob
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
def initialize(blob)
|
|
|
|
@blob = blob
|
2017-09-10 17:25:29 +05:30
|
|
|
@initially_binary = blob.binary?
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def self.partial_path
|
2017-09-10 17:25:29 +05:30
|
|
|
File.join(PARTIAL_PATH_PREFIX, partial_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.loading_partial_path
|
|
|
|
File.join(PARTIAL_PATH_PREFIX, loading_partial_name)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def self.rich?
|
|
|
|
type == :rich
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.simple?
|
|
|
|
type == :simple
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def self.auxiliary?
|
|
|
|
type == :auxiliary
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def self.load_async?
|
|
|
|
load_async
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def self.binary?
|
|
|
|
binary
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.text?
|
|
|
|
!binary?
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def self.can_render?(blob, verify_binary: true)
|
|
|
|
return false if verify_binary && binary? != blob.binary?
|
|
|
|
return true if extensions&.include?(blob.extension)
|
|
|
|
return true if file_types&.include?(blob.file_type)
|
|
|
|
|
|
|
|
false
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def collapsed?
|
|
|
|
return @collapsed if defined?(@collapsed)
|
|
|
|
|
|
|
|
@collapsed = !blob.expanded? && collapse_limit && blob.raw_size > collapse_limit
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def too_large?
|
|
|
|
return @too_large if defined?(@too_large)
|
|
|
|
|
|
|
|
@too_large = size_limit && blob.raw_size > size_limit
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def binary_detected_after_load?
|
|
|
|
!@initially_binary && blob.binary?
|
2017-08-17 22:00:37 +05:30
|
|
|
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.
|
|
|
|
#
|
|
|
|
# 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-09-10 17:25:29 +05:30
|
|
|
# binary from `blob_raw_path` and does its own format validation and error
|
2017-08-17 22:00:37 +05:30
|
|
|
# rendering, especially for potentially large binary formats.
|
|
|
|
def render_error
|
2017-09-10 17:25:29 +05:30
|
|
|
if too_large?
|
|
|
|
:too_large
|
|
|
|
elsif collapsed?
|
|
|
|
:collapsed
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def prepare!
|
|
|
|
# To be overridden by subclasses
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|