Skip to content

Commit

Permalink
Fix SanitizedFile#move_to wrongly detects content_type based on the p…
Browse files Browse the repository at this point in the history
…ath before move

Fixes #2495, Closes #2496
  • Loading branch information
mshibuya committed Jan 17, 2021
1 parent 8c6743c commit a42e1b4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/carrierwave/sanitized_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ def move_to(new_path, permissions=nil, directory_permissions=nil, keep_filename=
move!(new_path)
chmod!(new_path, permissions)
if keep_filename
self.file = {:tempfile => new_path, :filename => original_filename, :content_type => content_type}
self.file = {:tempfile => new_path, :filename => original_filename, :content_type => @content_type}
else
self.file = {:tempfile => new_path, :content_type => content_type}
self.file = {:tempfile => new_path, :content_type => @content_type}
end
self
end
Expand Down
12 changes: 10 additions & 2 deletions spec/sanitized_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,18 @@
expect(sanitized_file.move_to(file_path("gurr.png"))).to eq(sanitized_file)
end

it "should convert the file's content type" do
it "should preserve the file's content type" do
sanitized_file.content_type = 'application/octet-stream'
sanitized_file.move_to(file_path("new_dir","gurr.png"))

expect(sanitized_file.content_type).to eq("application/octet-stream")
end

it "should detect content type correctly using MagicMime when content_type is not set" do
sanitized_file.content_type = nil
sanitized_file.move_to(file_path("new_dir","gurr.png"))

expect(sanitized_file.content_type).to eq("image/jpeg")
expect(sanitized_file.content_type).to eq("invalid/invalid")
end

context 'target path only differs by case' do
Expand Down

0 comments on commit a42e1b4

Please sign in to comment.