2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
module Projects
|
|
|
|
class DownloadService < BaseService
|
|
|
|
WHITELIST = [
|
|
|
|
/^[^.]+\.fogbugz.com$/
|
2017-08-17 22:00:37 +05:30
|
|
|
].freeze
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
def initialize(project, url)
|
2021-04-29 21:17:54 +05:30
|
|
|
@project = project
|
|
|
|
@url = url
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2019-07-07 11:18:12 +05:30
|
|
|
return unless valid_url?(@url)
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
uploader = FileUploader.new(@project)
|
|
|
|
uploader.download!(@url)
|
|
|
|
uploader.store!
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
uploader.to_h
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def valid_url?(url)
|
|
|
|
url && http?(url) && valid_domain?(url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def http?(url)
|
2020-11-24 15:15:51 +05:30
|
|
|
url =~ /\A#{URI::DEFAULT_PARSER.make_regexp(%w(http https))}\z/
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def valid_domain?(url)
|
|
|
|
host = URI.parse(url).host
|
|
|
|
WHITELIST.any? { |entry| entry === host }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|