image_validate.rb 678 B

12345678910111213141516171819202122232425262728
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::Avatar::ImageValidate < Service::Base
  3. def execute(image_data:)
  4. begin
  5. data = StaticAssets.data_url_attributes(image_data)
  6. rescue
  7. return error(message: __('The image is invalid.'))
  8. end
  9. if !allowed_mime_type?(mime_type: data[:mime_type])
  10. return error(message: __('The MIME type of the image is invalid.'))
  11. end
  12. data
  13. end
  14. def allowed_mime_type?(mime_type:)
  15. !Rails.application.config.active_storage.web_image_content_types.exclude?(mime_type)
  16. end
  17. def error(message:)
  18. {
  19. message: message,
  20. error: true
  21. }
  22. end
  23. end