From 5a4921a1afc4b27786b00a4f681bb0a8859bb75c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Wed, 15 Sep 2021 19:05:58 +0200 Subject: [PATCH] Use URI.open to assign external cached attachments We were having issues with cached attachments and external services. A `Tempfile` is returned by `URI.open` when using S3, so we're dealing with this case as well. --- app/models/concerns/attachable.rb | 2 +- app/models/concerns/has_attachment.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/concerns/attachable.rb b/app/models/concerns/attachable.rb index fb13dc517..1fbe15512 100644 --- a/app/models/concerns/attachable.rb +++ b/app/models/concerns/attachable.rb @@ -37,7 +37,7 @@ module Attachable if Paperclip::Attachment.default_options[:storage] == :filesystem File.open(cached_attachment) { |file| self.attachment = file } else - self.attachment = URI.parse(cached_attachment) + self.attachment = URI.open(cached_attachment) end end diff --git a/app/models/concerns/has_attachment.rb b/app/models/concerns/has_attachment.rb index 450e515ea..6f3f67844 100644 --- a/app/models/concerns/has_attachment.rb +++ b/app/models/concerns/has_attachment.rb @@ -9,7 +9,7 @@ module HasAttachment alias_method :"paperclip_#{attribute}=", :"#{attribute}=" define_method :"#{attribute}=" do |file| - if file.is_a?(IO) + if file.is_a?(IO) || file.is_a?(Tempfile) && !file.is_a?(Ckeditor::Http::QqFile) send(:"storage_#{attribute}").attach(io: file, filename: File.basename(file.path)) elsif file.nil? send(:"storage_#{attribute}").detach