【问题标题】:Prawn asset pipeline images对虾资产管道图像
【发布时间】:2014-09-06 14:23:04
【问题描述】:

我正在使用 RefineryCMS 2.1.2 和 Prawn。我有一个属性引擎,其中有图像属性。我可以使用image_fu 在显示模板中显示这些内容,但是当我尝试使用这样的图片创建我的 pdf 文件时

class Refinery::Properties::PropertyPdf <  Prawn::Document
  def initialize(property)
    super()
    image @property.property_images.first.image.url, fit: [300,300]
  end
end

我收到此错误

ArgumentError (/system/images/W1siZiIsIjIwMTQvMDcvMTYvMTZfNTZfNDVfOTY3XzEuanBnIl1d/1.jpg not found):
  prawn (1.1.0) lib/prawn/images.rb:145:in `verify_and_open_image'
  prawn (1.1.0) lib/prawn/images.rb:79:in `build_image_object'
  prawn (1.1.0) lib/prawn/images.rb:67:in `image'
  vendor/extensions/properties/app/models/refinery/properties/property_pdf.rb:29:in `pictures'
  vendor/extensions/properties/app/models/refinery/properties/property_pdf.rb:9:in `initialize'
  vendor/extensions/properties/app/controllers/refinery/properties/properties_controller.rb:23:in `new'
  vendor/extensions/properties/app/controllers/refinery/properties/properties_controller.rb:23:in `block (2 levels) in show'
  actionpack (3.2.18) lib/action_controller/metal/mime_responds.rb:196:in `call'
  actionpack (3.2.18) lib/action_controller/metal/mime_responds.rb:196:in `respond_to'
  vendor/extensions/properties/app/controllers/refinery/properties/properties_controller.rb:20:in `show'
  actionpack (3.2.18) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (3.2.18) lib/abstract_controller/base.rb:167:in `process_action'
  actionpack (3.2.18) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (3.2.18) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
  activesupport (3.2.18) lib/active_support/callbacks.rb:447:in `_run__945948839670585827__process_action__3030439923241519075__callbacks'
  activesupport (3.2.18) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.18) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
  activesupport (3.2.18) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.18) lib/abstract_controller/callbacks.rb:17:in `process_action'
  actionpack (3.2.18) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (3.2.18) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
  activesupport (3.2.18) lib/active_support/notifications.rb:123:in `block in instrument'
  activesupport (3.2.18) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (3.2.18) lib/active_support/notifications.rb:123:in `instrument'
  actionpack (3.2.18) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
  actionpack (3.2.18) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
  activerecord (3.2.18) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  newrelic_rpm (3.8.1.221) lib/new_relic/agent/instrumentation/rails3/action_controller.rb:38:in `block in process_action'
  newrelic_rpm (3.8.1.221) lib/new_relic/agent/instrumentation/controller_instrumentation.rb:357:in `perform_action_with_newrelic_trace'
  newrelic_rpm (3.8.1.221) lib/new_relic/agent/instrumentation/rails3/action_controller.rb:37:in

我已经在我的计算机上以开发和生产模式进行了尝试,并且都遇到了同样的问题。如果我看http://localhost:3000/system/images/W1siZiIsIjIwMTQvMDcvMTYvMTZfNTZfNDVfOTY3XzEuanBnIl1d/1.jpg,我可以看到图像

在我的 production.rb 中,我有以下内容

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = true

  # Generate digests for assets URLs
  config.assets.digest = true

如果我使用以下内容,它仅适用于开发模式。

  image "public/system/refinery/images/#{first_image.image_uid}", fit: [300,300] 

更新

看起来 RefineryCMS 使用 Dragonfly 来存储图像

https://markevans.github.io/dragonfly/urls/

它使用“雾”与 Amazon S3 存储通信。

我可以在 Prawn 中使用以下内容从 S3 下载文件

require 'open-uri'
...
image open("https://s3.amazonaws.com/<my bucket>/2014/07/16/01/31/47/621/3.jpg")

这适用于我在生产模式下运行的本地计算机。现在我只需要找出如何获取图像的 S3 url。如果我部署此代码,则会收到 HTTP 403 禁止错误,因为我尝试直接访问 URL 而不是我的 S3 会话。

更新

通过 Fog 连接失败,因为这是将实际文件传递给它,我认为虾图像需要路径。

    con=Fog::Storage.new({provider: 'AWS', aws_access_key_id: ENV['S3_KEY'], aws_secret_access_key: ENV['S3_SECRET']})
    d=con.directories.get("<my bucket>")
    file=d.files.get('2014/07/16/01/31/47/621/3.jpg')
    image file.body, fit:[30,30]

更新:混乱的解决方案

这是我目前凌乱的解决方案,以便我可以在生产和开发环境中工作。它有效,但看起来很糟糕。

images = @property.property_images.where("image_id > 0")
expire = (Time.now + 600).strftime('%s')

if ENV['RAILS_ENV']=="production"
  # Make a connection to Amazon S3 through Fog
  connection=Fog::Storage.new({provider: 'AWS', aws_access_key_id: ENV['S3_KEY'], aws_secret_access_key: ENV['S3_SECRET']})
  dir=connection.directories.get("dcre_production")
end

if images.count > 0

  # Display a large first image.
  first_image = images.first.image
  unless first_image.nil?
    if ENV['RAILS_ENV']=="production"
      # Download the first image from Amazon S3
      file=dir.files.get(first_image.image_uid)
      image open(file.url(expire)), fit:[300,300]
    else
      # Get the first image from local drive
      image "public/system/refinery/images/#{first_image.image_uid}", fit: [300,300]
    end
  end

  # Display thumbnails underneath
  images.each do |property_image|
    unless property_image.image.nil? 
      unless property_image.image.image_uid == first_image
        if ENV['RAILS_ENV']=="production"
          file=dir.files.get(property_image.image.image_uid)
          image open(file.url(expire)), fit:[70,70]
        else
          filename = property_image.image.image_uid
          image "public/system/refinery/images/#{filename}", fit: [70,70]
        end
      end
    end
  end
end

我已经可以在 RefineryCMS 中使用雾连接了吗?

与 S3 相比,是否有更好的方法来处理从本地存储读取数据,这取决于我是否正在运行生产环境?

【问题讨论】:

  • 你好,有没有办法用大虾AWS镜像包含?

标签: ruby-on-rails refinerycms prawn


【解决方案1】:

如果你查看大虾的images.rb:145 (https://github.com/prawnpdf/prawn/blob/master/lib/prawn/images.rb),你会看到以下代码:

  io_or_path = Pathname.new(io_or_path)
  raise ArgumentError, "#{io_or_path} not found" unless io_or_path.file?
  io = io_or_path.open('rb')

在这里,它尝试从文件系统打开图像。我认为您提供的某些对虾路径是错误的或丢失的。使用调试器语句挂钩到 images.rb,并查看变量的值。

【讨论】:

    【解决方案2】:

    你需要给大虾你的文件存放的目录。
    你可以这样做:

    image = Rails.root.join('app','assets','images','image.jpg').to_s
    

    【讨论】:

    • 我使用 heroku 进行部署,使用 Amazon S3 进行文件
    • 我认为在这种情况下您可能需要在本地复制文件。你可以使用copy_to_local_file
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2012-06-28
    • 1970-01-01
    • 2016-02-10
    • 2013-07-04
    • 1970-01-01
    相关资源
    最近更新 更多