Skip to content

Commit a42e1b4

Browse files
committed
Fix SanitizedFile#move_to wrongly detects content_type based on the path before move
Fixes #2495, Closes #2496
1 parent 8c6743c commit a42e1b4

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/carrierwave/sanitized_file.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ def move_to(new_path, permissions=nil, directory_permissions=nil, keep_filename=
182182
move!(new_path)
183183
chmod!(new_path, permissions)
184184
if keep_filename
185-
self.file = {:tempfile => new_path, :filename => original_filename, :content_type => content_type}
185+
self.file = {:tempfile => new_path, :filename => original_filename, :content_type => @content_type}
186186
else
187-
self.file = {:tempfile => new_path, :content_type => content_type}
187+
self.file = {:tempfile => new_path, :content_type => @content_type}
188188
end
189189
self
190190
end

spec/sanitized_file_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,18 @@
371371
expect(sanitized_file.move_to(file_path("gurr.png"))).to eq(sanitized_file)
372372
end
373373

374-
it "should convert the file's content type" do
374+
it "should preserve the file's content type" do
375+
sanitized_file.content_type = 'application/octet-stream'
376+
sanitized_file.move_to(file_path("new_dir","gurr.png"))
377+
378+
expect(sanitized_file.content_type).to eq("application/octet-stream")
379+
end
380+
381+
it "should detect content type correctly using MagicMime when content_type is not set" do
382+
sanitized_file.content_type = nil
375383
sanitized_file.move_to(file_path("new_dir","gurr.png"))
376384

377-
expect(sanitized_file.content_type).to eq("image/jpeg")
385+
expect(sanitized_file.content_type).to eq("invalid/invalid")
378386
end
379387

380388
context 'target path only differs by case' do

0 commit comments

Comments
 (0)