@@ -248,91 +248,85 @@ export class UploadHttpClient {
248248 }
249249 } else {
250250 // the file that is being uploaded is greater than 64k in size, a temporary file gets created on disk using the
251- // npm tmp-promise package and this file gets used during compression for the GZip file that gets created
252- return tmp
253- . file ( )
254- . then ( async tmpFile => {
255- // create a GZip file of the original file being uploaded, the original file should not be modified in any way
256- uploadFileSize = await createGZipFileOnDisk (
251+ // npm tmp-promise package and this file gets used to create a GZipped file
252+ const tempFile = await tmp . file ( )
253+
254+ // create a GZip file of the original file being uploaded, the original file should not be modified in any way
255+ uploadFileSize = await createGZipFileOnDisk (
256+ parameters . file ,
257+ tempFile . path
258+ )
259+
260+ let uploadFilePath = tempFile . path
261+
262+ // compression did not help with size reduction, use the original file for upload and delete the temp GZip file
263+ if ( totalFileSize < uploadFileSize ) {
264+ uploadFileSize = totalFileSize
265+ uploadFilePath = parameters . file
266+ isGzip = false
267+ }
268+
269+ let abortFileUpload = false
270+ // upload only a single chunk at a time
271+ while ( offset < uploadFileSize ) {
272+ const chunkSize = Math . min (
273+ uploadFileSize - offset ,
274+ parameters . maxChunkSize
275+ )
276+
277+ // if an individual file is greater than 100MB (1024*1024*100) in size, display extra information about the upload status
278+ if ( uploadFileSize > 104857600 ) {
279+ this . statusReporter . updateLargeFileStatus (
257280 parameters . file ,
258- tmpFile . path
281+ offset ,
282+ uploadFileSize
259283 )
260- let uploadFilePath = tmpFile . path
261-
262- // compression did not help with size reduction, use the original file for upload and delete the temp GZip file
263- if ( totalFileSize < uploadFileSize ) {
264- uploadFileSize = totalFileSize
265- uploadFilePath = parameters . file
266- isGzip = false
267- tmpFile . cleanup ( )
268- }
284+ }
269285
270- let abortFileUpload = false
271- // upload only a single chunk at a time
272- while ( offset < uploadFileSize ) {
273- const chunkSize = Math . min (
274- uploadFileSize - offset ,
275- parameters . maxChunkSize
276- )
286+ const start = offset
287+ const end = offset + chunkSize - 1
288+ offset += parameters . maxChunkSize
277289
278- // if an individual file is greater than 100MB (1024*1024*100) in size, display extra information about the upload status
279- if ( uploadFileSize > 104857600 ) {
280- this . statusReporter . updateLargeFileStatus (
281- parameters . file ,
282- offset ,
283- uploadFileSize
284- )
285- }
290+ if ( abortFileUpload ) {
291+ // if we don't want to continue in the event of an error, any pending upload chunks will be marked as failed
292+ failedChunkSizes += chunkSize
293+ continue
294+ }
286295
287- const start = offset
288- const end = offset + chunkSize - 1
289- offset += parameters . maxChunkSize
296+ const result = await this . uploadChunk (
297+ httpClientIndex ,
298+ parameters . resourceUrl ,
299+ fs . createReadStream ( uploadFilePath , {
300+ start,
301+ end,
302+ autoClose : false
303+ } ) ,
304+ start ,
305+ end ,
306+ uploadFileSize ,
307+ isGzip ,
308+ totalFileSize
309+ )
290310
291- if ( abortFileUpload ) {
292- // if we don't want to continue in the event of an error, any pending upload chunks will be marked as failed
293- failedChunkSizes += chunkSize
294- continue
295- }
311+ if ( ! result ) {
312+ // Chunk failed to upload, report as failed and do not continue uploading any more chunks for the file. It is possible that part of a chunk was
313+ // successfully uploaded so the server may report a different size for what was uploaded
314+ isUploadSuccessful = false
315+ failedChunkSizes += chunkSize
316+ core . warning ( `Aborting upload for ${ parameters . file } due to failure` )
317+ abortFileUpload = true
318+ }
319+ }
296320
297- const result = await this . uploadChunk (
298- httpClientIndex ,
299- parameters . resourceUrl ,
300- fs . createReadStream ( uploadFilePath , {
301- start,
302- end,
303- autoClose : false
304- } ) ,
305- start ,
306- end ,
307- uploadFileSize ,
308- isGzip ,
309- totalFileSize
310- )
321+ // Delete the temporary file that was created as part of the upload. If the temp file does not get manually deleted by
322+ // calling cleanup, it gets removed when the node process exits. For more info see: https://www.npmjs.com/package/tmp-promise#about
323+ await tempFile . cleanup ( )
311324
312- if ( ! result ) {
313- // Chunk failed to upload, report as failed and do not continue uploading any more chunks for the file. It is possible that part of a chunk was
314- // successfully uploaded so the server may report a different size for what was uploaded
315- isUploadSuccessful = false
316- failedChunkSizes += chunkSize
317- core . warning (
318- `Aborting upload for ${ parameters . file } due to failure`
319- )
320- abortFileUpload = true
321- }
322- }
323- } )
324- . then (
325- async ( ) : Promise < UploadFileResult > => {
326- // only after the file upload is complete and the temporary file is deleted, return the UploadResult
327- return new Promise ( resolve => {
328- resolve ( {
329- isSuccess : isUploadSuccessful ,
330- successfulUploadSize : uploadFileSize - failedChunkSizes ,
331- totalSize : totalFileSize
332- } )
333- } )
334- }
335- )
325+ return {
326+ isSuccess : isUploadSuccessful ,
327+ successfulUploadSize : uploadFileSize - failedChunkSizes ,
328+ totalSize : totalFileSize
329+ }
336330 }
337331 }
338332
0 commit comments