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