correct regression around file renaming

This commit is contained in:
Sam
2018-08-20 16:08:05 +10:00
parent 038de4e037
commit f5fe58384f
3 changed files with 14 additions and 5 deletions

View File

@ -78,11 +78,20 @@ class UploadCreator
fixed_original_filename = nil fixed_original_filename = nil
if is_image if is_image
current_extension = File.extname(@filename).downcase.sub("jpeg", "jpg")
expected_extension = ".#{image_type}".downcase.sub("jpeg", "jpg")
# we have to correct original filename here, no choice # we have to correct original filename here, no choice
# otherwise validation will fail and we can not save # otherwise validation will fail and we can not save
# TODO decide if we only run the validation on the extension # TODO decide if we only run the validation on the extension
if File.extname(@filename) != ".#{image_type}" if current_extension != expected_extension
fixed_original_filename = "#{@filename}_fixed.#{image_type}" basename = File.basename(@filename, current_extension)
if basename.length == 0
basename = "image"
end
fixed_original_filename = "#{basename}#{expected_extension}"
end end
end end

View File

@ -41,7 +41,7 @@ RSpec.describe UploadCreator do
expect(upload.extension).to eq('png') expect(upload.extension).to eq('png')
expect(File.extname(upload.url)).to eq('.png') expect(File.extname(upload.url)).to eq('.png')
expect(upload.original_filename).to eq('png_as.bin_fixed.png') expect(upload.original_filename).to eq('png_as.png')
end end
end end
@ -65,7 +65,7 @@ RSpec.describe UploadCreator do
expect(upload.extension).to eq('jpeg') expect(upload.extension).to eq('jpeg')
expect(File.extname(upload.url)).to eq('.jpeg') expect(File.extname(upload.url)).to eq('.jpeg')
expect(upload.original_filename).to eq('logo.png_fixed.jpeg') expect(upload.original_filename).to eq('logo.jpg')
end end
end end
end end

View File

@ -212,7 +212,7 @@ describe UploadsController do
get "/uploads/#{site}/#{upload.sha1}.json" get "/uploads/#{site}/#{upload.sha1}.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(response.headers["Content-Disposition"]).to eq("attachment; filename=\"image_no_extension_fixed.png\"") expect(response.headers["Content-Disposition"]).to eq("attachment; filename=\"image_no_extension.png\"")
end end
it "handles file without extension" do it "handles file without extension" do