lib/carrierwave/storage/aliyun.rb in carrierwave-aliyun-1.1.0 vs lib/carrierwave/storage/aliyun.rb in carrierwave-aliyun-1.1.1

- old
+ new

@@ -1,45 +1,58 @@ # frozen_string_literal: true module CarrierWave module Storage class Aliyun < Abstract - def store!(file) + def store!(new_file) f = AliyunFile.new(uploader, self, uploader.store_path) headers = { - content_type: file.content_type, + content_type: new_file.content_type, content_disposition: uploader.try(:content_disposition) } - f.store(::File.open(file.file), headers) + f.store(new_file, headers) f end def retrieve!(identifier) AliyunFile.new(uploader, self, uploader.store_path(identifier)) end - def cache!(file) - f = AliyunFile.new(uploader, self, uploader.store_path) + def cache!(new_file) + f = AliyunFile.new(uploader, self, uploader.cache_path) headers = { - content_type: file.content_type, + content_type: new_file.content_type, content_disposition: uploader.try(:content_disposition) } - f.store(::File.open(file.file), headers) + f.store(new_file, headers) f end def retrieve_from_cache!(identifier) - AliyunFile.new(uploader, self, uploader.store_path(identifier)) + AliyunFile.new(uploader, self, uploader.cache_path(identifier)) end def delete_dir!(path) # do nothing, because there's no such things as 'empty directory' end def clean_cache!(_seconds) - raise 'use Object Lifecycle Management to clean the cache' + will_remove_keys = [] + bucket.list_objects(prefix: uploader.cache_path).each do |file| + next unless file.is_a?(Object) + time = file.key.scan(/(\d+)-\d+-\d+(?:-\d+)?/).first.map { |t| t.to_i } + time = Time.at(*time) + will_remove_keys << item.key if time < (Time.now.utc - seconds) + end + bucket.batch_delete_objects(will_remove_keys) end + + private + + def bucket + @bucket ||= CarrierWave::Aliyun::Bucket.new(uploader) + end end end end