image_helper.rb 643 B

12345678910111213141516171819202122232425262728293031
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module ImageHelper
  3. =begin
  4. file_attributes = ImageHelper.data_url_attributes(data_url)
  5. returns
  6. {
  7. mime_type: 'image/png',
  8. content: image_bin_content,
  9. file_extention: 'png',
  10. }
  11. =end
  12. def self.data_url_attributes(data_url)
  13. data = {}
  14. if data_url =~ %r{^data:(.+?);base64,(.+?)$}
  15. data[:mime_type] = $1
  16. data[:content] = Base64.decode64($2)
  17. if data[:mime_type] =~ %r{/(.+?)$}
  18. data[:file_extention] = $1
  19. end
  20. return data
  21. end
  22. raise "Unable to parse data url: #{data_url&.slice(0, 100)}"
  23. end
  24. end