From 48a8c8a37a25fb2dfeeee0e657e5761c0e47dca0 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Wed, 23 Aug 2023 16:01:57 -0400 Subject: [PATCH] =?UTF-8?q?Fix=20impl=C3=A9mentations=20de=20UploadDocumen?= =?UTF-8?q?t=20et=20models.NotFoundResponse()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/api.go | 4 ++-- api_handlers/api_handlers.go | 19 ++++++++++--------- models/responses.go | 4 ++-- web_handlers/web_handlers.go | 6 +++--- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/api/api.go b/api/api.go index f70d2bf..6ba2d36 100644 --- a/api/api.go +++ b/api/api.go @@ -138,8 +138,8 @@ func (a *API) Call(method, route string) ([]byte, error) { return nil, errors.New(fmt.Sprintf("method must be 'GET' or 'DELETE', got '%s'", method)) } -func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (models.UploadDocumentResponse, error) { - var response models.UploadDocumentResponse +func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (models.V1DocumentCreateResponse, error) { + var response models.V1DocumentCreateResponse endpoint := fmt.Sprintf("%s://%s:%d", a.Protocol, a.Host, diff --git a/api_handlers/api_handlers.go b/api_handlers/api_handlers.go index 1e8e232..9423f31 100644 --- a/api_handlers/api_handlers.go +++ b/api_handlers/api_handlers.go @@ -127,7 +127,7 @@ func HandleV1BucketRead(c echo.Context) error { } if !allowed { - return c.JSON(http.StatusNotFound, models.NotFoundResponse()) + return c.JSON(models.NotFoundResponse()) } ctx, cancel := context.WithCancel(context.Background()) @@ -153,7 +153,7 @@ func HandleV1BucketRead(c echo.Context) error { } if !exists { - return c.JSON(http.StatusNotFound, models.NotFoundResponse()) + return c.JSON(models.NotFoundResponse()) } objectCh := mediaClient.MinioClient.ListObjects(ctx, bucket, minio.ListObjectsOptions{}) @@ -208,7 +208,7 @@ func HandleV1DocumentCreate(c echo.Context) error { } if !allowed { - return c.JSON(http.StatusNotFound, models.NotFoundResponse()) + return c.JSON(models.NotFoundResponse()) } ctx, cancel := context.WithCancel(context.Background()) @@ -255,6 +255,7 @@ func HandleV1DocumentCreate(c echo.Context) error { } response.StatusCode = http.StatusOK + response.Message = "ok" response.Data.Bucket = info.Bucket response.Data.Key = info.Key response.Data.Size = info.Size @@ -282,7 +283,7 @@ func HandleV1DocumentRead(c echo.Context) error { } if !allowed { - return c.JSON(http.StatusNotFound, models.NotFoundResponse()) + return c.JSON(models.NotFoundResponse()) } ctx, cancel := context.WithCancel(context.Background()) @@ -303,7 +304,7 @@ func HandleV1DocumentRead(c echo.Context) error { } if !bucket_exists { - return c.JSON(http.StatusNotFound, models.NotFoundResponse()) + return c.JSON(models.NotFoundResponse()) } document_info, err := mediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{}) @@ -311,7 +312,7 @@ func HandleV1DocumentRead(c echo.Context) error { if err != nil { if err.Error() == "The specified key does not exist." { - return c.JSON(http.StatusNotFound, models.NotFoundResponse()) + return c.JSON(models.NotFoundResponse()) } return c.JSON(http.StatusInternalServerError, map[string]interface{}{ @@ -357,7 +358,7 @@ func HandleV1DocumentDelete(c echo.Context) error { } if !allowed { - return c.JSON(http.StatusNotFound, models.NotFoundResponse()) + return c.JSON(models.NotFoundResponse()) } ctx, cancel := context.WithCancel(context.Background()) @@ -378,14 +379,14 @@ func HandleV1DocumentDelete(c echo.Context) error { } if !bucket_exists { - return c.JSON(http.StatusNotFound, models.NotFoundResponse()) + return c.JSON(models.NotFoundResponse()) } document_info, err := mediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{}) if err != nil { if err.Error() == "The specified key does not exist." { - return c.JSON(http.StatusNotFound, models.NotFoundResponse()) + return c.JSON(models.NotFoundResponse()) } return c.JSON(http.StatusInternalServerError, map[string]interface{}{ diff --git a/models/responses.go b/models/responses.go index 6890d37..dd72936 100644 --- a/models/responses.go +++ b/models/responses.go @@ -24,8 +24,8 @@ func (r SimpleResponse) Respond() Responder { return r } -func NotFoundResponse() SimpleResponse { - return SimpleResponse{ +func NotFoundResponse() (int, SimpleResponse) { + return http.StatusNotFound, SimpleResponse{ Message: "Not Found", } } diff --git a/web_handlers/web_handlers.go b/web_handlers/web_handlers.go index e61902e..de19abd 100644 --- a/web_handlers/web_handlers.go +++ b/web_handlers/web_handlers.go @@ -130,7 +130,7 @@ func HandleFormulaires(c echo.Context) error { func HandlePublicDocumentation(c echo.Context) error { client, err := api.NewApiClientFromViper() if err != nil { - return c.JSON(http.StatusNotFound, models.NotFoundResponse()) + return c.JSON(models.NotFoundResponse()) } bucket := c.Param("bucket") @@ -138,7 +138,7 @@ func HandlePublicDocumentation(c echo.Context) error { result, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s/%s", bucket, document)) if err != nil { - return c.JSON(http.StatusNotFound, models.NotFoundResponse()) + return c.JSON(models.NotFoundResponse()) } // Check if result can fit inside a map containing a message @@ -259,7 +259,7 @@ func HandleAdminDocumentsUploadPOST(c echo.Context) error { // Format response var info, status string - info = fmt.Sprintf("[%.0f] /public/documentation/%s/%s", uploadDocumentResponse.Data.Size, uploadDocumentResponse.Data.Bucket, uploadDocumentResponse.Data.Object) + info = fmt.Sprintf("[%d] /public/documentation/%s/%s", uploadDocumentResponse.Data.Size, uploadDocumentResponse.Data.Bucket, uploadDocumentResponse.Data.Key) status = uploadDocumentResponse.Message -- 2.45.2