summaryrefslogtreecommitdiff
path: root/app/models/blob_viewer/base.rb
blob: 26a3778c2a3c2fec3e8c1da55bcc35096685880a (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
module BlobViewer
  class Base
    PARTIAL_PATH_PREFIX = 'projects/blob/viewers'.freeze

    class_attribute :partial_name, :loading_partial_name, :type, :extensions, :file_types, :load_async, :binary, :switcher_icon, :switcher_title, :overridable_max_size, :max_size

    self.loading_partial_name = 'loading'

    delegate :partial_path, :loading_partial_path, :rich?, :simple?, :text?, :binary?, to: :class

    attr_reader :blob
    attr_accessor :override_max_size

    delegate :project, to: :blob

    def initialize(blob)
      @blob = blob
    end

    def self.partial_path
      File.join(PARTIAL_PATH_PREFIX, partial_name)
    end

    def self.loading_partial_path
      File.join(PARTIAL_PATH_PREFIX, loading_partial_name)
    end

    def self.rich?
      type == :rich
    end

    def self.simple?
      type == :simple
    end

    def self.auxiliary?
      type == :auxiliary
    end

    def self.load_async?
      load_async
    end

    def self.binary?
      binary
    end

    def self.text?
      !binary?
    end

    def self.can_render?(blob, verify_binary: true)
      return false if verify_binary && binary? != blob.binary?
      return true if extensions&.include?(blob.extension)
      return true if file_types&.include?(Gitlab::FileDetector.type_of(blob.path))

      false
    end

    def load_async?
      self.class.load_async? && render_error.nil?
    end

    def exceeds_overridable_max_size?
      overridable_max_size && blob.raw_size > overridable_max_size
    end

    def exceeds_max_size?
      max_size && blob.raw_size > max_size
    end

    def can_override_max_size?
      exceeds_overridable_max_size? && !exceeds_max_size?
    end

    def too_large?
      if override_max_size
        exceeds_max_size?
      else
        exceeds_overridable_max_size?
      end
    end

    # This method is used on the server side to check whether we can attempt to
    # render the blob at all. Human-readable error messages are found in the
    # `BlobHelper#blob_render_error_reason` helper.
    #
    # This method does not and should not load the entire blob contents into
    # memory, and should not be overridden to do so in order to validate the
    # format of the blob.
    #
    # Prefer to implement a client-side viewer, where the JS component loads the
    # binary from `blob_raw_url` and does its own format validation and error
    # rendering, especially for potentially large binary formats.
    def render_error
      if too_large?
        :too_large
      end
    end

    def prepare!
      # To be overridden by subclasses
    end
  end
end