mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-11-11 10:06:53 +08:00
30 lines
1,017 B
Ruby
30 lines
1,017 B
Ruby
# frozen_string_literal: true
|
|
|
|
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)
|
|
"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
|