无法使用回形针保存来自 API 的图像

Can't save image from API with paperclip

我想在我的 rails 应用程序中保存来自我的 React 应用程序的图像。 我 post 这是我的 api:

当我尝试保存它时出现此错误:

{  
   "file":[  
      "has contents that are not what they are reported to be",
      "file type is not acceptable"
   ],
   "file_content_type":[  
      "file type is not acceptable"
   ]
}

我的模型是这样的:

class Photo < ApplicationRecord
  validates :title, presence: true

  belongs_to :user

  has_attached_file :file, styles: { square: "350x233#", medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
  validates_attachment_content_type :file, content_type: /\Aimage\/.*\z/, message: 'file type is not acceptable'
  validates_attachment_size :file, less_than: 10.megabytes, message: 'file size is more than 50MB'
end

和我的控制器:

def create
    uploaded_file = false

    if params[:photo][:image]
      tempfile = Tempfile.new(params[:photo][:filename])
      tempfile.binmode
      tempfile.write(Base64.decode64(params[:photo][:image]))

      uploaded_file = ActionDispatch::Http::UploadedFile.new(
        :tempfile => tempfile,
        :filename => params[:photo][:filename],
        :original_filename => params[:photo][:filename],
        :content_type => Mime::Type.lookup_by_extension(File.extname(params[:photo][:filename])[1..-1]).to_s
      )

      params[:photo][:file] = uploaded_file
    end

    params[:photo][:title] = params[:photo][:filename] if params[:photo][:title] == ""

    # @picture = Photo.new(photo_params)
    @photo = current_user.photos.build photo_params
    @photo.save

    render json: @photo.errors
  end

在我的日志中我发现了这个:

Unpermitted parameters: :image, :filename
Command :: file -b --mime '/tmp/homepage.jpg20180415-7-1wa7yr3'
[paperclip] Trying to link /tmp/homepage.jpg20180415-7-1wa7yr3 to /tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-jpgu5m.jpg
[paperclip] Trying to link /tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-jpgu5m.jpg to /tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-147ntu1.jpg
Command :: file -b --mime '/tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-147ntu1.jpg'
[paperclip] Content Type Spoof: Filename homepage.jpg (application/octet-stream from Headers, ["image/jpeg"] from Extension), content type discovered from file command: application/octet-stream. See documentation to allow this combination.
  [1m[35m (1.5ms)[0m  [1m[35mBEGIN[0m
[paperclip] Trying to link /tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-jpgu5m.jpg to /tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-ndp9hd.jpg
Command :: file -b --mime '/tmp/ba3988db0a3167093b1f74e8ae4a8e8320180415-7-ndp9hd.jpg'
[paperclip] Content Type Spoof: Filename homepage.jpg (application/octet-stream from Headers, ["image/jpeg"] from Extension), content type discovered from file command: application/octet-stream. See documentation to allow this combination.

我没听错。任何人都可以帮我上传吗? 我认为 content_type 不见了,但我不知道在哪里。

您是否尝试过使用 Paperclipio_adapter 将 base64 解码为附件对象?大致如下:

def create
    file = Paperclip.io_adapters.for(params[:photo][:image])
    file.original_filename = params[:photo][:filename]
    @photo = Photo.new(file: file, user: current_user)
    @photo.save
end