More flush and rewind

This commit is contained in:
Vasily Fedoseyev
2024-04-06 00:06:18 +03:00
parent 290365d2a1
commit c84571d94a
3 changed files with 10 additions and 3 deletions

View File

@@ -317,11 +317,17 @@ module Paperclip
new_original = Tempfile.new("paperclip-reprocess-#{instance.class.table_name}-#{instance.id}-")
new_original.binmode
old_original = to_file(:original)
# по идее копирование нужно не всегда
new_original.write( old_original.read )
new_original.flush
new_original.rewind
@queued_for_write = { :original => new_original }
post_process
old_original.close if old_original.respond_to?(:close)
if old_original.respond_to?(:close!)
old_original.close!
elsif old_original.respond_to?(:close)
old_original.close
end
save
end

View File

@@ -7,7 +7,7 @@ module Paperclip
# генерим файл из оригинального
source_style = options[:thumbnail] || :original
# TODO: вообще queued_for_write[source_style] место в NoCacheS3#to_file
f = attachment&.queued_for_write&.dig(source_style)&.tap(&:rewind)
f = attachment&.queued_for_write&.dig(source_style)&.tap(&:flush)&.tap(&:rewind)
# TODO: и надо сносить файл если все же была загрузка
f ||= attachment.to_file(source_style) rescue file # rubocop:disable Style/RescueModifier
super(f, options, attachment)

View File

@@ -143,7 +143,7 @@ module Paperclip
end
# HACK: Iostream пишет в tempfile, и он нигде не закрывается. Будем закрывать хотя бы тут
queued_for_write.each_value do |file|
file.is_a?(Tempfile) && file.close!
file.respond_to?(:close!) && file.close!
end
queued_for_write.clear
end
@@ -233,6 +233,7 @@ module Paperclip
}.merge(self.class.upload_options)
files.each do |style, file|
path = key(style)
file.flush
file.rewind
log "Saving to #{store_id}:#{path}"
store.put_object(common_options.merge(key: path, body: file))