Carrierwave和雾正在改变文件扩展名

时间:2017-05-04 00:13:55

标签: amazon-s3 imagemagick carrierwave fog minimagick

这段代码的作用是接收SVG图像文件并将其转换为PNG格式。然后使用雾

将其上传到Amazon S3
# encoding: utf-8

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick
  include CarrierWave::MimeTypes

  storage :fog
  process :set_content_type

  def store_dir
    "uploads/#{model.class.to_s.underscore.pluralize}/#{model.id}"
  end

  def filename
    "#{secure_token}.#{original_filename.split('.')[1]}" if original_filename.present?
  end

  version :thumb do
    # process :resize_to_limit => [150, nil]
    process :convert_svg_to_png
    def full_filename (for_file = model.logo.file)
      puts original_filename
      if original_filename.present?
        extension = original_filename.split('.')[1]
        if extension == 'svg'
          "small_#{secure_token}.png"
        else
          "small_#{secure_token}.#{extension}"
        end
      end

    end
  end

  def extension_white_list
    %w(jpg jpe png svg)
  end

  protected

  def convert_svg_to_png
    manipulate! do |img|
      path = img.path
      new_tmp_path = File.join(Rails.root, 'tmp', 'svg_to_png', "/#{SecureRandom.uuid}.png")

      MiniMagick::Tool::Convert.new do |convert|
        convert << "-background" << "none"
        convert << "#{ path }"
        convert << new_tmp_path
      end

      img = MiniMagick::Image.open(new_tmp_path)
      img.format 'png'
      img
    end
  end

  def secure_token
    var = :"@#{mounted_as}_secure_token"
    model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
  end

end

问题在于我不确定我是否发送了正确的文件或者Amazon S3无法识别转换

如您所见,两个版本都是成功创建的

enter image description here

但是当我尝试下载png版本时点击&#34;下载为&#34;,

enter image description here

图片扩展名为svg

enter image description here

如果我将图像保存到本地驱动器并更改扩展名,它可以正常工作,它是一个png图像。你知道发生了什么吗?

0 个答案:

没有答案
相关问题