diff --git a/config/initializers/number_to_human_size_converter.rb b/config/initializers/number_to_human_size_converter.rb new file mode 100644 index 000000000..e3041e580 --- /dev/null +++ b/config/initializers/number_to_human_size_converter.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true +# Source: https://github.com/rails/rails/issues/40054#issuecomment-674449143 +# Additional information: https://massive.io/file-transfer/gb-vs-gib-whats-the-difference/ +module ActiveSupport + module NumberHelper + class NumberToHumanSizeConverter < NumberConverter + private + + # Allows a base to be specified for the conversion + # 1024 was the default and that produces gigibytes + # 1000 produces gigabytes + def base + options[:base] || 1000 + end + end + end +end diff --git a/spec/models/s3_file_spec.rb b/spec/models/s3_file_spec.rb index f70950cc9..cb46bdfa2 100644 --- a/spec/models/s3_file_spec.rb +++ b/spec/models/s3_file_spec.rb @@ -33,6 +33,20 @@ end end + context "display file size" do + it "uses 1000 base by default when calculating display value" do + expect(s3_file.display_size).to eq "10.8 KB" + end + end + + describe "#number_to_human_size" do + it "honors the base if we pass it one" do + expect(s3_file.number_to_human_size(size)).to eq "10.8 KB" + expect(s3_file.number_to_human_size(size, base: 1000)).to eq "10.8 KB" + expect(s3_file.number_to_human_size(size, base: 1024)).to eq "10.5 KB" + end + end + context "safe_id" do it "calculates correct safe_id for files with spaces and non-alpha numeric characters" do expect(s3_file.safe_id).to eq "10-99999-123-abc-#{work.id}-filename--with-spaces--w-----chars-txt" diff --git a/spec/system/work_upload_s3_objects_spec.rb b/spec/system/work_upload_s3_objects_spec.rb index d48630958..04a5b5995 100644 --- a/spec/system/work_upload_s3_objects_spec.rb +++ b/spec/system/work_upload_s3_objects_spec.rb @@ -52,7 +52,7 @@ expect(page).to have_content upload_file_name expect(page).to have_content filename1 expect(page).to have_content filename2 - expect(page).to have_content "Total Size\n31.5 KB" + expect(page).to have_content "Total Size\n32.3 KB" expect(work.reload.pre_curation_uploads.length).to eq(3) end