基于 request.ssl 通过 HTTPS 动态提供文件?带有附件_fu
我看到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于您正在修改的方法 (
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 isTechnoweenie::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.
这是一个需要正确解决的重大问题,否则后果将非常严重(特别是如果您不在 IE 中进行测试,错误和警告可能会被您忽略)。我的解决方案是将以下内容放入
ApplicationController
该解决方案被设计为具有以下属性:
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
This solution was designed to have the following properties: