Change upload response
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
b559d28a38
commit
46401c1b98
17
api/http.go
17
api/http.go
@ -86,14 +86,14 @@ func (s *HTTPServer) HandlerAPIFilePost(w http.ResponseWriter, r *http.Request)
|
|||||||
}
|
}
|
||||||
s.Logger.Infow("Stored file.", "req_id", reqID, "id", f.ID, "remote_addr", r.RemoteAddr)
|
s.Logger.Infow("Stored file.", "req_id", reqID, "id", f.ID, "remote_addr", r.RemoteAddr)
|
||||||
fileURL := path.Join(s.config.URL, "/api/file", f.ID)
|
fileURL := path.Join(s.config.URL, "/api/file", f.ID)
|
||||||
var resp = struct {
|
resp := &ResponseAPIFilePost{
|
||||||
Message string `json:"message"`
|
|
||||||
ID string `json:"id"`
|
|
||||||
URL string `json:"url"`
|
|
||||||
}{
|
|
||||||
Message: "OK",
|
Message: "OK",
|
||||||
|
Files: []ResponseAPIFilePostFiles{
|
||||||
|
ResponseAPIFilePostFiles{
|
||||||
ID: f.ID,
|
ID: f.ID,
|
||||||
URL: fileURL,
|
URL: fileURL,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
@ -143,7 +143,7 @@ func (s *HTTPServer) HandlerAPIFileDelete(w http.ResponseWriter, r *http.Request
|
|||||||
func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.Request) {
|
func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
reqID := middleware.GetReqID(r.Context())
|
reqID := middleware.GetReqID(r.Context())
|
||||||
|
|
||||||
var responses []ResponseAPIFilePost
|
var resp ResponseAPIFilePost
|
||||||
|
|
||||||
if err := r.ParseMultipartForm(1024 * 1024 * 10); err != nil {
|
if err := r.ParseMultipartForm(1024 * 1024 * 10); err != nil {
|
||||||
s.Logger.Warnw("Error parsing multipart form.", "req_id", reqID, "err", err)
|
s.Logger.Warnw("Error parsing multipart form.", "req_id", reqID, "err", err)
|
||||||
@ -167,13 +167,14 @@ func (s *HTTPServer) processMultiPartFormUpload(w http.ResponseWriter, r *http.R
|
|||||||
s.Logger.Infow("Stored file.", "req_id", reqID, "id", f.ID, "filename", f.OriginalFilename, "remote_addr", r.RemoteAddr)
|
s.Logger.Infow("Stored file.", "req_id", reqID, "id", f.ID, "filename", f.OriginalFilename, "remote_addr", r.RemoteAddr)
|
||||||
|
|
||||||
fileURL := path.Join(s.config.URL, "/api/file", f.ID)
|
fileURL := path.Join(s.config.URL, "/api/file", f.ID)
|
||||||
responses = append(responses, ResponseAPIFilePost{Message: "OK", ID: f.ID, URL: fileURL})
|
fileResponse := ResponseAPIFilePostFiles{ID: f.ID, URL: fileURL}
|
||||||
|
resp.Files = append(resp.Files, fileResponse)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
if err := encoder.Encode(&responses); err != nil {
|
if err := encoder.Encode(&resp); err != nil {
|
||||||
s.Logger.Warnw("Error encoding response to client.", "req_id", reqID, "error", err, "remote_addr", r.RemoteAddr)
|
s.Logger.Warnw("Error encoding response to client.", "req_id", reqID, "error", err, "remote_addr", r.RemoteAddr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,22 +73,18 @@ func TestHandlers(t *testing.T) {
|
|||||||
t.Errorf("Returned unexpected status. Got %d want %d", status, http.StatusAccepted)
|
t.Errorf("Returned unexpected status. Got %d want %d", status, http.StatusAccepted)
|
||||||
}
|
}
|
||||||
|
|
||||||
var expectedResp []struct {
|
var expectedResp api.ResponseAPIFilePost
|
||||||
Message string `json:"message"`
|
|
||||||
ID string `json:"id"`
|
|
||||||
URL string `json:"url"`
|
|
||||||
}
|
|
||||||
|
|
||||||
decoder := json.NewDecoder(rr.Result().Body)
|
decoder := json.NewDecoder(rr.Result().Body)
|
||||||
if err := decoder.Decode(&expectedResp); err != nil {
|
if err := decoder.Decode(&expectedResp); err != nil {
|
||||||
t.Fatalf("error decoding response: %s", err)
|
t.Fatalf("error decoding response: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if l := len(expectedResp); l != 1 {
|
if l := len(expectedResp.Files); l != 1 {
|
||||||
t.Errorf("Response has wrong length. Got %d want %d", l, 1)
|
t.Errorf("Response has wrong length. Got %d want %d", l, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
uploadID := expectedResp[0].ID
|
uploadID := expectedResp.Files[0].ID
|
||||||
if uploadID == "" {
|
if uploadID == "" {
|
||||||
t.Errorf("Response has empty id")
|
t.Errorf("Response has empty id")
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,10 @@ type ResponseAPILogin struct {
|
|||||||
}
|
}
|
||||||
type ResponseAPIFilePost struct {
|
type ResponseAPIFilePost struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
|
Files []ResponseAPIFilePostFiles `json:"files"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ResponseAPIFilePostFiles struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
}
|
}
|
||||||
|
@ -159,7 +159,7 @@ func (c *Client) Download(ctx context.Context, id string) (io.ReadCloser, error)
|
|||||||
return resp.Body, nil
|
return resp.Body, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Upload(ctx context.Context, files ...*files.File) ([]api.ResponseAPIFilePost, error) {
|
func (c *Client) Upload(ctx context.Context, files ...*files.File) (*api.ResponseAPIFilePost, error) {
|
||||||
url := fmt.Sprintf("%s/api/file", c.BaseURL)
|
url := fmt.Sprintf("%s/api/file", c.BaseURL)
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
|
||||||
@ -194,7 +194,7 @@ func (c *Client) Upload(ctx context.Context, files ...*files.File) ([]api.Respon
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
var expectedResp []api.ResponseAPIFilePost
|
var expectedResp *api.ResponseAPIFilePost
|
||||||
|
|
||||||
decoder := json.NewDecoder(resp.Body)
|
decoder := json.NewDecoder(resp.Body)
|
||||||
if err := decoder.Decode(&expectedResp); err != nil {
|
if err := decoder.Decode(&expectedResp); err != nil {
|
||||||
|
@ -120,7 +120,7 @@ func TestClient(t *testing.T) {
|
|||||||
t.Fatalf("Error uploading: %s", err)
|
t.Fatalf("Error uploading: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
retrieved, err := srv.Files.Get(resp[0].ID)
|
retrieved, err := srv.Files.Get(resp.Files[0].ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error getting uploaded file from store: %s", err)
|
t.Fatalf("Error getting uploaded file from store: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ func ActionUpload(c *cli.Context) error {
|
|||||||
errmsg := fmt.Sprintf("Error uploading file: %s", err)
|
errmsg := fmt.Sprintf("Error uploading file: %s", err)
|
||||||
return cli.Exit(errmsg, 1)
|
return cli.Exit(errmsg, 1)
|
||||||
}
|
}
|
||||||
fmt.Printf("Uploaded file %s - %s", file.OriginalFilename, resp[0].URL)
|
fmt.Printf("Uploaded file %s - %s", file.OriginalFilename, resp.Files[0].URL)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user