summaryrefslogtreecommitdiff
path: root/app/presenters/snippet_blob_presenter.rb
blob: 84e98e18e3267741a965a1b8c863a4b20c51adde (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
# frozen_string_literal: true

class SnippetBlobPresenter < BlobPresenter
  include GitlabRoutingHelper

  delegator_override_with Gitlab::Utils::StrongMemoize # This module inclusion is expected. See https://gitlab.com/gitlab-org/gitlab/-/issues/352884.

  presents ::SnippetBlob

  def rich_data
    return unless blob.rich_viewer

    render_rich_partial
  end

  def raw_path
    snippet_blob_raw_route(only_path: true)
  end

  def raw_url
    snippet_blob_raw_route
  end

  def raw_directory
    raw_path.rpartition("/").first + "/"
  end

  def raw_plain_data
    blob.data unless blob.binary?
  end

  private

  def snippet
    blob.container
  end

  def gitattr_language
    nil
  end

  def render_rich_partial
    renderer.render(
      "projects/blob/viewers/_#{blob.rich_viewer.partial_name}",
      locals: { viewer: blob.rich_viewer, blob: blob, blob_raw_path: raw_path, blob_raw_url: raw_url, parent_dir_raw_path: raw_directory },
      layout: false
    )
  end

  def renderer
    proxy = Warden::Proxy.new({}, Warden::Manager.new({})).tap do |proxy_instance|
      proxy_instance.set_user(current_user, scope: :user)
    end

    ApplicationController.renderer.new('warden' => proxy)
  end

  def snippet_blob_raw_route(only_path: false)
    return gitlab_raw_snippet_url(snippet, only_path: only_path) unless snippet.repository_exists?

    gitlab_raw_snippet_blob_url(snippet, blob.path, only_path: only_path)
  end
end

SnippetBlobPresenter.prepend_mod