Skip to content

Commit 9298065

Browse files
author
winddies
authored
扩展 key 的支持范围 (#463)
* 扩展 key 的支持范围 * update version * fix review * locaStorage key 区分 * add test; * 删除无用的代码 * 优化逻辑
1 parent 7fa395c commit 9298065

File tree

8 files changed

+24
-15
lines changed

8 files changed

+24
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ qiniu.compressImage(file, options).then(data => {
180180

181181
* **bucket**: 上传的目标空间
182182
* **file**: `File` 对象,上传的文件
183-
* **key**: 文件资源名
183+
* **key**: 文件资源名,为空字符串时则文件资源名也为空,为 `null` 或者 `undefined` 时则自动使用文件的 `hash` 作为文件名
184184
* **token**: 上传验证信息,前端通过接口请求后端获得
185185
* **config**: `object`,其中的每一项都为可选
186186

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "qiniu-js",
33
"jsName": "qiniu",
4-
"version": "3.1.0",
4+
"version": "3.1.1",
55
"private": false,
66
"description": "Javascript SDK for Qiniu Resource (Cloud) Storage AP",
77
"main": "lib/index.js",

src/__tests__/utils.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createLocalKey } from '../utils'
2+
3+
describe('api function test', () => {
4+
test('createLocalKey', () => {
5+
expect(createLocalKey('test', null, 1024)).toMatch('qiniu_js_sdk_upload_file_name_test_size_1024')
6+
expect(createLocalKey('test', 'demo', 1024)).toMatch('qiniu_js_sdk_upload_file_name_test_key_demo_size_1024')
7+
})
8+
})

src/api.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ export async function getUploadUrl(config: Config, token: string): Promise<strin
4343
* @param key 目标文件名
4444
* @param uploadInfo 上传信息
4545
*/
46-
function getBaseUrl(bucket: string, key: string, uploadInfo: UploadInfo) {
46+
function getBaseUrl(bucket: string, key: string | null | undefined, uploadInfo: UploadInfo) {
4747
const { url, id } = uploadInfo
48-
return `${url}/buckets/${bucket}/objects/${urlSafeBase64Encode(key)}/uploads/${id}`
48+
return `${url}/buckets/${bucket}/objects/${key != null ? urlSafeBase64Encode(key) : '~'}/uploads/${id}`
4949
}
5050

5151
export interface InitPartsData {
@@ -64,10 +64,10 @@ export interface InitPartsData {
6464
export function initUploadParts(
6565
token: string,
6666
bucket: string,
67-
key: string,
67+
key: string | null | undefined,
6868
uploadUrl: string
6969
): utils.Response<InitPartsData> {
70-
const url = `${uploadUrl}/buckets/${bucket}/objects/${urlSafeBase64Encode(key)}/uploads`
70+
const url = `${uploadUrl}/buckets/${bucket}/objects/${key != null ? urlSafeBase64Encode(key) : '~'}/uploads`
7171
return utils.request<InitPartsData>(
7272
url,
7373
{
@@ -90,7 +90,7 @@ export interface UploadChunkData {
9090
*/
9191
export function uploadChunk(
9292
token: string,
93-
key: string,
93+
key: string | null | undefined,
9494
index: number,
9595
uploadInfo: UploadInfo,
9696
options: Partial<utils.RequestOptions>
@@ -114,7 +114,7 @@ export type UploadCompleteData = any
114114
*/
115115
export function uploadComplete(
116116
token: string,
117-
key: string,
117+
key: string | null | undefined,
118118
uploadInfo: UploadInfo,
119119
options: Partial<utils.RequestOptions>
120120
): utils.Response<UploadCompleteData> {
@@ -134,7 +134,7 @@ export function uploadComplete(
134134
*/
135135
export function deleteUploadedChunks(
136136
token: string,
137-
key: string,
137+
key: string | null | undefined,
138138
uploadinfo: UploadInfo
139139
): utils.Response<void> {
140140
const bucket = utils.getPutPolicy(token).bucket

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const statisticsLogger = new StatisticsLogger()
1717
*/
1818
function upload(
1919
file: File,
20-
key: string,
20+
key: string | null | undefined,
2121
token: string,
2222
putExtra?: Partial<Extra>,
2323
config?: Partial<Config>

src/upload/base.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface Config {
4444

4545
export interface UploadOptions {
4646
file: File
47-
key: string
47+
key: string | null | undefined
4848
token: string
4949
putExtra?: Partial<Extra>
5050
config?: Partial<Config>
@@ -88,7 +88,7 @@ export default abstract class Base {
8888
protected putExtra: Extra
8989
protected xhrList: XMLHttpRequest[] = []
9090
protected file: File
91-
protected key: string
91+
protected key: string | null | undefined
9292
protected aborted = false
9393
protected retryCount = 0
9494
protected token: string

src/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ export function setLocalFileInfo(localKey: string, info: LocalInfo) {
5353
}
5454
}
5555

56-
export function createLocalKey(name: string, key: string, size: number): string {
57-
return `qiniu_js_sdk_upload_file_name_${name}_key_${key}_size_${size}`
56+
export function createLocalKey(name: string, key: string | null | undefined, size: number): string {
57+
const localKey = key == null ? '_' : `_key_${key}_`
58+
return `qiniu_js_sdk_upload_file_name_${name}${localKey}size_${size}`
5859
}
5960

6061
export function removeLocalFileInfo(localKey: string) {

0 commit comments

Comments
 (0)