carrierwave-qiniu 1.2.1 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.env.sample +4 -0
- data/.gitignore +1 -0
- data/.rspec_status +21 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +3 -1
- data/Gemfile.lock +80 -81
- data/LICENSE.txt +21 -0
- data/README.md +44 -17
- data/Rakefile +5 -1
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/carrierwave-qiniu.gemspec +26 -17
- data/lib/carrierwave-qiniu.rb +8 -3
- data/lib/carrierwave/qiniu/configuration.rb +4 -3
- data/lib/carrierwave/qiniu/connection.rb +152 -0
- data/lib/carrierwave/qiniu/railtie.rb +0 -1
- data/lib/carrierwave/qiniu/url.rb +3 -4
- data/lib/{carrierwave-qiniu → carrierwave/qiniu}/version.rb +1 -2
- data/lib/carrierwave/storage/qiniu.rb +9 -272
- data/lib/carrierwave/storage/qiniu_file.rb +166 -0
- data/lib/carrierwave/tasks/qiniu.rake +1 -1
- metadata +25 -22
- data/LICENSE +0 -22
- data/lib/carrierwave/uploader/base.rb +0 -21
- data/spec/mm.jpg +0 -0
- data/spec/spec_helper.rb +0 -53
- data/spec/upload_spec.rb +0 -274
data/Rakefile
CHANGED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "carrierwave-qiniu"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/carrierwave-qiniu.gemspec
CHANGED
@@ -1,22 +1,31 @@
|
|
1
|
-
|
2
|
-
require File.expand_path('../lib/carrierwave-qiniu/version', __FILE__)
|
1
|
+
require_relative 'lib/carrierwave/qiniu/version'
|
3
2
|
|
4
|
-
Gem::Specification.new do |
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
gem.homepage = "https://github.com/huobazi/carrierwave-qiniu"
|
10
|
-
gem.license = "MIT"
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "carrierwave-qiniu"
|
5
|
+
spec.version = Carrierwave::Qiniu::VERSION
|
6
|
+
spec.authors = ["Marble Wu"]
|
7
|
+
spec.email = ["huobazi@gmail.com"]
|
11
8
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
9
|
+
spec.summary = %q{Qiniu Storage support for CarrierWave}
|
10
|
+
spec.description = %q{Qiniu Storage support for CarrierWave}
|
11
|
+
spec.homepage = "https://github.com/huobazi/carrierwave-qiniu"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = "https://github.com/huobazi/carrierwave-qiniu"
|
17
|
+
spec.metadata["changelog_uri"] = "https://github.com/huobazi/carrierwave-qiniu/blob/master/CHANGELOG.md.md"
|
18
18
|
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
19
27
|
|
20
|
-
|
21
|
-
|
28
|
+
|
29
|
+
spec.add_dependency "carrierwave" , ">= 1"
|
30
|
+
spec.add_dependency "qiniu", "~> 6.9", ">= 6.9.0"
|
22
31
|
end
|
data/lib/carrierwave-qiniu.rb
CHANGED
@@ -1,10 +1,15 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
|
3
|
-
require "carrierwave
|
2
|
+
|
3
|
+
require "carrierwave"
|
4
|
+
require "carrierwave/qiniu/version"
|
4
5
|
require "carrierwave/qiniu/configuration"
|
6
|
+
require "carrierwave/qiniu/connection"
|
5
7
|
require "carrierwave/qiniu/style"
|
6
|
-
require "carrierwave/uploader/base"
|
7
8
|
require "carrierwave/qiniu/railtie" if defined?(Rails)
|
9
|
+
require "carrierwave/storage/qiniu"
|
10
|
+
require "carrierwave/storage/qiniu_file"
|
11
|
+
require "qiniu"
|
12
|
+
require "qiniu/http"
|
8
13
|
|
9
14
|
::CarrierWave.configure do |config|
|
10
15
|
config.storage_engines[:qiniu] = "::CarrierWave::Storage::Qiniu".freeze
|
@@ -1,5 +1,4 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
|
3
2
|
module CarrierWave
|
4
3
|
module Qiniu
|
5
4
|
module Configuration
|
@@ -38,7 +37,8 @@ module CarrierWave
|
|
38
37
|
configure do |config|
|
39
38
|
config.qiniu_protocol = 'http'
|
40
39
|
config.qiniu_bucket_private = false
|
41
|
-
config.qiniu_block_size = 1024*1024*4
|
40
|
+
config.qiniu_block_size = 1024 * 1024 * 4
|
41
|
+
config.qiniu_async_ops = []
|
42
42
|
config.qiniu_persistent_ops = ''
|
43
43
|
config.qiniu_persistent_notify_url = ''
|
44
44
|
config.qiniu_persistent_pipeline = ''
|
@@ -46,10 +46,11 @@ module CarrierWave
|
|
46
46
|
config.qiniu_private_url_expires_in = 3600
|
47
47
|
config.qiniu_callback_url = ''
|
48
48
|
config.qiniu_callback_body = ''
|
49
|
-
config.qiniu_persistent_notify_url = ''
|
50
49
|
config.qiniu_style_separator = '-'
|
51
50
|
config.qiniu_style_inline = false
|
52
51
|
config.qiniu_delete_after_days = 0
|
52
|
+
|
53
|
+
# 使用 version 和 七牛的持久化参数需要文件从本地上传,而不能在云端缓存
|
53
54
|
config.cache_storage = :file
|
54
55
|
end
|
55
56
|
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
|
4
|
+
module CarrierWave
|
5
|
+
module Qiniu
|
6
|
+
class Connection
|
7
|
+
StatusOK = 200
|
8
|
+
@@connection_established = false
|
9
|
+
|
10
|
+
def initialize(options = {})
|
11
|
+
@qiniu_bucket_domain = options[:qiniu_bucket_domain]
|
12
|
+
@qiniu_bucket = options[:qiniu_bucket]
|
13
|
+
@qiniu_bucket_private = options[:qiniu_bucket_private] || false
|
14
|
+
@qiniu_access_key = options[:qiniu_access_key]
|
15
|
+
@qiniu_secret_key = options[:qiniu_secret_key]
|
16
|
+
@qiniu_block_size = options[:qiniu_block_size] || 1024 * 1024 * 4
|
17
|
+
@qiniu_protocol = options[:qiniu_protocol] || "http"
|
18
|
+
@qiniu_persistent_ops = options[:qiniu_persistent_ops] || options[:qiniu_async_ops] || ""
|
19
|
+
@qiniu_persistent_pipeline = options[:qiniu_persistent_pipeline] || ""
|
20
|
+
@qiniu_persistent_notify_url = options[:qiniu_persistent_notify_url] || ""
|
21
|
+
@qiniu_can_overwrite = options[:qiniu_can_overwrite] || false
|
22
|
+
@qiniu_expires_in = options[:qiniu_expires_in] || options[:expires_in] || 3600
|
23
|
+
@qiniu_up_host = options[:qiniu_up_host]
|
24
|
+
@qiniu_private_url_expires_in = options[:qiniu_private_url_expires_in] || 3600
|
25
|
+
@qiniu_callback_url = options[:qiniu_callback_url] || ""
|
26
|
+
@qiniu_callback_body = options[:qiniu_callback_body] || ""
|
27
|
+
@qiniu_style_separator = options[:qiniu_style_separator] || "-"
|
28
|
+
@qiniu_delete_after_days = options[:qiniu_delete_after_days] || 0
|
29
|
+
init
|
30
|
+
end
|
31
|
+
|
32
|
+
def upload_file(file_path, key)
|
33
|
+
overwrite_file = nil
|
34
|
+
overwrite_file = key if @qiniu_can_overwrite
|
35
|
+
|
36
|
+
put_policy = ::Qiniu::Auth::PutPolicy.new(
|
37
|
+
@qiniu_bucket,
|
38
|
+
overwrite_file,
|
39
|
+
@qiniu_expires_in,
|
40
|
+
nil
|
41
|
+
)
|
42
|
+
|
43
|
+
put_policy.callback_url = @qiniu_callback_url if @qiniu_callback_url.present?
|
44
|
+
put_policy.callback_body = @qiniu_callback_body if @qiniu_callback_body.present?
|
45
|
+
put_policy.persistent_ops = @qiniu_persistent_ops
|
46
|
+
put_policy.persistent_notify_url = @qiniu_persistent_notify_url if @qiniu_persistent_notify_url.present?
|
47
|
+
put_policy.persistent_pipeline = @qiniu_persistent_pipeline
|
48
|
+
|
49
|
+
resp_code, resp_body, resp_headers =
|
50
|
+
::Qiniu::Storage.upload_with_put_policy(put_policy, file_path, key, nil, bucket: @qiniu_bucket)
|
51
|
+
|
52
|
+
raise ::Qiniu::UploadFailedError.new(resp_code, resp_body) if resp_code != StatusOK
|
53
|
+
|
54
|
+
resp_body
|
55
|
+
end
|
56
|
+
|
57
|
+
#
|
58
|
+
# 复制
|
59
|
+
# @param origin [String]
|
60
|
+
# @param target [String]
|
61
|
+
# @return [Boolean]
|
62
|
+
#
|
63
|
+
def copy(origin, target)
|
64
|
+
success = ::Qiniu.copy(
|
65
|
+
@qiniu_bucket,
|
66
|
+
origin,
|
67
|
+
@qiniu_bucket,
|
68
|
+
target
|
69
|
+
)
|
70
|
+
success
|
71
|
+
end
|
72
|
+
|
73
|
+
#
|
74
|
+
# 移动
|
75
|
+
# @param origin [String]
|
76
|
+
# @param target [String]
|
77
|
+
# @return [Boolean]
|
78
|
+
#
|
79
|
+
def move(origin, target)
|
80
|
+
success = ::Qiniu.move(
|
81
|
+
@qiniu_bucket,
|
82
|
+
origin, # 源资源名
|
83
|
+
@qiniu_bucket,
|
84
|
+
target # 目标资源名
|
85
|
+
)
|
86
|
+
success
|
87
|
+
end
|
88
|
+
|
89
|
+
#
|
90
|
+
# 删除
|
91
|
+
# @param key [String]
|
92
|
+
# @return [Boolean]
|
93
|
+
#
|
94
|
+
def delete(key)
|
95
|
+
success = ::Qiniu.delete(
|
96
|
+
@qiniu_bucket,
|
97
|
+
key
|
98
|
+
)
|
99
|
+
success
|
100
|
+
end
|
101
|
+
|
102
|
+
#
|
103
|
+
# 获取文件信息
|
104
|
+
# @param key [String]
|
105
|
+
# @return [Hash]
|
106
|
+
#
|
107
|
+
def stat(key)
|
108
|
+
info = ::Qiniu.stat(
|
109
|
+
@qiniu_bucket, # 存储空间
|
110
|
+
key # 资源名
|
111
|
+
)
|
112
|
+
info
|
113
|
+
end
|
114
|
+
|
115
|
+
def get(path)
|
116
|
+
code, result, _ = ::Qiniu::HTTP.get(download_url(path))
|
117
|
+
code == 200 ? result : nil
|
118
|
+
end
|
119
|
+
|
120
|
+
def download_url(path)
|
121
|
+
encode_path = path_escape(path)
|
122
|
+
primitive_url = "#{@qiniu_protocol}://#{@qiniu_bucket_domain}/#{encode_path}"
|
123
|
+
@qiniu_bucket_private ? ::Qiniu::Auth.authorize_download_url(primitive_url, :expires_in => @qiniu_private_url_expires_in) : primitive_url
|
124
|
+
end
|
125
|
+
|
126
|
+
private
|
127
|
+
|
128
|
+
def init
|
129
|
+
establish_connection! unless @@connection_established
|
130
|
+
@@connection_established = true
|
131
|
+
end
|
132
|
+
|
133
|
+
UserAgent = "CarrierWave-Qiniu/#{Carrierwave::Qiniu::VERSION} (#{RUBY_PLATFORM}) Ruby/#{RUBY_VERSION}".freeze
|
134
|
+
|
135
|
+
def establish_connection!
|
136
|
+
options = {
|
137
|
+
:access_key => @qiniu_access_key,
|
138
|
+
:secret_key => @qiniu_secret_key,
|
139
|
+
:user_agent => UserAgent,
|
140
|
+
}
|
141
|
+
options[:block_size] = @qiniu_block_size if @qiniu_block_size
|
142
|
+
options[:up_host] = @qiniu_up_host if @qiniu_up_host
|
143
|
+
::Qiniu.establish_connection! options
|
144
|
+
end
|
145
|
+
|
146
|
+
#fix chinese file name, same as encodeURIComponent in js but preserve slash '/'
|
147
|
+
def path_escape(value)
|
148
|
+
::URI::DEFAULT_PARSER.escape value
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
@@ -25,17 +25,16 @@ module CarrierWave
|
|
25
25
|
version = args.first.to_sym
|
26
26
|
if styles.has_key? version
|
27
27
|
options = args.last
|
28
|
-
|
29
28
|
# Usage: avatar.url(:version, inline: true)
|
30
29
|
url_options = if options.present? && options.is_a?(Hash) && options[:inline] && styles[version]
|
31
|
-
{
|
30
|
+
{style: styles[version]}
|
32
31
|
else
|
33
32
|
# global inline mode
|
34
33
|
if self.class.qiniu_style_inline && styles[version]
|
35
|
-
{
|
34
|
+
{style: styles[version]}
|
36
35
|
else
|
37
36
|
# Usage: avatar.url(:version)
|
38
|
-
{
|
37
|
+
{version: version}
|
39
38
|
end
|
40
39
|
end
|
41
40
|
return file.url(url_options) if url_options
|
@@ -1,288 +1,25 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require "carrierwave"
|
3
|
-
require "qiniu"
|
4
|
-
require "qiniu/http"
|
5
|
-
|
6
2
|
module CarrierWave
|
7
3
|
module Storage
|
8
4
|
class Qiniu < Abstract
|
9
|
-
class Connection
|
10
|
-
def initialize(options = {})
|
11
|
-
@qiniu_bucket_domain = options[:qiniu_bucket_domain]
|
12
|
-
@qiniu_bucket = options[:qiniu_bucket]
|
13
|
-
@qiniu_bucket_private = options[:qiniu_bucket_private] || false
|
14
|
-
@qiniu_access_key = options[:qiniu_access_key]
|
15
|
-
@qiniu_secret_key = options[:qiniu_secret_key]
|
16
|
-
@qiniu_block_size = options[:qiniu_block_size] || 1024 * 1024 * 4
|
17
|
-
@qiniu_protocol = options[:qiniu_protocol] || "http"
|
18
|
-
@qiniu_persistent_ops = options[:qiniu_persistent_ops] || options[:qiniu_async_ops] || ""
|
19
|
-
@qiniu_persistent_pipeline = options[:qiniu_persistent_pipeline] || ""
|
20
|
-
@qiniu_persistent_notify_url = options[:qiniu_persistent_notify_url] || ""
|
21
|
-
@qiniu_can_overwrite = options[:qiniu_can_overwrite] || false
|
22
|
-
@qiniu_expires_in = options[:qiniu_expires_in] || options[:expires_in] || 3600
|
23
|
-
@qiniu_up_host = options[:qiniu_up_host]
|
24
|
-
@qiniu_private_url_expires_in = options[:qiniu_private_url_expires_in] || 3600
|
25
|
-
@qiniu_callback_url = options[:qiniu_callback_url] || ""
|
26
|
-
@qiniu_callback_body = options[:qiniu_callback_body] || ""
|
27
|
-
@qiniu_style_separator = options[:qiniu_style_separator] || "-"
|
28
|
-
@qiniu_delete_after_days = options[:qiniu_delete_after_days] || 0
|
29
|
-
init
|
30
|
-
end
|
31
|
-
|
32
|
-
def store(file, key)
|
33
|
-
overwrite_file = nil
|
34
|
-
overwrite_file = key if @qiniu_can_overwrite
|
35
|
-
|
36
|
-
put_policy = ::Qiniu::Auth::PutPolicy.new(
|
37
|
-
@qiniu_bucket,
|
38
|
-
overwrite_file,
|
39
|
-
@qiniu_expires_in,
|
40
|
-
nil
|
41
|
-
)
|
42
|
-
|
43
|
-
put_policy.callback_url = @qiniu_callback_url if @qiniu_callback_url.present?
|
44
|
-
put_policy.callback_body = @qiniu_callback_body if @qiniu_callback_body.present?
|
45
|
-
put_policy.persistent_ops = @qiniu_persistent_ops || @qiniu_async_ops
|
46
|
-
put_policy.persistent_notify_url = @qiniu_persistent_notify_url if @qiniu_persistent_notify_url.present?
|
47
|
-
put_policy.persistent_pipeline = @persistent_pipeline
|
48
|
-
|
49
|
-
resp_code, resp_body, resp_headers =
|
50
|
-
::Qiniu::Storage.upload_with_put_policy(
|
51
|
-
put_policy,
|
52
|
-
file.path,
|
53
|
-
key,
|
54
|
-
nil,
|
55
|
-
bucket: @qiniu_bucket,
|
56
|
-
)
|
57
|
-
|
58
|
-
if resp_code < 200 or resp_code >= 300
|
59
|
-
raise ::CarrierWave::UploadError, "Upload failed, status code: #{resp_code}, response: #{resp_body}"
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
#
|
64
|
-
# @note 复制
|
65
|
-
# @param origin [String]
|
66
|
-
# @param target [String]
|
67
|
-
# @return [Boolean]
|
68
|
-
#
|
69
|
-
def copy(origin, target)
|
70
|
-
resp_code, resp_body, _ = ::Qiniu::Storage.copy(@qiniu_bucket, origin, @qiniu_bucket, target)
|
71
|
-
if resp_code < 200 or resp_code >= 300
|
72
|
-
raise ::CarrierWave::IntegrityError, "Copy failed, status code: #{resp_code}, response: #{resp_body}"
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
def delete(key)
|
77
|
-
::Qiniu::Storage.delete(@qiniu_bucket, key) rescue nil
|
78
|
-
end
|
79
|
-
|
80
|
-
def stat(key)
|
81
|
-
code, result, _ = ::Qiniu::Storage.stat(@qiniu_bucket, key)
|
82
|
-
code == 200 ? result : {}
|
83
|
-
end
|
84
|
-
|
85
|
-
def get(path)
|
86
|
-
code, result, _ = ::Qiniu::HTTP.get(download_url(path))
|
87
|
-
code == 200 ? result : nil
|
88
|
-
end
|
89
|
-
|
90
|
-
def download_url(path)
|
91
|
-
encode_path = path_escape(path)
|
92
|
-
primitive_url = "#{@qiniu_protocol}://#{@qiniu_bucket_domain}/#{encode_path}"
|
93
|
-
@qiniu_bucket_private ? ::Qiniu::Auth.authorize_download_url(primitive_url, :expires_in => @qiniu_private_url_expires_in) : primitive_url
|
94
|
-
end
|
95
|
-
|
96
|
-
def clean_cache!(seconds)
|
97
|
-
code, result, response_headers, s, d = Qiniu::Storage.list(Qiniu::Storage::ListPolicy.new(
|
98
|
-
@qiniu_bucket,# 存储空间
|
99
|
-
1000,# 列举的条目数
|
100
|
-
'', # 指定前缀
|
101
|
-
''# 指定目录分隔符
|
102
|
-
)).items.each do |file|
|
103
|
-
# generate_cache_id returns key formated TIMEINT-PID(-COUNTER)-RND
|
104
|
-
time = file.key.scan(/(\d+)-\d+-\d+(?:-\d+)?/).first.map { |t| t.to_i }
|
105
|
-
time = Time.at(*time)
|
106
|
-
delete(file.key) if time < (Time.now.utc - seconds)
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
private
|
111
|
-
|
112
|
-
def init
|
113
|
-
init_qiniu_rs_connection
|
114
|
-
end
|
115
|
-
|
116
|
-
UserAgent = "CarrierWave-Qiniu/#{Carrierwave::Qiniu::VERSION} (#{RUBY_PLATFORM}) Ruby/#{RUBY_VERSION}".freeze
|
117
|
-
|
118
|
-
def init_qiniu_rs_connection
|
119
|
-
options = {
|
120
|
-
:access_key => @qiniu_access_key,
|
121
|
-
:secret_key => @qiniu_secret_key,
|
122
|
-
:user_agent => UserAgent,
|
123
|
-
}
|
124
|
-
options[:block_size] = @qiniu_block_size if @qiniu_block_size
|
125
|
-
options[:up_host] = @qiniu_up_host if @qiniu_up_host
|
126
|
-
|
127
|
-
::Qiniu.establish_connection! options
|
128
|
-
end
|
129
|
-
|
130
|
-
#fix chinese file name, same as encodeURIComponent in js but preserve slash '/'
|
131
|
-
def path_escape(value)
|
132
|
-
::URI::DEFAULT_PARSER.escape value
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
class File
|
137
|
-
def initialize(uploader, path)
|
138
|
-
@uploader, @path = uploader, path
|
139
|
-
end
|
140
|
-
|
141
|
-
def path
|
142
|
-
@path
|
143
|
-
end
|
144
|
-
|
145
|
-
##
|
146
|
-
# Return qiniu URl, maybe with style
|
147
|
-
#
|
148
|
-
# === Parameters
|
149
|
-
# [options (Hash)] optional options hash, 图片样式 { version: :thumb } 或者 { style: "imageView2/1/w/200" }
|
150
|
-
#
|
151
|
-
# === Returns
|
152
|
-
#
|
153
|
-
# [String]
|
154
|
-
#
|
155
|
-
def url(options = {})
|
156
|
-
path = options.present? ? path_with_style(options) : @path
|
157
|
-
qiniu_connection.download_url(path)
|
158
|
-
end
|
159
|
-
|
160
|
-
def store(file)
|
161
|
-
qiniu_connection.store(file, @path)
|
162
|
-
end
|
163
|
-
|
164
|
-
def delete
|
165
|
-
qiniu_connection.delete(@path)
|
166
|
-
end
|
167
|
-
|
168
|
-
def exists?
|
169
|
-
return true if qiniu_connection.stat(path).present?
|
170
|
-
false
|
171
|
-
end
|
172
|
-
|
173
|
-
#
|
174
|
-
# @note 从指定路径复制图片
|
175
|
-
# @param origin_path [String] 原图片路径
|
176
|
-
# @return [Boolean]
|
177
|
-
#
|
178
|
-
def copy_from(origin_path)
|
179
|
-
|
180
|
-
# 先删除目标图片,避免出现0字节文件,无法复制
|
181
|
-
qiniu_connection.delete(@path)
|
182
|
-
|
183
|
-
qiniu_connection.copy(origin_path, @path)
|
184
|
-
end
|
185
|
-
|
186
|
-
##
|
187
|
-
# Reads the contents of the file from Cloud Files
|
188
|
-
#
|
189
|
-
# === Returns
|
190
|
-
#
|
191
|
-
# [String] contents of the file
|
192
|
-
#
|
193
|
-
def read
|
194
|
-
qiniu_connection.get(@path) if self.size > 0
|
195
|
-
end
|
196
|
-
|
197
|
-
def content_type
|
198
|
-
file_info["mimeType"] || "application/octet-stream".freeze
|
199
|
-
end
|
200
|
-
|
201
|
-
def size
|
202
|
-
file_info["fsize"] || 0
|
203
|
-
end
|
204
|
-
|
205
|
-
def extension
|
206
|
-
path.split(".").last
|
207
|
-
end
|
208
|
-
|
209
|
-
def filename
|
210
|
-
::File.basename(path)
|
211
|
-
end
|
212
|
-
|
213
|
-
def clean_cache!(seconds)
|
214
|
-
qiniu_connection.clean_cache!(seconds)
|
215
|
-
end
|
216
|
-
|
217
|
-
private
|
218
|
-
|
219
|
-
def qiniu_connection
|
220
|
-
@qiniu_connection ||= begin
|
221
|
-
config = {
|
222
|
-
:qiniu_access_key => @uploader.qiniu_access_key,
|
223
|
-
:qiniu_secret_key => @uploader.qiniu_secret_key,
|
224
|
-
:qiniu_bucket => @uploader.qiniu_bucket,
|
225
|
-
:qiniu_bucket_domain => @uploader.qiniu_bucket_domain,
|
226
|
-
:qiniu_bucket_private => @uploader.qiniu_bucket_private,
|
227
|
-
:qiniu_block_size => @uploader.qiniu_block_size,
|
228
|
-
:qiniu_protocol => @uploader.qiniu_protocol,
|
229
|
-
:qiniu_expires_in => @uploader.qiniu_expires_in,
|
230
|
-
:qiniu_up_host => @uploader.qiniu_up_host,
|
231
|
-
:qiniu_private_url_expires_in => @uploader.qiniu_private_url_expires_in,
|
232
|
-
:qiniu_callback_url => @uploader.qiniu_callback_url,
|
233
|
-
:qiniu_callback_body => @uploader.qiniu_callback_body,
|
234
|
-
:qiniu_persistent_notify_url => @uploader.qiniu_persistent_notify_url,
|
235
|
-
:qiniu_persistent_pipeline => @uploader.qiniu_persistent_pipeline,
|
236
|
-
:qiniu_style_separator => @uploader.qiniu_style_separator,
|
237
|
-
:qiniu_delete_after_days => @uploader.qiniu_delete_after_days,
|
238
|
-
}
|
239
|
-
|
240
|
-
config[:qiniu_persistent_ops] = Array(@uploader.qiniu_persistent_ops || @uploader.qiniu_async_ops).join(";") rescue ""
|
241
|
-
config[:qiniu_can_overwrite] = @uploader.try :qiniu_can_overwrite rescue false
|
242
|
-
|
243
|
-
Connection.new config
|
244
|
-
end
|
245
|
-
end
|
246
|
-
|
247
|
-
def file_info
|
248
|
-
@file_info ||= qiniu_connection.stat(@path)
|
249
|
-
end
|
250
|
-
|
251
|
-
def path_with_style(options)
|
252
|
-
return @path unless options
|
253
|
-
|
254
|
-
if version = options[:version]
|
255
|
-
"#{@path}#{@uploader.class.qiniu_style_separator}#{version}"
|
256
|
-
elsif style = options[:style]
|
257
|
-
"#{@path}?#{style}"
|
258
|
-
else
|
259
|
-
@path
|
260
|
-
end
|
261
|
-
end
|
262
|
-
end
|
263
|
-
|
264
5
|
def store!(file)
|
265
|
-
|
266
|
-
|
267
|
-
f.copy_from file.copy_from_path
|
268
|
-
else
|
269
|
-
f.store(file)
|
6
|
+
QiniuFile.new(uploader, uploader.store_path).tap do |qiniu_file|
|
7
|
+
qiniu_file.store(file)
|
270
8
|
end
|
271
|
-
f
|
272
9
|
end
|
273
10
|
|
274
11
|
def cache!(file)
|
275
|
-
|
276
|
-
|
277
|
-
|
12
|
+
QiniuFile.new(uploader, uploader.cache_path).tap do |qiniu_file|
|
13
|
+
qiniu_file.store(file)
|
14
|
+
end
|
278
15
|
end
|
279
16
|
|
280
17
|
def retrieve!(identifier)
|
281
|
-
|
18
|
+
QiniuFile.new(uploader, uploader.store_path(identifier))
|
282
19
|
end
|
283
20
|
|
284
21
|
def retrieve_from_cache!(identifier)
|
285
|
-
|
22
|
+
QiniuFile.new(uploader, uploader.cache_path(identifier))
|
286
23
|
end
|
287
24
|
|
288
25
|
##
|
@@ -293,9 +30,9 @@ module CarrierWave
|
|
293
30
|
end
|
294
31
|
|
295
32
|
def clean_cache!(seconds)
|
296
|
-
|
33
|
+
# 如果缓存目录在云端,建议使用七牛云存储的生命周期设置, 以减少主动 API 调用次数
|
34
|
+
raise 'Use Qiniu Object Lifecycle Management to clean the cache'
|
297
35
|
end
|
298
|
-
|
299
36
|
end
|
300
37
|
end
|
301
38
|
end
|