@@ -41,11 +41,29 @@ function isPositiveInteger(n: number) {
41
41
}
42
42
43
43
export default class Resume extends Base {
44
+ /**
45
+ * @description 文件的分片 chunks
46
+ */
44
47
private chunks : Blob [ ]
45
- /** 当前上传过程中已完成的上传信息 */
48
+
49
+ /**
50
+ * @description 来自缓存的上传信息
51
+ */
52
+ private cachedUploadedList : UploadedChunkStorage [ ]
53
+
54
+ /**
55
+ * @description 当前上传过程中已完成的上传信息
56
+ */
46
57
private uploadedList : UploadedChunkStorage [ ]
47
- /** 当前上传片进度信息 */
58
+
59
+ /**
60
+ * @description 当前上传片进度信息
61
+ */
48
62
private loaded : ChunkLoaded
63
+
64
+ /**
65
+ * @description 当前上传任务的 id
66
+ */
49
67
private uploadId : string
50
68
51
69
/**
@@ -98,12 +116,13 @@ export default class Resume extends Base {
98
116
99
117
private async uploadChunk ( chunkInfo : ChunkInfo ) {
100
118
const { index, chunk } = chunkInfo
101
- const info = this . uploadedList [ index ]
119
+ const info = this . cachedUploadedList [ index ]
102
120
this . logger . info ( `upload part ${ index } .` , info )
103
121
104
122
const shouldCheckMD5 = this . config . checkByMD5
105
123
const reuseSaved = ( ) => {
106
124
info . fromCache = true
125
+ this . uploadedList [ index ] = info
107
126
this . updateChunkProgress ( chunk . size , index )
108
127
}
109
128
@@ -122,9 +141,7 @@ export default class Resume extends Base {
122
141
}
123
142
124
143
// 有缓存但是没有使用则调整标记
125
- if ( info ) {
126
- info . fromCache = false
127
- }
144
+ if ( info ) { info . fromCache = false }
128
145
129
146
const onProgress = ( data : Progress ) => {
130
147
this . updateChunkProgress ( data . loaded , index )
@@ -193,31 +210,32 @@ export default class Resume extends Base {
193
210
}
194
211
195
212
private async initBeforeUploadChunks ( ) {
196
- const localInfo = utils . getLocalFileInfo ( this . getLocalKey ( ) , this . logger )
213
+ this . uploadedList = [ ]
214
+ const cachedInfo = utils . getLocalFileInfo ( this . getLocalKey ( ) , this . logger )
197
215
198
216
// 分片必须和当时使用的 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.' )
202
220
const res = await initUploadParts (
203
221
this . token ,
204
222
this . bucketName ,
205
223
this . key ,
206
224
this . uploadHost ! . getUrl ( )
207
225
)
208
- this . logger . info ( `resume upload parts of id: ${ res . data . uploadId } .` )
226
+ this . logger . info ( `init upload parts of id: ${ res . data . uploadId } .` )
209
227
this . uploadId = res . data . uploadId
210
- this . uploadedList = [ ]
228
+ this . cachedUploadedList = [ ]
211
229
} else {
212
230
const infoMessage = [
213
231
'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 } .`
216
234
]
217
235
218
236
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
221
239
}
222
240
223
241
this . chunks = utils . getChunks ( this . file , this . config . chunkSize )
0 commit comments