Fix file handling in protocol import/copying [SCI-3935][SCI-3926][SCI-3934]

This commit is contained in:
Oleksii Kriuchykhin 2019-09-30 10:25:11 +02:00
parent 0da995280f
commit 584f2d07c0
6 changed files with 32 additions and 45 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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