Skip to content

Commit b87b62f

Browse files
committed
chore(filer): change post file procotol
1 parent 575b735 commit b87b62f

2 files changed

Lines changed: 13 additions & 41 deletions

File tree

volumes/filer.go

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
package volumes
33

44
import (
5-
"bytes"
65
"encoding/json"
76
"fmt"
87
"io"
9-
"mime/multipart"
108
"net/http"
119
"net/url"
12-
"strings"
1310

1411
drycc "github.com/drycc/controller-sdk-go"
1512
"github.com/drycc/controller-sdk-go/api"
@@ -45,29 +42,14 @@ func GetFile(c *drycc.Client, appID, volumeID, path string) (*http.Response, err
4542

4643
// Put file to an app's volume.
4744
func PostFile(c *drycc.Client, appID, volumeID, volumePath, name string, size int64, reader io.Reader) (*http.Response, error) {
48-
49-
buffer := new(bytes.Buffer)
50-
writer := multipart.NewWriter(buffer)
51-
if err := writer.WriteField("path", volumePath); err != nil {
52-
return nil, err
53-
}
54-
if _, err := writer.CreateFormFile("file", name); err != nil {
55-
return nil, err
56-
}
57-
size += int64(buffer.Len())
58-
head := strings.NewReader(buffer.String())
59-
buffer.Reset()
60-
writer.Close()
61-
bottom := strings.NewReader(buffer.String())
62-
size += int64(buffer.Len())
6345
u := fmt.Sprintf("/v2/apps/%s/volumes/%s/client/", appID, volumeID)
64-
65-
r, err := c.NewRequest("POST", u, io.MultiReader(head, reader, bottom))
46+
r, err := c.NewRequest("POST", u, reader)
6647
if err != nil {
6748
return nil, err
6849
}
6950
r.ContentLength = size
70-
r.Header.Add("Content-Type", writer.FormDataContentType())
51+
r.Header.Add("Content-Type", "filer/octet-stream")
52+
r.Header.Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"; filepath="%s"`, name, volumePath))
7153
return c.Do(r)
7254
}
7355

volumes/filer_test.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,17 @@ func (f *fakeFilerServer) ServeHTTP(res http.ResponseWriter, req *http.Request)
3838
res.Write([]byte(`{"results":[], "count": 0}`))
3939
return
4040
} else if req.Method == "POST" {
41-
if err := req.ParseMultipartForm(1024 * 1024); err != nil {
42-
http.Error(res, fmt.Sprintf("Parse multipart form error: %v", err), http.StatusBadRequest)
43-
return
41+
body, err := io.ReadAll(req.Body)
42+
if err != nil {
43+
fmt.Println(err)
44+
res.WriteHeader(http.StatusInternalServerError)
45+
res.Write(nil)
4446
}
45-
for _, tmpFiles := range req.MultipartForm.File {
46-
for _, tmpFile := range tmpFiles {
47-
srcFile, err := tmpFile.Open()
48-
if err != nil {
49-
return
50-
}
51-
body, err := io.ReadAll(srcFile)
52-
if err != nil {
53-
return
54-
}
55-
if string(body) != volumeFileContentExpected {
56-
fmt.Printf("Expected '%s', Got '%s'\n", volumeFileContentExpected, body)
57-
res.WriteHeader(http.StatusInternalServerError)
58-
res.Write(nil)
59-
return
60-
}
61-
}
47+
if string(body) != volumeFileContentExpected {
48+
fmt.Printf("Expected '%s', Got '%s'\n", volumeFileContentExpected, body)
49+
res.WriteHeader(http.StatusInternalServerError)
50+
res.Write(nil)
51+
return
6252
}
6353
return
6454
}

0 commit comments

Comments
 (0)