diff options
author | Jared Szechy <jared.szechy@brilligent.com> | 2015-08-04 18:21:12 -0400 |
---|---|---|
committer | Jared Szechy <jared.szechy@brilligent.com> | 2015-09-08 20:23:01 -0400 |
commit | e156f42079ebf8247b6a39fa6314d4d5c6b73d12 (patch) | |
tree | 8a4c08adda59c7cdd09fb2164df43d8b950b0389 /app/services | |
parent | 86556a079e34eb1267e63f7b39cc018665e21bfc (diff) | |
download | gitlab-ce-e156f42079ebf8247b6a39fa6314d4d5c6b73d12.tar.gz |
FogBugz project import
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/projects/download_service.rb | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/app/services/projects/download_service.rb b/app/services/projects/download_service.rb new file mode 100644 index 00000000000..99f22293d0d --- /dev/null +++ b/app/services/projects/download_service.rb @@ -0,0 +1,43 @@ +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! + + filename = uploader.image? ? uploader.file.basename : uploader.file.filename + + { + 'alt' => filename, + 'url' => uploader.secure_url, + 'is_image' => uploader.image? + } + 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 |