Skip to content

Commit ea4c117

Browse files
committed
调整实现细节
1 parent 89608f2 commit ea4c117

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/upload/base.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ export default abstract class Base {
330330
return {
331331
size,
332332
loaded,
333-
fromCache,
334-
percent: loaded / size * 100
333+
percent: loaded / size * 100,
334+
...(fromCache == null ? {} : { fromCache })
335335
}
336336
}
337337
}

src/upload/resume.ts

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

77
export interface UploadedChunkStorage extends UploadChunkData {
88
size: number
9-
10-
fromCache?: boolean
119
}
1210

1311
export interface ChunkLoaded {
@@ -46,6 +44,11 @@ export default class Resume extends Base {
4644
*/
4745
private chunks: Blob[]
4846

47+
/**
48+
* @description 使用的缓存的 chunks
49+
*/
50+
private usedCacheList: boolean[]
51+
4952
/**
5053
* @description 来自缓存的上传信息
5154
*/
@@ -121,8 +124,10 @@ export default class Resume extends Base {
121124

122125
const shouldCheckMD5 = this.config.checkByMD5
123126
const reuseSaved = () => {
124-
info.fromCache = true
125-
this.uploadedList[index] = info
127+
this.uploadedList[index] = { ...info }
128+
this.usedCacheList[index] = true
129+
this.updateLocalCache()
130+
126131
this.updateChunkProgress(chunk.size, index)
127132
}
128133

@@ -140,9 +145,6 @@ export default class Resume extends Base {
140145
return
141146
}
142147

143-
// 有缓存但是没有使用则调整标记
144-
if (info) { info.fromCache = false }
145-
146148
const onProgress = (data: Progress) => {
147149
this.updateChunkProgress(data.loaded, index)
148150
}
@@ -175,10 +177,7 @@ export default class Resume extends Base {
175177
size: chunk.size
176178
}
177179

178-
utils.setLocalFileInfo(this.getLocalKey(), {
179-
id: this.uploadId,
180-
data: this.uploadedList
181-
}, this.logger)
180+
this.updateLocalCache()
182181
}
183182

184183
private async mkFileReq() {
@@ -211,6 +210,7 @@ export default class Resume extends Base {
211210

212211
private async initBeforeUploadChunks() {
213212
this.uploadedList = []
213+
this.usedCacheList = []
214214
const cachedInfo = utils.getLocalFileInfo(this.getLocalKey(), this.logger)
215215

216216
// 分片必须和当时使用的 uploadId 配套,所以断点续传需要把本地存储的 uploadId 拿出来
@@ -257,6 +257,13 @@ export default class Resume extends Base {
257257
return utils.createLocalKey(this.file.name, this.key, this.file.size)
258258
}
259259

260+
private updateLocalCache() {
261+
utils.setLocalFileInfo(this.getLocalKey(), {
262+
id: this.uploadId,
263+
data: this.uploadedList
264+
}, this.logger)
265+
}
266+
260267
private updateChunkProgress(loaded: number, index: number) {
261268
this.loaded.chunks[index] = loaded
262269
this.notifyResumeProgress()
@@ -275,8 +282,7 @@ export default class Resume extends Base {
275282
this.file.size + 1 // 防止在 complete 未调用的时候进度显示 100%
276283
),
277284
chunks: this.chunks.map((chunk, index) => {
278-
const info = this.uploadedList[index]
279-
const fromCache = info && info.fromCache
285+
const fromCache = this.usedCacheList[index]
280286
return this.getProgressInfoItem(this.loaded.chunks[index], chunk.size, fromCache)
281287
}),
282288
uploadInfo: {

0 commit comments

Comments
 (0)