Module: Gitlab::BlobHelper
Constant Summary
collapse
- MEGABYTE =
1024 * 1024
Instance Method Summary
collapse
#clear_memoization, #strong_memoize, #strong_memoized?
Instance Method Details
#_mime_type ⇒ Object
Internal: Lookup mime type for extension.
Returns a MIME::Type rubocop:disable Gitlab/ModuleWithInstanceVariables
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/gitlab/blob_helper.rb', line 57
def _mime_type
if defined? @_mime_type
@_mime_type
else
guesses = ::MIME::Types.type_for(extname.to_s)
@_mime_type = guesses.detect { |type| type.ascii? } || guesses.first
end
end
|
#binary_in_repo? ⇒ Boolean
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/gitlab/blob_helper.rb', line 26
def binary_in_repo?
if data.nil?
true
elsif data == ""
false
elsif encoding.nil?
true
else
detect_encoding[:type] == :binary
end
end
|
#binary_mime_type? ⇒ Boolean
81
82
83
|
# File 'lib/gitlab/blob_helper.rb', line 81
def binary_mime_type?
_mime_type ? _mime_type.binary? : false
end
|
#content_type ⇒ Object
115
116
117
118
119
120
121
122
|
# File 'lib/gitlab/blob_helper.rb', line 115
def content_type
@content_type ||= binary_mime_type? || binary_in_repo? ? mime_type :
(encoding ? "text/plain; charset=#{encoding.downcase}" : "text/plain")
end
|
#detect_encoding ⇒ Object
151
152
153
|
# File 'lib/gitlab/blob_helper.rb', line 151
def detect_encoding
@detect_encoding ||= CharlockHolmes::EncodingDetector.new.detect(data) if data end
|
#empty? ⇒ Boolean
155
156
157
|
# File 'lib/gitlab/blob_helper.rb', line 155
def empty?
data.nil? || data == ""
end
|
#encoded_newlines_re ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/gitlab/blob_helper.rb', line 124
def encoded_newlines_re
strong_memoize(:encoded_newlines_re) do
newlines = ["\r\n", "\r", "\n"]
data_encoding = data&.encoding
if ruby_encoding && data_encoding
newlines.map! do |nl|
nl.encode(ruby_encoding, "ASCII-8BIT").force_encoding(data_encoding)
end
end
Regexp.union(newlines)
end
end
|
#encoding ⇒ Object
145
146
147
148
149
|
# File 'lib/gitlab/blob_helper.rb', line 145
def encoding
if hash = detect_encoding
hash[:encoding]
end
end
|
#extname ⇒ Object
8
9
10
|
# File 'lib/gitlab/blob_helper.rb', line 8
def extname
File.extname(name.to_s)
end
|
#image? ⇒ Boolean
49
50
51
|
# File 'lib/gitlab/blob_helper.rb', line 49
def image?
['.png', '.jpg', '.jpeg', '.gif', '.svg'].include?(extname.downcase)
end
|
#known_extension? ⇒ Boolean
12
13
14
|
# File 'lib/gitlab/blob_helper.rb', line 12
def known_extension?
LanguageData.extensions.include?(extname)
end
|
#large? ⇒ Boolean
22
23
24
|
# File 'lib/gitlab/blob_helper.rb', line 22
def large?
size.to_i > MEGABYTE
end
|
#lines ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/gitlab/blob_helper.rb', line 85
def lines
@lines ||=
if viewable? && data
begin
data.split(encoded_newlines_re, -1)
rescue Encoding::ConverterNotFoundError
[data]
end
else
[]
end
end
|
#mime_type ⇒ Object
Public: Get the actual blob mime type
Examples
Returns a mime type String.
77
78
79
|
# File 'lib/gitlab/blob_helper.rb', line 77
def mime_type
_mime_type ? _mime_type.to_s : 'text/plain'
end
|
#ruby_encoding ⇒ Object
139
140
141
142
143
|
# File 'lib/gitlab/blob_helper.rb', line 139
def ruby_encoding
if hash = detect_encoding
hash[:ruby_encoding]
end
end
|
#text_in_repo? ⇒ Boolean
45
46
47
|
# File 'lib/gitlab/blob_helper.rb', line 45
def text_in_repo?
!binary_in_repo?
end
|
#viewable? ⇒ Boolean
16
17
18
|
# File 'lib/gitlab/blob_helper.rb', line 16
def viewable?
!large? && text_in_repo?
end
|