@@ -80,14 +80,16 @@ function urlSafeBase64(string) {
8080
8181class ShardUploader {
8282 constructor ( uploadInfo , data , file , saveOptions ) {
83+ this . uploadInfo = uploadInfo ;
8384 this . data = data ;
8485 this . file = file ;
8586 this . size = undefined ;
8687 this . offset = 0 ;
8788 this . uploadedChunks = 0 ;
8889
8990 const key = urlSafeBase64 ( uploadInfo . key ) ;
90- this . baseURL = `https://upload.qiniup.com/buckets/${ uploadInfo . bucket } /objects/${ key } /uploads` ;
91+ const uploadURL = uploadInfo . upload_url || 'https://upload.qiniup.com' ;
92+ this . baseURL = `${ uploadURL } /buckets/${ uploadInfo . bucket } /objects/${ key } /uploads` ;
9193 this . upToken = 'UpToken ' + uploadInfo . token ;
9294
9395 this . uploaded = 0 ;
@@ -158,40 +160,47 @@ class ShardUploader {
158160
159161 upload ( ) {
160162 const parts = [ ] ;
161- return this . getUploadId ( ) . then ( uploadId => {
162- const uploadPart = ( ) => {
163- return Promise . resolve ( this . getChunk ( ) )
164- . then ( chunk => {
165- if ( ! chunk ) {
166- return ;
167- }
168- const partNumber = parts . length + 1 ;
169- return this . uploadPart ( uploadId , partNumber , chunk ) . then ( part => {
170- parts . push ( part ) ;
171- this . uploadedChunks ++ ;
172- return uploadPart ( ) ;
173- } ) ;
174- } )
175- . catch ( error =>
176- this . stopUpload ( uploadId ) . then ( ( ) => Promise . reject ( error ) )
177- ) ;
178- } ;
163+ return this . getUploadId ( )
164+ . then ( uploadId => {
165+ const uploadPart = ( ) => {
166+ return Promise . resolve ( this . getChunk ( ) )
167+ . then ( chunk => {
168+ if ( ! chunk ) {
169+ return ;
170+ }
171+ const partNumber = parts . length + 1 ;
172+ return this . uploadPart ( uploadId , partNumber , chunk ) . then ( part => {
173+ parts . push ( part ) ;
174+ this . uploadedChunks ++ ;
175+ return uploadPart ( ) ;
176+ } ) ;
177+ } )
178+ . catch ( error =>
179+ this . stopUpload ( uploadId ) . then ( ( ) => Promise . reject ( error ) )
180+ ) ;
181+ } ;
179182
180- return uploadPart ( ) . then ( ( ) =>
181- ajax ( {
182- method : 'POST' ,
183- url : `${ this . baseURL } /${ uploadId } ` ,
184- headers : {
185- Authorization : this . upToken ,
186- } ,
187- data : {
188- parts,
189- fname : this . file . attributes . name ,
190- mimeType : this . file . attributes . mime_type ,
191- } ,
192- } )
193- ) ;
194- } ) ;
183+ return uploadPart ( ) . then ( ( ) =>
184+ ajax ( {
185+ method : 'POST' ,
186+ url : `${ this . baseURL } /${ uploadId } ` ,
187+ headers : {
188+ Authorization : this . upToken ,
189+ } ,
190+ data : {
191+ parts,
192+ fname : this . file . attributes . name ,
193+ mimeType : this . file . attributes . mime_type ,
194+ } ,
195+ } )
196+ ) ;
197+ } )
198+ . then ( ( ) => {
199+ this . file . attributes . url = this . uploadInfo . url ;
200+ this . file . _bucket = this . uploadInfo . bucket ;
201+ this . file . id = this . uploadInfo . objectId ;
202+ return this . file ;
203+ } ) ;
195204 }
196205}
197206
0 commit comments