44 "context"
55 "fmt"
66 "io"
7- "math"
87 "net/http"
98 "strconv"
109
@@ -70,9 +69,16 @@ func (d *Pan123) completeS3(ctx context.Context, upReq *UploadResp, file model.F
7069}
7170
7271func (d * Pan123 ) newUpload (ctx context.Context , upReq * UploadResp , file model.FileStreamer , up driver.UpdateProgress ) error {
73- chunkSize := int64 (1024 * 1024 * 16 )
72+ chunkSize := int64 (16 * utils . MB )
7473 // fetch s3 pre signed urls
75- chunkCount := int (math .Ceil (float64 (file .GetSize ()) / float64 (chunkSize )))
74+ size := file .GetSize ()
75+ chunkCount := int (size / chunkSize )
76+ lastChunkSize := size % chunkSize
77+ if lastChunkSize > 0 {
78+ chunkCount ++
79+ } else {
80+ lastChunkSize = chunkSize
81+ }
7682 // only 1 batch is allowed
7783 isMultipart := chunkCount > 1
7884 batchSize := 1
@@ -87,10 +93,7 @@ func (d *Pan123) newUpload(ctx context.Context, upReq *UploadResp, file model.Fi
8793 return ctx .Err ()
8894 }
8995 start := i
90- end := i + batchSize
91- if end > chunkCount + 1 {
92- end = chunkCount + 1
93- }
96+ end := min (i + batchSize , chunkCount + 1 )
9497 s3PreSignedUrls , err := getS3UploadUrl (ctx , upReq , start , end )
9598 if err != nil {
9699 return err
@@ -100,11 +103,10 @@ func (d *Pan123) newUpload(ctx context.Context, upReq *UploadResp, file model.Fi
100103 if utils .IsCanceled (ctx ) {
101104 return ctx .Err ()
102105 }
103- curSize := chunkSize
104106 if j == chunkCount {
105- curSize = file . GetSize () - ( int64 ( chunkCount ) - 1 ) * chunkSize
107+ chunkSize = lastChunkSize
106108 }
107- err = d .uploadS3Chunk (ctx , upReq , s3PreSignedUrls , j , end , io .LimitReader (limited , chunkSize ), curSize , false , getS3UploadUrl )
109+ err = d .uploadS3Chunk (ctx , upReq , s3PreSignedUrls , j , end , io .LimitReader (limited , chunkSize ), chunkSize , false , getS3UploadUrl )
108110 if err != nil {
109111 return err
110112 }
0 commit comments