Skip to content

Commit

Permalink
Removing special characters from temporary file name
Browse files Browse the repository at this point in the history
  • Loading branch information
carolyncole committed Mar 3, 2017
1 parent 8526692 commit 76027a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
7 changes: 6 additions & 1 deletion lib/scholarsphere/import/version_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def build(file_set, generic_file_versions)
private

def create(file_set, version)
filename_on_disk = File.join Rails.root, "tmp/uploads", "#{file_set.id}_#{version[:label]}_#{file_set.label}"
filename_on_disk = temp_file_name(file_set, version)
Rails.logger.debug "[IMPORT] Downloading #{version} to #{filename_on_disk}"

source_request = sufia6_version_open_uri(version[:uri])
Expand Down Expand Up @@ -81,5 +81,10 @@ def characterize(file_set, filename_on_disk)
def f3_to_f4_migration_date?(date)
date.starts_with?("2015-04-11")
end

def temp_file_name(file_set, version)
label = file_set.label.gsub(/[^0-9A-Za-z.\-]/, '_')
File.join Rails.root, "tmp/uploads", "#{file_set.id}_#{version[:label]}_#{label}"
end
end
end
18 changes: 12 additions & 6 deletions spec/lib/scholarsphere/import/version_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@
context "good http" do
before do
allow(Hydra::Works::VirusCheckerService).to receive(:file_has_virus?).and_return(false)
FileUtils.copy version1.to_path, File.join(Rails.root, "tmp/uploads", "#{file_set.id}_version1_#{file_set.label}")
FileUtils.copy version2.to_path, File.join(Rails.root, "tmp/uploads", "#{file_set.id}_version2_#{file_set.label}")
copy_version(version1, 'version1', file_set)
copy_version(version2, 'version2', file_set)
end
it "creates versions" do
expect(Net::HTTP).to receive(:start).twice
expect(CharacterizeJob).to receive(:perform_now).with(file_set, anything, /.*version2_my label.txt/).and_return(true)
expect(CharacterizeJob).to receive(:perform_now).with(file_set, anything, /.*version2_my_label.txt/).and_return(true)
builder.build(file_set, versions)
expect(output_file.versions.all.map(&:label)).to contain_exactly("version1", "version2")
expect(output_file.content).to eq("hello world! version2")
Expand Down Expand Up @@ -107,14 +107,14 @@

before do
allow(Hydra::Works::VirusCheckerService).to receive(:file_has_virus?).and_return(false)
FileUtils.copy version1.to_path, File.join(Rails.root, "tmp/uploads", "#{file_set.id}_version1_#{file_set.label}")
FileUtils.copy version2.to_path, File.join(Rails.root, "tmp/uploads", "#{file_set.id}_version2_#{file_set.label}")
copy_version(version1, 'version1', file_set)
copy_version(version2, 'version2', file_set)
file_set.date_uploaded = DateTime.parse("2013-03-09T20:43:36.592+00:00")
end

it "changes the date" do
expect(Net::HTTP).to receive(:start).twice
expect(CharacterizeJob).to receive(:perform_now).with(file_set, anything, /.*version2_my label.txt/).and_return(true)
expect(CharacterizeJob).to receive(:perform_now).with(file_set, anything, /.*version2_my_label.txt/).and_return(true)
builder.build(file_set, versions)
expect(output_file.versions.all.map(&:label)).to contain_exactly("version1", "version2")
expect(output_file.content).to eq("hello world! version2")
Expand All @@ -124,4 +124,10 @@
end
end
end

def copy_version(version, version_label, file_set)
to_path = File.join(Rails.root, "tmp/uploads", "#{file_set.id}_#{version_label}_#{file_set.label.tr(' ', '_')}")
puts to_path
FileUtils.copy version.to_path, to_path
end
end

0 comments on commit 76027a3

Please sign in to comment.