2019-06-24 21:17:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-01-28 21:43:45 +08:00
|
|
|
require 'active_storage/service/s3_service'
|
2019-06-24 21:17:42 +08:00
|
|
|
|
|
|
|
module ActiveStorage
|
2021-01-28 21:43:45 +08:00
|
|
|
class Service::CustomS3Service < Service::S3Service
|
2019-06-24 21:17:42 +08:00
|
|
|
attr_reader :subfolder
|
|
|
|
|
2021-01-28 21:43:45 +08:00
|
|
|
def initialize(bucket:, upload: {}, public: false, **options)
|
2019-06-24 21:17:42 +08:00
|
|
|
@subfolder = options.delete(:subfolder)
|
2021-01-28 21:43:45 +08:00
|
|
|
super
|
2019-06-24 21:17:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_prefixed(prefix)
|
2019-10-10 20:25:19 +08:00
|
|
|
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
|