Skip to content

Commit

Permalink
Extract code to ensure attachment is masked
Browse files Browse the repository at this point in the history
Add new `ensure_masked` callback to the `AttachmentController`.
Currently just used by the `show` action but will be used for
`show_as_html` soon.
  • Loading branch information
gbp committed Oct 25, 2023
1 parent 30494ba commit 68d5dec
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions app/controllers/attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,11 @@ class AttachmentsController < ApplicationController
before_action :authenticate_attachment
before_action :authenticate_attachment_as_html, only: :show_as_html

around_action :ensure_masked, only: :show
around_action :cache_attachments, only: :show_as_html

def show
if @attachment.masked?
render body: @attachment.body, content_type: content_type
else
FoiAttachmentMaskJob.perform_once_later(@attachment)

Timeout.timeout(5) do
until @attachment.masked?
sleep 0.5
@attachment.reload
end
redirect_to(request.fullpath)
end
end

rescue Timeout::Error
redirect_to wait_for_attachment_mask_path(
@attachment.to_signed_global_id,
referer: verifier.generate(request.fullpath)
)
render body: @attachment.body, content_type: content_type
end

def show_as_html
Expand Down Expand Up @@ -135,6 +118,28 @@ def authenticate_attachment_as_html
raise ActiveRecord::RecordNotFound, 'Attachment HTML not found.'
end

def ensure_masked
if @attachment.masked?
yield
else
FoiAttachmentMaskJob.perform_once_later(@attachment)

Timeout.timeout(5) do
until @attachment.masked?
sleep 0.5
@attachment.reload
end
redirect_to(request.fullpath)
end
end

rescue Timeout::Error
redirect_to wait_for_attachment_mask_path(
@attachment.to_signed_global_id,
referer: verifier.generate(request.fullpath)
)
end

# special caching code so mime types are handled right
def cache_attachments
if !params[:skip_cache].nil?
Expand Down

0 comments on commit 68d5dec

Please sign in to comment.