From dc41ba1f0bf716e2298ec42a89c6802114b0033c Mon Sep 17 00:00:00 2001 From: Marble Wu Date: Mon, 20 Jan 2020 00:54:14 +0800 Subject: [PATCH] Implement of cache* method for carrierwave 2.0 --- .gitignore | 2 ++ CHANGELOG.md | 4 ++++ README.md | 6 +++--- lib/carrierwave-qiniu/version.rb | 2 +- lib/carrierwave/qiniu/configuration.rb | 1 + lib/carrierwave/storage/qiniu.rb | 30 ++++++++++++++++++++++++++ lib/carrierwave/uploader/base.rb | 13 +---------- spec/spec_helper.rb | 1 - spec/upload_spec.rb | 1 - 9 files changed, 42 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 87ce45f..7194cd8 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,5 @@ test/tmp test/version_tmp tmp uploads/ +.idea/ +.rakeTasks \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 955a7af..9335d3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ ## CHANGE LOG +### v1.2.1 + +- Implement of cache* method for carrierwave 2.0 + ### v1.2.0 - Fit to carrierwave 2.0 (支持 CarrierWave 2.0) diff --git a/README.md b/README.md index 043b1fc..4c76ae6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Carrierwave::Qiniu -[![Gem Version](https://badge.fury.io/rb/carrierwave-qiniu@2x.png?1.2.0)](http://badge.fury.io/rb/carrierwave-qiniu) +[![Gem Version](https://badge.fury.io/rb/carrierwave-qiniu@2x.png?1.2.1)](http://badge.fury.io/rb/carrierwave-qiniu) This gem adds storage support for [Qiniu](http://qiniutek.com) to [Carrierwave](https://github.com/jnicklas/carrierwave) @@ -10,7 +10,7 @@ example: https://github.com/huobazi/carrierwave-qiniu-example Add the following to your application's Gemfile: - gem 'carrierwave-qiniu', '~> 1.2.0' + gem 'carrierwave-qiniu', '~> 1.2.1' gem 'carrierwave-i18n' # If you need to use locales other than English And then execute: @@ -19,7 +19,7 @@ And then execute: Or install it yourself as: - $ gem install carrierwave-qiniu -v 1.2.0 + $ gem install carrierwave-qiniu -v 1.2.1 ## Usage diff --git a/lib/carrierwave-qiniu/version.rb b/lib/carrierwave-qiniu/version.rb index 6f3a545..85458bd 100644 --- a/lib/carrierwave-qiniu/version.rb +++ b/lib/carrierwave-qiniu/version.rb @@ -1,6 +1,6 @@ # encoding: utf-8 module Carrierwave module Qiniu - VERSION = "1.2.0" + VERSION = "1.2.1" end end diff --git a/lib/carrierwave/qiniu/configuration.rb b/lib/carrierwave/qiniu/configuration.rb index b2e80f2..074eda4 100644 --- a/lib/carrierwave/qiniu/configuration.rb +++ b/lib/carrierwave/qiniu/configuration.rb @@ -50,6 +50,7 @@ def reset_qiniu_config config.qiniu_style_separator = '-' config.qiniu_style_inline = false config.qiniu_delete_after_days = 0 + config.cache_storage = :file end end diff --git a/lib/carrierwave/storage/qiniu.rb b/lib/carrierwave/storage/qiniu.rb index 40d8684..e114d8d 100644 --- a/lib/carrierwave/storage/qiniu.rb +++ b/lib/carrierwave/storage/qiniu.rb @@ -93,6 +93,20 @@ def download_url(path) @qiniu_bucket_private ? ::Qiniu::Auth.authorize_download_url(primitive_url, :expires_in => @qiniu_private_url_expires_in) : primitive_url end + def clean_cache!(seconds) + code, result, response_headers, s, d = Qiniu::Storage.list(Qiniu::Storage::ListPolicy.new( + @qiniu_bucket,# 存储空间 + 1000,# 列举的条目数 + '', # 指定前缀 + ''# 指定目录分隔符 + )).items.each do |file| + # generate_cache_id returns key formated TIMEINT-PID(-COUNTER)-RND + time = file.key.scan(/(\d+)-\d+-\d+(?:-\d+)?/).first.map { |t| t.to_i } + time = Time.at(*time) + delete(file.key) if time < (Time.now.utc - seconds) + end + end + private def init @@ -196,6 +210,10 @@ def filename ::File.basename(path) end + def clean_cache!(seconds) + qiniu_connection.clean_cache!(seconds) + end + private def qiniu_connection @@ -266,6 +284,18 @@ def retrieve!(identifier) def retrieve_from_cache!(identifier) ::CarrierWave::Storage::Qiniu::File.new(uploader, uploader.cache_path(identifier)) end + + ## + # Deletes a cache dir + # + def delete_dir!(path) + # do nothing, because there's no such things as 'empty directory' + end + + def clean_cache!(seconds) + ::CarrierWave::Storage::Qiniu::File.new(uploader, nil).clean_cache!(seconds) + end + end end end diff --git a/lib/carrierwave/uploader/base.rb b/lib/carrierwave/uploader/base.rb index e42167a..024595b 100644 --- a/lib/carrierwave/uploader/base.rb +++ b/lib/carrierwave/uploader/base.rb @@ -1,32 +1,21 @@ #encoding: utf-8 module CarrierWave - class SanitizedFile - attr_accessor :copy_from_path - end module Uploader module Cache - alias_method :old_cache!, :cache! - def cache!(new_file = sanitized_file) - old_cache! new_file - if new_file.kind_of? CarrierWave::Storage::Qiniu::File @file.copy_from_path = new_file.path elsif new_file.kind_of? CarrierWave::Uploader::Base return unless new_file.file.present? - @file.copy_from_path = new_file.file.path + @file.copy_from_path = new_file.file.path if @file.respond_to?(:copy_from_path) end - end end - end - - end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a3bde0b..6a4f637 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -38,7 +38,6 @@ def root # 或者在根目录下新建 `.env` 文件,包含 = ::CarrierWave.configure do |config| config.storage = :qiniu - config.cache_storage = :file config.qiniu_access_key = ENV["qiniu_access_key"] config.qiniu_secret_key = ENV["qiniu_secret_key"] diff --git a/spec/upload_spec.rb b/spec/upload_spec.rb index 966243a..403757a 100644 --- a/spec/upload_spec.rb +++ b/spec/upload_spec.rb @@ -103,7 +103,6 @@ class WrongPhoto < ActiveRecord::Base URI.open(photo.image.url).should_not be_nil - puts "The thumb image:" puts photo.image.url(:thumb)