@@ -46,31 +46,46 @@ export default class Resume extends Base {
46
46
private uploadId : string
47
47
48
48
protected async run ( ) {
49
+ this . logger . info ( 'start run Resume.' )
49
50
if ( ! this . config . chunkSize || ! isPositiveInteger ( this . config . chunkSize ) ) {
50
- throw new Error ( 'chunkSize must be a positive integer' )
51
+ const errorMessage = 'chunkSize must be a positive integer.'
52
+ this . logger . error ( errorMessage )
53
+ throw new Error ( errorMessage )
51
54
}
52
55
53
56
if ( this . config . chunkSize > 1024 ) {
54
- throw new Error ( 'chunkSize maximum value is 1024' )
57
+ const errorMessage = 'chunkSize maximum value is 1024.'
58
+ this . logger . error ( errorMessage )
59
+ throw new Error ( errorMessage )
55
60
}
56
61
57
- await this . initBeforeUploadChunks ( )
62
+ try {
63
+ await this . initBeforeUploadChunks ( )
64
+ } catch ( error ) {
65
+ const errorMessage = 'initBeforeUploadChunks failed.'
66
+ this . logger . warn ( errorMessage , error )
67
+ }
58
68
59
69
const pool = new Pool (
60
70
( chunkInfo : ChunkInfo ) => this . uploadChunk ( chunkInfo ) ,
61
71
this . config . concurrentRequestLimit
62
72
)
73
+
74
+ const localKey = this . getLocalKey ( )
63
75
const uploadChunks = this . chunks . map ( ( chunk , index ) => pool . enqueue ( { chunk, index } ) )
64
76
65
77
const result = Promise . all ( uploadChunks ) . then ( ( ) => this . mkFileReq ( ) )
66
78
result . then (
67
79
( ) => {
68
- utils . removeLocalFileInfo ( this . getLocalKey ( ) )
80
+ try { utils . removeLocalFileInfo ( localKey ) }
81
+ catch ( error ) { this . logger . error ( error ) }
69
82
} ,
70
83
err => {
84
+ this . logger . error ( 'uploadChunks failed.' , err )
71
85
// uploadId 无效,上传参数有误(多由于本地存储信息的 uploadId 失效
72
86
if ( err . code === 612 || err . code === 400 ) {
73
- utils . removeLocalFileInfo ( this . getLocalKey ( ) )
87
+ try { utils . removeLocalFileInfo ( localKey ) }
88
+ catch ( error ) { this . logger . error ( error ) }
74
89
}
75
90
}
76
91
)
@@ -80,6 +95,7 @@ export default class Resume extends Base {
80
95
private async uploadChunk ( chunkInfo : ChunkInfo ) {
81
96
const { index, chunk } = chunkInfo
82
97
const info = this . uploadedList [ index ]
98
+ this . logger . info ( `upload part ${ index } .` , info )
83
99
84
100
const shouldCheckMD5 = this . config . checkByMD5
85
101
const reuseSaved = ( ) => {
@@ -92,6 +108,7 @@ export default class Resume extends Base {
92
108
}
93
109
94
110
const md5 = await utils . computeMd5 ( chunk )
111
+ this . logger . info ( `computed part md5.` , md5 )
95
112
96
113
if ( info && md5 === info . md5 ) {
97
114
reuseSaved ( )
@@ -108,13 +125,15 @@ export default class Resume extends Base {
108
125
onCreate : ( xhr : XMLHttpRequest ) => this . addXhr ( xhr )
109
126
}
110
127
128
+ this . logger . info ( `part ${ index } start uploading.` )
111
129
const response = await uploadChunk (
112
130
this . token ,
113
131
this . key ,
114
132
chunkInfo . index + 1 ,
115
133
this . getUploadInfo ( ) ,
116
134
requestOptions
117
135
)
136
+ this . logger . info ( `part ${ index } upload completed.` )
118
137
119
138
// 在某些浏览器环境下,xhr 的 progress 事件无法被触发,progress 为 null,这里在每次分片上传完成后都手动更新下 progress
120
139
onProgress ( {
@@ -128,11 +147,14 @@ export default class Resume extends Base {
128
147
size : chunk . size
129
148
}
130
149
131
- utils . setLocalFileInfo ( this . getLocalKey ( ) , {
132
- id : this . uploadId ,
133
- data : this . uploadedList
134
- } )
135
-
150
+ try {
151
+ utils . setLocalFileInfo ( this . getLocalKey ( ) , {
152
+ id : this . uploadId ,
153
+ data : this . uploadedList
154
+ } )
155
+ } catch ( error ) {
156
+ this . logger . info ( `set part ${ index } cache failed.` , error )
157
+ }
136
158
}
137
159
138
160
private async mkFileReq ( ) {
@@ -146,7 +168,8 @@ export default class Resume extends Base {
146
168
...this . putExtra . customVars && { customVars : this . putExtra . customVars } ,
147
169
...this . putExtra . metadata && { metadata : this . putExtra . metadata }
148
170
}
149
-
171
+
172
+ this . logger . info ( 'parts upload completed, make file.' , data )
150
173
const result = await uploadComplete (
151
174
this . token ,
152
175
this . key ,
@@ -156,23 +179,35 @@ export default class Resume extends Base {
156
179
body : JSON . stringify ( data )
157
180
}
158
181
)
182
+
183
+ this . logger . info ( 'finishResumeProgress.' )
159
184
this . updateMkFileProgress ( 1 )
160
185
return result
161
186
}
162
187
163
188
private async initBeforeUploadChunks ( ) {
164
- const localInfo = utils . getLocalFileInfo ( this . getLocalKey ( ) )
189
+ let localInfo : LocalInfo | null = null
190
+ try { localInfo = utils . getLocalFileInfo ( this . getLocalKey ( ) ) }
191
+ catch ( error ) { this . logger . warn ( error ) }
192
+
165
193
// 分片必须和当时使用的 uploadId 配套,所以断点续传需要把本地存储的 uploadId 拿出来
166
194
// 假如没有 localInfo 本地信息并重新获取 uploadId
167
195
if ( ! localInfo ) {
168
- // 防止本地信息已被破坏,初始化时 clear 一下
169
- utils . removeLocalFileInfo ( this . getLocalKey ( ) )
196
+ this . logger . info ( 'resume upload parts from api.' )
170
197
const res = await initUploadParts ( this . token , this . bucket , this . key , this . uploadUrl )
198
+ this . logger . info ( `resume upload parts of id: ${ res . data . uploadId } .` )
171
199
this . uploadId = res . data . uploadId
172
200
this . uploadedList = [ ]
173
201
} else {
174
- this . uploadId = localInfo . id
202
+ const infoMessage = [
203
+ 'resume upload parts from local cache' ,
204
+ `total ${ localInfo . data . length } part` ,
205
+ `id is ${ localInfo . id } .`
206
+ ]
207
+
208
+ this . logger . info ( infoMessage . join ( ', ' ) )
175
209
this . uploadedList = localInfo . data
210
+ this . uploadId = localInfo . id
176
211
}
177
212
178
213
this . chunks = utils . getChunks ( this . file , this . config . chunkSize )
@@ -220,5 +255,4 @@ export default class Resume extends Base {
220
255
}
221
256
this . onData ( this . progress )
222
257
}
223
-
224
258
}
0 commit comments