基于 request.ssl 通过 HTTPS 动态提供文件?带有附件_fu

发布于 2024-08-04 05:48:49 字数 619 浏览 5 评论 0原文

我看到 attachment_fu 中有一个 :user_ssl 选项,它检查 amazon_s3.yml 文件,以便通过 https:// 提供文件

在 s3_backend.rb 中,您有此方法:

    def self.protocol
      @protocol ||= s3_config[:use_ssl] ? 'https://' : 'http://'
    end

但这会使其通过 SSL 为所有 s3 附件提供服务。我想使其动态化,具体取决于当前请求是否是通过 https:// 发出的,即:

  if request.ssl?
    @protocol = "https://"
  else
    @protocol = "http://"  
  end

我怎样才能使其以这种方式工作?我尝试修改该方法,然后收到 NameError: undefined local variable or method `request' for Technoweenie::AttachmentFu::Backends::S3Backend:Module 错误

I see there is a :user_ssl option in attachment_fu which checks the amazon_s3.yml file in order to serve files via https://

In the s3_backend.rb you have this method:

    def self.protocol
      @protocol ||= s3_config[:use_ssl] ? 'https://' : 'http://'
    end

But this then makes it serve ALL s3 attachments with SSL. I'd like to make it dynamic depending if the current request was made with https:// i.e:

  if request.ssl?
    @protocol = "https://"
  else
    @protocol = "http://"  
  end

How can I make it work in this way? I've tried modifying the method and then get the NameError: undefined local variable or method `request' for Technoweenie::AttachmentFu::Backends::S3Backend:Module error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灼疼热情 2024-08-11 05:48:50

问题在于您正在修改的方法 (Technoweenie::AttachmentFu::Backends::AWS::S3.protocol) 是静态的,并且无权访问相关文件或请求。您要修改的是 Technoweenie::AttachmentFu::Backends::AWS::S3#s3_url(thumbnail)。您必须添加一个选项参数,以便您的控制器可以传入是否需要 SSL,因为此模型级包不了解控制器级问题,例如“当前请求”(也不应该)。

然而,真正的答案是“你可能不想这样做”。如果客户说“我们有一个免费增值模式,其中只有我们的付费客户才能获得照片的 SSL 传输”,您应该反驳:“实际上,破坏 SSL 文件传输更困难,而且可能只会在未来引入错误。让我们考虑提供另一种免费增值选项。”如果客户并不真正关心,您不妨为所有上传打开 SSL。

The problem is that the method you're modifying (Technoweenie::AttachmentFu::Backends::AWS::S3.protocol) is static and does not have access to the file or request in question. The one you want to modify is Technoweenie::AttachmentFu::Backends::AWS::S3#s3_url(thumbnail). You'll have to add an options argument so your controller can pass in whether it wants SSL or not, since this model-level package has no understanding of controller-level issues like "current request" (nor should it).

The real answer, though, is "you probably don't want to do this." If the customer is saying something like "we have a freemium model wherein only our paying customers get SSL transfers of their photos," you should push back: "it's actually harder to cripple SSL file transfers, and it's likely to just introduce bugs down the road. Let's think of another freemium option to offer." If the customer doesn't really care, you might as well just turn SSL on for all uploads.

凉城 2024-08-11 05:48:50

这是一个需要正确解决的重大问题,否则后果将非常严重(特别是如果您不在 IE 中进行测试,错误和警告可能会被您忽略)。我的解决方案是将以下内容放入 ApplicationController

around_filter :set_attachment_fu_protocol

def set_attachment_fu_protocol
  protocol = Technoweenie::AttachmentFu::Backends::S3Backend.instance_variable_get(:@protocol)
  Technoweenie::AttachmentFu::Backends::S3Backend.instance_variable_set(:@protocol, request.protocol)
  yield
ensure
  Technoweenie::AttachmentFu::Backends::S3Backend.instance_variable_set(:@protocol, protocol)
end

该解决方案被设计为具有以下属性:

  • 不需要修补 Attachment_fu
  • 根据请求设置 S3 后端的协议
  • 即使出现异常也重置协议发生
  • 如果从控制台运行,则保留默认的 :use_ssl 设置
  • 不需要 around_filter 是通用的,因为它总是在每次请求后将其重置为原始状态

This is significant issue that needs to be solved correctly, or the implications are quite nasty (particularly if you don't test in IE, the errors and warnings may slip by you). My solution is to put the following in ApplicationController

around_filter :set_attachment_fu_protocol

def set_attachment_fu_protocol
  protocol = Technoweenie::AttachmentFu::Backends::S3Backend.instance_variable_get(:@protocol)
  Technoweenie::AttachmentFu::Backends::S3Backend.instance_variable_set(:@protocol, request.protocol)
  yield
ensure
  Technoweenie::AttachmentFu::Backends::S3Backend.instance_variable_set(:@protocol, protocol)
end

This solution was designed to have the following properties:

  • Doesn't require patching attachment_fu
  • Sets the protocol for S3 Backend per request
  • Resets the protocol even if an exception occurs
  • Preserves the default :use_ssl setting if you are running from the console
  • Doesn't require the around_filter to be universal since it always resets it to the original state after each request
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文