mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-01-06 15:40:31 +08:00
Fix base64 image extraction for non-saved models [SCI-7918]
This commit is contained in:
parent
dc8b147282
commit
8d434c7be4
1 changed files with 15 additions and 0 deletions
|
@ -11,6 +11,7 @@ module TinyMceImages
|
||||||
|
|
||||||
before_validation :extract_base64_images
|
before_validation :extract_base64_images
|
||||||
before_save :clean_tiny_mce_image_urls
|
before_save :clean_tiny_mce_image_urls
|
||||||
|
after_create :ensure_extracted_image_object_references
|
||||||
|
|
||||||
def prepare_for_report(field, base64_encoded_imgs = false)
|
def prepare_for_report(field, base64_encoded_imgs = false)
|
||||||
description = self[field]
|
description = self[field]
|
||||||
|
@ -167,6 +168,7 @@ module TinyMceImages
|
||||||
def extract_base64_images
|
def extract_base64_images
|
||||||
# extracts and uploads any base64 encoded images,
|
# extracts and uploads any base64 encoded images,
|
||||||
# so they get stored as files instead of directly in the text
|
# so they get stored as files instead of directly in the text
|
||||||
|
@extracted_base64_images = []
|
||||||
|
|
||||||
object_field = Extends::RICH_TEXT_FIELD_MAPPINGS[self.class.name]
|
object_field = Extends::RICH_TEXT_FIELD_MAPPINGS[self.class.name]
|
||||||
return unless object_field
|
return unless object_field
|
||||||
|
@ -190,6 +192,8 @@ module TinyMceImages
|
||||||
filename: "#{Asset.generate_unique_secure_token}.#{tiny_image.image.type}"
|
filename: "#{Asset.generate_unique_secure_token}.#{tiny_image.image.type}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@extracted_base64_images << tiny_image
|
||||||
|
|
||||||
encoded_id = Base62.encode(tiny_image.id)
|
encoded_id = Base62.encode(tiny_image.id)
|
||||||
|
|
||||||
sanitized_text.gsub!(
|
sanitized_text.gsub!(
|
||||||
|
@ -201,5 +205,16 @@ module TinyMceImages
|
||||||
assign_attributes(object_field => sanitized_text)
|
assign_attributes(object_field => sanitized_text)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def ensure_extracted_image_object_references
|
||||||
|
# for models that were not yet in database in time of image extraction
|
||||||
|
# we need to update image references after creation
|
||||||
|
|
||||||
|
@extracted_base64_images&.each do |image|
|
||||||
|
next if image.object
|
||||||
|
|
||||||
|
image.update(object: self)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue