Copy protocol assets through tempfile

This commit is contained in:
Anton Ignatov 2019-07-19 15:23:34 +02:00
parent b5a35f583a
commit b762ae6a90
4 changed files with 20 additions and 4 deletions

View file

@ -448,6 +448,14 @@ 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
tempfile << file.download
tempfile.rewind
tempfile
end
private
def tempdir

View file

@ -73,7 +73,7 @@ module TinyMceImages
tiny_img_clone.transaction do
tiny_img_clone.save!
tiny_img_clone.image.attach(io: StringIO.new(tiny_img.image.download),
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)
@ -99,7 +99,7 @@ module TinyMceImages
next if asset && asset.object == self && asset.team_id != asset_team_id
new_image = StringIO.new(asset.image.download)
new_image = asset.generate_temp_file
new_image_filename = new_image.file_name
else
# We need implement size and type checks here

View file

@ -294,7 +294,7 @@ class Protocol < ApplicationRecord
step.assets.each do |asset|
asset2 = asset.dup
asset2.save!
asset2.file.attach(io: StringIO.new(asset.file.download),
asset2.file.attach(io: asset.generate_temp_file,
filename: asset.file_name,
content_type: asset.content_type,
metadata: asset.file.metadata)

View file

@ -170,7 +170,7 @@ class TinyMceAsset < ApplicationRecord
tiny_img_clone.transaction do
tiny_img_clone.save!
tiny_img_clone.image.attach(io: StringIO.new(image.download),
tiny_img_clone.image.attach(io: image.generate_temp_file,
filename: file_name,
content_type: content_type,
metadta: image.metadata)
@ -191,6 +191,14 @@ class TinyMceAsset < ApplicationRecord
obj.reassign_tiny_mce_image_references(cloned_img_ids)
end
def generate_temp_file
tempfile = Tempfile.new
tempfile.binmode
tempfile << image.download
tempfile.rewind
tempfile
end
private
def self_destruct