cba*_*ass 4 ruby-on-rails amazon-s3 paperclip ruby-on-rails-3
我写了一个rake任务,从wikipedia下载了一个名人的名字,但出于某种原因,当存储在S3上时,文件扩展名被删除或更改为.txt
否则该文件是正确的.
有任何想法吗?
从我的名人模特:
has_attached_file :pic,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_style => :medium,
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:style/:img_name.:extension"
Run Code Online (Sandbox Code Playgroud)
从我的佣金任务:
desc "Update celeb pics from wiki"
task :update_celeb_pics => [:environment] do
include ApplicationHelper
Celeb.all.each do |celeb|
if !celeb.name.start_with?("(")
puts celeb.name
celeb.pic = open(getImage(celeb.name))
celeb.save
end
end
end
Run Code Online (Sandbox Code Playgroud)
getImage方法是一个返回字符串的帮助器
require 'open-uri'
require 'uri'
module ApplicationHelper
def getInfo(name)
Nokogiri::XML(open(URI.escape("http://en.wikipedia.org/w/api.php?action=opensearch&search=#{name}&limit=1&namespace=0&format=xml")))
end
def nokoPage(name)
Nokogiri::XML(open(getURL(name)))
end
def getImage(name)
"http:" + nokoPage(name).css("table img")[0].attribute("src").value if !nokoPage(name).css("table img").empty?
end
def getDescription(name)
getInfo(name).css("Description").text
end
def getURL(name)
getInfo(name).css("Url").text
end
def getBday(name)
bday = nokoPage(name).css("span.bday")
return Date.parse(bday[0].text) if !bday.empty?
return Date.today
end
def getDday(name)
dday = nokoPage(name).css("span.dday")
return Date.parse(dday[0].text) if !dday.empty?
end
end
Run Code Online (Sandbox Code Playgroud)
这是因为
self.pic = open("http://something.com/bla/image.png")
Run Code Online (Sandbox Code Playgroud)
这不是最好的解决方案.就在昨天,我将一个Pull Request合并到Paperclip中,让你这样做
self.pic = URI.parse(getImage(name))
Run Code Online (Sandbox Code Playgroud)
这将确保您的pic的内容类型与下载的文件匹配,pic的文件名设置为下载的文件的名称.
你获得txt扩展的原因是因为open返回一个StringIO对象,该对象实际上将文件名命名为"stringio.txt".您的文件名可能由s3代码更改,但扩展名仍为".txt"
我建议你将你的Gemfile链接到paperclip的github repo,运行bundle再试一次.
干杯,Aditya
归档时间: |
|
查看次数: |
933 次 |
最近记录: |