虾资产管道图像

时间:2014-07-16 07:06:50

标签: ruby-on-rails refinerycms prawn

我正在使用RefineryCMS 2.1.2和Prawn。我有一个属性引擎,其中我有图像的属性。我可以使用image_fu在show模板中显示这些内容,但是当我尝试用这样的图片创建我的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

我在开发中尝试过它&amp;我的电脑上的生产模式都有同样的问题。如果我查看http://localhost:3000/system/images/W1siZiIsIjIwMTQvMDcvMTYvMTZfNTZfNDVfOTY3XzEuanBnIl1d/1.jpg我可以看到图片

在我的produ.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/

它使用'fog'与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网址。如果我部署此代码,我会收到HTTP 403禁止错误,因为我正在尝试直接访问URL而不是我的S3会话。

更新

通过Fog连接失败,因为这是传递实际文件,我认为prawn图像需要一个路径。

    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相比,是否有更好的方法来处理从本地存储读取的内容,这取决于我是否正在运行生产?

2 个答案:

答案 0 :(得分:5)

您需要向对象提供存储文件的目录 你可以这样做:

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

答案 1 :(得分:0)

如果您从大虾中查看images.rb:145https://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')

此处,它尝试从文件系统中打开图像。我认为你提供的一些Prawn路径是错误的,或者是丢失的。使用调试器语句挂钩到images.rb,然后查看变量的值。