scinote-web/app/models/concerns/active_storage_concerns.rb

31 lines
1,017 B
Ruby
Raw Normal View History

# frozen_string_literal: true
2019-09-23 22:33:57 +08:00
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
2019-09-23 22:33:57 +08:00
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)
2019-09-25 19:45:34 +08:00
rescue StandardError => e
Rails.logger.error e.message
2019-09-23 22:33:57 +08:00
end
end