简体   繁体   English

Carrierwave将文件编码为base64作为进程

[英]Carrierwave encode file to base64 as process

I am trying to solve this. 我想解决这个问题。 I would like to encode file as base64 as one of the process. 我想将文件编码为base64作为其中一个过程。

My code so far looks like 到目前为止我的代码看起来像

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

  storage :file

  def extension_white_list
    %w(jpg jpeg png)
  end

  def store_dir
    "/tmp/images/#{model.class.to_s.underscore}/#{model.id}"
  end

  process :set_content_type
  process :format_to_jpg
  process :strip_metadata
  process :encode_base64

  def format_to_jpg
    manipulate! do |img|
      img.format 'jpg'
      img
    end
  end

  def strip_metadata
    manipulate! do |img|
      img.strip
      img
    end
  end

  def encode_base64
    #What should be here?
  end
end

I am not sure what I should place in the encode_base64 method. 我不确定我应该在encode_base64方法中放置什么。 The method for encoding is Base64.encode64() as parameter should be sent the file content (self.read probably). 编码方法是Base64.encode64()作为参数应该发送文件内容(可能是self.read)。 but I am not sure how I should do this to follow Carrierwave best practices. 但我不确定如何遵循Carrierwave的最佳实践。

Find out that I don't have to do anything special here. 发现我不需要做任何特别的事情。

def encode_base64
  File.write(path,Base64.encode64(File.read(path)))
end

does the magic 是魔术吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM