gitlab-org--gitlab-foss/app/services/projects/download_service.rb

37 lines
673 B
Ruby
Raw Normal View History

2015-08-04 22:21:12 +00:00
module Projects
class DownloadService < BaseService
WHITELIST = [
/^[^.]+\.fogbugz.com$/
2017-02-21 23:32:18 +00:00
].freeze
2015-08-04 22:21:12 +00:00
def initialize(project, url)
@project, @url = project, url
end
def execute
return nil unless valid_url?(@url)
uploader = FileUploader.new(@project)
uploader.download!(@url)
uploader.store!
2016-01-08 16:38:53 +00:00
uploader.to_h
2015-08-04 22:21:12 +00:00
end
private
def valid_url?(url)
url && http?(url) && valid_domain?(url)
end
def http?(url)
2017-02-22 17:46:57 +00:00
url =~ /\A#{URI.regexp(%w(http https))}\z/
2015-08-04 22:21:12 +00:00
end
def valid_domain?(url)
host = URI.parse(url).host
WHITELIST.any? { |entry| entry === host }
end
end
end