mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-09-14 09:04:56 +08:00
Reuse duplicate file method
This commit is contained in:
parent
7e379d4e0d
commit
24da016465
4 changed files with 18 additions and 26 deletions
|
@ -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
|
||||
|
|
|
@ -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'] = ''
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue