From 79a06befdee40d83cb06f76b2b054a1171cb7b27 Mon Sep 17 00:00:00 2001 From: seandong Date: Mon, 16 Mar 2020 15:20:42 +0800 Subject: [PATCH 1/5] ADD: rails 6 support --- lib/active_storage/service/qiniu_service.rb | 61 ++++++++++----------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/lib/active_storage/service/qiniu_service.rb b/lib/active_storage/service/qiniu_service.rb index f13eb57..27e4c67 100644 --- a/lib/active_storage/service/qiniu_service.rb +++ b/lib/active_storage/service/qiniu_service.rb @@ -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 # @@ -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 @@ -113,28 +115,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) @@ -149,6 +129,16 @@ def headers_for_direct_upload(key, content_type:, checksum:, **) private + def private_url(key, expires_in:, **options) + 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, # 存储空间 @@ -160,6 +150,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] || key}" + "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) From 7fb976d49d781d2a29939c0e54405e52ba851eaf Mon Sep 17 00:00:00 2001 From: seandong Date: Mon, 16 Mar 2020 16:00:16 +0800 Subject: [PATCH 2/5] ADD: implement url --- lib/active_storage/service/qiniu_service.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/active_storage/service/qiniu_service.rb b/lib/active_storage/service/qiniu_service.rb index 27e4c67..f1e3406 100644 --- a/lib/active_storage/service/qiniu_service.rb +++ b/lib/active_storage/service/qiniu_service.rb @@ -90,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 @@ -127,6 +142,10 @@ 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, expires_in:, **options) From 64d859f6ae9a153414e6771d6fa68570c8bd47ae Mon Sep 17 00:00:00 2001 From: seandong Date: Thu, 4 Nov 2021 15:17:06 +0800 Subject: [PATCH 3/5] FIX: missing expires_in --- lib/active_storage/service/qiniu_service.rb | 3 ++- lib/activestorage_qiniu/version.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/active_storage/service/qiniu_service.rb b/lib/active_storage/service/qiniu_service.rb index f1e3406..9b6b208 100644 --- a/lib/active_storage/service/qiniu_service.rb +++ b/lib/active_storage/service/qiniu_service.rb @@ -148,7 +148,8 @@ def public? private - def private_url(key, expires_in:, **options) + 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 diff --git a/lib/activestorage_qiniu/version.rb b/lib/activestorage_qiniu/version.rb index 97b53d6..595dc52 100644 --- a/lib/activestorage_qiniu/version.rb +++ b/lib/activestorage_qiniu/version.rb @@ -1,3 +1,3 @@ module ActivestorageQiniu - VERSION = "0.2.0" + VERSION = "0.2.1" end From 82c1ff5bed5ab4999d25553020dc081f993ed72f Mon Sep 17 00:00:00 2001 From: seandong Date: Fri, 5 Nov 2021 13:33:55 +0800 Subject: [PATCH 4/5] FIX: bug --- lib/active_storage/service/qiniu_service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/active_storage/service/qiniu_service.rb b/lib/active_storage/service/qiniu_service.rb index 9b6b208..0f3041c 100644 --- a/lib/active_storage/service/qiniu_service.rb +++ b/lib/active_storage/service/qiniu_service.rb @@ -174,7 +174,7 @@ def fop_for(options) if options[:fop].present? # 内容预处理 options[:fop] elsif options[:disposition].to_s == 'attachment' # 下载附件 - attname = URI.escape "#{options[:filename] || key}" + attname = URI.escape "#{options[:filename] || 'file'}" "attname=#{attname}" end end From 0c4a35f4bff34da2227bd562c68987838aea446e Mon Sep 17 00:00:00 2001 From: seandong Date: Mon, 8 Nov 2021 17:03:04 +0800 Subject: [PATCH 5/5] CHG: version --- lib/activestorage_qiniu/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/activestorage_qiniu/version.rb b/lib/activestorage_qiniu/version.rb index 595dc52..50d4420 100644 --- a/lib/activestorage_qiniu/version.rb +++ b/lib/activestorage_qiniu/version.rb @@ -1,3 +1,3 @@ module ActivestorageQiniu - VERSION = "0.2.1" + VERSION = "0.2.2" end