Skip to content

Commit

Permalink
add download utils
Browse files Browse the repository at this point in the history
  • Loading branch information
omohokcoj authored and AlexBTurchyn committed Sep 9, 2024
1 parent 71b8cc7 commit 5a1efd0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def create
end

render json: build_create_json(submissions)
rescue Submitters::NormalizeValues::BaseError => e
rescue Submitters::NormalizeValues::BaseError, DownloadUtils::UnableToDownload => e
Rollbar.warning(e) if defined?(Rollbar)

render json: { error: e.message }, status: :unprocessable_entity
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/submitters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def update
with_urls: true,
with_events: false,
params:)
rescue Submitters::NormalizeValues::BaseError => e
rescue Submitters::NormalizeValues::BaseError, DownloadUtils::UnableToDownload => e
Rollbar.warning(e) if defined?(Rollbar)

render json: { error: e.message }, status: :unprocessable_entity
Expand Down
8 changes: 1 addition & 7 deletions app/controllers/templates_uploads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def save_template!(template, url_params)
def create_file_params_from_url
tempfile = Tempfile.new
tempfile.binmode
tempfile.write(conn.get(Addressable::URI.parse(params[:url]).display_uri.to_s).body)
tempfile.write(DownloadUtils.call(params[:url]).body)
tempfile.rewind

file = ActionDispatch::Http::UploadedFile.new(
Expand All @@ -65,10 +65,4 @@ def create_file_params_from_url

{ files: [file] }
end

def conn
Faraday.new do |faraday|
faraday.response :follow_redirects
end
end
end
33 changes: 33 additions & 0 deletions lib/download_utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module DownloadUtils
LOCALHOSTS = %w[0.0.0.0 127.0.0.1 localhost].freeze

UnableToDownload = Class.new(StandardError)

module_function

def call(url)
uri = Addressable::URI.parse(url)

if Docuseal.multitenant?
raise UnableToDownload, "Error loading: #{uri.display_uri}. Only HTTPS is allowed." if uri.scheme != 'https'

if uri.host.in?(LOCALHOSTS)
raise UnableToDownload, "Error loading: #{uri.display_uri}. Can't download from localhost."
end
end

resp = conn.get(uri.display_uri.to_s)

raise UnableToDownload, "Error loading: #{uri.display_uri}" if resp.status >= 400

resp
end

def conn
Faraday.new do |faraday|
faraday.response :follow_redirects
end
end
end
14 changes: 1 addition & 13 deletions lib/submitters/normalize_values.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module NormalizeValues
UnknownFieldName = Class.new(BaseError)
InvalidDefaultValue = Class.new(BaseError)
UnknownSubmitterName = Class.new(BaseError)
UnableToDownload = Class.new(BaseError)

TRUE_VALUES = ['1', 'true', true, 'TRUE', 'True', 'yes', 'YES', 'Yes'].freeze
FALSE_VALUES = ['0', 'false', false, 'FALSE', 'False', 'no', 'NO', 'No'].freeze
Expand Down Expand Up @@ -185,12 +184,7 @@ def find_or_create_blob_from_url(account, url)

return blob if blob

uri = Addressable::URI.parse(url)
resp = conn.get(uri.display_uri.to_s)

raise UnableToDownload, "Error loading: #{uri.display_uri}" if resp.status >= 400

data = resp.body
data = DownloadUtils.call(url).body

checksum = Digest::MD5.base64digest(data)

Expand All @@ -215,11 +209,5 @@ def find_blob_by_checksum(checksum, account)

nil
end

def conn
Faraday.new do |faraday|
faraday.response :follow_redirects
end
end
end
end

0 comments on commit 5a1efd0

Please sign in to comment.