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
|
|
|
|
|
2016-08-12 11:19:17 -04:00
|
|
|
# The maximum size of an SVG that can be displayed.
|
|
|
|
MAXIMUM_SVG_SIZE = 2.megabytes
|
|
|
|
|
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
|
|
|
|
def self.decorate(blob)
|
|
|
|
return if blob.nil?
|
|
|
|
|
|
|
|
new(blob)
|
|
|
|
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?
|
|
|
|
size && size > 1.megabyte
|
|
|
|
end
|
|
|
|
|
|
|
|
def only_display_raw?
|
2016-05-24 01:59:35 -04:00
|
|
|
size && truncated?
|
2016-04-14 06:44:35 -04:00
|
|
|
end
|
|
|
|
|
2016-02-18 18:19:43 -05:00
|
|
|
def svg?
|
|
|
|
text? && language && language.name == 'SVG'
|
|
|
|
end
|
|
|
|
|
2017-04-03 14:39:50 -04:00
|
|
|
def pdf?
|
|
|
|
name && File.extname(name) == '.pdf'
|
|
|
|
end
|
|
|
|
|
2017-03-16 13:04:58 -04:00
|
|
|
def ipython_notebook?
|
2017-03-29 04:52:56 -04:00
|
|
|
text? && language&.name == 'Jupyter Notebook'
|
2017-03-16 13:04:58 -04:00
|
|
|
end
|
|
|
|
|
2017-03-29 01:16:59 -04:00
|
|
|
def sketch?
|
|
|
|
binary? && extname.downcase.delete('.') == 'sketch'
|
|
|
|
end
|
|
|
|
|
2016-08-12 11:19:17 -04:00
|
|
|
def size_within_svg_limits?
|
|
|
|
size <= MAXIMUM_SVG_SIZE
|
|
|
|
end
|
|
|
|
|
2016-07-25 11:08:36 -04:00
|
|
|
def video?
|
|
|
|
UploaderHelper::VIDEO_EXT.include?(extname.downcase.delete('.'))
|
|
|
|
end
|
|
|
|
|
2017-03-07 12:57:30 -05:00
|
|
|
def to_partial_path(project)
|
2016-02-18 18:19:43 -05:00
|
|
|
if lfs_pointer?
|
2017-03-07 12:57:30 -05:00
|
|
|
if project.lfs_enabled?
|
|
|
|
'download'
|
|
|
|
else
|
|
|
|
'text'
|
|
|
|
end
|
2016-02-18 18:19:43 -05:00
|
|
|
elsif image? || svg?
|
|
|
|
'image'
|
2017-04-03 14:39:50 -04:00
|
|
|
elsif pdf?
|
|
|
|
'pdf'
|
2017-03-16 13:04:58 -04:00
|
|
|
elsif ipython_notebook?
|
|
|
|
'notebook'
|
2017-03-29 01:16:59 -04:00
|
|
|
elsif sketch?
|
|
|
|
'sketch'
|
2016-02-18 18:19:43 -05:00
|
|
|
elsif text?
|
|
|
|
'text'
|
|
|
|
else
|
|
|
|
'download'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|