diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index 10abd9aef..19d1ad0a8 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -321,17 +321,15 @@ class ProtocolsController < ApplicationController respond_to do |format| transaction_error = false Protocol.transaction do - begin - @new = @protocol.copy_to_repository( - copy_to_repository_params[:name], - copy_to_repository_params[:protocol_type], - link_protocols, - current_user - ) - rescue Exception - transaction_error = true - raise ActiveRecord:: Rollback - end + @new = @protocol.copy_to_repository( + copy_to_repository_params[:name], + copy_to_repository_params[:protocol_type], + link_protocols, + current_user + ) + rescue StandardError + transaction_error = true + raise ActiveRecord:: Rollback end if transaction_error diff --git a/app/models/asset.rb b/app/models/asset.rb index cc76425ea..1335624fb 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -227,14 +227,19 @@ class Asset < ApplicationRecord new_asset = dup return unless new_asset.save - return new_asset unless file.attached? - duplicate_file(new_asset) new_asset end def duplicate_file(to_asset) - copy_attachment(to_asset.file) + return unless file.attached? + + raise ArgumentError, 'Destination asset should be persisted first!' unless to_asset.persisted? + + file.blob.open do |tmp_file| + to_blob = ActiveStorage::Blob.create_after_upload!(io: tmp_file, filename: blob.filename, metadata: blob.metadata) + to_asset.file.attach(to_blob) + end to_asset.post_process_file(to_asset.team) end diff --git a/app/models/concerns/active_storage_concerns.rb b/app/models/concerns/active_storage_concerns.rb index 97f38d492..0fb871a9a 100644 --- a/app/models/concerns/active_storage_concerns.rb +++ b/app/models/concerns/active_storage_concerns.rb @@ -4,27 +4,10 @@ module ActiveStorageConcerns extend ActiveSupport::Concern def convert_variant_to_base64(variant) - image_link = if ENV['ACTIVESTORAGE_SERVICE'] == 'amazon' - URI.parse(variant.processed.service_url).open.to_a.join - else - File.open(variant.processed.service_url).to_a.join - end - encoded_data = Base64.strict_encode64(image_link) + encoded_data = Base64.strict_encode64(variant.service.download(variant.processed.key)) "data:#{variant.image.blob.content_type};base64,#{encoded_data}" rescue StandardError => e Rails.logger.error e.message "data:#{variant.image.blob.content_type};base64," end - - def copy_attachment(target) - # Target must be empty attachment. Example - asset.file - temp_file = Tempfile.new - temp_file.binmode - blob.download { |chunk| temp_file.write(chunk) } - temp_file.flush - temp_file.rewind - target.attach(io: temp_file, filename: blob.filename, metadata: blob.metadata) - rescue StandardError => e - Rails.logger.error e.message - end end diff --git a/app/models/concerns/tiny_mce_images.rb b/app/models/concerns/tiny_mce_images.rb index ae842a249..2c400d092 100644 --- a/app/models/concerns/tiny_mce_images.rb +++ b/app/models/concerns/tiny_mce_images.rb @@ -65,17 +65,13 @@ module TinyMceImages def clone_tinymce_assets(target, team) cloned_img_ids = [] tiny_mce_assets.each do |tiny_img| - tiny_img_clone = TinyMceAsset.new( + tiny_img_clone = TinyMceAsset.create( estimated_size: tiny_img.estimated_size, object: target, team: team ) - tiny_img_clone.transaction do - tiny_img_clone.save! - tiny_img.duplicate_file(tiny_img_clone) - end - + tiny_img.duplicate_file(tiny_img_clone) target.tiny_mce_assets << tiny_img_clone cloned_img_ids << [tiny_img.id, tiny_img_clone.id] end diff --git a/app/models/protocol.rb b/app/models/protocol.rb index 6ad5a8366..0d4e8ab00 100644 --- a/app/models/protocol.rb +++ b/app/models/protocol.rb @@ -236,20 +236,19 @@ class Protocol < ApplicationRecord # Deep-clone given array of assets def self.deep_clone_assets(assets_to_clone) assets_to_clone.each do |src_id, dest_id| - src = Asset.find_by_id(src_id) - dest = Asset.find_by_id(dest_id) + src = Asset.find_by(id: src_id) + dest = Asset.find_by(id: dest_id) dest.destroy! if src.blank? && dest.present? next unless src.present? && dest.present? # Clone file - src.duplicate_file(dst) + src.duplicate_file(dest) end end def self.clone_contents(src, dest, current_user, clone_keywords) assets_to_clone = [] dest.update(description: src.description) - src.clone_tinymce_assets(dest, dest.team) # Update keywords if clone_keywords @@ -302,7 +301,6 @@ class Protocol < ApplicationRecord step.assets.each do |asset| asset2 = asset.dup asset2.save! - 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 9e02c4e95..e9ecf058b 100644 --- a/app/models/tiny_mce_asset.rb +++ b/app/models/tiny_mce_asset.rb @@ -72,7 +72,7 @@ class TinyMceAsset < ApplicationRecord tm_assets.each do |tm_asset| asset_id = tm_asset.attr('data-mce-token') new_asset = obj.tiny_mce_assets.find_by(id: Base62.decode(asset_id)) - if new_asset + if new_asset&.image&.attached? tm_asset.attributes['src'].value = Rails.application.routes.url_helpers.url_for(new_asset.image) tm_asset['class'] = 'img-responsive' end @@ -203,7 +203,14 @@ class TinyMceAsset < ApplicationRecord end def duplicate_file(to_asset) - copy_attachment(to_asset.image) + return unless image.attached? + + raise ArgumentError, 'Destination TinyMce asset should be persisted first!' unless to_asset.persisted? + + image.blob.open do |tmp_file| + to_blob = ActiveStorage::Blob.create_after_upload!(io: tmp_file, filename: blob.filename, metadata: blob.metadata) + to_asset.image.attach(to_blob) + end TinyMceAsset.update_estimated_size(to_asset.id) end