2023-05-21 17:59:36 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class SubmissionsDownloadController < ApplicationController
|
|
|
|
|
skip_before_action :authenticate_user!
|
2023-09-17 00:45:57 +03:00
|
|
|
skip_authorization_check
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2024-02-25 02:00:18 +02:00
|
|
|
TTL = 40.minutes
|
|
|
|
|
FILES_TTL = 5.minutes
|
2024-02-18 19:29:14 +02:00
|
|
|
|
2023-05-21 17:59:36 +03:00
|
|
|
def index
|
2025-05-03 13:59:15 +03:00
|
|
|
@submitter = Submitter.find_signed(params[:sig], purpose: :download_completed) if params[:sig].present?
|
2024-02-25 02:00:18 +02:00
|
|
|
|
|
|
|
|
signature_valid =
|
2025-05-03 13:59:15 +03:00
|
|
|
if @submitter&.slug == params[:submitter_slug]
|
2024-02-25 02:00:18 +02:00
|
|
|
true
|
|
|
|
|
else
|
2025-05-03 13:59:15 +03:00
|
|
|
@submitter = nil
|
2024-02-25 02:00:18 +02:00
|
|
|
end
|
|
|
|
|
|
2025-05-03 13:59:15 +03:00
|
|
|
@submitter ||= Submitter.find_by!(slug: params[:submitter_slug])
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2025-05-03 13:59:15 +03:00
|
|
|
Submissions::EnsureResultGenerated.call(@submitter)
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2025-05-03 13:59:15 +03:00
|
|
|
last_submitter = @submitter.submission.submitters.where.not(completed_at: nil).order(:completed_at).last
|
2023-09-09 00:43:32 +03:00
|
|
|
|
2025-03-25 15:34:37 +02:00
|
|
|
return head :not_found unless last_submitter
|
2023-09-09 00:43:32 +03:00
|
|
|
|
2025-03-25 15:34:37 +02:00
|
|
|
Submissions::EnsureResultGenerated.call(last_submitter)
|
2024-02-18 19:29:14 +02:00
|
|
|
|
2026-02-14 16:31:34 +02:00
|
|
|
if !signature_valid && !current_user_submitter?(last_submitter)
|
|
|
|
|
return head :not_found unless Submitters::AuthorizedForForm.call(@submitter, current_user, request)
|
2024-02-18 19:29:14 +02:00
|
|
|
|
2026-02-14 16:31:34 +02:00
|
|
|
if last_submitter.completed_at < TTL.ago
|
|
|
|
|
Rollbar.info("TTL: #{last_submitter.id}") if defined?(Rollbar)
|
|
|
|
|
|
|
|
|
|
return head :not_found
|
|
|
|
|
end
|
2024-02-18 19:29:14 +02:00
|
|
|
end
|
|
|
|
|
|
2024-08-23 23:46:22 +03:00
|
|
|
if params[:combined] == 'true'
|
2026-02-14 16:31:34 +02:00
|
|
|
respond_with_combined(last_submitter)
|
2024-05-25 20:57:36 +03:00
|
|
|
else
|
|
|
|
|
render json: build_urls(last_submitter)
|
|
|
|
|
end
|
2024-02-25 02:00:18 +02:00
|
|
|
end
|
2023-06-22 01:31:39 +03:00
|
|
|
|
2024-02-25 02:00:18 +02:00
|
|
|
private
|
|
|
|
|
|
2026-02-14 16:31:34 +02:00
|
|
|
def respond_with_combined(submitter)
|
|
|
|
|
url = build_combined_url(submitter)
|
|
|
|
|
|
|
|
|
|
if url
|
|
|
|
|
render json: [url]
|
|
|
|
|
else
|
|
|
|
|
head :not_found
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-02-25 02:00:18 +02:00
|
|
|
def current_user_submitter?(submitter)
|
2026-02-14 19:13:05 +02:00
|
|
|
current_user && current_ability.can?(:read, submitter)
|
2024-02-25 02:00:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def build_urls(submitter)
|
2024-11-20 01:12:26 +02:00
|
|
|
filename_format = AccountConfig.find_or_initialize_by(account_id: submitter.account_id,
|
|
|
|
|
key: AccountConfig::DOCUMENT_FILENAME_FORMAT_KEY)&.value
|
|
|
|
|
|
2024-02-25 02:00:18 +02:00
|
|
|
Submitters.select_attachments_for_download(submitter).map do |attachment|
|
2026-02-19 15:54:15 +02:00
|
|
|
ActiveStorage::Blob.proxy_path(
|
2024-11-20 21:48:12 +02:00
|
|
|
attachment.blob,
|
|
|
|
|
expires_at: FILES_TTL.from_now.to_i,
|
|
|
|
|
filename: Submitters.build_document_filename(submitter, attachment.blob, filename_format)
|
|
|
|
|
)
|
2024-02-25 02:00:18 +02:00
|
|
|
end
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
2024-05-25 20:57:36 +03:00
|
|
|
|
|
|
|
|
def build_combined_url(submitter)
|
|
|
|
|
return if submitter.submission.submitters.exists?(completed_at: nil)
|
|
|
|
|
return if submitter.submission.submitters.order(:completed_at).last != submitter
|
|
|
|
|
|
|
|
|
|
attachment = submitter.submission.combined_document_attachment
|
2025-09-15 09:10:03 +03:00
|
|
|
attachment ||= Submissions::EnsureCombinedGenerated.call(submitter)
|
2024-05-25 20:57:36 +03:00
|
|
|
|
2024-11-20 01:12:26 +02:00
|
|
|
filename_format = AccountConfig.find_or_initialize_by(account_id: submitter.account_id,
|
|
|
|
|
key: AccountConfig::DOCUMENT_FILENAME_FORMAT_KEY)&.value
|
|
|
|
|
|
2026-02-19 15:54:15 +02:00
|
|
|
ActiveStorage::Blob.proxy_path(
|
2024-11-20 21:48:12 +02:00
|
|
|
attachment.blob,
|
|
|
|
|
expires_at: FILES_TTL.from_now.to_i,
|
|
|
|
|
filename: Submitters.build_document_filename(submitter, attachment.blob, filename_format)
|
2024-11-20 01:12:26 +02:00
|
|
|
)
|
2024-05-25 20:57:36 +03:00
|
|
|
end
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|