scinote-web/lib/active_storage/service/custom_s3_service.rb

33 lines
778 B
Ruby
Raw Normal View History

2019-06-24 21:17:42 +08:00
# frozen_string_literal: true
require 'active_storage/service/s3_service'
2019-06-24 21:17:42 +08:00
module ActiveStorage
class Service::CustomS3Service < Service::S3Service
2019-06-24 21:17:42 +08:00
attr_reader :subfolder
def initialize(bucket:, upload: {}, public: false, **options)
2019-06-24 21:17:42 +08:00
@subfolder = options.delete(:subfolder)
super
2019-06-24 21:17:42 +08:00
end
def delete_prefixed(prefix)
prefix = subfolder.present? ? File.join(subfolder, prefix) : prefix
2019-06-24 21:17:42 +08:00
instrument :delete_prefixed, prefix: prefix do
bucket.objects(prefix: prefix).batch_delete!
end
end
2021-02-17 21:36:23 +08:00
def path_for(key)
subfolder.present? ? File.join(subfolder, key) : key
end
2019-06-24 21:17:42 +08:00
private
def object_for(key)
key = subfolder.present? ? File.join(subfolder, key) : key
bucket.object(key)
end
end
end