From 3d5b071ef727e4bfef18ca3916c6ba1a054a12b1 Mon Sep 17 00:00:00 2001 From: Rohan Verma Date: Fri, 18 Oct 2019 13:15:03 +0530 Subject: [PATCH] fix: change multipart.File -> io.ReadSeeker --- simples3.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/simples3.go b/simples3.go index 6296aee..14afca4 100644 --- a/simples3.go +++ b/simples3.go @@ -38,7 +38,7 @@ type UploadInput struct { ObjectKey string FileName string ContentType string - Body multipart.File + Body io.ReadSeeker } // UploadResponse receives the following XML @@ -143,19 +143,18 @@ func (s3 *S3) getClient() *http.Client { return s3.Client } -func detectFileSize(body multipart.File) int64 { - switch r := body.(type) { - case io.Seeker: - pos, _ := r.Seek(0, 1) - defer r.Seek(pos, 0) - - n, err := r.Seek(0, 2) - if err != nil { - return -1 - } - return n +func detectFileSize(body io.Seeker) (int64, error) { + pos, err := body.Seek(0, 1) + if err != nil { + return -1, err } - return -1 + defer body.Seek(pos, 0) + + n, err := body.Seek(0, 2) + if err != nil { + return -1, err + } + return n, nil } // SetClient can be used to set the http client to be @@ -173,12 +172,16 @@ func (s3 *S3) SetClient(client *http.Client) *S3 { // FileUpload makes a POST call with the file written as multipart // and on successful upload, checks for 200 OK. func (s3 *S3) FileUpload(u UploadInput) (UploadResponse, error) { + fSize, err := detectFileSize(u.Body) + if err != nil { + return UploadResponse{}, err + } policies, err := s3.CreateUploadPolicies(UploadConfig{ UploadURL: fmt.Sprintf(s3.URIFormat, s3.Region, u.Bucket), BucketName: u.Bucket, ObjectKey: u.ObjectKey, ContentType: u.ContentType, - FileSize: detectFileSize(u.Body), + FileSize: fSize, MetaData: map[string]string{ "success_action_status": "201", // returns XML doc on success },