1
我想允許用戶上傳.jpg
文件,但只有當他們實際上是.jpg
(例如,cat.gif
更名爲cat.jpg
會不工作carrierwave:拒絕.gif注意改名到JPG
目前。 ImageUploader.rb
我:
include CarrierWave::RMagick
include CarrierWave::MimeTypes
process :set_content_type
def extension_white_list
%w(jpg jpeg png)
end
,在我的Rspec的測試文件我測試了三種方式:
# Works
describe "with invalid mime type and invalid extension" do
before do
image.image = File.open(File.join(Rails.root, 'spec', 'support', 'image', 'images', 'executable.jpg')) # this is a .exe renamed to .jpg
end
it { image.should_not be_valid }
end
# Works
describe "with invalid mime type and invalid extension" do
before do
image.image = File.open(File.join(Rails.root, 'spec', 'support', 'image', 'images', 'test_gif.gif')) # this is a .gif
end
it { should_not be_valid }
end
# Doesn't work
describe "with invalid mime type and valid extension" do
before do
image.image = File.open(File.join(Rails.root, 'spec', 'support', 'image', 'images', 'test_gif.jpg')) # this is a .gif renamed to .jpg
end
it { image.should_not be_valid }
end
測試通過了前兩項,但第二項失敗。我不知道爲什麼,因爲我沒有在白名單上的gif
,我正在檢查MIME類型。
有什麼建議嗎?
(轉換GIF圖片JPG格式是我的備份,但我寧願只是拒絕他們。)
我認爲extension_white_list只檢查擴展。我認爲你需要實際檢查content_type屬性:https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Store-the-uploaded-file-size-and-content-type – micapam
第二個「描述「是」它不應該be_valid}「和其他人是」它{image.should_not ...「 你確定第二個」它「是主題? – kukrt
@micapam通過包括MimeTypes我在印象中,我正在檢查content_type:https://github.com/jnicklas/carrierwave#setting-the-content-type – pseudosudo