3288e1a874
This style change enforces `return if ...` instead of `return nil if ...` to save maintainers a few minor review points
38 lines
700 B
Ruby
38 lines
700 B
Ruby
# frozen_string_literal: true
|
|
|
|
module Projects
|
|
class DownloadService < BaseService
|
|
WHITELIST = [
|
|
/^[^.]+\.fogbugz.com$/
|
|
].freeze
|
|
|
|
def initialize(project, url)
|
|
@project, @url = project, 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.regexp(%w(http https))}\z/
|
|
end
|
|
|
|
def valid_domain?(url)
|
|
host = URI.parse(url).host
|
|
WHITELIST.any? { |entry| entry === host }
|
|
end
|
|
end
|
|
end
|