initial commit

This commit is contained in:
Alex Turchyn
2023-05-21 17:59:36 +03:00
commit 97c462ead2
159 changed files with 10987 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
# frozen_string_literal: true
module ActionMailerConfigsInterceptor
module_function
def delivering_email(message)
return message unless Rails.env.production?
email_configs = EncryptedConfig.find_by(key: EncryptedConfig::EMAIL_SMTP_KEY)
if email_configs
message.delivery_method(:smtp, user_name: email_configs.value['username'],
password: email_configs.value['password'],
address: email_configs.value['host'],
port: email_configs.value['port'],
tls: email_configs.value['port'].to_s == '465')
message.from = email_configs.value['from_email']
else
message.delivery_method(:test)
end
message
end
end
+20
View File
@@ -0,0 +1,20 @@
# frozen_string_literal: true
module Flows
module_function
def build_field_areas_index(flow)
hash = {}
flow.fields.each do |field|
(field['areas'] || []).each do |area|
hash[area['attachment_uuid']] ||= {}
acc = (hash[area['attachment_uuid']][area['page']] ||= [])
acc << { area:, field: }
end
end
hash
end
end
+69
View File
@@ -0,0 +1,69 @@
# frozen_string_literal: true
module Flows
module ProcessDocument
DPI = 200
FORMAT = '.jpg'
ATTACHMENT_NAME = 'preview_images'
PDF_CONTENT_TYPE = 'application/pdf'
Q = 35
MAX_WIDTH = 1400
module_function
def call(attachment)
if attachment.content_type == PDF_CONTENT_TYPE
generate_pdf_preview_images(attachment)
elsif attachment.content_type.starts_with?('image/')
generate_preview_image(attachment)
end
attachment
end
def generate_preview_image(attachment)
binary = attachment.download
ActiveStorage::Attachment.where(name: ATTACHMENT_NAME, record: attachment).destroy_all
image = Vips::Image.new_from_buffer(binary, '')
image = image.resize(MAX_WIDTH / image.width.to_f)
io = StringIO.new(image.write_to_buffer(FORMAT, Q: Q))
ActiveStorage::Attachment.create!(
blob: ActiveStorage::Blob.create_and_upload!(
io:, filename: "0#{FORMAT}",
metadata: { analyzed: true, identified: true, width: image.width, height: image.height }
),
name: ATTACHMENT_NAME,
record: attachment
)
end
def generate_pdf_preview_images(attachment)
binary = attachment.download
ActiveStorage::Attachment.where(name: ATTACHMENT_NAME, record: attachment).destroy_all
(0..).each do |page_number|
page = Vips::Image.new_from_buffer(binary, '', dpi: DPI, page: page_number)
page = page.resize(MAX_WIDTH / page.width.to_f)
io = StringIO.new(page.write_to_buffer(FORMAT, Q: Q))
ActiveStorage::Attachment.create!(
blob: ActiveStorage::Blob.create_and_upload!(
io:, filename: "#{page_number}#{FORMAT}",
metadata: { analyzed: true, identified: true, width: page.width, height: page.height }
),
name: ATTACHMENT_NAME,
record: attachment
)
rescue Vips::Error
break
end
end
end
end
+36
View File
@@ -0,0 +1,36 @@
# frozen_string_literal: true
module LoadActiveStorageConfigs
STORAGE_YML_PATH = Rails.root.join('config/storage.yml')
module_function
def call
reload unless loaded?
end
def loaded?
@loaded
end
def reload
encrypted_config = EncryptedConfig.find_by(key: EncryptedConfig::FILES_STORAGE_KEY)
return unless encrypted_config
service, configs = encrypted_config.value.values_at('service', 'configs')
service_configurations = ActiveSupport::ConfigurationFile.parse(STORAGE_YML_PATH)
service_configurations[service].merge!(configs) if configs.present?
service_configurations[service][:force_path_style] = true if configs&.dig('endpoint').present?
if service == 'google'
service_configurations[service][:credentials] = JSON.parse(configs.fetch('credentials', '{}'))
end
ActiveStorage::Blob.services = ActiveStorage::Service::Registry.new(service_configurations)
ActiveStorage::Blob.service = ActiveStorage::Blob.services.fetch(service.to_sym)
ensure
@loaded = true
end
end
+4
View File
@@ -0,0 +1,4 @@
# frozen_string_literal: true
module Submissions
end
@@ -0,0 +1,56 @@
# frozen_string_literal: true
module Submissions
module GenerateResultAttachments
module_function
def call(submission)
zip_file = Tempfile.new
zip_stream = Zip::ZipOutputStream.open(zip_file)
submission.flow.schema.map do |item|
document = submission.flow.documents.find { |e| e.uuid == item['attachment_uuid'] }
field_area_index = Flows.build_field_areas_index(submission.flow)
document.open do |tempfile|
pdf = CombinePDF.load(tempfile.path)
pdf.pages.each_with_index do |page, index|
blocks = field_area_index.dig(document.uuid, index)
next if blocks.blank?
scale = page.page_size[2] / 1400.0
blocks.each do |block|
area, field = block.values_at(:area, :field)
page.textbox(submission.values[field['uuid']],
x: (area['x'] * scale) - 10,
y: page.page_size[2] - (area['y'] * scale) + 150,
width: area['w'] * scale,
height: area['h'] * scale)
end
end
string = pdf.to_pdf
io = StringIO.new(string)
zip_stream.put_next_entry("#{item['name']}.pdf")
zip_stream.write(string)
ActiveStorage::Attachment.create!(
blob: ActiveStorage::Blob.create_and_upload!(
io:, filename: "#{item['name']}.pdf"
),
name: 'documents',
record: submission
)
end
end
zip_stream.close
submission.archive.attach(io: zip_file, filename: 'submission.zip')
end
end
end
View File
+57
View File
@@ -0,0 +1,57 @@
# frozen_string_literal: true
if Rails.env.development?
require 'annotate'
task set_annotation_options: :environment do
Annotate.set_defaults(
'active_admin' => 'false',
'additional_file_patterns' => [],
'routes' => 'false',
'models' => 'true',
'position_in_routes' => 'before',
'position_in_class' => 'before',
'position_in_test' => 'before',
'position_in_fixture' => 'before',
'position_in_factory' => 'before',
'position_in_serializer' => 'before',
'show_foreign_keys' => 'true',
'show_complete_foreign_keys' => 'false',
'show_indexes' => 'true',
'simple_indexes' => 'false',
'model_dir' => 'app/models',
'root_dir' => '',
'include_version' => 'false',
'require' => '',
'exclude_tests' => 'true',
'exclude_fixtures' => 'true',
'exclude_factories' => 'true',
'exclude_serializers' => 'false',
'exclude_scaffolds' => 'true',
'exclude_controllers' => 'true',
'exclude_helpers' => 'true',
'exclude_sti_subclasses' => 'false',
'ignore_model_sub_dir' => 'false',
'ignore_columns' => nil,
'ignore_routes' => nil,
'ignore_unknown_models' => 'false',
'hide_limit_column_types' => 'integer,bigint,boolean',
'hide_default_column_types' => 'json,jsonb,hstore',
'skip_on_db_migrate' => 'false',
'format_bare' => 'true',
'format_rdoc' => 'false',
'format_yard' => 'false',
'format_markdown' => 'false',
'sort' => 'false',
'force' => 'false',
'frozen' => 'false',
'classified_sort' => 'true',
'trace' => 'false',
'wrapper_open' => nil,
'wrapper_close' => nil,
'with_comment' => 'true'
)
end
Annotate.load_tasks
end