relative_link_filter.rb 4.3 KB
Newer Older
1 2
require 'uri'

3 4
module Banzai
  module Filter
5
    # HTML filter that "fixes" relative links to uploads or files in a repository.
6 7 8
    #
    # Context options:
    #   :commit
9
    #   :group
10 11 12
    #   :project
    #   :project_wiki
    #   :ref
13
    #   :requested_path
14
    class RelativeLinkFilter < HTML::Pipeline::Filter
15
      include Gitlab::Utils::StrongMemoize
16

17
      def call
18
        @uri_types = {}
19
        clear_memoization(:linkable_files)
20

21
        doc.search('a:not(.gfm)').each do |el|
22 23 24
          process_link_attr el.attribute('href')
        end

25
        doc.css('img, video').each do |el|
26
          process_link_attr el.attribute('src')
27
          process_link_attr el.attribute('data-src')
28 29 30 31 32 33 34
        end

        doc
      end

      protected

35
      def linkable_files?
36 37 38
        strong_memoize(:linkable_files) do
          context[:project_wiki].nil? && repository.try(:exists?) && !repository.empty?
        end
39 40
      end

41 42
      def process_link_attr(html_attr)
        return if html_attr.blank?
43
        return if html_attr.value.start_with?('//')
44

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        if html_attr.value.start_with?('/uploads/')
          process_link_to_upload_attr(html_attr)
        elsif linkable_files?
          process_link_to_repository_attr(html_attr)
        end
      end

      def process_link_to_upload_attr(html_attr)
        uri_parts = [html_attr.value]

        if group
          uri_parts.unshift(relative_url_root, 'groups', group.full_path, '-')
        elsif project
          uri_parts.unshift(relative_url_root, project.full_path)
        end

        html_attr.value = File.join(*uri_parts)
      end

      def process_link_to_repository_attr(html_attr)
65 66 67 68
        uri = URI(html_attr.value)
        if uri.relative? && uri.path.present?
          html_attr.value = rebuild_relative_uri(uri).to_s
        end
69
      rescue URI::Error, Addressable::URI::InvalidURIError
70
        # noop
71 72 73
      end

      def rebuild_relative_uri(uri)
74
        file_path = relative_file_path(uri)
75 76 77

        uri.path = [
          relative_url_root,
78
          project.full_path,
79
          uri_type(file_path),
80 81
          Addressable::URI.escape(ref),
          Addressable::URI.escape(file_path)
82 83 84 85 86
        ].compact.join('/').squeeze('/').chomp('/')

        uri
      end

87 88 89 90
      def relative_file_path(uri)
        path = Addressable::URI.unescape(uri.path)
        request_path = Addressable::URI.unescape(context[:requested_path])
        nested_path = build_relative_path(path, request_path)
91 92 93
        file_exists?(nested_path) ? nested_path : path
      end

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
      # Convert a relative path into its correct location based on the currently
      # requested path
      #
      # path         - Relative path String
      # request_path - Currently-requested path String
      #
      # Examples:
      #
      #   # File in the same directory as the current path
      #   build_relative_path("users.md", "doc/api/README.md")
      #   # => "doc/api/users.md"
      #
      #   # File in the same directory, which is also the current path
      #   build_relative_path("users.md", "doc/api")
      #   # => "doc/api/users.md"
      #
      #   # Going up one level to a different directory
      #   build_relative_path("../update/7.14-to-8.0.md", "doc/api/README.md")
      #   # => "doc/update/7.14-to-8.0.md"
      #
      # Returns a String
      def build_relative_path(path, request_path)
116 117
        return request_path if path.empty?
        return path unless request_path
118
        return path[1..-1] if path.start_with?('/')
119 120

        parts = request_path.split('/')
121
        parts.pop if uri_type(request_path) != :tree
122

123
        path.sub!(%r{\A\./}, '')
124

125
        while path.start_with?('../')
126 127 128 129
          parts.pop
          path.sub!('../', '')
        end

130 131 132 133
        parts.push(path).join('/')
      end

      def file_exists?(path)
134
        path.present? && !!uri_type(path)
135 136
      end

137
      def uri_type(path)
138
        @uri_types[path] ||= current_commit.uri_type(path)
139 140
      end

141
      def current_commit
142
        @current_commit ||= context[:commit] || repository.commit(ref)
143 144 145 146 147 148
      end

      def relative_url_root
        Gitlab.config.gitlab.relative_url_root.presence || '/'
      end

149
      def ref
150 151 152 153 154 155 156 157 158
        context[:ref] || project.default_branch
      end

      def group
        context[:group]
      end

      def project
        context[:project]
159 160 161
      end

      def repository
162
        @repository ||= project&.repository
163 164 165 166
      end
    end
  end
end