Browse Source

CLDC-4280: also apply with_timecop to s3

pull/3262/head
Nat Dean-Lewis 1 month ago
parent
commit
b0e62f3d0d
  1. 9
      app/helpers/timecop_helper.rb
  2. 11
      app/mailers/devise_notify_mailer.rb
  3. 11
      app/mailers/notify_mailer.rb
  4. 76
      app/services/storage/s3_service.rb

9
app/helpers/timecop_helper.rb

@ -0,0 +1,9 @@
module TimecopHelper
def without_timecop(&block)
if defined?(Timecop)
Timecop.return(&block)
else
yield
end
end
end

11
app/mailers/devise_notify_mailer.rb

@ -1,4 +1,5 @@
class DeviseNotifyMailer < Devise::Mailer class DeviseNotifyMailer < Devise::Mailer
include TimecopHelper
require "notifications/client" require "notifications/client"
def notify_client def notify_client
@ -8,15 +9,7 @@ class DeviseNotifyMailer < Devise::Mailer
def send_email(email_address, template_id, personalisation) def send_email(email_address, template_id, personalisation)
return true if intercept_send?(email_address) return true if intercept_send?(email_address)
if defined?(Timecop) without_timecop do
Timecop.return do
notify_client.send_email(
email_address:,
template_id:,
personalisation:,
)
end
else
notify_client.send_email( notify_client.send_email(
email_address:, email_address:,
template_id:, template_id:,

11
app/mailers/notify_mailer.rb

@ -1,4 +1,5 @@
class NotifyMailer < ApplicationMailer class NotifyMailer < ApplicationMailer
include TimecopHelper
require "notifications/client" require "notifications/client"
def notify_client def notify_client
@ -8,15 +9,7 @@ class NotifyMailer < ApplicationMailer
def send_email(email, template_id, personalisation) def send_email(email, template_id, personalisation)
return true if intercept_send?(email) return true if intercept_send?(email)
if defined?(Timecop) without_timecop do
Timecop.return do
notify_client.send_email(
email_address: email,
template_id:,
personalisation:,
)
end
else
notify_client.send_email( notify_client.send_email(
email_address: email, email_address: email,
template_id:, template_id:,

76
app/services/storage/s3_service.rb

@ -1,5 +1,7 @@
module Storage module Storage
class S3Service < StorageService class S3Service < StorageService
include TimecopHelper
attr_reader :configuration attr_reader :configuration
def initialize(config_service, instance_name) def initialize(config_service, instance_name)
@ -11,61 +13,79 @@ module Storage
end end
def list_files(folder) def list_files(folder)
@client.list_objects_v2(bucket: @configuration.bucket_name, prefix: folder) without_timecop do
.flat_map { |response| response.contents.map(&:key) } @client.list_objects_v2(bucket: @configuration.bucket_name, prefix: folder)
.flat_map { |response| response.contents.map(&:key) }
end
end end
def folder_present?(folder) def folder_present?(folder)
response = @client.list_objects_v2(bucket: @configuration.bucket_name, prefix: folder, max_keys: 1) without_timecop do
response.key_count == 1 response = @client.list_objects_v2(bucket: @configuration.bucket_name, prefix: folder, max_keys: 1)
response.key_count == 1
end
end end
def get_presigned_url(file_name, duration, response_content_disposition: nil) def get_presigned_url(file_name, duration, response_content_disposition: nil)
Aws::S3::Presigner without_timecop do
.new({ client: @client }) Aws::S3::Presigner
.presigned_url(:get_object, bucket: @configuration.bucket_name, key: file_name, expires_in: duration, response_content_disposition:) .new({ client: @client })
.presigned_url(:get_object, bucket: @configuration.bucket_name, key: file_name, expires_in: duration, response_content_disposition:)
end
end end
def get_file_io(file_name) def get_file_io(file_name)
@client.get_object(bucket: @configuration.bucket_name, key: file_name) without_timecop do
.body @client.get_object(bucket: @configuration.bucket_name, key: file_name)
.body
end
end end
def get_file(file_name) def get_file(file_name)
@client.get_object(bucket: @configuration.bucket_name, key: file_name) without_timecop do
.body.read @client.get_object(bucket: @configuration.bucket_name, key: file_name)
.body.read
end
end end
def write_file(file_name, data, content_type: nil) def write_file(file_name, data, content_type: nil)
if content_type.nil? without_timecop do
@client.put_object( if content_type.nil?
body: data, @client.put_object(
bucket: @configuration.bucket_name, body: data,
key: file_name, bucket: @configuration.bucket_name,
) key: file_name,
else )
@client.put_object( else
body: data, @client.put_object(
bucket: @configuration.bucket_name, body: data,
key: file_name, bucket: @configuration.bucket_name,
content_type:, key: file_name,
) content_type:,
)
end
end end
end end
def get_file_metadata(file_name) def get_file_metadata(file_name)
@client.head_object(bucket: @configuration.bucket_name, key: file_name) without_timecop do
@client.head_object(bucket: @configuration.bucket_name, key: file_name)
end
end end
def file_exists?(file_name) def file_exists?(file_name)
@client.head_object(bucket: @configuration.bucket_name, key: file_name) without_timecop do
true @client.head_object(bucket: @configuration.bucket_name, key: file_name)
true
end
rescue Aws::S3::Errors::NotFound rescue Aws::S3::Errors::NotFound
false false
end end
def delete_file(file_name) def delete_file(file_name)
@client.delete_object(bucket: @configuration.bucket_name, key: file_name) without_timecop do
@client.delete_object(bucket: @configuration.bucket_name, key: file_name)
end
end end
private private

Loading…
Cancel
Save