Skip to content

Commit 2ac7357

Browse files
committed
调整 isCache 实现
1 parent 97be9c5 commit 2ac7357

File tree

4 files changed

+23
-20
lines changed

4 files changed

+23
-20
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,13 @@ qiniu.compressImage(file, options).then(data => {
167167
* uploadInfo: `object`,只有分片上传时才返回该字段
168168
* uploadInfo.id: 上传任务的唯一标识。
169169
* uploadInfo.url: 上传地址。
170-
* total: 包含`loaded``total``percent`三个属性:
170+
* chunks: `Array<ProgressCompose>` 每个 `chunk` 的上传信息,只有分片上传有此字段
171+
* ProgressCompose 的信息如下
172+
* size: `number` chunk 的尺寸
173+
* loaded: `number` 已经发送完毕的尺寸
174+
* percent: `number` 进度比例,范围在 0 - 1
175+
* isCache?: `boolean` 是否使用的缓存
176+
* total: 包含 `loaded``total``percent` 三个属性:
171177
* total.loaded: `number`,已上传大小,单位为字节。
172178
* total.total: `number`,本次上传的总量控制信息,单位为字节,注意这里的 total 跟文件大小并不一致。
173179
* total.percent: `number`,当前上传进度,范围:0100

src/upload/base.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,15 @@ export interface UploadHandlers {
8383
}
8484

8585
export interface Progress {
86-
loaded: number
8786
total: number
87+
loaded: number
8888
}
8989

9090
export interface ProgressCompose {
91-
loaded: number
9291
size: number
92+
loaded: number
9393
percent: number
94+
isCache?: boolean
9495
}
9596

9697
export type XHRHandler = (xhr: XMLHttpRequest) => void
@@ -325,10 +326,11 @@ export default abstract class Base {
325326
})
326327
}
327328

328-
public getProgressInfoItem(loaded: number, size: number) {
329+
public getProgressInfoItem(loaded: number, size: number, isCache?: boolean): ProgressCompose {
329330
return {
330-
loaded,
331331
size,
332+
loaded,
333+
isCache,
332334
percent: loaded / size * 100
333335
}
334336
}

src/upload/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import Resume from './resume'
22
import Direct from './direct'
33
import Logger from '../logger'
4-
import { QiniuError, QiniuNetworkError, QiniuRequestError } from '../errors'
54
import { UploadCompleteData } from '../api'
65
import { Observable, IObserver, MB, normalizeUploadConfig } from '../utils'
6+
import { QiniuError, QiniuNetworkError, QiniuRequestError } from '../errors'
77
import { Extra, UploadOptions, UploadHandlers, UploadProgress, Config } from './base'
88
import { HostPool } from './hosts'
99

@@ -43,7 +43,7 @@ export default function upload(
4343
key: string | null | undefined,
4444
token: string,
4545
putExtra?: Partial<Extra>,
46-
config?: Config
46+
config?: Partial<Config>
4747
): Observable<UploadProgress, QiniuError | QiniuRequestError | QiniuNetworkError, UploadCompleteData> {
4848

4949
// 为每个任务创建单独的 Logger

src/upload/resume.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import Base, { Progress, UploadInfo, Extra } from './base'
66

77
export interface UploadedChunkStorage extends UploadChunkData {
88
size: number
9-
10-
// 标记该 chunk 是否来自缓存
11-
isLocalCache?: boolean
129
}
1310

1411
export interface ChunkLoaded {
@@ -43,6 +40,8 @@ function isPositiveInteger(n: number) {
4340

4441
export default class Resume extends Base {
4542
private chunks: Blob[]
43+
/** 使用的缓存分片索引 */
44+
private usedCaches: number[] = []
4645
/** 当前上传过程中已完成的上传信息 */
4746
private uploadedList: UploadedChunkStorage[]
4847
/** 当前上传片进度信息 */
@@ -104,10 +103,11 @@ export default class Resume extends Base {
104103

105104
const shouldCheckMD5 = this.config.checkByMD5
106105
const reuseSaved = () => {
106+
this.usedCaches.push(index)
107107
this.updateChunkProgress(chunk.size, index)
108108
}
109109

110-
// FIXME: 至少比对一下 size
110+
// FIXME: 至少判断一下 size
111111
if (info && !shouldCheckMD5) {
112112
reuseSaved()
113113
return
@@ -212,12 +212,6 @@ export default class Resume extends Base {
212212

213213
this.logger.info(infoMessage.join(', '))
214214
this.uploadedList = localInfo.data
215-
if (Array.isArray(this.uploadedList)) {
216-
this.uploadedList.forEach(chunk => {
217-
chunk.isLocalCache = true
218-
})
219-
}
220-
221215
this.uploadId = localInfo.id
222216
}
223217

@@ -256,9 +250,10 @@ export default class Resume extends Base {
256250
utils.sum(this.loaded.chunks) + this.loaded.mkFileProgress,
257251
this.file.size + 1 // 防止在 complete 未调用的时候进度显示 100%
258252
),
259-
chunks: this.chunks.map((chunk, index) => (
260-
this.getProgressInfoItem(this.loaded.chunks[index], chunk.size)
261-
)),
253+
chunks: this.chunks.map((chunk, index) => {
254+
const isCache = this.usedCaches.includes(index)
255+
return this.getProgressInfoItem(this.loaded.chunks[index], chunk.size, isCache)
256+
}),
262257
uploadInfo: {
263258
id: this.uploadId,
264259
url: this.uploadHost!.getUrl()

0 commit comments

Comments
 (0)