从带有扩展名的网址上传

从带有扩展名的网址上传

本文介绍了回形针:从带有扩展名的网址上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在S3存储上通过回形针上传图片。
我使用:

  Ruby 1.9.3 
Rails 3.2.6
paperclip 3.1 .3
aws-sdk 1.3.9

我有我的图片模型:

  class资产

has_attached_file:资产,
:styles => {:thumb => 60x60>},
:storage => :s3,
:s3_credentials => #{Rails.root} /config/s3.yml,
:path => /pictures/:id/:style.:extension

validates_attachment_content_type:asset,:content_type => ['image / gif','image / jpeg','image / png','image / x-ms-bmp']

end

所以基本上我是这样从URL下载我的文件的:

  picture = Asset.new(asset:open(http://www.my_url.com/my_picture.jpg))
picture.save

但是它保存了一个不好的file_name文件,并且它没有设置文件的扩展名:

 #<资产id:5,asset_file_name:open-uri20120717-6028-1k3f7xz,asset_content_type:image / jpeg,asset_update_at:nil,asset_file_size:91565,created_at: 2012-07-17 12:41:40,updated_at:2012-07-17 12:41:40> 
p.asset.url
=> http://s3.amazonaws.com/my_assets_path/pictures/5/original。

您可以看到没有扩展名。



我找到了解决方法,但我确信我可以有更好的方法。这个解决方案是在我的电脑上复制文件,然后我像这样在S3上发送它:

  filename =#{Rails。 root} /tmp/my_picture.jpg
open(filename,'wb')do | file |
档案<< open(http://www.my_url.com/my_picture.jpg).read
end

picture = Asset :: Picture.new(asset:open(filename))
picture.save

这适用于我的电脑:

 #< Asset :: Picture id:10,asset_file_name:my_picture.jpg,asset_content_type:image / jpeg,asset_update_at:nil,asset_file_size:91565, created_at:2012-07-17 12:45:30,updated_at:2012-07-17 12:45:30> 
p.asset.url
=>

然而,我不知道这种方法是否可以在Heroku上工作(我托管我的应用程序)。



没有更好的方法,文件?

解决方案

好时机。我刚刚发送了一个请求,在几个小时前(2012年7月20日)进行了修补,这应该让您的生活变得轻松自在。

  self.asset = URI.parse(http://s3.amazonaws.com/blah/blah/blah.jpg)

这将下载您的jpeg图像,确保文件名为blah.jpg,内容类型为'image / jpg'。

回形针版本> 3.1.3(您需要将它链接到github repo,直到它被发布)。
$ b

更新:确认使用回形针版本> = 3.1.4

p>

I would like to upload pictures from URLs by paperclip on S3 storage.I work with :

Ruby 1.9.3
Rails 3.2.6
paperclip 3.1.3
aws-sdk 1.3.9

I have my Picture Model:

class Asset

  has_attached_file :asset,
    :styles => {:thumb => "60x60>"},
    :storage => :s3,
    :s3_credentials => "#{Rails.root}/config/s3.yml",
    :path => "/pictures/:id/:style.:extension"

  validates_attachment_content_type :asset, :content_type => ['image/gif', 'image/jpeg', 'image/png', 'image/x-ms-bmp']

end

So basically I made this to download my file from an URL:

picture = Asset.new(asset: open("http://www.my_url.com/my_picture.jpg"))
picture.save

But it save my file with a bad file_name and it doesn't set the extension of the file :

#<Asset id: 5, asset_file_name: "open-uri20120717-6028-1k3f7xz", asset_content_type: "image/jpeg", asset_update_at: nil, asset_file_size: 91565, created_at: "2012-07-17 12:41:40", updated_at: "2012-07-17 12:41:40">
p.asset.url
  => http://s3.amazonaws.com/my_assets_path/pictures/5/original.

As you can see there is no extension.

I found a method to solve it but I'm sure I can have a better way. This solution is to copy the file on my computer then I send it on S3 like this:

filename = "#{Rails.root}/tmp/my_picture.jpg"
open(filename, 'wb') do |file|
  file << open("http://www.my_url.com/my_picture.jpg").read
end

picture = Asset::Picture.new(asset: open(filename))
picture.save

This works on my computer:

#<Asset::Picture id: 10, asset_file_name: "my_picture.jpg", asset_content_type: "image/jpeg", asset_update_at: nil, asset_file_size: 91565, created_at: "2012-07-17 12:45:30", updated_at: "2012-07-17 12:45:30">
p.asset.url
  => "http://s3.amazonaws.com/assets.tests/my_assets_path/10/original.jpg"

However I don't know if this method will work on Heroku (I host my app on it).

There is not a better way without pass by a temporary file?

解决方案

Good timing. I just sent a pull request that was patched in a few hours ago (Jul 20 2012) which should make your life real easy.

self.asset = URI.parse("http://s3.amazonaws.com/blah/blah/blah.jpg")

This will download your jpeg image, ensure that the filename is blah.jpg and the content type is 'image/jpg'

Paperclip version > 3.1.3 (you'll need to link it to github repo until it gets released).

UPDATE: confirmed working with paperclip version >= 3.1.4

这篇关于回形针:从带有扩展名的网址上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:19