2016-06-02 11:05:42 +05:30
|
|
|
# Blob is a Rails-specific wrapper around Gitlab::Git::Blob objects
|
|
|
|
class Blob < SimpleDelegator
|
|
|
|
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
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
# The maximum size of an SVG that can be displayed.
|
|
|
|
MAXIMUM_SVG_SIZE = 2.megabytes
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
# 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
|
|
|
|
def self.decorate(blob)
|
|
|
|
return if blob.nil?
|
|
|
|
|
|
|
|
new(blob)
|
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
# 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-06-02 11:05:42 +05:30
|
|
|
def no_highlighting?
|
|
|
|
size && size > 1.megabyte
|
|
|
|
end
|
|
|
|
|
|
|
|
def only_display_raw?
|
2016-06-16 23:09:34 +05:30
|
|
|
size && truncated?
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def svg?
|
|
|
|
text? && language && language.name == 'SVG'
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def size_within_svg_limits?
|
|
|
|
size <= MAXIMUM_SVG_SIZE
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def video?
|
|
|
|
UploaderHelper::VIDEO_EXT.include?(extname.downcase.delete('.'))
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def to_partial_path
|
|
|
|
if lfs_pointer?
|
|
|
|
'download'
|
|
|
|
elsif image? || svg?
|
|
|
|
'image'
|
|
|
|
elsif text?
|
|
|
|
'text'
|
|
|
|
else
|
|
|
|
'download'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|