2022-03-06 18:59:57 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ActiveStorage
|
|
|
|
# Modificar {DiskController} para poder asociar el blob a un sitio
|
|
|
|
module DiskControllerDecorator
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2022-10-26 20:25:58 +00:00
|
|
|
alias_method :original_show, :show
|
|
|
|
|
|
|
|
# Permitir incrustar archivos subidos (especialmente PDFs) desde
|
|
|
|
# otros sitios.
|
|
|
|
def show
|
|
|
|
original_show.tap do |s|
|
|
|
|
response.headers.delete 'X-Frame-Options'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-06 18:59:57 +00:00
|
|
|
# Asociar el archivo subido al sitio correspondiente. Cada sitio
|
|
|
|
# tiene su propio servicio de subida de archivos.
|
|
|
|
def update
|
|
|
|
if (token = decode_verified_token)
|
|
|
|
if acceptable_content?(token)
|
|
|
|
blob = ActiveStorage::Blob.find_by_key token[:key]
|
|
|
|
site = Site.find_by_name token[:service_name]
|
|
|
|
|
2023-03-31 20:21:50 +00:00
|
|
|
if remote_file?(token)
|
|
|
|
begin
|
|
|
|
url = request.body.read
|
|
|
|
body = Down.download(url, max_size: 111.megabytes)
|
2023-04-07 17:24:47 +00:00
|
|
|
checksum = Digest::MD5.file(body.path).base64digest
|
|
|
|
blob.metadata[:url] = url
|
|
|
|
blob.update_columns checksum: checksum, byte_size: body.size, metadata: blob.metadata
|
2023-03-31 20:21:50 +00:00
|
|
|
rescue StandardError => e
|
|
|
|
ExceptionNotifier.notify_exception(e, data: { key: token[:key], url: url, site: site.name })
|
|
|
|
|
|
|
|
head :content_too_large
|
|
|
|
end
|
|
|
|
else
|
|
|
|
body = request.body
|
|
|
|
checksum = token[:checksum]
|
|
|
|
end
|
|
|
|
|
|
|
|
named_disk_service(token[:service_name]).upload token[:key], body, checksum: checksum
|
|
|
|
|
2022-03-06 18:59:57 +00:00
|
|
|
site.static_files.attach(blob)
|
|
|
|
else
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
else
|
|
|
|
head :not_found
|
|
|
|
end
|
|
|
|
rescue ActiveStorage::IntegrityError
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
2023-03-31 20:21:50 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def remote_file?(token)
|
|
|
|
token[:content_type] == 'sutty/download-from-url'
|
|
|
|
end
|
2022-03-06 18:59:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ActiveStorage::DiskController.include ActiveStorage::DiskControllerDecorator
|