scinote-web/lib/active_storage/service/custom_s3_service.rb
2021-02-17 14:36:23 +01:00

32 lines
778 B
Ruby

# frozen_string_literal: true
require 'active_storage/service/s3_service'
module ActiveStorage
class Service::CustomS3Service < Service::S3Service
attr_reader :subfolder
def initialize(bucket:, upload: {}, public: false, **options)
@subfolder = options.delete(:subfolder)
super
end
def delete_prefixed(prefix)
prefix = subfolder.present? ? File.join(subfolder, prefix) : prefix
instrument :delete_prefixed, prefix: prefix do
bucket.objects(prefix: prefix).batch_delete!
end
end
def path_for(key)
subfolder.present? ? File.join(subfolder, key) : key
end
private
def object_for(key)
key = subfolder.present? ? File.join(subfolder, key) : key
bucket.object(key)
end
end
end