activestorage_qinium 0.4.0 → 0.4.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e590bb1798ce61b646b065e247820d3febcc2eebb0efa5475e2240701ccbf14a
4
- data.tar.gz: 7711785ef96a898e174fa7157608a2b8f1e4d84a3b8e9a386ea0ff07fdf3c5f1
3
+ metadata.gz: '08e39c5af245eba9281ed6e5013a0747c36d7da69620adaf2244d70847a3e648'
4
+ data.tar.gz: 1158e940e113c509c04e56e9a3a99fdeb1cb1aa88144123c3078378b21b0e3db
5
5
  SHA512:
6
- metadata.gz: 0e53820bad4cb1197da14cef634b6476d11b35222321033b90a4c7b7d356de72418dd33ebb2d3daaf660c167a6b13af1f1498e6e1571e3cc29bbd794abf0c830
7
- data.tar.gz: a706dc0cb47e2b2e91832439d815e72e1a53ecce9426330ad28811860e61084dd99476d79f65f08034b1d57d5ee976c621ee58802096251f3ffb64c1f9724083
6
+ metadata.gz: 4ab2a096e129d3bf26b14c7766479dbd8b4ed6cdbcf70ba49d99d60a677eb63f375a1f2fd74c88dfeede77fe3b8e121c807ff9b99f71a6bdfc87de9626811130
7
+ data.tar.gz: bfadf92e3ac9affddf56a48438ef49d5780cb04183c356c506a04443126526b15056c5b1d5959d4ec130bca24eb37e9a418d4c82ea3af2042c25acb44f076df8
@@ -10,7 +10,7 @@ module ActiveStorage
10
10
  to: :config
11
11
 
12
12
  def self.analyzers
13
- [ActiveStorage::Analyzer::QiniumImageAnalyzer]
13
+ @analyzers ||= [ActiveStorage::Analyzer::QiniumImageAnalyzer]
14
14
  end
15
15
 
16
16
  def initialize(options)
@@ -132,25 +132,46 @@ module ActiveStorage
132
132
 
133
133
  def url(key, **options)
134
134
  instrument :url, key: key do |payload|
135
- fop = if options[:fop].present? # 内容预处理
136
- options[:fop]
137
- elsif options[:disposition].to_s == "attachment" # 下载附件
138
- attname = URI.encode_www_form_component (options[:filename] || key).to_s
139
- "attname=#{attname}"
140
- end
141
-
135
+ # 根据七牛云文档:https://developer.qiniu.com/kodo/1659/download-setting
136
+ # 1. 使用 attname 参数可以让浏览器下载而不是打开
137
+ # 2. 使用 response-content-disposition 参数可以自定义 Content-Disposition 响应头
138
+ # 3. 使用 response-content-type 参数可以自定义 Content-Type 响应头
139
+
140
+ disposition = options[:disposition].to_s.downcase
141
+ content_type = options[:content_type]
142
+
143
+ # 构建查询参数
144
+ query_params = []
145
+
146
+ if options[:fop].present? # 内容预处理
147
+ query_params << options[:fop]
148
+ elsif disposition == "attachment" # 下载附件
149
+ attname = URI.encode_www_form_component (options[:filename] || key).to_s
150
+ query_params << "attname=#{attname}"
151
+ elsif disposition == "inline" # 预览(inline)
152
+ # 明确设置 Content-Disposition 为 inline,确保浏览器预览而不是下载
153
+ query_params << "response-content-disposition=inline"
154
+ # 如果提供了 content_type,也设置 Content-Type 响应头
155
+ # 这对于 PDF 文件特别重要,确保 Content-Type: application/pdf
156
+ query_params << "response-content-type=#{URI.encode_www_form_component(content_type)}" if content_type.present?
157
+ end
158
+
159
+ # 构建 URL
142
160
  url = if config.public
143
161
  url_encoded_key = key.split("/").map { |x| CGI.escape(x) }.join("/")
144
- ["#{protocol}://#{domain}/#{url_encoded_key}", fop].compact.join("?")
162
+ base_url = "#{protocol}://#{domain}/#{url_encoded_key}"
163
+ query_params.any? ? "#{base_url}?#{query_params.join('&')}" : base_url
145
164
  else
146
165
  expires_in = options[:expires_in] ||
147
166
  Rails.application.config.active_storage.service_urls_expire_in ||
148
167
  3600
168
+ # 对于私有资源,需要将查询参数传递给授权 URL 生成
169
+ fop = query_params.join('&') if query_params.any?
149
170
  Qinium::Auth.authorize_download_url(domain, key,
150
171
  access_key, secret_key,
151
172
  schema: protocol, fop: fop, expires_in: expires_in)
152
173
  end
153
-
174
+
154
175
  payload[:url] = url
155
176
  url
156
177
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveStorageQinium
4
- VERSION = "0.4.0"
4
+ VERSION = "0.4.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activestorage_qinium
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - xiaohui
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-01-17 00:00:00.000000000 Z
11
+ date: 2025-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: qinium