diff --git a/app/models/asset.rb b/app/models/asset.rb index 316e0b978..016fbccb4 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -241,7 +241,7 @@ class Asset < ApplicationRecord download_blob_to_tempfile do |tmp_file| to_asset.file.attach(io: tmp_file.open, filename: file_name) end - new_asset.post_process_file(new_asset.team) + to_asset.post_process_file(to_asset.team) end def extract_image_quality @@ -448,14 +448,6 @@ class Asset < ApplicationRecord !locked? && %r{^image/#{Regexp.union(Constants::WHITELISTED_IMAGE_TYPES_EDITABLE)}} =~ file.content_type end - def generate_temp_file - tempfile = Tempfile.new - tempfile.binmode - file.blob.download { |chunk| tempfile.write(chunk) } - tempfile.rewind - tempfile - end - private def tempdir diff --git a/app/models/concerns/tiny_mce_images.rb b/app/models/concerns/tiny_mce_images.rb index 59ccbf3e0..091b961cd 100644 --- a/app/models/concerns/tiny_mce_images.rb +++ b/app/models/concerns/tiny_mce_images.rb @@ -73,10 +73,7 @@ module TinyMceImages tiny_img_clone.transaction do tiny_img_clone.save! - tiny_img_clone.image.attach(io: tiny_img.generate_temp_file, - filename: tiny_img.file_name, - content_type: tiny_img.content_type, - metadata: tiny_img.image.metadata) + tiny_img.duplicate_file(tiny_img_clone) end target.tiny_mce_assets << tiny_img_clone @@ -99,8 +96,6 @@ module TinyMceImages next if asset && asset.object == self && asset.team_id != asset_team_id - new_image = asset.generate_temp_file - new_image_filename = new_image.file_name else # We need implement size and type checks here new_image = URI.parse(image['src']).open @@ -114,7 +109,11 @@ module TinyMceImages new_asset.transaction do new_asset.save! - new_asset.image.attach(io: new_image, filename: new_image_filename) + if image['data-mce-token'] + asset.duplicate_file(new_asset) + else + new_asset.image.attach(io: new_image, filename: new_image_filename) + end end image['src'] = '' diff --git a/app/models/protocol.rb b/app/models/protocol.rb index 00aa986f9..a6dc19245 100644 --- a/app/models/protocol.rb +++ b/app/models/protocol.rb @@ -294,10 +294,7 @@ class Protocol < ApplicationRecord step.assets.each do |asset| asset2 = asset.dup asset2.save! - asset2.file.attach(io: asset.generate_temp_file, - filename: asset.file_name, - content_type: asset.content_type, - metadata: asset.file.metadata) + asset.duplicate_file(asset2) step2.assets << asset2 assets_to_clone << [asset.id, asset2.id] end diff --git a/app/models/tiny_mce_asset.rb b/app/models/tiny_mce_asset.rb index 5bdd455b1..7213f1543 100644 --- a/app/models/tiny_mce_asset.rb +++ b/app/models/tiny_mce_asset.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true class TinyMceAsset < ApplicationRecord + include ActiveStorage::Downloading extend ProtocolsExporter attr_accessor :reference before_create :set_reference, optional: true @@ -191,12 +192,15 @@ class TinyMceAsset < ApplicationRecord obj.reassign_tiny_mce_image_references(cloned_img_ids) end - def generate_temp_file - tempfile = Tempfile.new - tempfile.binmode - image.blob.download { |chunk| tempfile.write(chunk) } - tempfile.rewind - tempfile + def blob + image&.blob + end + + def duplicate_file(to_asset) + download_blob_to_tempfile do |tmp_file| + to_asset.image.attach(io: tmp_file.open, filename: file_name) + end + TinyMceAsset.update_estimated_size(to_asset.id) end private