5

我想从我的旧网站导入一些图标。这些图标的大小小于 10kb。因此,当我尝试导入图标时,它会返回 stringio.txt 文件。

require "open-uri"
class Category < ActiveRecord::Base
   has_attached_file :icon,  :path => ":rails_root/public/:attachment/:id/:style/:basename.:extension"
  def icon_from_url(url)
    self.icon = open(url)
   end    
end

在 rake 任务中。

   category = Category.new
   category.icon_from_url "https://xyz.com/images/dog.png"
   category.save
4

5 回答 5

36

尝试:

def icon_from_url(url)
  extname = File.extname(url)
  basename = File.basename(url, extname)

  file = Tempfile.new([basename, extname])
  file.binmode

  open(URI.parse(url)) do |data|  
    file.write data.read
  end

  file.rewind

  self.icon = file
end
于 2011-06-12T22:12:04.017 回答
9

要覆盖 Paperclip 中“假文件上传”的默认文件名(stringio.txt在小文件上或在大文件上几乎随机的临时名称),您有两种主要可能性:

original_filename在 IO 上定义一个:

def icon_from_url(url)
  io = open(url)
  io.original_filename = "foo.png"
  self.icon = io
end

您还可以从 URI 中获取文件名:

io.original_filename = File.basename(URI.parse(url).path)

或替换:basename您的:path

has_attached_file :icon, :path => ":rails_root/public/:attachment/:id/:style/foo.png", :url => "/:attachment/:id/:style/foo.png"

改的:url时候记得一定要改:path,否则icon.url方法会出错。

您还可以定义自己的自定义插值(例如:rails_root/public/:whatever)。

于 2011-06-13T07:48:41.830 回答
1

我想你快到了,尝试打开解析的 uri,而不是字符串。

require "open-uri"
class Category < ActiveRecord::Base
   has_attached_file :icon,  :path =>:rails_root/public/:attachment/:id/:style/:basename.:extension"
  def icon_from_url(url)
    self.icon = open(URI.parse(url))
  end    
end

当然这不处理错误

于 2011-06-12T21:55:02.810 回答
1

您还可以禁止 OpenURI 创建 StringIO 对象,并强制它创建临时文件。看到这个答案:

为什么 Ruby open-uri 的 open 在我的单元测试中返回一个 StringIO,但在我的控制器中返回一个 FileIO?

于 2013-03-22T19:37:31.787 回答
-2

过去,我发现检索远程文件最可靠的方法是使用命令行工具“wget”。以下代码大部分是直接从现有的生产 (Rails 2.x) 应用程序中复制而来,并进行了一些调整以适合您的代码示例:

class CategoryIconImporter
  def self.download_to_tempfile (url)
    system(wget_download_command_for(url))
    @@tempfile.path
  end

  def self.clear_tempfile
    @@tempfile.delete if @@tempfile && @@tempfile.path && File.exist?(@@tempfile.path)
    @@tempfile = nil
  end

  def self.set_wget
    # used for retrieval in NrlImage (and in future from other sies?)
    if !@@wget
      stdin, stdout, stderr = Open3.popen3('which wget')
      @@wget = stdout.gets
      @@wget ||= '/usr/local/bin/wget'
      @@wget.strip!
    end
  end
  def self.wget_download_command_for (url)
    set_wget
    @@tempfile = Tempfile.new url.sub(/\?.+$/, '').split(/[\/\\]/).last
    command = [ @@wget ]
    command << '-q'
    if url =~ /^https/
      command << '--secure-protocol=auto'
      command << '--no-check-certificate'
    end
    command << '-O'
    command << @@tempfile.path
    command << url
    command.join(' ')
  end

  def self.import_from_url (category_params, url)
    clear_tempfile

    filename = url.sub(/\?.+$/, '').split(/[\/\\]/).last
    found = MIME::Types.type_for(filename)
    content_type = !found.empty? ? found.first.content_type : nil

    download_to_tempfile url

    nicer_path = RAILS_ROOT + '/tmp/' + filename
    File.copy @@tempfile.path, nicer_path

    Category.create(category_params.merge({:icon => ActionController::TestUploadedFile.new(nicer_path, content_type, true)}))
  end
end

rake 任务逻辑可能如下所示:

[
  ['Cat', 'cat'],
  ['Dog', 'dog'],
].each do |name, icon|
  CategoryIconImporter.import_from_url {:name => name}, "https://xyz.com/images/#{icon}.png"
end

这使用 mime-types gem 进行内容类型发现:

gem 'mime-types', :require => 'mime/types'
于 2011-06-15T02:25:20.817 回答