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

download_service.rb « projects « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d67b7832bf8b4ac164d11d79c3d38f4571c582dd (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
# frozen_string_literal: true

module Projects
  class DownloadService < BaseService
    ALLOWLIST = [
      /^[^.]+\.fogbugz.com$/
    ].freeze

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

    def execute
      return unless valid_url?(@url)

      uploader = FileUploader.new(@project)
      uploader.download!(@url)
      uploader.store!

      uploader.to_h
    end

    private

    def valid_url?(url)
      url && http?(url) && valid_domain?(url)
    end

    def http?(url)
      url =~ /\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/
    end

    def valid_domain?(url)
      host = URI.parse(url).host
      ALLOWLIST.any? { |entry| entry === host }
    end
  end
end