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.
This commit is contained in:
Senén Rodero Rodríguez
2021-09-15 19:05:58 +02:00
committed by Javi Martín
parent b5026e12a7
commit 5a4921a1af
2 changed files with 2 additions and 2 deletions

View File

@@ -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

View File

@@ -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