Skip to content

扩展 key 的支持范围 #463

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

Merged
merged 7 commits into from
Jul 31, 2020
Merged
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
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