From 5bdcaef73887529ad2be6f0638bcf7ef42527b9c Mon Sep 17 00:00:00 2001 From: f Date: Fri, 31 Mar 2023 17:21:50 -0300 Subject: [PATCH] feat: descargar archivos remotos sutty/editor#62 --- .../disk_controller_decorator.rb | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/app/controllers/active_storage/disk_controller_decorator.rb b/app/controllers/active_storage/disk_controller_decorator.rb index 0634a2ec..e49c22f1 100644 --- a/app/controllers/active_storage/disk_controller_decorator.rb +++ b/app/controllers/active_storage/disk_controller_decorator.rb @@ -21,11 +21,26 @@ module ActiveStorage def update if (token = decode_verified_token) if acceptable_content?(token) - named_disk_service(token[:service_name]).upload token[:key], request.body, checksum: token[:checksum] - blob = ActiveStorage::Blob.find_by_key token[:key] site = Site.find_by_name token[:service_name] + if remote_file?(token) + begin + url = request.body.read + body = Down.download(url, max_size: 111.megabytes) + checksum = nil + 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 + site.static_files.attach(blob) else head :unprocessable_entity @@ -36,6 +51,12 @@ module ActiveStorage rescue ActiveStorage::IntegrityError head :unprocessable_entity end + + private + + def remote_file?(token) + token[:content_type] == 'sutty/download-from-url' + end end end end