Skip to content

Commit 89608f2

Browse files
committed
优化缓存调用逻辑
1 parent 71b1482 commit 89608f2

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

src/upload/resume.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,29 @@ function isPositiveInteger(n: number) {
4141
}
4242

4343
export default class Resume extends Base {
44+
/**
45+
* @description 文件的分片 chunks
46+
*/
4447
private chunks: Blob[]
45-
/** 当前上传过程中已完成的上传信息 */
48+
49+
/**
50+
* @description 来自缓存的上传信息
51+
*/
52+
private cachedUploadedList: UploadedChunkStorage[]
53+
54+
/**
55+
* @description 当前上传过程中已完成的上传信息
56+
*/
4657
private uploadedList: UploadedChunkStorage[]
47-
/** 当前上传片进度信息 */
58+
59+
/**
60+
* @description 当前上传片进度信息
61+
*/
4862
private loaded: ChunkLoaded
63+
64+
/**
65+
* @description 当前上传任务的 id
66+
*/
4967
private uploadId: string
5068

5169
/**
@@ -98,12 +116,13 @@ export default class Resume extends Base {
98116

99117
private async uploadChunk(chunkInfo: ChunkInfo) {
100118
const { index, chunk } = chunkInfo
101-
const info = this.uploadedList[index]
119+
const info = this.cachedUploadedList[index]
102120
this.logger.info(`upload part ${index}.`, info)
103121

104122
const shouldCheckMD5 = this.config.checkByMD5
105123
const reuseSaved = () => {
106124
info.fromCache = true
125+
this.uploadedList[index] = info
107126
this.updateChunkProgress(chunk.size, index)
108127
}
109128

@@ -122,9 +141,7 @@ export default class Resume extends Base {
122141
}
123142

124143
// 有缓存但是没有使用则调整标记
125-
if (info) {
126-
info.fromCache = false
127-
}
144+
if (info) { info.fromCache = false }
128145

129146
const onProgress = (data: Progress) => {
130147
this.updateChunkProgress(data.loaded, index)
@@ -193,31 +210,32 @@ export default class Resume extends Base {
193210
}
194211

195212
private async initBeforeUploadChunks() {
196-
const localInfo = utils.getLocalFileInfo(this.getLocalKey(), this.logger)
213+
this.uploadedList = []
214+
const cachedInfo = utils.getLocalFileInfo(this.getLocalKey(), this.logger)
197215

198216
// 分片必须和当时使用的 uploadId 配套,所以断点续传需要把本地存储的 uploadId 拿出来
199-
// 假如没有 localInfo 本地信息并重新获取 uploadId
200-
if (!localInfo) {
201-
this.logger.info('resume upload parts from api.')
217+
// 假如没有 cachedInfo 本地信息并重新获取 uploadId
218+
if (!cachedInfo) {
219+
this.logger.info('init upload parts from api.')
202220
const res = await initUploadParts(
203221
this.token,
204222
this.bucketName,
205223
this.key,
206224
this.uploadHost!.getUrl()
207225
)
208-
this.logger.info(`resume upload parts of id: ${res.data.uploadId}.`)
226+
this.logger.info(`init upload parts of id: ${res.data.uploadId}.`)
209227
this.uploadId = res.data.uploadId
210-
this.uploadedList = []
228+
this.cachedUploadedList = []
211229
} else {
212230
const infoMessage = [
213231
'resume upload parts from local cache',
214-
`total ${localInfo.data.length} part`,
215-
`id is ${localInfo.id}.`
232+
`total ${cachedInfo.data.length} part`,
233+
`id is ${cachedInfo.id}.`
216234
]
217235

218236
this.logger.info(infoMessage.join(', '))
219-
this.uploadedList = localInfo.data
220-
this.uploadId = localInfo.id
237+
this.cachedUploadedList = cachedInfo.data
238+
this.uploadId = cachedInfo.id
221239
}
222240

223241
this.chunks = utils.getChunks(this.file, this.config.chunkSize)

0 commit comments

Comments
 (0)