2015-09-25 12:07:36 +05:30
|
|
|
module Projects
|
|
|
|
class DownloadService < BaseService
|
|
|
|
|
|
|
|
WHITELIST = [
|
|
|
|
/^[^.]+\.fogbugz.com$/
|
|
|
|
]
|
|
|
|
|
|
|
|
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-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)
|
|
|
|
url =~ /\A#{URI::regexp(['http', 'https'])}\z/
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_domain?(url)
|
|
|
|
host = URI.parse(url).host
|
|
|
|
WHITELIST.any? { |entry| entry === host }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|