image_validate.rb 764 B

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