Read about our upcoming Code of Conduct on this issue

blob.rb 6.46 KB
Newer Older
1
2
# frozen_string_literal: true

Mark Chao's avatar
Mark Chao committed
3
# Blob is a Rails-specific wrapper around Gitlab::Git::Blob, SnippetBlob and Ci::ArtifactBlob
4
class Blob < SimpleDelegator
Mark Chao's avatar
Mark Chao committed
5
  include Presentable
6
  include BlobLanguageFromGitAttributes
7
  include BlobActiveModel
Mark Chao's avatar
Mark Chao committed
8

Jacob Vosmaer's avatar
Jacob Vosmaer committed
9
10
11
  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

12
13
14
15
16
17
18
19
20
21
22
23
24
25
  # 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.
26
  RICH_VIEWERS = [
27
28
29
    BlobViewer::Markup,
    BlobViewer::Notebook,
    BlobViewer::SVG,
30
    BlobViewer::OpenApi,
31

Douwe Maan's avatar
Douwe Maan committed
32
33
    BlobViewer::Image,
    BlobViewer::Sketch,
34
    BlobViewer::Balsamiq,
35

36
    BlobViewer::Video,
37
    BlobViewer::Audio,
38

39
40
    BlobViewer::PDF,

Douwe Maan's avatar
Douwe Maan committed
41
    BlobViewer::BinarySTL,
42
    BlobViewer::TextSTL
Douwe Maan's avatar
Douwe Maan committed
43
  ].sort_by { |v| v.binary? ? 0 : 1 }.freeze
44

Douwe Maan's avatar
Douwe Maan committed
45
46
47
  AUXILIARY_VIEWERS = [
    BlobViewer::GitlabCiYml,
    BlobViewer::RouteMap,
48

Douwe Maan's avatar
Douwe Maan committed
49
    BlobViewer::Readme,
50
    BlobViewer::License,
51
    BlobViewer::Contributing,
52
53
    BlobViewer::Changelog,

54
    BlobViewer::CargoToml,
55
56
57
58
59
60
61
62
63
64
65
    BlobViewer::Cartfile,
    BlobViewer::ComposerJson,
    BlobViewer::Gemfile,
    BlobViewer::Gemspec,
    BlobViewer::GodepsJson,
    BlobViewer::PackageJson,
    BlobViewer::Podfile,
    BlobViewer::Podspec,
    BlobViewer::PodspecJson,
    BlobViewer::RequirementsTxt,
    BlobViewer::YarnLock
Douwe Maan's avatar
Douwe Maan committed
66
  ].freeze
67

68
69
70
71
  attr_reader :container

  delegate :repository, to: :container, allow_nil: true
  delegate :project, to: :repository, allow_nil: true
Douwe Maan's avatar
Douwe Maan committed
72

73
74
75
76
77
78
79
80
81
82
  # 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
83
  def self.decorate(blob, container = nil)
84
85
    return if blob.nil?

86
    new(blob, container)
Douwe Maan's avatar
Douwe Maan committed
87
88
  end

89
90
  def self.lazy(container, commit_id, path, blob_size_limit: Gitlab::Git::Blob::MAX_DATA_DISPLAY_SIZE)
    BatchLoader.for([commit_id, path]).batch(key: container.repository) do |items, loader, args|
91
      args[:key].blobs_at(items, blob_size_limit: blob_size_limit).each do |blob|
92
        loader.call([blob.commit_id, blob.path], blob) if blob
93
94
95
96
      end
    end
  end

97
98
  def initialize(blob, container = nil)
    @container = container
Douwe Maan's avatar
Douwe Maan committed
99
100

    super(blob)
101
102
  end

103
104
105
106
  def inspect
    "#<#{self.class.name} oid:#{id[0..8]} commit:#{commit_id[0..8]} path:#{path}>"
  end

107
108
109
110
111
  # 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
112
    if binary_in_repo?
113
114
115
116
117
118
      super
    else
      @data ||= super.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
    end
  end

