无法使用回形针从API保存图像

时间:2018-04-15 13:09:01

标签: ruby-on-rails paperclip rails-api

我想在我的rails app中保存我的反应应用中的图片。 我发布到我的api: enter image description here

当我尝试保存时,我收到此错误:

{  
   "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,但我不知道在哪里。

1 个答案:

答案 0 :(得分:2)

您是否尝试过使用Paperclip的{​​{1}}将base64解码为附件对象?有点像:

io_adapter