Files

75 lines
2.4 KiB
Ruby
Raw Permalink Normal View History

2023-05-21 17:59:36 +03:00
# frozen_string_literal: true
module Api
2024-06-15 15:55:09 +03:00
class AttachmentsController < ActionController::API
include ActionController::Cookies
include ActiveStorage::SetCurrent
COOKIE_STORE_LIMIT = 10
2023-05-21 17:59:36 +03:00
def create
2026-05-06 21:02:15 +03:00
@submitter = Submitter.find_by!(slug: params[:submitter_slug])
2023-05-28 23:20:48 +03:00
2026-05-06 21:02:15 +03:00
unless can_upload?(@submitter)
2026-04-07 17:59:20 +03:00
return render json: { error: I18n.t('form_has_been_archived') }, status: :unprocessable_content
end
2026-05-21 15:38:16 +03:00
file = params[:file]
2025-07-29 12:14:26 +03:00
if params[:type].in?(%w[initials signature])
2026-05-21 15:38:16 +03:00
image = ImageUtils.load_vips(file.read, content_type: file.content_type)
2025-06-26 11:04:25 +03:00
2025-07-29 12:14:26 +03:00
if ImageUtils.blank?(image)
2026-05-06 21:02:15 +03:00
Rollbar.error("Empty signature: #{@submitter.id}") if defined?(Rollbar)
2025-07-29 12:14:26 +03:00
2025-08-14 09:25:35 +03:00
return render json: { error: "#{params[:type]} is empty" }, status: :unprocessable_content
2025-07-29 12:14:26 +03:00
end
if ImageUtils.error?(image)
2026-05-06 21:02:15 +03:00
Rollbar.error("Error signature: #{@submitter.id}") if defined?(Rollbar)
2025-07-29 12:14:26 +03:00
return render json: { error: "#{params[:type]} error, try to sign on another device" },
2025-08-14 09:25:35 +03:00
status: :unprocessable_content
2025-07-29 12:14:26 +03:00
end
2026-05-30 22:37:39 +03:00
metadata = { analyzed: true, identified: true, width: image.width, height: image.height }
2025-06-26 11:04:25 +03:00
end
2026-05-30 22:37:39 +03:00
attachment = Submitters.create_attachment!(@submitter, file, metadata:)
2023-05-21 17:59:36 +03:00
2026-05-06 21:02:15 +03:00
if params[:remember_signature] == 'true' && @submitter.email.present?
cookies.encrypted[:signature_uuids] = build_new_cookie_signatures_json(@submitter, attachment)
2024-06-15 15:55:09 +03:00
end
2024-07-24 20:55:35 +03:00
render json: attachment.as_json(only: %i[uuid created_at], methods: %i[url filename content_type])
2025-10-30 08:34:30 +02:00
rescue Submitters::MaliciousFileExtension => e
2025-10-31 10:26:10 +02:00
Rollbar.error(e) if defined?(Rollbar)
2025-12-08 09:32:03 +02:00
render json: { error: e.message }, status: :unprocessable_content
2023-05-21 17:59:36 +03:00
end
2024-06-15 15:55:09 +03:00
2026-04-07 17:59:20 +03:00
def can_upload?(submitter)
!submitter.declined_at? &&
!submitter.completed_at? &&
!submitter.submission.archived_at? &&
!submitter.submission.expired? &&
!submitter.submission.template&.archived_at?
end
2024-06-15 15:55:09 +03:00
def build_new_cookie_signatures_json(submitter, attachment)
values =
begin
JSON.parse(cookies.encrypted[:signature_uuids].presence || '{}')
rescue JSON::ParserError
{}
end
values[submitter.email] = attachment.uuid
values = values.to_a.last(COOKIE_STORE_LIMIT).to_h if values.size > COOKIE_STORE_LIMIT
values.to_json
end
2023-05-21 17:59:36 +03:00
end
end