我如何从Base64获取文件类型

时间:2016-03-03 23:51:39

标签: ruby-on-rails ruby image

您好我正在创建一个api并使用Paperclip和Base64上传图片。

这是我的方法。我希望每个文件都被称为avatarimage_type)。

如何获得image_type

这是代码。

    9: def set_image(image_as_base64)
 => 10:   binding.pry
    11:   file = Paperclip.io_adapters.for(image_as_base64)
    12:   file.original_filename = "avatar.gif"
    13:   self.avatar = file
    14: end

4.2.5.1@2.2.4 (#<Profile:0x007f9eabb58f00>)> image_as_base64
=> "data:image/gif;base64,R0lGODlhZABkAPABAAAAAP///yH5BAAAAAAALAAAAABkAGQAAAL+hI+hy+jAYnhJLnrsxVBP7n1YyHVaSYIeuqqna7Hv2NIzldoxLMm73uNFfEHgUNhAboxJ5hJXg94eOenPWsQenQriVtsEP6lR8tRRNV/VWfbXHYaP0WX6uXLPhOr4td3f1/YnGPg2aFgYd6iYONfYpQTJJSImWUkpZ5mJueiol2eQ1qk5yvlo+gmYSriK2Mr46hnKF0t66gU7C2qiqtvLy+obDOwqXEyciyyrbFuLapwsehv5zFwt7VyCu4wNzU3r3Rx+DW69LW5OfU6+250+uR6vPg9Pfylfn39vv4m/r9+PXyl/AQEOFDjNYMJ/DAs2PKjQmbuJ5Si2q4jxosb+XxY5ZvS4cVhHkR9Jhjw2EmVJlSejrXTZ8ltMdClhgrxpEidLnTZz+tz5syfQYwQhPlzoMKlRpUiXOm0KNRtCqRHHFY1qdWrWqu+OUvW6FWxXpl/JhjU79mlZtWfZpsX6di3cmjL3BK17l+ZLvEOF+uX7V+9MdjwBGxZcGHFewouvyn3cdu5exX0PM65MOfBlzY4jQ47rOTTo0XTRdSY9ebPl06VVZ16tFfVg1qlpz47d2nZi17x1N8ZdG/htrrmF7/aNuTe14sSDNx8ulnl059Oho5V+nXp2626xd9f+nbtk8Z+9jz9u/Pdz9Nqqs3evHn5y5JzTz7dff3387e9C+e8P3x+A/503YHngEXiffgnKlx+DdimnYIP+LTihhAJSeKGFCGpoIHmimddhgBvCFiGJDr6GIoQnqlihiS2meFoBADs="
4.2.5.1@2.2.4 (#<Profile:0x007f9eabb58f00>)> file = Paperclip.io_adapters.for(image_as_base64)
=> Paperclip::DataUriAdapter: data
4.2.5.1@2.2.4 (#<Profile:0x007f9eabb58f00>)> file.original_filename = "avatar.gif"
=> "avatar.gif"
4.2.5.1@2.2.4 (#<Profile:0x007f9eabb58f00>)> self.avatar = file
=> Paperclip::DataUriAdapter: avatar.gif
4.2.5.1@2.2.4 (#<Profile:0x007f9eabb58f00>)> self
=> #<Profile:0x007f9eabb58f00
 id: 657,
 user_id: 378,
 birthday: Tue, 29 Apr 2003,
 bio: "Quo incidunt labore et et et. Ea quasi cupiditate ipsam aut. Dolore nihil libero quia ipsum.",
 phone: "(899) 372-6477",
 address_line_1: "Bobby Freeway",
 address_line_2: "Llewellyn Shoal",
 suburb: "New Ryann",
 state: "Utah",
 postcode: "70026",
 country_code: "PE",
 first_name: "Pansy",
 last_name: "Medhurst",
 avatar_file_name: "avatar.gif",
 avatar_content_type: "image/gif",
 avatar_file_size: 617,
 avatar_updated_at: Thu, 03 Mar 2016 23:50:26 UTC +00:00>
4.2.5.1@2.2.4 (#<Profile:0x007f9eabb58f00>)>

3 个答案:

答案 0 :(得分:2)

您可以使用作为Rails依赖项的Rack:Mime

您可以查看以下答案:

Rails: how to get a file extension/postfix based on the mime type

答案 1 :(得分:1)

执行此操作的方法如下。 content_type方法为您提供了一个`&#34; image / jpg&#34;我只想说明一下:

  def add_avatar(image_as_base64)
    file = Paperclip.io_adapters.for(image_as_base64)
    file_name = change_file_name(file)
    file.original_filename = file_name
    self.avatar = file
  end

  def change_file_name(file)
    file_name = file.content_type
    file_name.gsub("image/", "avatar.")
  end

答案 2 :(得分:0)

由于所有MIME类型都有“/”作为分隔符(请参阅http://www.sitepoint.com/web-foundations/mime-types-complete-list/

您可以使用“avatar_content_type”值返回mime类型并获取如下类型:

image_type = file.avatar_content_type.split("/")[1]
# OUTPUT: gif

现在你可以建立这样的欲望名称:

name = "avatar (#{image_type})"