Skip to content

Commit 70d1f2d

Browse files
authored
新增 key 参数,更明确指定上传文件名 (#663)
* 新增 key 参数,更明确指定上传文件名 * MockProgress 兼容多次调用 start * update version
1 parent dcc659e commit 70d1f2d

File tree

15 files changed

+167
-215
lines changed

15 files changed

+167
-215
lines changed

packages/browser/src/file/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export class UploadFile implements BaseUploadFile {
4949
return { result: 'browser platform files' }
5050
}
5151

52+
async key(): Promise<Result<string | null>> {
53+
return { result: this.fileData?.key || null }
54+
}
55+
5256
async name(): Promise<Result<string | null>> {
5357
const realFilename = this.fileData.type === 'file' && this.fileData.data.name
5458
return { result: this.fileData?.filename || realFilename || null }

packages/common/src/helper/progress/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ type ProgressListener = (progress: number) => void
33
/** 虚拟进度条;在没有调用 end 之前会无限慢慢逼近 100%,但不会到达 100% */
44
export class MockProgress {
55
private progress = 0
6-
private intervalId: number | null = null
6+
private intervalIds: number[] = []
77
private listeners: ProgressListener[] = []
88
constructor(
99
/** 最大时间;单位为秒 */
1010
private timeConstant = 1
1111
) {}
1212

1313
private clearInterval() {
14-
if (this.intervalId) {
15-
clearInterval(this.intervalId)
16-
this.intervalId = null
14+
for (const intervalId of this.intervalIds) {
15+
clearInterval(intervalId)
1716
}
17+
18+
this.intervalIds = []
1819
}
1920

2021
private callListeners() {
@@ -29,13 +30,17 @@ export class MockProgress {
2930
}
3031

3132
start() {
33+
this.clearInterval()
34+
3235
let time = 0
3336
this.progress = 0
3437
const intervalFrequency = 100
35-
this.intervalId = setInterval(() => {
38+
const intervalIds = setInterval(() => {
3639
time += intervalFrequency
3740
this.setProgress(1 - Math.exp(-1 * time / (this.timeConstant * 1000)))
3841
}, intervalFrequency)
42+
43+
this.intervalIds.push(intervalIds)
3944
}
4045

4146
end() {

packages/common/src/types/file.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Result } from './types'
22

33
export type FileData = {
4-
/** 文件名;该文件保存到空间时的名称 */
4+
/** 文件名;如果未指定,则为 filename */
5+
key?: string
6+
/** 本地文件名;如果未指定,默认为随机字符串 */
57
filename?: string
68
/** 文件的媒体类型;该文件保存到空间时的媒体类型 */
79
mimeType?: string
@@ -20,7 +22,9 @@ export interface UploadFile {
2022
size(): Promise<Result<number>>
2123
/** 文件路径;返回文件的完整路径 */
2224
path(): Promise<Result<string>>
23-
/** 文件名 */
25+
/** 目标文件名,最终存储的文件名 */
26+
key(): Promise<Result<string | null>>
27+
/** 原始文件名,如果不存在则会是随机字符串 */
2428
name(): Promise<Result<string | null>>
2529
/** 媒体类型 */
2630
mimeType(): Promise<Result<string | null>>

packages/common/src/upload/direct/index.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,22 @@ class DirectUploadTask implements Task {
3939
}
4040

4141
async process(notify: () => void): Promise<Result> {
42-
const fileNameResult = await this.file.name()
43-
if (!isSuccessResult(fileNameResult)) {
44-
if (isErrorResult(fileNameResult)) {
45-
this.context.error = fileNameResult.error
42+
const filenameResult = await this.file.name()
43+
if (!isSuccessResult(filenameResult)) {
44+
if (isErrorResult(filenameResult)) {
45+
this.context.error = filenameResult.error
4646
}
4747

48-
return fileNameResult
48+
return filenameResult
49+
}
50+
51+
const fileKeyResult = await this.file.key()
52+
if (!isSuccessResult(fileKeyResult)) {
53+
if (isErrorResult(fileKeyResult)) {
54+
this.context.error = fileKeyResult.error
55+
}
56+
57+
return fileKeyResult
4958
}
5059

5160
const fileMetaResult = await this.file.metadata()
@@ -71,9 +80,9 @@ class DirectUploadTask implements Task {
7180
customVars: this.vars,
7281
token: this.context!.token!,
7382
metadata: fileMetaResult.result,
74-
key: fileNameResult.result || undefined,
7583
uploadHostUrl: this.context!.host!.getUrl(),
76-
fileName: fileNameResult.result || generateRandomString(), // 接口要求必传且建议没有有效文件名时传随机字符串
84+
fileName: filenameResult.result || generateRandomString(), // 接口要求必传且建议没有有效文件名时传随机字符串
85+
key: fileKeyResult.result || filenameResult.result || undefined,
7786
onProgress: progress => {
7887
this.context!.progress.details.directUpload.percent = progress.percent
7988
this.context!.progress.details.directUpload.size = fileSizeResult.result

packages/common/src/upload/multipartv1/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ class MkfileTask implements Task {
131131
async process(notify: () => void): Promise<Result> {
132132
this.updateProgress(0, notify)
133133

134+
const fileKeyResult = await this.file.key()
135+
if (!isSuccessResult(fileKeyResult)) return fileKeyResult
136+
134137
const filenameResult = await this.file.name()
135138
if (!isSuccessResult(filenameResult)) return filenameResult
136139

@@ -149,10 +152,10 @@ class MkfileTask implements Task {
149152
userVars: this.vars,
150153
token: this.context.token!,
151154
fileSize: fileSizeResult.result,
152-
key: filenameResult.result || undefined,
155+
uploadHostUrl: this.context!.host!.getUrl(),
153156
fname: filenameResult.result || generateRandomString(),
154157
lastCtxOfBlock: this.context.uploadBlocks.map(i => i.ctx),
155-
uploadHostUrl: this.context!.host!.getUrl(),
158+
key: fileKeyResult.result || filenameResult.result || undefined,
156159
onProgress: progress => { this.updateProgress(progress.percent, notify) }
157160
})
158161

packages/common/src/upload/multipartv2/index.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ class InitPartUploadTask implements Task {
6464
const filenameResult = await this.file.name()
6565
if (!isSuccessResult(filenameResult)) return filenameResult
6666

67+
const fileKeyResult = await this.file.key()
68+
if (!isSuccessResult(fileKeyResult)) return fileKeyResult
69+
6770
// 首先检查 context 上的 upload id 有没有过期
6871
if (this.context.uploadPartId) {
6972
const nowTime = Date.now() / 1e3
@@ -75,9 +78,9 @@ class InitPartUploadTask implements Task {
7578
abort: this.abort,
7679
token: this.context!.token!,
7780
bucket: this.context.token!.bucket,
78-
key: filenameResult.result || undefined,
7981
uploadHostUrl: this.context!.host!.getUrl(),
8082
uploadId: this.context.uploadPartId.uploadId,
83+
key: fileKeyResult.result || filenameResult.result || undefined,
8184
onProgress: progress => { this.updateProgress(progress.percent, notify) }
8285
})
8386

@@ -105,8 +108,8 @@ class InitPartUploadTask implements Task {
105108
abort: this.abort,
106109
token: this.context!.token!,
107110
bucket: this.context!.token!.bucket,
108-
key: filenameResult.result || undefined,
109111
uploadHostUrl: this.context!.host!.getUrl(),
112+
key: fileKeyResult.result || filenameResult.result || undefined,
110113
onProgress: progress => { this.updateProgress(progress.percent, notify) }
111114
})
112115

@@ -168,6 +171,9 @@ class UploadPartTask implements Task {
168171
const filenameResult = await this.file.name()
169172
if (!isSuccessResult(filenameResult)) return filenameResult
170173

174+
const fileKeyResult = await this.file.key()
175+
if (!isSuccessResult(fileKeyResult)) return fileKeyResult
176+
171177
const fileSizeResult = await this.file.size()
172178
if (!isSuccessResult(fileSizeResult)) return fileSizeResult
173179

@@ -178,9 +184,9 @@ class UploadPartTask implements Task {
178184
partIndex: this.index,
179185
token: this.context!.token!,
180186
bucket: this.context!.token!.bucket,
181-
key: filenameResult.result || undefined,
182187
uploadHostUrl: this.context!.host!.getUrl(),
183188
uploadId: this.context!.uploadPartId!.uploadId!,
189+
key: fileKeyResult.result || filenameResult.result || undefined,
184190
onProgress: progress => { this.updateProgress(false, fileSizeResult.result, progress.percent, notify) }
185191
})
186192

@@ -233,6 +239,10 @@ class CompletePartUploadTask implements Task {
233239

234240
async process(notify: () => void): Promise<Result> {
235241
this.updateProgress(0, notify)
242+
243+
const fileKeyResult = await this.file.key()
244+
if (!isSuccessResult(fileKeyResult)) return fileKeyResult
245+
236246
const filenameResult = await this.file.name()
237247
if (!isSuccessResult(filenameResult)) return filenameResult
238248

@@ -253,10 +263,10 @@ class CompletePartUploadTask implements Task {
253263
customVars: this.vars,
254264
token: this.context!.token!,
255265
metadata: metadataResult.result,
256-
key: filenameResult.result || undefined,
257266
uploadHostUrl: this.context!.host!.getUrl(),
258267
mimeType: mimeTypeResult.result || undefined,
259268
uploadId: this.context!.uploadPartId!.uploadId!,
269+
key: fileKeyResult.result || filenameResult.result || undefined,
260270
fileName: filenameResult.result || generateRandomString(), // 和直传行为保持一致
261271
onProgress: progress => { this.updateProgress(progress.percent, notify) }
262272
})

packages/harmony/entry/oh-package-lock.json5

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

packages/harmony/entry/src/main/ets/pages/Index.ets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ struct Index {
5454

5555
const file = this.getUploadFile()
5656

57+
file.key = "test-test"
5758
file.filename = "test-test"
5859
file.mimeType = "232312323/2312"
5960
file.metadata = {
6061
'2321': "2323232"
6162
}
6263

63-
6464
const task = createMultipartUploadV2Task(context, file, {
6565
logLevel: 'INFO',
6666
vars: { test: '222222' },

packages/harmony/library/BuildProfile.ets

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Use these variables when you tailor your ArkTS code. They must be of the const type.
33
*/
4-
export const HAR_VERSION = '1.0.0';
4+
export const HAR_VERSION = '1.0.1';
55
export const BUILD_MODE_NAME = 'debug';
66
export const DEBUG = true;
77
export const TARGET_NAME = 'default';
@@ -14,4 +14,4 @@ export default class BuildProfile {
1414
static readonly BUILD_MODE_NAME = BUILD_MODE_NAME;
1515
static readonly DEBUG = DEBUG;
1616
static readonly TARGET_NAME = TARGET_NAME;
17-
}
17+
}

packages/harmony/library/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Change logs
22

3+
## v1.0.1 (2024-07-09)
4+
5+
### 改动
6+
7+
- 修复 onProgress 在任务完成之后依旧触发的问题
8+
- UploadFile 添加 key 属性用于指定上传存储目标 key
9+
10+
## v1.0.0 (2024-05-08)
11+
12+
### 改动
13+
14+
- rc 版本升级为 1.0.0
15+
316
## v1.0.0-rc.5 (2024-03-14)
417

518
### 改动

packages/harmony/library/oh-package.json5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"modelVersion": "5.0.0",
33
"name": "@qiniu/upload",
4-
"version": "1.0.0-rc.6",
4+
"version": "1.0.1",
55
"keywords": ["qiniu", "upload", "oss"],
66
"description": "Qiniu Cloud object storage upload sdk",
77
"repository": "https://github.com/qiniu/js-sdk.git",

0 commit comments

Comments
 (0)