diff --git a/api/http.go b/api/http.go index 08b59c1..8fdfdfa 100644 --- a/api/http.go +++ b/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) fileURL := path.Join(s.config.URL, "/api/file", f.ID) - var resp = struct { - Message string `json:"message"` - ID string `json:"id"` - URL string `json:"url"` - }{ + resp := &ResponseAPIFilePost{ Message: "OK", - ID: f.ID, - URL: fileURL, + Files: []ResponseAPIFilePostFiles{ + ResponseAPIFilePostFiles{ + ID: f.ID, + URL: fileURL, + }, + }, } w.WriteHeader(http.StatusAccepted) 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) { reqID := middleware.GetReqID(r.Context()) - var responses []ResponseAPIFilePost + var resp ResponseAPIFilePost if err := r.ParseMultipartForm(1024 * 1024 * 10); err != nil { 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) 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) 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) } } diff --git a/api/http_test.go b/api/http_test.go index e1e277c..8bcefee 100644 --- a/api/http_test.go +++ b/api/http_test.go @@ -73,22 +73,18 @@ func TestHandlers(t *testing.T) { t.Errorf("Returned unexpected status. Got %d want %d", status, http.StatusAccepted) } - var expectedResp []struct { - Message string `json:"message"` - ID string `json:"id"` - URL string `json:"url"` - } + var expectedResp api.ResponseAPIFilePost decoder := json.NewDecoder(rr.Result().Body) if err := decoder.Decode(&expectedResp); err != nil { 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) } - uploadID := expectedResp[0].ID + uploadID := expectedResp.Files[0].ID if uploadID == "" { t.Errorf("Response has empty id") } diff --git a/api/json.go b/api/json.go index 73c5ad4..5059ec5 100644 --- a/api/json.go +++ b/api/json.go @@ -14,9 +14,13 @@ type ResponseAPILogin struct { Token string `json:"token"` } type ResponseAPIFilePost struct { - Message string `json:"message"` - ID string `json:"id"` - URL string `json:"url"` + Message string `json:"message"` + Files []ResponseAPIFilePostFiles `json:"files"` +} + +type ResponseAPIFilePostFiles struct { + ID string `json:"id"` + URL string `json:"url"` } type ResponseAPIUserList struct { diff --git a/client/client.go b/client/client.go index 5e76ecc..c564554 100644 --- a/client/client.go +++ b/client/client.go @@ -159,7 +159,7 @@ func (c *Client) Download(ctx context.Context, id string) (io.ReadCloser, error) 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) client := &http.Client{} @@ -194,7 +194,7 @@ func (c *Client) Upload(ctx context.Context, files ...*files.File) ([]api.Respon } defer resp.Body.Close() - var expectedResp []api.ResponseAPIFilePost + var expectedResp *api.ResponseAPIFilePost decoder := json.NewDecoder(resp.Body) if err := decoder.Decode(&expectedResp); err != nil { diff --git a/client/client_test.go b/client/client_test.go index fcaaae8..4b63369 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -120,7 +120,7 @@ func TestClient(t *testing.T) { 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 { t.Fatalf("Error getting uploaded file from store: %s", err) } diff --git a/cmd/client/actions/actions.go b/cmd/client/actions/actions.go index 67c5095..4eb43a0 100644 --- a/cmd/client/actions/actions.go +++ b/cmd/client/actions/actions.go @@ -34,7 +34,7 @@ func ActionUpload(c *cli.Context) error { errmsg := fmt.Sprintf("Error uploading file: %s", err) 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 }