簡體   English   中英

從S3的視圖中救援403禁止顯示的圖像

[英]Rescue 403 forbidden images in views from S3

我想在我的應用中從S3搶救每個403響應。 它們主要位於圖像標簽中,從而破壞了視圖中的圖像。 當圖像標簽沒有引發異常(它們只是視圖中的圖像已損壞)時,如何在ApplicationController做到這一點?

rescue_from ??? do |e|
  logger.debug "broken image: #{e}"
end

某些圖像在幫助器中呈現如下:

def user_image(img_style)
 "<img src='#{asset.image(img_style)}' />"
end

其中asset.image(img_style)返回一個URL(在這種情況下是禁止的)URL。

其他的就像:

<%= image_tag document.other_asset.image(:small) %>

我猜沒有單個父控制器(保存ApplicationController )。

但是,只有一個多態類( ImageAttachment )存儲圖像。 我是否應該事先在模型中獲取它並引發錯誤? 這不包括兩次提出請求嗎?

這個解決方案絕對可怕的,並且每個圖像的請求數量增加了一倍,但是為了完整起見:

def found?(style = :small)
  return true if URI.open(image(style))
rescue OpenURI::HTTPError => exception
  logger.debug exception.message
  false
end

顯然,更好的實現是在Javascript中進行,因此不必兩次加載資源,但是由於問題是從Ruby開始的,因此也將在那里結束。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM