2016-02-18 18:19:43 -05:00
|
|
|
# Blob is a Rails-specific wrapper around Gitlab::Git::Blob objects
|
|
|
|
class Blob < SimpleDelegator
|
2016-03-07 08:27:53 -05:00
|
|
|
CACHE_TIME = 60 # Cache raw blobs referred to by a (mutable) ref for 1 minute
|
|
|
|
CACHE_TIME_IMMUTABLE = 3600 # Cache blobs referred to by an immutable reference for 1 hour
|
|
|
|
|
2017-04-13 13:14:08 -04:00
|
|
|
MAXIMUM_TEXT_HIGHLIGHT_SIZE = 1.megabyte
|
|
|
|
|
2017-04-26 16:16:38 -04:00
|
|
|
# Finding a viewer for a blob happens based only on extension and whether the
|
|
|
|
# blob is binary or text, which means 1 blob should only be matched by 1 viewer,
|
|
|
|
# and the order of these viewers doesn't really matter.
|
|
|
|
#
|
|
|
|
# However, when the blob is an LFS pointer, we cannot know for sure whether the
|
|
|
|
# file being pointed to is binary or text. In this case, we match only on
|
|
|
|
# extension, preferring binary viewers over text ones if both exist, since the
|
|
|
|
# large files referred to in "Large File Storage" are much more likely to be
|
|
|
|
# binary than text.
|
|
|
|
#
|
|
|
|
# `.stl` files, for example, exist in both binary and text forms, and are
|
|
|
|
# handled by different viewers (`BinarySTL` and `TextSTL`) depending on blob
|
|
|
|
# type. LFS pointers to `.stl` files are assumed to always be the binary kind,
|
|
|
|
# and use the `BinarySTL` viewer.
|
2017-04-13 13:14:08 -04:00
|
|
|
RICH_VIEWERS = [
|
2017-04-26 16:16:38 -04:00
|
|
|
BlobViewer::Markup,
|
|
|
|
BlobViewer::Notebook,
|
|
|
|
BlobViewer::SVG,
|
|
|
|
|
2017-04-13 13:21:07 -04:00
|
|
|
BlobViewer::Image,
|
|
|
|
BlobViewer::Sketch,
|
2017-04-28 06:32:40 -04:00
|
|
|
BlobViewer::Balsamiq,
|
2017-04-26 16:16:38 -04:00
|
|
|
|
2017-04-13 12:48:37 -04:00
|
|
|
BlobViewer::Video,
|
2017-05-02 18:45:50 -04:00
|
|
|
|
2017-04-26 16:16:38 -04:00
|
|
|
BlobViewer::PDF,
|
|
|
|
|
2017-04-13 13:21:07 -04:00
|
|
|
BlobViewer::BinarySTL,
|
2017-05-03 07:22:03 -04:00
|
|
|
BlobViewer::TextSTL
|
2017-05-08 19:50:23 -04:00
|
|
|
].sort_by { |v| v.binary? ? 0 : 1 }.freeze
|
2016-08-12 11:19:17 -04:00
|
|
|
|
2017-05-08 19:50:23 -04:00
|
|
|
AUXILIARY_VIEWERS = [
|
|
|
|
BlobViewer::GitlabCiYml,
|
|
|
|
BlobViewer::RouteMap,
|
2017-05-13 13:06:51 -04:00
|
|
|
|
2017-05-14 15:01:12 -04:00
|
|
|
BlobViewer::Readme,
|
2017-05-13 13:06:51 -04:00
|
|
|
BlobViewer::License,
|
2017-05-17 14:00:13 -04:00
|
|
|
BlobViewer::Contributing,
|
|
|
|
BlobViewer::Changelog
|
2017-05-08 19:50:23 -04:00
|
|
|
].freeze
|
2017-04-26 16:16:38 -04:00
|
|
|
|
2017-04-13 12:59:52 -04:00
|
|
|
attr_reader :project
|
|
|
|
|
2016-02-18 18:19:43 -05:00
|
|
|
# Wrap a Gitlab::Git::Blob object, or return nil when given nil
|
|
|
|
#
|
|
|
|
# This method prevents the decorated object from evaluating to "truthy" when
|
|
|
|
# given a nil value. For example:
|
|
|
|
#
|
|
|
|
# blob = Blob.new(nil)
|
|
|
|
# puts "truthy" if blob # => "truthy"
|
|
|
|
#
|
|
|
|
# blob = Blob.decorate(nil)
|
|
|
|
# puts "truthy" if blob # No output
|
2017-04-20 19:34:57 -04:00
|
|
|
def self.decorate(blob, project = nil)
|
2016-02-18 18:19:43 -05:00
|
|
|
return if blob.nil?
|
|
|
|
|
2017-04-13 12:59:52 -04:00
|
|
|
new(blob, project)
|
|
|
|
end
|
|
|
|
|
2017-04-20 19:34:57 -04:00
|
|
|
def initialize(blob, project = nil)
|
2017-04-13 12:59:52 -04:00
|
|
|
@project = project
|
|
|
|
|
|
|
|
super(blob)
|
2016-02-18 18:19:43 -05:00
|
|
|
end
|
|
|
|
|
2016-09-12 11:25:35 -04:00
|
|
|
# Returns the data of the blob.
|
|
|
|
#
|
|
|
|
# If the blob is a text based blob the content is converted to UTF-8 and any
|
|
|
|
# invalid byte sequences are replaced.
|
|
|
|
def data
|
|
|
|
if binary?
|
|
|
|
super
|
|
|
|
else
|
|
|
|
@data ||= super.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-14 06:44:35 -04:00
|
|
|
def no_highlighting?
|
2017-05-02 18:45:50 -04:00
|
|
|
raw_size && raw_size > MAXIMUM_TEXT_HIGHLIGHT_SIZE
|
|
|
|
end
|
|
|
|
|
|
|
|
def empty?
|
|
|
|
raw_size == 0
|
2016-04-14 06:44:35 -04:00
|
|
|
end
|
|
|
|
|
2017-04-13 13:14:08 -04:00
|
|
|
def too_large?
|
2016-05-24 01:59:35 -04:00
|
|
|
size && truncated?
|
2016-04-14 06:44:35 -04:00
|
|
|
end
|
|
|
|
|
2017-05-02 18:45:50 -04:00
|
|
|
def external_storage_error?
|
|
|
|
if external_storage == :lfs
|
|
|
|
!project&.lfs_enabled?
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stored_externally?
|
|
|
|
return @stored_externally if defined?(@stored_externally)
|
|
|
|
|
|
|
|
@stored_externally = external_storage && !external_storage_error?
|
|
|
|
end
|
|
|
|
|
2017-04-24 17:27:43 -04:00
|
|
|
# Returns the size of the file that this blob represents. If this blob is an
|
|
|
|
# LFS pointer, this is the size of the file stored in LFS. Otherwise, this is
|
|
|
|
# the size of the blob itself.
|
2017-04-13 13:14:08 -04:00
|
|
|
def raw_size
|
2017-05-02 18:45:50 -04:00
|
|
|
if stored_externally?
|
|
|
|
external_size
|
2017-04-13 13:14:08 -04:00
|
|
|
else
|
|
|
|
size
|
|
|
|
end
|
2017-04-10 17:23:19 -04:00
|
|
|
end
|
|
|
|
|
2017-04-24 17:27:43 -04:00
|
|
|
# Returns whether the file that this blob represents is binary. If this blob is
|
|
|
|
# an LFS pointer, we assume the file stored in LFS is binary, unless a
|
|
|
|
# text-based rich blob viewer matched on the file's extension. Otherwise, this
|
|
|
|
# depends on the type of the blob itself.
|
2017-04-13 13:14:08 -04:00
|
|
|
def raw_binary?
|
2017-05-02 18:45:50 -04:00
|
|
|
if stored_externally?
|
2017-04-24 10:27:19 -04:00
|
|
|
if rich_viewer
|
|
|
|
rich_viewer.binary?
|
2017-05-02 18:45:50 -04:00
|
|
|
elsif Linguist::Language.find_by_filename(name).any?
|
|
|
|
false
|
|
|
|
elsif _mime_type
|
|
|
|
_mime_type.binary?
|
2017-04-24 10:27:19 -04:00
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
2017-04-13 13:14:08 -04:00
|
|
|
else
|
|
|
|
binary?
|
|
|
|
end
|
2016-02-18 18:19:43 -05:00
|
|
|
end
|
|
|
|
|
2017-04-13 13:14:08 -04:00
|
|
|
def extension
|
|
|
|
@extension ||= extname.downcase.delete('.')
|
2017-04-03 14:39:50 -04:00
|
|
|
end
|
|
|
|
|
2017-04-13 13:14:08 -04:00
|
|
|
def video?
|
|
|
|
UploaderHelper::VIDEO_EXT.include?(extension)
|
2017-03-16 13:04:58 -04:00
|
|
|
end
|
|
|
|
|
2017-04-13 13:14:08 -04:00
|
|
|
def readable_text?
|
2017-05-02 18:45:50 -04:00
|
|
|
text? && !stored_externally? && !too_large?
|
2017-04-06 06:02:24 -04:00
|
|
|
end
|
|
|
|
|
2017-04-13 13:14:08 -04:00
|
|
|
def simple_viewer
|
|
|
|
@simple_viewer ||= simple_viewer_class.new(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def rich_viewer
|
|
|
|
return @rich_viewer if defined?(@rich_viewer)
|
|
|
|
|
2017-04-24 17:27:43 -04:00
|
|
|
@rich_viewer = rich_viewer_class&.new(self)
|
2017-04-13 13:14:08 -04:00
|
|
|
end
|
|
|
|
|
2017-05-08 19:50:23 -04:00
|
|
|
def auxiliary_viewer
|
|
|
|
return @auxiliary_viewer if defined?(@auxiliary_viewer)
|
|
|
|
|
|
|
|
@auxiliary_viewer = auxiliary_viewer_class&.new(self)
|
|
|
|
end
|
|
|
|
|
2017-04-21 14:59:34 -04:00
|
|
|
def rendered_as_text?(ignore_errors: true)
|
2017-04-24 10:27:19 -04:00
|
|
|
simple_viewer.text? && (ignore_errors || simple_viewer.render_error.nil?)
|
2017-04-13 13:14:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def show_viewer_switcher?
|
2017-04-21 14:59:34 -04:00
|
|
|
rendered_as_text? && rich_viewer
|
|
|
|
end
|
|
|
|
|
|
|
|
def override_max_size!
|
|
|
|
simple_viewer&.override_max_size = true
|
|
|
|
rich_viewer&.override_max_size = true
|
2017-04-13 13:14:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-04-24 10:27:19 -04:00
|
|
|
def simple_viewer_class
|
|
|
|
if empty?
|
|
|
|
BlobViewer::Empty
|
|
|
|
elsif raw_binary?
|
|
|
|
BlobViewer::Download
|
|
|
|
else # text
|
|
|
|
BlobViewer::Text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rich_viewer_class
|
2017-05-08 19:50:23 -04:00
|
|
|
viewer_class_from(RICH_VIEWERS)
|
|
|
|
end
|
|
|
|
|
|
|
|
def auxiliary_viewer_class
|
|
|
|
viewer_class_from(AUXILIARY_VIEWERS)
|
|
|
|
end
|
|
|
|
|
|
|
|
def viewer_class_from(classes)
|
2017-05-02 18:45:50 -04:00
|
|
|
return if empty? || external_storage_error?
|
2017-04-24 10:27:19 -04:00
|
|
|
|
2017-05-08 19:50:23 -04:00
|
|
|
verify_binary = !stored_externally?
|
2017-04-24 17:27:43 -04:00
|
|
|
|
2017-05-08 19:50:23 -04:00
|
|
|
classes.find { |viewer_class| viewer_class.can_render?(self, verify_binary: verify_binary) }
|
2017-04-24 10:27:19 -04:00
|
|
|
end
|
2016-02-18 18:19:43 -05:00
|
|
|
end
|