-
-
Notifications
You must be signed in to change notification settings - Fork 196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attachment background processing #7681
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
86f1178
Refactor attachment controller specs
gbp bcfaeeb
Active Job uniqueness
gbp 9974cf0
Add FoiAttachmentMaskJob
gbp 321dfe9
Update attachment masking
gbp 1dea003
Add FoiAttachment#masked_at column
gbp 4403837
Refactor FoiAttachment#body
gbp 707c7fe
Add AttachmentMaskController#wait action
gbp fa0035a
Add AttachmentMasksController#done action
gbp 00851fd
Add AttachmentsController#show timeout
gbp 9581868
Remove AttachmentsController#show custom caching
gbp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
## | ||
# Controller to process FoiAttachment objects before being served publicly by | ||
# applying masks and censor rules. | ||
# | ||
class AttachmentMasksController < ApplicationController | ||
before_action :set_no_crawl_headers | ||
before_action :find_attachment | ||
before_action :ensure_attachment, :ensure_referer | ||
|
||
def wait | ||
if @attachment.masked? | ||
redirect_to done_attachment_mask_path( | ||
id: @attachment.to_signed_global_id, | ||
referer: params[:referer] | ||
) | ||
|
||
else | ||
FoiAttachmentMaskJob.perform_later(@attachment) | ||
end | ||
end | ||
|
||
def done | ||
unless @attachment.masked? | ||
redirect_to wait_for_attachment_mask_path( | ||
id: @attachment.to_signed_global_id, | ||
referer: params[:referer] | ||
) | ||
end | ||
|
||
@show_attachment_path = params[:referer] | ||
end | ||
|
||
private | ||
|
||
def set_no_crawl_headers | ||
headers['X-Robots-Tag'] = 'noindex' | ||
end | ||
|
||
def find_attachment | ||
@attachment = GlobalID::Locator.locate_signed(params[:id]) | ||
end | ||
|
||
def ensure_attachment | ||
raise ActiveRecord::RecordNotFound unless @attachment | ||
end | ||
|
||
def ensure_referer | ||
raise RouteNotFound unless params[:referer].present? | ||
gbp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
## | ||
# Job to apply masks and censor rules to FoiAttachment objects. Masked file will | ||
# be stored as FoiAttachment#file ActiveStorage association. | ||
# | ||
# Example: | ||
# FoiAttachmentMaskJob.perform(FoiAttachment.first) | ||
# | ||
class FoiAttachmentMaskJob < ApplicationJob | ||
queue_as :default | ||
unique :until_and_while_executing, on_conflict: :log | ||
|
||
attr_reader :attachment | ||
|
||
delegate :incoming_message, to: :attachment | ||
delegate :info_request, to: :incoming_message | ||
|
||
def perform(attachment) | ||
@attachment = attachment | ||
|
||
body = AlaveteliTextMasker.apply_masks( | ||
gbp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
attachment.unmasked_body, | ||
attachment.content_type, | ||
masks | ||
) | ||
|
||
if attachment.content_type == 'text/html' | ||
body = | ||
Loofah.scrub_document(body, :prune). | ||
to_html(encoding: 'UTF-8'). | ||
try(:html_safe) | ||
end | ||
|
||
attachment.update(body: body, masked_at: Time.zone.now) | ||
end | ||
|
||
private | ||
|
||
def masks | ||
{ | ||
censor_rules: info_request.applicable_censor_rules, | ||
masks: info_request.masks | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<% @title = _('Attachment available for download.') %> | ||
<h1><%= @title %></h1> | ||
|
||
<p> | ||
<%= _('The attachment has now been processed and is available for ' \ | ||
'download.') %> | ||
</p> | ||
|
||
<p> | ||
<%= link_to _('Download attachment'), @show_attachment_path, class: 'button' %> | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<% @title = _('Attachment processing...') %> | ||
<h1><%= @title %></h1> | ||
|
||
<p> | ||
<%= _('We don\'t have this attachment in our cache at present, we are now ' \ | ||
'processing your request and this page will reload once the ' \ | ||
'attachment is available.') %> | ||
</p> | ||
|
||
<% content_for :javascript_head do %> | ||
<meta http-equiv="refresh" content="2"> | ||
<% end%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'redis_connection' | ||
|
||
ActiveJob::Uniqueness.configure do |config| | ||
config.redlock_servers = [RedisConnection.instance] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,9 @@ | ||
require File.expand_path('../load_env.rb', __dir__) | ||
|
||
def redis_config | ||
{ url: ENV['REDIS_URL'], password: ENV['REDIS_PASSWORD'] }. | ||
merge(redis_sentinel_config) | ||
end | ||
|
||
def redis_sentinel_config | ||
return {} unless ENV['REDIS_SENTINELS'] | ||
|
||
sentinels = ENV['REDIS_SENTINELS'].split(',').map do |ip_and_port| | ||
ip, port = ip_and_port.split(/:(\d+)$/) | ||
ip = Regexp.last_match[1] if ip =~ /\[(.*?)\]/ | ||
{ host: ip, port: port&.to_i || 26_379 } | ||
end | ||
|
||
{ sentinels: sentinels, role: :master } | ||
end | ||
require 'redis_connection' | ||
|
||
Sidekiq.configure_client do |config| | ||
config.redis = redis_config | ||
config.redis = RedisConnection.configuration | ||
end | ||
|
||
Sidekiq.configure_server do |config| | ||
config.redis = redis_config | ||
config.redis = RedisConnection.configuration | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
db/migrate/20230717201410_add_masked_at_to_foi_attachments.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddMaskedAtToFoiAttachments < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :foi_attachments, :masked_at, :datetime | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require File.expand_path('../config/load_env.rb', __dir__) | ||
|
||
## | ||
# Module to parse Redis ENV variables into usable configuration for Sidekiq and | ||
# ActiveJob::Uniqueness gems. | ||
# | ||
module RedisConnection | ||
def self.instance | ||
Redis.new(configuration) | ||
end | ||
|
||
def self.configuration | ||
{ url: ENV['REDIS_URL'], password: ENV['REDIS_PASSWORD'] }. | ||
merge(sentinel_configuration) | ||
end | ||
|
||
def self.sentinel_configuration | ||
return {} unless ENV['REDIS_SENTINELS'] | ||
|
||
sentinels = ENV['REDIS_SENTINELS'].split(',').map do |ip_and_port| | ||
ip, port = ip_and_port.split(/:(\d+)$/) | ||
ip = Regexp.last_match[1] if ip =~ /\[(.*?)\]/ | ||
{ host: ip, port: port&.to_i || 26_379 } | ||
end | ||
|
||
{ sentinels: sentinels, role: :master } | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the commit message:
Redirecting to the actual attachment seems more preferable than requiring an extra click to do so…
…but I'm not sure I understand the conditions where this happens.
I'd have thought we could do:
One to demo perhaps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After the
redirect_to attachment_path(@attachment)
the users browser would show the previous view as this will be treated link any other download link.We could automatically download via JS or meta tag on the
done
view (and change the wording to say "Your download will start automatically...").Happy to demo