relative_link_filter.rb 4.6 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
        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)
53
        path_parts = [Addressable::URI.unescape(html_attr.value)]
54 55

        if group
56
          path_parts.unshift(relative_url_root, 'groups', group.full_path, '-')
57
        elsif project
58
          path_parts.unshift(relative_url_root, project.full_path)
59 60
        end

61
        path = Addressable::URI.escape(File.join(*path_parts))
62 63 64 65 66

        html_attr.value =
          if context[:only_path]
            path
          else
67
            Addressable::URI.join(Gitlab.config.gitlab.base_url, path).to_s
68
          end
69 70 71
      end

      def process_link_to_repository_attr(html_attr)
72 73 74 75
        uri = URI(html_attr.value)
        if uri.relative? && uri.path.present?
          html_attr.value = rebuild_relative_uri(uri).to_s
        end
76
      rescue URI::Error, Addressable::URI::InvalidURIError
77
        # noop
78 79 80
      end

      def rebuild_relative_uri(uri)
81
        file_path = relative_file_path(uri)
82 83 84

        uri.path = [
          relative_url_root,
85
          project.full_path,
86
          uri_type(file_path),
87 88
          Addressable::URI.escape(ref),
          Addressable::URI.escape(file_path)
89 90 91 92 93
        ].compact.join('/').squeeze('/').chomp('/')

        uri
      end

94 95 96 97
      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)
98 99 100
        file_exists?(nested_path) ? nested_path : path
      end

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
      # 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)
123 124
        return request_path if path.empty?
        return path unless request_path
125
        return path[1..-1] if path.start_with?('/')
126 127

        parts = request_path.split('/')
128
        parts.pop if uri_type(request_path) != :tree
129

130
        path.sub!(%r{\A\./}, '')
131

132
        while path.start_with?('../')
133 134 135 136
          parts.pop
          path.sub!('../', '')
        end

137 138 139 140
        parts.push(path).join('/')
      end

      def file_exists?(path)
141
        path.present? && !!uri_type(path)
142 143
      end

144
      def uri_type(path)
145
        @uri_types[path] ||= current_commit.uri_type(path)
146 147
      end

148
      def current_commit
149
        @current_commit ||= context[:commit] || repository.commit(ref)
150 151 152 153 154 155
      end

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

156
      def ref
157 158 159 160 161 162 163 164 165
        context[:ref] || project.default_branch
      end

      def group
        context[:group]
      end

      def project
        context[:project]
166 167 168
      end

      def repository
169
        @repository ||= project&.repository
170 171 172 173
      end
    end
  end
end