Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 6 support #27

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 50 additions & 31 deletions lib/active_storage/service/qiniu_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module ActiveStorage
# bucket: <%= ENV['QINIU_BUCKET'] %>
# domain: <%= ENV['QINIU_DOMAIN'] %>
# protocol: <%= ENV.fetch("QINIU_PROTOCOL") { "http" } %>
# public: <%= ENV['QINIU_BUCKET_IS_PUBLIC'] %>
#
# more options. https://github.com/qiniu/ruby-sdk/blob/master/lib/qiniu/auth.rb#L49
#
Expand All @@ -26,18 +27,19 @@ module ActiveStorage
#
class Service::QiniuService < Service
BLOCK_SIZE = 4 * 1024 * 1024
attr_reader :bucket, :domain, :upload_options, :protocol, :bucket_private
attr_reader :bucket, :domain, :upload_options, :protocol

def initialize(access_key:, secret_key:, bucket:, domain:, **options)
def initialize(access_key:, secret_key:, bucket:, domain:, public: false, protocol: 'https', **options)
@bucket = bucket
@public = public
@domain = domain
@protocol = (options.delete(:protocol) || 'https').to_sym
bucket_private = options.delete(:bucket_private)
@bucket_private = bucket_private.nil? ? false : !!bucket_private
Qiniu.establish_connection! access_key: access_key,
secret_key: secret_key,
protocol: @protocol,
**options
@protocol = protocol
Qiniu.establish_connection!(
access_key: access_key,
secret_key: secret_key,
protocol: @protocol,
**options
)

@upload_options = options
end
Expand Down Expand Up @@ -88,6 +90,21 @@ def exist?(key)
end
end

def url(key, **options)
instrument :url, key: key do |payload|
generated_url =
if public?
public_url(key, **options)
else
private_url(key, **options)
end

payload[:url] = generated_url

generated_url
end
end

def download(key)
if block_given?
instrument :streaming_download, key: key do
Expand All @@ -113,28 +130,6 @@ def download_chunk(key, range)
end
end

def url(key, **options)
instrument :url, key: key do |payload|
fop = if options[:fop].present? # 内容预处理
options[:fop]
elsif options[:disposition].to_s == 'attachment' # 下载附件
attname = URI.escape "#{options[:filename] || key}"
"attname=#{attname}"
end

url = if bucket_private
expires_in = options[:expires_in] || url_expires_in
Qiniu::Auth.authorize_download_url_2(domain, key, schema: protocol, fop: fop, expires_in: expires_in)
else
url_encoded_key = CGI::escape(key)
"#{protocol}://#{domain}/#{url_encoded_key}?#{fop}"
end

payload[:url] = url
url
end
end

def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:)
instrument :url, key: key do |payload|
url = Qiniu::Config.up_host(bucket)
Expand All @@ -147,8 +142,23 @@ def headers_for_direct_upload(key, content_type:, checksum:, **)
{ "Content-Type" => content_type, "Content-MD5" => checksum, "x-token" => generate_uptoken(key) }
end

def public?
@public
end

private

def private_url(key, **options)
expires_in = options[:expires_in] || ActiveStorage.service_urls_expire_in
Qiniu::Auth.authorize_download_url_2(domain, key, schema: protocol, fop: fop_for(options), expires_in: expires_in)
end

def public_url(key, **options)
fop = fop_for(options)
fop = "?#{fop}" if fop.present?
"#{protocol}://#{domain}/#{CGI::escape(key)}#{fop}"
end

def items_for(prefix='')
list_policy = Qiniu::Storage::ListPolicy.new(
bucket, # 存储空间
Expand All @@ -160,6 +170,15 @@ def items_for(prefix='')
result['items']
end

def fop_for(options)
if options[:fop].present? # 内容预处理
options[:fop]
elsif options[:disposition].to_s == 'attachment' # 下载附件
attname = URI.escape "#{options[:filename] || 'file'}"
"attname=#{attname}"
end
end

def generate_uptoken(key=nil, expires_in=nil)
expires_in ||= 3600
put_policy = Qiniu::Auth::PutPolicy.new(bucket, key, expires_in)
Expand Down
2 changes: 1 addition & 1 deletion lib/activestorage_qiniu/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ActivestorageQiniu
VERSION = "0.2.0"
VERSION = "0.2.2"
end