Skip to content

Commit

Permalink
新增 key 参数,更明确指定上传文件名
Browse files Browse the repository at this point in the history
  • Loading branch information
yinxulai committed Jul 1, 2024
1 parent 610aba0 commit 92b6d3a
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 18 deletions.
5 changes: 5 additions & 0 deletions packages/browser/src/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export class UploadFile implements BaseUploadFile {
return { result: 'browser platform files' }
}

async key(): Promise<Result<string | null>> {
const realFilename = this.fileData.type === 'file' && this.fileData.key
return { result: this.fileData?.key || realFilename || 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
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
11 changes: 10 additions & 1 deletion packages/common/src/upload/direct/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ class DirectUploadTask implements Task {
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()
if (!isSuccessResult(fileMetaResult)) {
if (isErrorResult(fileMetaResult)) {
Expand All @@ -71,7 +80,7 @@ class DirectUploadTask implements Task {
customVars: this.vars,
token: this.context!.token!,
metadata: fileMetaResult.result,
key: fileNameResult.result || undefined,
key: fileKeyResult.result || undefined,
uploadHostUrl: this.context!.host!.getUrl(),
fileName: fileNameResult.result || generateRandomString(), // 接口要求必传且建议没有有效文件名时传随机字符串
onProgress: progress => {
Expand Down
5 changes: 4 additions & 1 deletion 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,7 +152,7 @@ class MkfileTask implements Task {
userVars: this.vars,
token: this.context.token!,
fileSize: fileSizeResult.result,
key: filenameResult.result || undefined,
key: fileKeyResult.result || undefined,
fname: filenameResult.result || generateRandomString(),
lastCtxOfBlock: this.context.uploadBlocks.map(i => i.ctx),
uploadHostUrl: this.context!.host!.getUrl(),
Expand Down
20 changes: 12 additions & 8 deletions packages/common/src/upload/multipartv2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class InitPartUploadTask implements Task {

this.updateProgress(0, notify)

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) {
Expand All @@ -75,7 +75,7 @@ class InitPartUploadTask implements Task {
abort: this.abort,
token: this.context!.token!,
bucket: this.context.token!.bucket,
key: filenameResult.result || undefined,
key: fileKeyResult.result || undefined,
uploadHostUrl: this.context!.host!.getUrl(),
uploadId: this.context.uploadPartId.uploadId,
onProgress: progress => { this.updateProgress(progress.percent, notify) }
Expand Down Expand Up @@ -105,7 +105,7 @@ class InitPartUploadTask implements Task {
abort: this.abort,
token: this.context!.token!,
bucket: this.context!.token!.bucket,
key: filenameResult.result || undefined,
key: fileKeyResult.result || undefined,
uploadHostUrl: this.context!.host!.getUrl(),
onProgress: progress => { this.updateProgress(progress.percent, notify) }
})
Expand Down Expand Up @@ -165,8 +165,8 @@ 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,7 +178,7 @@ class UploadPartTask implements Task {
partIndex: this.index,
token: this.context!.token!,
bucket: this.context!.token!.bucket,
key: filenameResult.result || undefined,
key: fileKeyResult.result || undefined,
uploadHostUrl: this.context!.host!.getUrl(),
uploadId: this.context!.uploadPartId!.uploadId!,
onProgress: progress => { this.updateProgress(false, fileSizeResult.result, progress.percent, notify) }
Expand Down Expand Up @@ -233,6 +233,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,7 +257,7 @@ class CompletePartUploadTask implements Task {
customVars: this.vars,
token: this.context!.token!,
metadata: metadataResult.result,
key: filenameResult.result || undefined,
key: fileKeyResult.result || undefined,
uploadHostUrl: this.context!.host!.getUrl(),
mimeType: mimeTypeResult.result || undefined,
uploadId: this.context!.uploadPartId!.uploadId!,
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.

1 change: 0 additions & 1 deletion packages/harmony/entry/src/main/ets/pages/Index.ets
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ struct Index {
'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.0-rc.6';
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;
}
}
10 changes: 9 additions & 1 deletion packages/harmony/library/src/main/ets/components/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ export class UploadFile implements common.FileData {
type: string
data: string | ArrayBuffer

/** 文件名;如果未指定,则为 filename */
key?: string

/** 文件名;该文件保存到空间时的名称 */
/** 本地文件名;如果未指定,默认为随机字符串 */
filename?: string

/** 文件的媒体类型;该文件保存到空间时的媒体类型 */
Expand Down Expand Up @@ -50,9 +51,11 @@ export class UploadFile implements common.FileData {

return new UploadFile('path', path)
}

static fromString(data: string): UploadFile {
return new UploadFile('string', data)
}

static fromArrayBuffer(data: ArrayBuffer): UploadFile {
return new UploadFile('array-buffer', data)
}
Expand Down Expand Up @@ -124,6 +127,11 @@ export class InternalUploadFile implements common.UploadFile {
error: new common.UploadError('FileSystemError', 'Invalid file data')
}
}
if (typeof this.data.data !== 'string') {
return {
error: new common.UploadError('FileSystemError', 'Invalid file data')
}
}

// 标准 file uri
if (this.data.data.startsWith('file://')) {
Expand Down
2 changes: 1 addition & 1 deletion packages/harmony/library/src/main/ets/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function onCancel(task: UploadTask, listener: () => Promise<Result>) {
}

/**
* @deprecated 受限于当前版本的系统接口暂时无法获取上传之后的结果,优先考虑使用分片
* 直传,大文件请优先考虑使用分片
*/
export function createDirectUploadTask(context: ohCommon.Context, file: UploadFile, config: UploadConfig) {
const innerFile = new InternalUploadFile(context, file)
Expand Down
4 changes: 4 additions & 0 deletions packages/wechat-miniprogram/src/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ export class UploadFile implements BaseUploadFile {
return { result: this.filePath! }
}

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

async name(): Promise<Result<string | null>> {
return { result: this.fileData?.filename || null }
}
Expand Down

0 comments on commit 92b6d3a

Please sign in to comment.