Skip to content

Commit

Permalink
新增 key 参数,更明确指定上传文件名 (#663)
Browse files Browse the repository at this point in the history
* 新增 key 参数,更明确指定上传文件名

* MockProgress 兼容多次调用 start

* update version
  • Loading branch information
yinxulai authored Jul 9, 2024
1 parent dcc659e commit 70d1f2d
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 215 deletions.
4 changes: 4 additions & 0 deletions packages/browser/src/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export class UploadFile implements BaseUploadFile {
return { result: 'browser platform files' }
}

async key(): Promise<Result<string | null>> {
return { result: this.fileData?.key || null }
}

async name(): Promise<Result<string | null>> {
const realFilename = this.fileData.type === 'file' && this.fileData.data.name
return { result: this.fileData?.filename || realFilename || null }
Expand Down
15 changes: 10 additions & 5 deletions packages/common/src/helper/progress/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ type ProgressListener = (progress: number) => void
/** 虚拟进度条;在没有调用 end 之前会无限慢慢逼近 100%,但不会到达 100% */
export class MockProgress {
private progress = 0
private intervalId: number | null = null
private intervalIds: number[] = []
private listeners: ProgressListener[] = []
constructor(
/** 最大时间;单位为秒 */
private timeConstant = 1
) {}

private clearInterval() {
if (this.intervalId) {
clearInterval(this.intervalId)
this.intervalId = null
for (const intervalId of this.intervalIds) {
clearInterval(intervalId)
}

this.intervalIds = []
}

private callListeners() {
Expand All @@ -29,13 +30,17 @@ export class MockProgress {
}

start() {
this.clearInterval()

let time = 0
this.progress = 0
const intervalFrequency = 100
this.intervalId = setInterval(() => {
const intervalIds = setInterval(() => {
time += intervalFrequency
this.setProgress(1 - Math.exp(-1 * time / (this.timeConstant * 1000)))
}, intervalFrequency)

this.intervalIds.push(intervalIds)
}

end() {
Expand Down
8 changes: 6 additions & 2 deletions packages/common/src/types/file.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Result } from './types'

export type FileData = {
/** 文件名;该文件保存到空间时的名称 */
/** 文件名;如果未指定,则为 filename */
key?: string
/** 本地文件名;如果未指定,默认为随机字符串 */
filename?: string
/** 文件的媒体类型;该文件保存到空间时的媒体类型 */
mimeType?: string
Expand All @@ -20,7 +22,9 @@ export interface UploadFile {
size(): Promise<Result<number>>
/** 文件路径;返回文件的完整路径 */
path(): Promise<Result<string>>
/** 文件名 */
/** 目标文件名,最终存储的文件名 */
key(): Promise<Result<string | null>>
/** 原始文件名,如果不存在则会是随机字符串 */
name(): Promise<Result<string | null>>
/** 媒体类型 */
mimeType(): Promise<Result<string | null>>
Expand Down
23 changes: 16 additions & 7 deletions packages/common/src/upload/direct/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,22 @@ class DirectUploadTask implements Task {
}

async process(notify: () => void): Promise<Result> {
const fileNameResult = await this.file.name()
if (!isSuccessResult(fileNameResult)) {
if (isErrorResult(fileNameResult)) {
this.context.error = fileNameResult.error
const filenameResult = await this.file.name()
if (!isSuccessResult(filenameResult)) {
if (isErrorResult(filenameResult)) {
this.context.error = filenameResult.error
}

return fileNameResult
return filenameResult
}

const fileKeyResult = await this.file.key()
if (!isSuccessResult(fileKeyResult)) {
if (isErrorResult(fileKeyResult)) {
this.context.error = fileKeyResult.error
}

return fileKeyResult
}

const fileMetaResult = await this.file.metadata()
Expand All @@ -71,9 +80,9 @@ class DirectUploadTask implements Task {
customVars: this.vars,
token: this.context!.token!,
metadata: fileMetaResult.result,
key: fileNameResult.result || undefined,
uploadHostUrl: this.context!.host!.getUrl(),
fileName: fileNameResult.result || generateRandomString(), // 接口要求必传且建议没有有效文件名时传随机字符串
fileName: filenameResult.result || generateRandomString(), // 接口要求必传且建议没有有效文件名时传随机字符串
key: fileKeyResult.result || filenameResult.result || undefined,
onProgress: progress => {
this.context!.progress.details.directUpload.percent = progress.percent
this.context!.progress.details.directUpload.size = fileSizeResult.result
Expand Down
7 changes: 5 additions & 2 deletions packages/common/src/upload/multipartv1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ class MkfileTask implements Task {
async process(notify: () => void): Promise<Result> {
this.updateProgress(0, notify)

const fileKeyResult = await this.file.key()
if (!isSuccessResult(fileKeyResult)) return fileKeyResult

const filenameResult = await this.file.name()
if (!isSuccessResult(filenameResult)) return filenameResult

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

Expand Down
18 changes: 14 additions & 4 deletions packages/common/src/upload/multipartv2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class InitPartUploadTask implements Task {
const filenameResult = await this.file.name()
if (!isSuccessResult(filenameResult)) return filenameResult

const fileKeyResult = await this.file.key()
if (!isSuccessResult(fileKeyResult)) return fileKeyResult

// 首先检查 context 上的 upload id 有没有过期
if (this.context.uploadPartId) {
const nowTime = Date.now() / 1e3
Expand All @@ -75,9 +78,9 @@ class InitPartUploadTask implements Task {
abort: this.abort,
token: this.context!.token!,
bucket: this.context.token!.bucket,
key: filenameResult.result || undefined,
uploadHostUrl: this.context!.host!.getUrl(),
uploadId: this.context.uploadPartId.uploadId,
key: fileKeyResult.result || filenameResult.result || undefined,
onProgress: progress => { this.updateProgress(progress.percent, notify) }
})

Expand Down Expand Up @@ -105,8 +108,8 @@ class InitPartUploadTask implements Task {
abort: this.abort,
token: this.context!.token!,
bucket: this.context!.token!.bucket,
key: filenameResult.result || undefined,
uploadHostUrl: this.context!.host!.getUrl(),
key: fileKeyResult.result || filenameResult.result || undefined,
onProgress: progress => { this.updateProgress(progress.percent, notify) }
})

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

const fileKeyResult = await this.file.key()
if (!isSuccessResult(fileKeyResult)) return fileKeyResult

const fileSizeResult = await this.file.size()
if (!isSuccessResult(fileSizeResult)) return fileSizeResult

Expand All @@ -178,9 +184,9 @@ class UploadPartTask implements Task {
partIndex: this.index,
token: this.context!.token!,
bucket: this.context!.token!.bucket,
key: filenameResult.result || undefined,
uploadHostUrl: this.context!.host!.getUrl(),
uploadId: this.context!.uploadPartId!.uploadId!,
key: fileKeyResult.result || filenameResult.result || undefined,
onProgress: progress => { this.updateProgress(false, fileSizeResult.result, progress.percent, notify) }
})

Expand Down Expand Up @@ -233,6 +239,10 @@ class CompletePartUploadTask implements Task {

async process(notify: () => void): Promise<Result> {
this.updateProgress(0, notify)

const fileKeyResult = await this.file.key()
if (!isSuccessResult(fileKeyResult)) return fileKeyResult

const filenameResult = await this.file.name()
if (!isSuccessResult(filenameResult)) return filenameResult

Expand All @@ -253,10 +263,10 @@ class CompletePartUploadTask implements Task {
customVars: this.vars,
token: this.context!.token!,
metadata: metadataResult.result,
key: filenameResult.result || undefined,
uploadHostUrl: this.context!.host!.getUrl(),
mimeType: mimeTypeResult.result || undefined,
uploadId: this.context!.uploadPartId!.uploadId!,
key: fileKeyResult.result || filenameResult.result || undefined,
fileName: filenameResult.result || generateRandomString(), // 和直传行为保持一致
onProgress: progress => { this.updateProgress(progress.percent, notify) }
})
Expand Down
2 changes: 1 addition & 1 deletion packages/harmony/entry/oh-package-lock.json5

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

2 changes: 1 addition & 1 deletion packages/harmony/entry/src/main/ets/pages/Index.ets
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ struct Index {

const file = this.getUploadFile()

file.key = "test-test"
file.filename = "test-test"
file.mimeType = "232312323/2312"
file.metadata = {
'2321': "2323232"
}


const task = createMultipartUploadV2Task(context, file, {
logLevel: 'INFO',
vars: { test: '222222' },
Expand Down
4 changes: 2 additions & 2 deletions packages/harmony/library/BuildProfile.ets
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Use these variables when you tailor your ArkTS code. They must be of the const type.
*/
export const HAR_VERSION = '1.0.0';
export const HAR_VERSION = '1.0.1';
export const BUILD_MODE_NAME = 'debug';
export const DEBUG = true;
export const TARGET_NAME = 'default';
Expand All @@ -14,4 +14,4 @@ export default class BuildProfile {
static readonly BUILD_MODE_NAME = BUILD_MODE_NAME;
static readonly DEBUG = DEBUG;
static readonly TARGET_NAME = TARGET_NAME;
}
}
13 changes: 13 additions & 0 deletions packages/harmony/library/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Change logs

## v1.0.1 (2024-07-09)

### 改动

- 修复 onProgress 在任务完成之后依旧触发的问题
- UploadFile 添加 key 属性用于指定上传存储目标 key

## v1.0.0 (2024-05-08)

### 改动

- rc 版本升级为 1.0.0

## v1.0.0-rc.5 (2024-03-14)

### 改动
Expand Down
2 changes: 1 addition & 1 deletion packages/harmony/library/oh-package.json5
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"modelVersion": "5.0.0",
"name": "@qiniu/upload",
"version": "1.0.0-rc.6",
"version": "1.0.1",
"keywords": ["qiniu", "upload", "oss"],
"description": "Qiniu Cloud object storage upload sdk",
"repository": "https://github.com/qiniu/js-sdk.git",
Expand Down
Loading

0 comments on commit 70d1f2d

Please sign in to comment.