Skip to content

Commit

Permalink
扩展 key 的支持范围 (#463)
Browse files Browse the repository at this point in the history
* 扩展 key 的支持范围

* update version

* fix review

* locaStorage key 区分

* add test;

* 删除无用的代码

* 优化逻辑
  • Loading branch information
winddies authored Jul 31, 2020
1 parent 7fa395c commit 9298065
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ qiniu.compressImage(file, options).then(data => {

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

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "qiniu-js",
"jsName": "qiniu",
"version": "3.1.0",
"version": "3.1.1",
"private": false,
"description": "Javascript SDK for Qiniu Resource (Cloud) Storage AP",
"main": "lib/index.js",
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createLocalKey } from '../utils'

describe('api function test', () => {
test('createLocalKey', () => {
expect(createLocalKey('test', null, 1024)).toMatch('qiniu_js_sdk_upload_file_name_test_size_1024')
expect(createLocalKey('test', 'demo', 1024)).toMatch('qiniu_js_sdk_upload_file_name_test_key_demo_size_1024')
})
})
14 changes: 7 additions & 7 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export async function getUploadUrl(config: Config, token: string): Promise<strin
* @param key 目标文件名
* @param uploadInfo 上传信息
*/
function getBaseUrl(bucket: string, key: string, uploadInfo: UploadInfo) {
function getBaseUrl(bucket: string, key: string | null | undefined, uploadInfo: UploadInfo) {
const { url, id } = uploadInfo
return `${url}/buckets/${bucket}/objects/${urlSafeBase64Encode(key)}/uploads/${id}`
return `${url}/buckets/${bucket}/objects/${key != null ? urlSafeBase64Encode(key) : '~'}/uploads/${id}`
}

export interface InitPartsData {
Expand All @@ -64,10 +64,10 @@ export interface InitPartsData {
export function initUploadParts(
token: string,
bucket: string,
key: string,
key: string | null | undefined,
uploadUrl: string
): utils.Response<InitPartsData> {
const url = `${uploadUrl}/buckets/${bucket}/objects/${urlSafeBase64Encode(key)}/uploads`
const url = `${uploadUrl}/buckets/${bucket}/objects/${key != null ? urlSafeBase64Encode(key) : '~'}/uploads`
return utils.request<InitPartsData>(
url,
{
Expand All @@ -90,7 +90,7 @@ export interface UploadChunkData {
*/
export function uploadChunk(
token: string,
key: string,
key: string | null | undefined,
index: number,
uploadInfo: UploadInfo,
options: Partial<utils.RequestOptions>
Expand All @@ -114,7 +114,7 @@ export type UploadCompleteData = any
*/
export function uploadComplete(
token: string,
key: string,
key: string | null | undefined,
uploadInfo: UploadInfo,
options: Partial<utils.RequestOptions>
): utils.Response<UploadCompleteData> {
Expand All @@ -134,7 +134,7 @@ export function uploadComplete(
*/
export function deleteUploadedChunks(
token: string,
key: string,
key: string | null | undefined,
uploadinfo: UploadInfo
): utils.Response<void> {
const bucket = utils.getPutPolicy(token).bucket
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const statisticsLogger = new StatisticsLogger()
*/
function upload(
file: File,
key: string,
key: string | null | undefined,
token: string,
putExtra?: Partial<Extra>,
config?: Partial<Config>
Expand Down
4 changes: 2 additions & 2 deletions src/upload/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface Config {

export interface UploadOptions {
file: File
key: string
key: string | null | undefined
token: string
putExtra?: Partial<Extra>
config?: Partial<Config>
Expand Down Expand Up @@ -88,7 +88,7 @@ export default abstract class Base {
protected putExtra: Extra
protected xhrList: XMLHttpRequest[] = []
protected file: File
protected key: string
protected key: string | null | undefined
protected aborted = false
protected retryCount = 0
protected token: string
Expand Down
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ export function setLocalFileInfo(localKey: string, info: LocalInfo) {
}
}

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

export function removeLocalFileInfo(localKey: string) {
Expand Down

0 comments on commit 9298065

Please sign in to comment.