Welcome to mirror list, hosted at ThFree Co, Russian Federation.

attachment.rb « markdown « github_import « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a5cf5ffa60e466181cf846b67fc5f2c2af7d90b5 (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
# frozen_string_literal: true

module Gitlab
  module GithubImport
    module Markdown
      class Attachment
        MEDIA_TYPES = %w[gif jpeg jpg mov mp4 png svg webm].freeze
        DOC_TYPES = %w[
          csv docx fodg fodp fods fodt gz log md odf odg odp ods
          odt pdf pptx tgz txt xls xlsx zip
        ].freeze

        class << self
          # markdown_node - CommonMarker::Node
          def from_markdown(markdown_node)
            case markdown_node.type
            when :html, :inline_html
              from_inline_html(markdown_node)
            when :image
              from_markdown_image(markdown_node)
            when :link
              from_markdown_link(markdown_node)
            end
          end

          private

          def from_markdown_image(markdown_node)
            url = markdown_node.url

            return unless github_url?(url, media: true)
            return unless whitelisted_type?(url, media: true)

            new(markdown_node.to_plaintext.strip, url)
          end

          def from_markdown_link(markdown_node)
            url = markdown_node.url

            return unless github_url?(url, docs: true)
            return unless whitelisted_type?(url, docs: true)

            new(markdown_node.to_plaintext.strip, url)
          end

          def from_inline_html(markdown_node)
            img = Nokogiri::HTML.parse(markdown_node.string_content).xpath('//img')[0]

            return unless img
            return unless github_url?(img[:src], media: true)
            return unless whitelisted_type?(img[:src], media: true)

            new(img[:alt], img[:src])
          end

          def github_url?(url, docs: false, media: false)
            if media
              url.start_with?(::Gitlab::GithubImport::MarkdownText::GITHUB_MEDIA_CDN)
            elsif docs
              url.start_with?(::Gitlab::GithubImport::MarkdownText.github_url)
            end
          end

          def whitelisted_type?(url, docs: false, media: false)
            if media
              MEDIA_TYPES.any? { |type| url.end_with?(type) }
            elsif docs
              DOC_TYPES.any? { |type| url.end_with?(type) }
            end
          end
        end

        attr_reader :name, :url

        def initialize(name, url)
          @name = name
          @url = url
        end

        def inspect
          "<#{self.class.name}: { name: #{name}, url: #{url} }>"
        end
      end
    end
  end
end