119
  def load_all_data!
120
    # Endpoint needed: https://gitlab.com/gitlab-org/gitaly/issues/756
121
    Gitlab::GitalyClient.allow_n_plus_1_calls do
122
      super(repository) if container
123
    end
124
125
  end

126
127
  def empty?
    raw_size == 0
128
129
  end

130
131
132
133
134
135
136
137
138
139
140
141
142
143
  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

Douwe Maan's avatar
Douwe Maan committed
144
145
146
  # 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.
147
  def raw_size
148
149
    if stored_externally?
      external_size
150
151
152
    else
      size
    end
153
154
  end

Douwe Maan's avatar
Douwe Maan committed
155
156
157
158
  # 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.
159
  def binary?
160
    if stored_externally?
161
162
      if rich_viewer
        rich_viewer.binary?
163
      elsif known_extension?
164
165
166
        false
      elsif _mime_type
        _mime_type.binary?
167
168
169
      else
        true
      end
170
    else
171
      binary_in_repo?
172
    end
173
174
  end

175
176
  def extension
    @extension ||= extname.downcase.delete('.')
Sam Rose's avatar
Sam Rose committed
177
178
  end

179
  def file_type
180
181
182
    name = File.basename(path)

    Gitlab::FileDetector.type_of(path) || Gitlab::FileDetector.type_of(name)
183
184
  end

185
  def video?
186
    UploaderHelper::SAFE_VIDEO_EXT.include?(extension)
Phil Hughes's avatar
Phil Hughes committed
187
188
  end

189
190
191
192
  def audio?
    UploaderHelper::SAFE_AUDIO_EXT.include?(extension)
  end

193
  def readable_text?
194
    text_in_repo? && !stored_externally? && !truncated?
Phil Hughes's avatar
Phil Hughes committed
195
196
  end

197
198
199
200
201
202
203
  def simple_viewer
    @simple_viewer ||= simple_viewer_class.new(self)
  end

  def rich_viewer
    return @rich_viewer if defined?(@rich_viewer)

Douwe Maan's avatar
Douwe Maan committed
204
    @rich_viewer = rich_viewer_class&.new(self)
205
206
  end

Douwe Maan's avatar
Douwe Maan committed
207
208
209
210
211
212
  def auxiliary_viewer
    return @auxiliary_viewer if defined?(@auxiliary_viewer)

    @auxiliary_viewer = auxiliary_viewer_class&.new(self)
  end

Douwe Maan's avatar
Douwe Maan committed
213
  def rendered_as_text?(ignore_errors: true)
214
    simple_viewer.is_a?(BlobViewer::Text) && (ignore_errors || simple_viewer.render_error.nil?)
215
216
217
  end

  def show_viewer_switcher?
Douwe Maan's avatar
Douwe Maan committed
218
219
220
    rendered_as_text? && rich_viewer
  end

221
222
223
224
  def expanded?
    !!@expanded
  end

225
  def expand!
226
    @expanded = true
227
228
229
230
  end

  private

231
232
233
  def simple_viewer_class
    if empty?
      BlobViewer::Empty
234
    elsif binary?
235
236
237
238
239
240
241
      BlobViewer::Download
    else # text
      BlobViewer::Text
    end
  end

  def rich_viewer_class
Douwe Maan's avatar
Douwe Maan committed
242
243
244
245
246
247
248
249
    viewer_class_from(RICH_VIEWERS)
  end

  def auxiliary_viewer_class
    viewer_class_from(AUXILIARY_VIEWERS)
  end

  def viewer_class_from(classes)
250
    return if empty? || external_storage_error?
251

Douwe Maan's avatar
Douwe Maan committed
252
    verify_binary = !stored_externally?
Douwe Maan's avatar
Douwe Maan committed
253

Douwe Maan's avatar
Douwe Maan committed
254
    classes.find { |viewer_class| viewer_class.can_render?(self, verify_binary: verify_binary) }
255
  end
256
end
257
258

Blob.prepend_if_ee('EE::Blob')