88 removeLocalFileInfo ,
99 isContainFileMimeType ,
1010 sum ,
11+ getDomainFromUrl ,
12+ getPortFromUrl ,
1113 getHeadersForChunkUpload ,
1214 getHeadersForMkFile ,
1315 request ,
@@ -19,10 +21,11 @@ import {
1921let BLOCK_SIZE = 4 * 1024 * 1024 ;
2022
2123export class UploadManager {
22- constructor ( options , handlers ) {
24+ constructor ( options , handlers , statisticsLogger ) {
2325 this . config = Object . assign (
2426 {
2527 useCdnDomain : true ,
28+ disableStatisticsReport : false ,
2629 region : null
2730 } ,
2831 options . config
@@ -35,6 +38,8 @@ export class UploadManager {
3538 } ,
3639 options . putExtra
3740 ) ;
41+ this . statisticsLogger = statisticsLogger ;
42+ this . progress = null ;
3843 this . xhrList = [ ] ;
3944 this . xhrHandler = xhr => this . xhrList . push ( xhr ) ;
4045 this . file = options . file ;
@@ -60,14 +65,25 @@ export class UploadManager {
6065 }
6166
6267 this . uploadUrl = getUploadUrl ( this . config ) ;
68+ this . uploadAt = new Date ( ) . getTime ( ) ;
6369
64- let upload =
65- this . file . size > BLOCK_SIZE ? this . resumeUpload ( ) : this . directUpload ( ) ;
66- upload . then ( res => this . onComplete ( res ) , err => {
70+ let upload = this . file . size > BLOCK_SIZE ? this . resumeUpload ( ) : this . directUpload ( ) ;
71+ upload . then ( res => {
72+ this . onComplete ( res . data ) ;
73+ if ( ! this . config . disableStatisticsReport ) {
74+ this . sendLog ( res . reqId , 200 ) ;
75+ }
76+ } , err => {
6777 this . onError ( err ) ;
78+ if ( err . isRequestError && ! this . config . disableStatisticsReport ) {
79+ if ( err . code !== 0 ) {
80+ this . sendLog ( err . reqId , err . code ) ;
81+ } else {
82+ this . sendLog ( "" , - 2 ) ;
83+ }
84+ }
6885 this . stop ( ) ;
6986 } )
70-
7187 return upload ;
7288 }
7389
@@ -76,6 +92,21 @@ export class UploadManager {
7692 this . xhrList = [ ] ;
7793 }
7894
95+ sendLog ( reqId , code ) {
96+ this . statisticsLogger . log ( {
97+ code : code ,
98+ reqId : reqId ,
99+ host : getDomainFromUrl ( this . uploadUrl ) ,
100+ remoteIp : "" ,
101+ port : getPortFromUrl ( this . uploadUrl ) ,
102+ duration : ( new Date ( ) . getTime ( ) - this . uploadAt ) / 1000 ,
103+ time : Math . floor ( this . uploadAt / 1000 ) ,
104+ bytesSent : this . progress ? this . progress . total . loaded : 0 ,
105+ upType : "jssdk-h5" ,
106+ size : this . file . size
107+ } , this . token )
108+ }
109+
79110 // 直传
80111 directUpload ( ) {
81112 let formData = new FormData ( ) ;
@@ -185,8 +216,7 @@ export class UploadManager {
185216 let headers = getHeadersForMkFile ( this . token ) ;
186217 let onCreate = this . xhrHandler ;
187218 let method = "POST" ;
188-
189- return request ( requestUrL , { method, body, headers, onCreate } ) . then (
219+ return request ( requestUrL , { method, body, headers, onCreate} ) . then (
190220 res => {
191221 this . updateMkFileProgress ( 1 ) ;
192222 return Promise . resolve ( res ) ;
@@ -195,9 +225,8 @@ export class UploadManager {
195225 }
196226
197227 updateDirectProgress ( loaded , total ) {
198- this . onData (
199- { total : this . getProgressInfoItem ( loaded , total ) }
200- )
228+ this . progress = { total : this . getProgressInfoItem ( loaded , total ) }
229+ this . onData ( this . progress )
201230 }
202231
203232 initChunksProgress ( ) {
@@ -216,17 +245,16 @@ export class UploadManager {
216245 }
217246
218247 notifyResumeProgress ( ) {
219- this . onData (
220- {
221- total : this . getProgressInfoItem (
222- sum ( this . loaded . chunks ) + this . loaded . mkFileProgress ,
223- this . file . size + 1
224- ) ,
225- chunks : this . chunks . map ( ( chunk , index ) => {
226- return this . getProgressInfoItem ( this . loaded . chunks [ index ] , chunk . size ) ;
227- } )
228- }
229- ) ;
248+ this . progress = {
249+ total : this . getProgressInfoItem (
250+ sum ( this . loaded . chunks ) + this . loaded . mkFileProgress ,
251+ this . file . size + 1
252+ ) ,
253+ chunks : this . chunks . map ( ( chunk , index ) => {
254+ return this . getProgressInfoItem ( this . loaded . chunks [ index ] , chunk . size ) ;
255+ } )
256+ } ;
257+ this . onData ( this . progress ) ;
230258 }
231259
232260 getProgressInfoItem ( loaded , size ) {
0 commit comments