diff --git a/app/assets/javascripts/file_upload.js b/app/assets/javascripts/file_upload.js index e236d972..e916d3e5 100644 --- a/app/assets/javascripts/file_upload.js +++ b/app/assets/javascripts/file_upload.js @@ -39,10 +39,9 @@ function bgUpload(elem) { progressBar.removeClass('active'); // extract key and generate URL from response var key = $(data.jqXHR.responseXML).find("Key").text(); - var url = 'https://' + form.data('host') + '/' + key; // create hidden field - var input = $("", { type:'hidden', name: fileInput.attr('name'), value: url, class: 's3-file' }); + var input = $("", { type:'hidden', name: fileInput.attr('name'), value: key, class: 's3-file' }); container.append(input); }, fail: function(e, data) { diff --git a/app/controllers/concerns/file_handling_for_datasets.rb b/app/controllers/concerns/file_handling_for_datasets.rb index 8f272f91..f02e86bf 100644 --- a/app/controllers/concerns/file_handling_for_datasets.rb +++ b/app/controllers/concerns/file_handling_for_datasets.rb @@ -13,13 +13,14 @@ def process_files if [ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile].include?(f["file"].class) Rails.logger.info "file is an Http::UploadedFile (non javascript?)" - storage_object = FileStorageService.create_and_upload_public_object(f["file"].original_filename, f["file"].read) + key = URI.encode(f["file"].original_filename) + storage_object = FileStorageService.create_and_upload_public_object(key, f["file"].read) - f["storage_key"] = storage_object.key(f["file"].original_filename) + f["storage_key"] = storage_object.key(key) f["file"] = storage_object.public_url else - Rails.logger.info "file is not an http uploaded file, it's a URL" - f["storage_key"] = URI(f["file"]).path.gsub(/^\//, '') unless f["file"].nil? + Rails.logger.info "file is not an http uploaded file, it's an s3 storage key" + f["storage_key"] = f["file"] unless f["file"].nil? end end end diff --git a/app/models/dataset_file.rb b/app/models/dataset_file.rb index 3c0a0e43..0b5dbf00 100644 --- a/app/models/dataset_file.rb +++ b/app/models/dataset_file.rb @@ -41,7 +41,7 @@ def self.file_from_url(file) end def self.file_from_url_with_storage_key(file, storage_key) - Rails.logger.info "DatasetFile: In file_from_url_with_storage_key" + Rails.logger.info "DatasetFile: In file_from_url_with_storage_key - '#{storage_key}'" fs_file = FileStorageService.get_string_io(storage_key) ActionDispatch::Http::UploadedFile.new filename: File.basename(file), diff --git a/spec/controllers/datasets/create_spec.rb b/spec/controllers/datasets/create_spec.rb index 1ae20335..216a7ccc 100644 --- a/spec/controllers/datasets/create_spec.rb +++ b/spec/controllers/datasets/create_spec.rb @@ -166,6 +166,35 @@ def creation_assertions(publishing_method = :github_public) expect(@user.datasets.first.owner).to eq @user.github_username end + it 'handles whitespace in filenames' do + expect(GitData).to receive(:create).with(@user.github_username, @name, restricted: false, client: a_kind_of(Octokit::Client)) { + @repo + } + + filename = "file with whitespace.csv" + storage_key = "uploads/#{SecureRandom.uuid}/#{filename}" + files = [{ + :title => "file with whitespace", + :description => "description", + :file => url_with_stubbed_get_for_storage_key(storage_key, filename), + :storage_key => storage_key + }] + + request = post :create, params: { dataset: { + name: dataset_name, + description: description, + publisher_name: publisher_name, + publisher_url: publisher_url, + license: license, + frequency: frequency, + publishing_method: :github_public, + owner: controller.send(:current_user).github_username + }, files: files } + + creation_assertions + expect(@user.datasets.first.owner).to eq @user.github_username + end + it 'creates a restricted dataset' do expect(GitData).to receive(:create).with(@user.github_username, @name, restricted: true, client: a_kind_of(Octokit::Client)) { @repo diff --git a/spec/controllers/datasets/update_spec.rb b/spec/controllers/datasets/update_spec.rb index 0d73ba94..0d778493 100644 --- a/spec/controllers/datasets/update_spec.rb +++ b/spec/controllers/datasets/update_spec.rb @@ -305,7 +305,7 @@ { title: "New file", description: "New file description", - file: "http://example.com/new-file.csv", + file: "new-file.csv", storage_key: 'new-file.csv' }, { @@ -322,7 +322,7 @@ { "title" => "New file", "description" => "New file description", - "file" => "http://example.com/new-file.csv", + "file" => "new-file.csv", "storage_key" => "new-file.csv" } ], channel_id: nil diff --git a/spec/fixtures/file with whitespace.csv b/spec/fixtures/file with whitespace.csv new file mode 100644 index 00000000..9eb3f079 --- /dev/null +++ b/spec/fixtures/file with whitespace.csv @@ -0,0 +1,3 @@ +hat,size +panama,59.5 +stetson,big diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 428a0ce6..4eda1fc0 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -163,7 +163,7 @@ def get_json_from_url(url) def get_string_io_from_fixture_file(storage_key) unless storage_key.nil? - filename = storage_key.split('/').last + filename = URI.decode(storage_key).split('/').last StringIO.new(read_fixture_file(filename)) end end