Files
docuseal/config/initializers/active_storage.rb
T

82 lines
2.3 KiB
Ruby
Raw 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
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
2024-01-30 17:53:25 +02:00
def preview_image_url
2024-02-18 10:04:56 +02:00
first_page = preview_images.joins(:blob).find_by(blob: { filename: '0.jpg' })
return unless first_page
2024-02-18 11:26:30 +02:00
ActiveStorage::Blob.proxy_url(first_page.blob)
2024-01-30 17:53:25 +02:00
end
2023-05-21 17:59:36 +03:00
end
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-02-18 11:26:30 +02:00
def self.proxy_url(blob, expires_in: nil)
Rails.application.routes.url_helpers.blobs_proxy_url(
signed_uuid: blob.signed_uuid(expires_in:), filename: blob.filename,
**Docuseal.default_url_options
)
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-18 11:26:30 +02:00
def signed_uuid(expires_in: nil)
2024-02-19 21:13:56 +02:00
ApplicationRecord.signed_id_verifier.generate([uuid, 'blob'], expires_in:)
2024-02-18 11:26:30 +02:00
end
2024-02-17 13:36:55 +02:00
def delete
service.delete(key)
end
end
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
response.set_header('Cache-Control', 'public, max-age=31536000') if action_name == 'show'
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
next if current_user
2023-07-05 20:40:49 +03:00
next if Submitter.find_signed(cookies[:submitter_sid])
2023-06-24 12:27:34 +03:00
head :forbidden
end
2023-05-21 17:59:36 +03:00
LoadActiveStorageConfigs.call
2023-06-09 01:43:43 +03:00
rescue StandardError => e
2023-06-13 00:29:07 +03:00
Rails.logger.error(e)
2023-06-09 01:43:43 +03:00
2023-05-21 17:59:36 +03:00
nil
end