Files

95 lines
2.8 KiB
Ruby
Raw Permalink Normal View History

2023-05-21 17:59:36 +03:00
# frozen_string_literal: true
ActiveSupport.on_load(:active_storage_attachment) do
attribute :uuid, :string, default: -> { SecureRandom.uuid }
has_many_attached :preview_images
2024-01-30 17:53:25 +02:00
2026-06-19 16:33:55 +03:00
def self.service_url_time
return unless Docuseal.multitenant?
now = Time.current
now.min < 30 ? now.beginning_of_hour : now.beginning_of_hour + 30.minutes
end
2024-02-17 22:14:28 +02:00
def signed_uuid
2024-02-19 21:13:56 +02:00
@signed_uuid ||= ApplicationRecord.signed_id_verifier.generate(uuid, expires_in: 6.hours, purpose: :attachment)
2024-02-17 22:14:28 +02:00
end
2026-02-12 09:22:08 +02:00
def signed_key
@signed_key ||= ApplicationRecord.signed_id_verifier.generate([id, uuid], expires_in: 6.hours, purpose: :attachment)
end
2023-05-21 17:59:36 +03:00
end
2026-02-19 15:54:15 +02:00
# rubocop:disable Metrics/BlockLength
2024-02-17 13:36:55 +02:00
ActiveSupport.on_load(:active_storage_blob) do
2024-02-18 10:24:43 +02:00
attribute :uuid, :string, default: -> { SecureRandom.uuid }
2024-12-01 00:10:07 +02:00
attribute :io_data, :string, default: ''
2024-02-18 10:24:43 +02:00
2024-11-21 23:03:30 +02:00
def self.proxy_url(blob, expires_at: nil, filename: nil, host: nil)
2024-02-18 11:26:30 +02:00
Rails.application.routes.url_helpers.blobs_proxy_url(
2024-11-20 01:12:26 +02:00
signed_uuid: blob.signed_uuid(expires_at:), filename: filename || blob.filename,
2024-11-21 23:03:30 +02:00
**Docuseal.default_url_options,
**{ host: }.compact
2024-02-18 11:26:30 +02:00
)
end
2026-02-19 15:54:15 +02:00
def self.proxy_path(blob, expires_at: nil, filename: nil)
Rails.application.routes.url_helpers.blobs_proxy_path(
signed_uuid: blob.signed_uuid(expires_at:), filename: filename || blob.filename
)
end
2024-02-18 10:24:43 +02:00
def uuid
super || begin
new_uuid = SecureRandom.uuid
update_columns(uuid: new_uuid)
new_uuid
end
end
2024-02-25 02:00:18 +02:00
def signed_uuid(expires_at: nil)
expires_at = expires_at.to_i if expires_at
ApplicationRecord.signed_id_verifier.generate([uuid, 'blob', expires_at].compact)
2024-02-18 11:26:30 +02:00
end
2024-02-17 13:36:55 +02:00
def delete
service.delete(key)
end
end
2026-02-19 15:54:15 +02:00
# rubocop:enable Metrics/BlockLength
2024-02-17 13:36:55 +02:00
2023-06-24 22:50:12 +03:00
ActiveStorage::LogSubscriber.detach_from(:active_storage) if Rails.env.production?
2023-05-21 17:59:36 +03:00
Rails.configuration.to_prepare do
ActiveStorage::DiskController.after_action do
2024-12-20 17:01:27 +02:00
response.set_header('cache-control', 'public, max-age=31536000') if action_name == 'show'
2023-05-21 17:59:36 +03:00
end
2023-08-12 12:34:02 +03:00
ActiveStorage::Blobs::ProxyController.before_action do
response.set_header('Access-Control-Allow-Origin', '*')
response.set_header('Access-Control-Allow-Methods', 'GET')
response.set_header('Access-Control-Allow-Headers', '*')
response.set_header('Access-Control-Max-Age', '1728000')
end
ActiveStorage::Blobs::RedirectController.before_action do
response.set_header('Access-Control-Allow-Origin', '*')
response.set_header('Access-Control-Allow-Methods', 'GET')
response.set_header('Access-Control-Allow-Headers', '*')
response.set_header('Access-Control-Max-Age', '1728000')
end
2023-06-24 12:27:34 +03:00
ActiveStorage::DirectUploadsController.before_action do
head :forbidden
end
2023-05-21 17:59:36 +03:00
LoadActiveStorageConfigs.call
2023-06-09 01:43:43 +03:00
rescue StandardError => e
2024-03-06 23:14:07 +02:00
Rails.logger.error(e) unless Rails.env.production?
2023-06-09 01:43:43 +03:00
2023-05-21 17:59:36 +03:00
nil
end