【发布时间】:2020-05-14 14:13:48
【问题描述】:
所以我使用 Active Storage 将 PDF 附加到我的模型中,这一切都很好。作为我项目的一部分,我被要求在一个特定资源页面上显示 PDF 预览,该页面列出了网站上所有可用的 PDF 以供下载 (25+)。我正在使用标准配置 - PopplerPDFPreviewer & MuPDFPreviewer。
irb(main):001:0> Rails.application.config.active_storage.previewers
=> [ActiveStorage::Previewer::PopplerPDFPreviewer, ActiveStorage::Previewer::MuPDFPreviewer, ActiveStorage::Previewer::VideoPreviewer]
代码:
- @products.each do |product|
- if product.installation_guide.attached?
- if product.installation_guide.previewable?
= image_tag product.installation_guide.preview(resize: '200x200')
这也有效,但是,由于它不断地重新生成图像,我有时会收到超时和过多的请求消息。
@net_http_res #<Net::HTTPTooManyRequests 429 Too Many Requests readbody=true>
这不好!
现在api.rubyonrails.org 建议使用 .processed.service_url 生成一个稳定的 URL(不是视图),该 URL 将重定向到将由 .service_url返回的短期 URL >。
blob.preview(resize_to_limit: [100, 100]).processed.service_url
好的,但同样,这意味着我必须不断重新生成预览...有没有办法在上传 PDF 时生成预览,并保存该预览直到它被删除或替换?
【问题讨论】:
标签: ruby-on-rails rails-activestorage