如何让CarrierWave在其内容上添加正确的扩展名(取决于 )?例如,如果我上传文件“徽标”(PNG文件 没有扩展名),CarrierWave应将其保存为“logo.png”。并且文件“img.gif”(扩展名不正确的JPG文件)应分别保存为“img.jpg”。CarrierWave和正确的文件扩展名,具体取决于其内容
回答
有几件事你可以做,取决于你是否使用process
或version
来做到这一点。
如果是一个版本,carrierwave wiki有办法做条件版本。 https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Do-conditional-processing
version :big, :if => :png? do
process ...
end
protected
def png?(new_file)
new_file.content_type.include? 'png'
end
如果您使用的process
方法,你可能想看看这个:https://gist.github.com/995663。
添加这些到您的代码,以绕过约束process
具有
# create a new "process_extensions" method. It is like "process", except
# it takes an array of extensions as the first parameter, and registers
# a trampoline method which checks the extension before invocation
def self.process_extensions(*args)
extensions = args.shift
args.each do |arg|
if arg.is_a?(Hash)
arg.each do |method, args|
processors.push([:process_trampoline, [extensions, method, args]])
end
else
processors.push([:process_trampoline, [extensions, arg, []]])
end
end
end
# our trampoline method which only performs processing if the extension matches
def process_trampoline(extensions, method, args)
extension = File.extname(original_filename).downcase
extension = extension[1..-1] if extension[0,1] == '.'
self.send(method, *args) if extensions.include?(extension)
end
然后,您可以用它来打电话曾经被认为是过程,有选择地对每个文件类型
PNG = %w(png)
JPG = %w(jpg jpeg)
GIF = %w(gif)
def extension_white_list
PNG + JPG + GIF
end
process_extensions PNG, :resize_to_fit => [1024, 768]
process_extensions JPG, :...
process_extensions GIF, :...
的问题在于首先确定正确的内容。 Carrierwave使用MimeType gem,它从扩展名中确定其MIME类型。因为,在你的情况下,扩展是不正确的,你需要一个获得正确的MIME类型的替代方法。这是我能够想出的最佳解决方案,但它取决于使用RMagick gem读取图像文件的能力。
我遇到了这个问题,不得不为我的上传器覆盖默认的set_content_type方法。这假设你在你的Gemfile中有Rmagick宝石,这样你就可以从阅读图像中获得正确的mime类型,而不是做出最好的猜测。
注意:如果图像被仅支持JPG和PNG图像的Prawn使用,此功能特别有用。
上传类:
process :set_content_type
def set_content_type #Note we are overriding the default set_content_type_method for this uploader
real_content_type = Magick::Image::read(file.path).first.mime_type
if file.respond_to?(:content_type=)
file.content_type = real_content_type
else
file.instance_variable_set(:@content_type, real_content_type)
end
end
图片型号:
class Image < ActiveRecord::Base
mount_uploader :image, ImageUploader
validates_presence_of :image
validate :validate_content_type_correctly
before_validation :update_image_attributes
private
def update_image_attributes
if image.present? && image_changed?
self.content_type = image.file.content_type
end
end
def validate_content_type_correctly
if not ['image/png', 'image/jpg'].include?(content_type)
errors.add_to_base "Image format is not a valid JPEG or PNG."
return false
else
return true
end
end
end
在你的情况,你可以添加,改变在此基础上正确的MIME类型的扩展名的附加的方法(CONTENT_TYPE )。
让我走上正轨。 – phillyslick
- 1. PHP:如何提供正确的内容类型(取决于文件扩展名?)?
- 2. GetTempFileName()和正确的文件扩展名
- 3. 如何编写取决于其他扩展名的php扩展?
- 4. 正确的SpreadsheetML文件扩展名
- 5. 正确的C++文件扩展名
- 6. 获取原始文件扩展名和内容类型
- 7. 上传文件时获取不正确的文件扩展名和内容类型
- 8. Rails和Carrierwave,保存具有不同扩展名的版本
- 9. powershell - 提取文件名和扩展名
- 10. 无扩展名的文件获取内容
- 11. 获取JavaScript中内容类型的默认文件扩展名?
- 12. Sublime Text 2双引号字体样式取决于文件扩展名吗?
- 13. 移动具有特定文件名和扩展名的文件
- 14. 如何从内容类型获取文件扩展名?
- 15. 正确的MIME'text/enriched'文件格式的文件扩展名?
- 16. 什么是T4模板文件的正确文件扩展名?
- 17. 自动重命名批量文件扩展正确的文件
- 18. 扩展和加载正确的内容类型与Django模型
- 19. 使用正则表达式查找具有确切扩展名的文件(并且没有任何内容)
- 20. 用Python获取文件名和扩展名为hdfs的文件
- 21. Carrierwave如何获得文件扩展
- 22. 当内容扩展时,内容和包装不能正常扩展
- 23. 获取具有纯扩展名的文件名
- 24. ColdFusion REGEX - 确定文件的扩展名
- 25. 如何使用CarrierWave更正电影缩略图生成的文件扩展名
- 26. 具有相同扩展名但内容不同的文件的不同图标?
- 27. 从URL和带文件扩展名获取文件名
- 28. grep文件扩展名存在于文件内
- 29. 通过文件扩展名获取扩展名和图标的最快方法?
- 30. 使用mac中的.conf扩展名文件的内容
考虑版本路线,如果我想纠正N个版本的M扩展,该怎么办?我需要有M * N条件来处理这个问题吗? – lulalala