@@ -10,6 +10,9 @@ import (
1010 "io"
1111 "mime/multipart"
1212 "net/http"
13+ "net/textproto"
14+ "strconv"
15+ "strings"
1316 "time"
1417
1518 "github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/baseclient"
@@ -211,7 +214,7 @@ func (c MtaRestClient) UploadMtaFile(file util.NamedReadSeeker, fileSize int64,
211214 errChan := make (chan error , 1 )
212215 go func () {
213216 defer pipeWriter .Close ()
214- errChan <- c .writeFileToRequest (file , form )
217+ errChan <- c .writeFileToRequest (file , fileSize , form )
215218 }()
216219
217220 ctx , done := context .WithTimeout (context .Background (), time .Hour )
@@ -255,7 +258,11 @@ func (c MtaRestClient) UploadMtaFile(file util.NamedReadSeeker, fileSize int64,
255258func (c MtaRestClient ) calculateRequestSize (fileName string , fileSize int64 ) (int64 , error ) {
256259 var body bytes.Buffer
257260 form := multipart .NewWriter (& body )
258- _ , err := form .CreateFormFile ("file" , fileName )
261+ h := make (textproto.MIMEHeader )
262+ h .Set ("Content-Disposition" , fmt .Sprintf (`form-data; name="file"; filename="%s"` , escapeQuotes (fileName )))
263+ h .Set ("Content-Type" , "application/octet-stream" )
264+ h .Set ("Content-Length" , strconv .FormatInt (fileSize , 10 ))
265+ _ , err := form .CreatePart (h )
259266 if err != nil {
260267 return 0 , err
261268 }
@@ -266,10 +273,14 @@ func (c MtaRestClient) calculateRequestSize(fileName string, fileSize int64) (in
266273 return int64 (body .Len ()) + fileSize , nil
267274}
268275
269- func (c MtaRestClient ) writeFileToRequest (file util.NamedReadSeeker , form * multipart.Writer ) error {
270- fileWriter , err := form .CreateFormFile ("file" , file .Name ())
276+ func (c MtaRestClient ) writeFileToRequest (file util.NamedReadSeeker , fileSize int64 , form * multipart.Writer ) error {
277+ h := make (textproto.MIMEHeader )
278+ h .Set ("Content-Disposition" , fmt .Sprintf (`form-data; name="file"; filename="%s"` , escapeQuotes (file .Name ())))
279+ h .Set ("Content-Type" , "application/octet-stream" )
280+ h .Set ("Content-Length" , strconv .FormatInt (fileSize , 10 ))
281+ fileWriter , err := form .CreatePart (h )
271282 if err != nil {
272- return fmt .Errorf ("could not write to HTTP request : %v" , err )
283+ return fmt .Errorf ("could not create multipart file part : %v" , err )
273284 }
274285
275286 _ , err = io .Copy (fileWriter , file )
@@ -284,6 +295,10 @@ func (c MtaRestClient) writeFileToRequest(file util.NamedReadSeeker, form *multi
284295 return nil
285296}
286297
298+ func escapeQuotes (s string ) string {
299+ return strings .NewReplacer ("\\ " , "\\ \\ " , `"` , "\\ \" " ).Replace (s )
300+ }
301+
287302func (c MtaRestClient ) StartUploadMtaArchiveFromUrl (fileUrl string , namespace * string ) (http.Header , error ) {
288303 requestUrl := "https://" + c .dsHost + "/" + restBaseURL + spacesURL + c .spaceGuid + "/files/async"
289304 if namespace != nil && len (* namespace ) != 0 {
0 commit comments