Skip to content

Commit

Permalink
Implement of cache* method for carrierwave 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
huobazi committed Jan 19, 2020
1 parent b6d9905 commit dc41ba1
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -17,3 +17,5 @@ test/tmp
test/version_tmp
tmp
uploads/
.idea/
.rakeTasks
4 changes: 4 additions & 0 deletions 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)
Expand Down
6 changes: 3 additions & 3 deletions 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)

Expand All @@ -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:
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion 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
1 change: 1 addition & 0 deletions lib/carrierwave/qiniu/configuration.rb
Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions lib/carrierwave/storage/qiniu.rb
Expand Up @@ -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
Expand Down Expand Up @@ -196,6 +210,10 @@ def filename
::File.basename(path)
end

def clean_cache!(seconds)
qiniu_connection.clean_cache!(seconds)
end

private

def qiniu_connection
Expand Down Expand Up @@ -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
13 changes: 1 addition & 12 deletions 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
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -38,7 +38,6 @@ def root
# 或者在根目录下新建 `.env` 文件,包含 <key>=<value>
::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"]

Expand Down
1 change: 0 additions & 1 deletion spec/upload_spec.rb
Expand Up @@ -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)

Expand Down

0 comments on commit dc41ba1

Please sign in to comment.