2017-04-13 17:14:08 +00:00
|
|
|
module BlobViewer
|
|
|
|
class Base
|
2017-05-08 23:50:23 +00:00
|
|
|
PARTIAL_PATH_PREFIX = 'projects/blob/viewers'.freeze
|
2017-04-13 17:14:08 +00:00
|
|
|
|
2017-05-12 19:31:29 +00:00
|
|
|
class_attribute :partial_name, :loading_partial_name, :type, :extensions, :file_types, :client_side, :binary, :switcher_icon, :switcher_title, :max_size, :absolute_max_size
|
2017-05-08 23:50:23 +00:00
|
|
|
|
|
|
|
self.loading_partial_name = 'loading'
|
|
|
|
|
|
|
|
delegate :partial_path, :loading_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
|
2017-05-08 23:50:23 +00: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 17:14:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.rich?
|
|
|
|
type == :rich
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.simple?
|
|
|
|
type == :simple
|
|
|
|
end
|
|
|
|
|
2017-05-08 23:50:23 +00:00
|
|
|
def self.auxiliary?
|
|
|
|
type == :auxiliary
|
|
|
|
end
|
|
|
|
|
2017-04-13 17:14:08 +00:00
|
|
|
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 17:51:56 +00:00
|
|
|
def self.binary?
|
|
|
|
binary
|
2017-04-13 17:14:08 +00:00
|
|
|
end
|
|
|
|
|
2017-04-24 17:51:56 +00:00
|
|
|
def self.text?
|
|
|
|
!binary?
|
2017-04-24 14:27:19 +00:00
|
|
|
end
|
|
|
|
|
2017-05-08 23:50:23 +00:00
|
|
|
def self.can_render?(blob, verify_binary: true)
|
|
|
|
return false if verify_binary && binary? != blob.binary?
|
|
|
|
return true if extensions&.include?(blob.extension)
|
2017-05-12 19:31:29 +00:00
|
|
|
return true if file_types&.include?(Gitlab::FileDetector.type_of(blob.path))
|
2017-05-08 23:50:23 +00:00
|
|
|
|
|
|
|
false
|
2017-04-13 17:14:08 +00:00
|
|
|
end
|
|
|
|
|
2017-04-21 18:59:34 +00:00
|
|
|
def too_large?
|
2017-05-12 19:31:29 +00:00
|
|
|
max_size && 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?
|
2017-05-12 19:31:29 +00:00
|
|
|
absolute_max_size && blob.raw_size > absolute_max_size
|
2017-04-21 18:59:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_override_max_size?
|
|
|
|
too_large? && !absolutely_too_large?
|
2017-04-13 17:14:08 +00:00
|
|
|
end
|
|
|
|
|
2017-04-26 20:29:12 +00:00
|
|
|
# This method is used on the server side to check whether we can attempt to
|
2017-04-27 13:42:32 +00:00
|
|
|
# render the blob at all. Human-readable error messages are found in the
|
2017-04-26 20:29:12 +00: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
|
|
|
|
# binary from `blob_raw_url` and does its own format validation and error
|
|
|
|
# rendering, especially for potentially large binary formats.
|
2017-04-21 18:59:34 +00:00
|
|
|
def render_error
|
2017-04-26 20:48:49 +00:00
|
|
|
return @render_error if defined?(@render_error)
|
|
|
|
|
|
|
|
@render_error =
|
2017-05-02 22:45:50 +00:00
|
|
|
if server_side_but_stored_externally?
|
|
|
|
# Files that are not stored in the repository, like LFS files and
|
|
|
|
# build artifacts, can only be rendered using a client-side viewer,
|
2017-04-26 20:48:49 +00:00
|
|
|
# since we do not want to read large amounts of data into memory on the
|
|
|
|
# server side. Client-side viewers use JS and can fetch the file from
|
|
|
|
# `blob_raw_url` using AJAX.
|
2017-05-02 22:45:50 +00:00
|
|
|
:server_side_but_stored_externally
|
2017-04-26 20:48:49 +00:00
|
|
|
elsif override_max_size ? absolutely_too_large? : too_large?
|
|
|
|
:too_large
|
|
|
|
end
|
2017-04-13 17:14:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def prepare!
|
2017-05-08 23:50:23 +00:00
|
|
|
# To be overridden by subclasses
|
2017-04-13 17:14:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-05-02 22:45:50 +00:00
|
|
|
def server_side_but_stored_externally?
|
|
|
|
server_side? && blob.stored_externally?
|
2017-04-13 17:14:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|