@@ -6,8 +6,6 @@ import Base, { Progress, UploadInfo, Extra } from './base'
6
6
7
7
export interface UploadedChunkStorage extends UploadChunkData {
8
8
size : number
9
-
10
- fromCache ?: boolean
11
9
}
12
10
13
11
export interface ChunkLoaded {
@@ -41,11 +39,34 @@ function isPositiveInteger(n: number) {
41
39
}
42
40
43
41
export default class Resume extends Base {
42
+ /**
43
+ * @description 文件的分片 chunks
44
+ */
44
45
private chunks : Blob [ ]
45
- /** 当前上传过程中已完成的上传信息 */
46
+
47
+ /**
48
+ * @description 使用缓存的 chunks
49
+ */
50
+ private usedCacheList : boolean [ ]
51
+
52
+ /**
53
+ * @description 来自缓存的上传信息
54
+ */
55
+ private cachedUploadedList : UploadedChunkStorage [ ]
56
+
57
+ /**
58
+ * @description 当前上传过程中已完成的上传信息
59
+ */
46
60
private uploadedList : UploadedChunkStorage [ ]
47
- /** 当前上传片进度信息 */
61
+
62
+ /**
63
+ * @description 当前上传片进度信息
64
+ */
48
65
private loaded : ChunkLoaded
66
+
67
+ /**
68
+ * @description 当前上传任务的 id
69
+ */
49
70
private uploadId : string
50
71
51
72
/**
@@ -98,33 +119,33 @@ export default class Resume extends Base {
98
119
99
120
private async uploadChunk ( chunkInfo : ChunkInfo ) {
100
121
const { index, chunk } = chunkInfo
101
- const info = this . uploadedList [ index ]
102
- this . logger . info ( `upload part ${ index } . ` , info )
122
+ const cachedInfo = this . cachedUploadedList [ index ]
123
+ this . logger . info ( `upload part ${ index } , cache: ` , cachedInfo )
103
124
104
125
const shouldCheckMD5 = this . config . checkByMD5
105
126
const reuseSaved = ( ) => {
106
- info . fromCache = true
127
+ this . usedCacheList [ index ] = true
107
128
this . updateChunkProgress ( chunk . size , index )
129
+ this . uploadedList [ index ] = cachedInfo
130
+ this . updateLocalCache ( )
108
131
}
109
132
110
133
// FIXME: 至少判断一下 size
111
- if ( info && ! shouldCheckMD5 ) {
134
+ if ( cachedInfo && ! shouldCheckMD5 ) {
112
135
reuseSaved ( )
113
136
return
114
137
}
115
138
116
139
const md5 = await utils . computeMd5 ( chunk )
117
140
this . logger . info ( 'computed part md5.' , md5 )
118
141
119
- if ( info && md5 === info . md5 ) {
142
+ if ( cachedInfo && md5 === cachedInfo . md5 ) {
120
143
reuseSaved ( )
121
144
return
122
145
}
123
146
124
- // 有缓存但是没有使用则调整标记
125
- if ( info ) {
126
- info . fromCache = false
127
- }
147
+ // 没有使用缓存设置标记为 false
148
+ this . usedCacheList [ index ] = false
128
149
129
150
const onProgress = ( data : Progress ) => {
130
151
this . updateChunkProgress ( data . loaded , index )
@@ -158,16 +179,14 @@ export default class Resume extends Base {
158
179
size : chunk . size
159
180
}
160
181
161
- utils . setLocalFileInfo ( this . getLocalKey ( ) , {
162
- id : this . uploadId ,
163
- data : this . uploadedList
164
- } , this . logger )
182
+ this . updateLocalCache ( )
165
183
}
166
184
167
185
private async mkFileReq ( ) {
168
186
const data : UploadChunkBody = {
169
187
parts : this . uploadedList . map ( ( value , index ) => ( {
170
188
etag : value . etag ,
189
+ // 接口要求 index 需要从 1 开始,所以需要整体 + 1
171
190
partNumber : index + 1
172
191
} ) ) ,
173
192
fname : this . putExtra . fname ,
@@ -193,31 +212,33 @@ export default class Resume extends Base {
193
212
}
194
213
195
214
private async initBeforeUploadChunks ( ) {
196
- const localInfo = utils . getLocalFileInfo ( this . getLocalKey ( ) , this . logger )
215
+ this . uploadedList = [ ]
216
+ this . usedCacheList = [ ]
217
+ const cachedInfo = utils . getLocalFileInfo ( this . getLocalKey ( ) , this . logger )
197
218
198
219
// 分片必须和当时使用的 uploadId 配套,所以断点续传需要把本地存储的 uploadId 拿出来
199
- // 假如没有 localInfo 本地信息并重新获取 uploadId
200
- if ( ! localInfo ) {
201
- this . logger . info ( 'resume upload parts from api.' )
220
+ // 假如没有 cachedInfo 本地信息并重新获取 uploadId
221
+ if ( ! cachedInfo ) {
222
+ this . logger . info ( 'init upload parts from api.' )
202
223
const res = await initUploadParts (
203
224
this . token ,
204
225
this . bucketName ,
205
226
this . key ,
206
227
this . uploadHost ! . getUrl ( )
207
228
)
208
- this . logger . info ( `resume upload parts of id: ${ res . data . uploadId } .` )
229
+ this . logger . info ( `initd upload parts of id: ${ res . data . uploadId } .` )
209
230
this . uploadId = res . data . uploadId
210
- this . uploadedList = [ ]
231
+ this . cachedUploadedList = [ ]
211
232
} else {
212
233
const infoMessage = [
213
- 'resume upload parts from local cache' ,
214
- `total ${ localInfo . data . length } part` ,
215
- `id is ${ localInfo . id } .`
234
+ 'resume upload parts from local cache, ' ,
235
+ `total ${ cachedInfo . data . length } part, ` ,
236
+ `id is ${ cachedInfo . id } .`
216
237
]
217
238
218
- this . logger . info ( infoMessage . join ( ', ' ) )
219
- this . uploadedList = localInfo . data
220
- this . uploadId = localInfo . id
239
+ this . logger . info ( infoMessage . join ( ' ' ) )
240
+ this . cachedUploadedList = cachedInfo . data
241
+ this . uploadId = cachedInfo . id
221
242
}
222
243
223
244
this . chunks = utils . getChunks ( this . file , this . config . chunkSize )
@@ -239,6 +260,13 @@ export default class Resume extends Base {
239
260
return utils . createLocalKey ( this . file . name , this . key , this . file . size )
240
261
}
241
262
263
+ private updateLocalCache ( ) {
264
+ utils . setLocalFileInfo ( this . getLocalKey ( ) , {
265
+ id : this . uploadId ,
266
+ data : this . uploadedList
267
+ } , this . logger )
268
+ }
269
+
242
270
private updateChunkProgress ( loaded : number , index : number ) {
243
271
this . loaded . chunks [ index ] = loaded
244
272
this . notifyResumeProgress ( )
@@ -257,8 +285,7 @@ export default class Resume extends Base {
257
285
this . file . size + 1 // 防止在 complete 未调用的时候进度显示 100%
258
286
) ,
259
287
chunks : this . chunks . map ( ( chunk , index ) => {
260
- const info = this . uploadedList [ index ]
261
- const fromCache = info && info . fromCache
288
+ const fromCache = this . usedCacheList [ index ]
262
289
return this . getProgressInfoItem ( this . loaded . chunks [ index ] , chunk . size , fromCache )
263
290
} ) ,
264
291
uploadInfo : {
0 commit comments