config.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Store::Provider::S3::Config
  3. class << self
  4. PATH = Rails.root.join('config/zammad/storage.yml')
  5. VARIABLE = 'S3_URL'.freeze
  6. NAME = 's3'.freeze
  7. def bucket
  8. settings[:bucket]
  9. end
  10. def max_chunk_size
  11. settings[:max_chunk_size].presence || 100.megabytes
  12. end
  13. def apply
  14. return true if Aws.config.present?
  15. begin
  16. config = settings.deep_dup
  17. credentials = Aws::Credentials.new(config[:access_key_id], config[:secret_access_key])
  18. config[:credentials] = credentials
  19. %i[access_key_id secret_access_key bucket max_chunk_size].each do |key|
  20. config.delete(key)
  21. end
  22. Aws.config.update(config)
  23. rescue => e
  24. Rails.logger.error { "#{name}: #{e.message}" }
  25. raise Store::Provider::S3::Error, __('Simple Storage Service configuration not found or invalid.')
  26. end
  27. true
  28. end
  29. def reset
  30. @config = nil
  31. Aws.config = {}
  32. true
  33. end
  34. private
  35. def settings
  36. return @config if @config.present?
  37. config = Zammad::Service::Configuration.parse(yaml: PATH, env: VARIABLE, adapter: NAME)
  38. @config = config.presence || (raise Store::Provider::S3::Error, __('Simple Storage Service configuration not found or invalid.'))
  39. end
  40. end
  41. end