summaryrefslogtreecommitdiff
path: root/app/models/blob.rb
blob: 12cc5aaafba1bbeb13a361e93d88f8bdce6264cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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

  # The maximum size of an SVG that can be displayed.
  MAXIMUM_SVG_SIZE = 2.megabytes

  # 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

  def no_highlighting?
    size && size > 1.megabyte
  end

  def only_display_raw?
    size && truncated?
  end

  def svg?
    text? && language && language.name == 'SVG'
  end

  def size_within_svg_limits?
    size <= MAXIMUM_SVG_SIZE
  end

  def video?
    UploaderHelper::VIDEO_EXT.include?(extname.downcase.delete('.'))
  end

  def to_partial_path
    if lfs_pointer?
      'download'
    elsif image? || svg?
      'image'
    elsif text?
      'text'
    else
      'download'
    end
  end
end