blob.rb 1,6 КБ
Newer Older
Robert Speicher's avatar
Robert Speicher включено в состав коммита
1
2
# Blob is a Rails-specific wrapper around Gitlab::Git::Blob objects
class Blob < SimpleDelegator
Jacob Vosmaer's avatar
Jacob Vosmaer включено в состав коммита
3
4
5
  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

Yorick Peterse's avatar
Yorick Peterse включено в состав коммита
6
7
8
  # The maximum size of an SVG that can be displayed.
  MAXIMUM_SVG_SIZE = 2.megabytes

Robert Speicher's avatar
Robert Speicher включено в состав коммита
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  # 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

Yorick Peterse's avatar
Yorick Peterse включено в состав коммита
25
26
27
28
29
30
31
32
33
34
35
36
  # 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

Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg включено в состав коммита
37
38
39
40
41
  def no_highlighting?
    size && size > 1.megabyte
  end

  def only_display_raw?
Stan Hu's avatar
Stan Hu включено в состав коммита
42
    size && truncated?
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg включено в состав коммита
43
44
  end

Robert Speicher's avatar
Robert Speicher включено в состав коммита
45
46
47
48
  def svg?
    text? && language && language.name == 'SVG'
  end

Yorick Peterse's avatar
Yorick Peterse включено в состав коммита
49
50
51
52
  def size_within_svg_limits?
    size <= MAXIMUM_SVG_SIZE
  end

Rémy Coutable's avatar
Rémy Coutable включено в состав коммита
53
54
55
56
  def video?
    UploaderHelper::VIDEO_EXT.include?(extname.downcase.delete('.'))
  end

Christopher Bartz's avatar
Christopher Bartz включено в состав коммита
57
  def to_partial_path(project)
Robert Speicher's avatar
Robert Speicher включено в состав коммита
58
    if lfs_pointer?
Christopher Bartz's avatar
Christopher Bartz включено в состав коммита
59
60
61
62
63
      if project.lfs_enabled?
        'download'
      else
        'text'
      end
Robert Speicher's avatar
Robert Speicher включено в состав коммита
64
65
66
67
68
69
70
71
72
    elsif image? || svg?
      'image'
    elsif text?
      'text'
    else
      'download'
    end
  end
end