Fix implémentations de UploadDocument et models.NotFoundResponse() #140

Merged
vlbeaudoin merged 1 commit from fix/not-found-and-v1-document-create-response into main 2023-08-23 16:03:01 -04:00
4 changed files with 17 additions and 16 deletions

View file

@ -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)) 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) { func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (models.V1DocumentCreateResponse, error) {
var response models.UploadDocumentResponse var response models.V1DocumentCreateResponse
endpoint := fmt.Sprintf("%s://%s:%d", endpoint := fmt.Sprintf("%s://%s:%d",
a.Protocol, a.Protocol,
a.Host, a.Host,

View file

@ -127,7 +127,7 @@ func HandleV1BucketRead(c echo.Context) error {
} }
if !allowed { if !allowed {
return c.JSON(http.StatusNotFound, models.NotFoundResponse()) return c.JSON(models.NotFoundResponse())
} }
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
@ -153,7 +153,7 @@ func HandleV1BucketRead(c echo.Context) error {
} }
if !exists { if !exists {
return c.JSON(http.StatusNotFound, models.NotFoundResponse()) return c.JSON(models.NotFoundResponse())
} }
objectCh := mediaClient.MinioClient.ListObjects(ctx, bucket, minio.ListObjectsOptions{}) objectCh := mediaClient.MinioClient.ListObjects(ctx, bucket, minio.ListObjectsOptions{})
@ -208,7 +208,7 @@ func HandleV1DocumentCreate(c echo.Context) error {
} }
if !allowed { if !allowed {
return c.JSON(http.StatusNotFound, models.NotFoundResponse()) return c.JSON(models.NotFoundResponse())
} }
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
@ -255,6 +255,7 @@ func HandleV1DocumentCreate(c echo.Context) error {
} }
response.StatusCode = http.StatusOK response.StatusCode = http.StatusOK
response.Message = "ok"
response.Data.Bucket = info.Bucket response.Data.Bucket = info.Bucket
response.Data.Key = info.Key response.Data.Key = info.Key
response.Data.Size = info.Size response.Data.Size = info.Size
@ -282,7 +283,7 @@ func HandleV1DocumentRead(c echo.Context) error {
} }
if !allowed { if !allowed {
return c.JSON(http.StatusNotFound, models.NotFoundResponse()) return c.JSON(models.NotFoundResponse())
} }
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
@ -303,7 +304,7 @@ func HandleV1DocumentRead(c echo.Context) error {
} }
if !bucket_exists { 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{}) 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 != nil {
if err.Error() == "The specified key does not exist." { 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{}{ return c.JSON(http.StatusInternalServerError, map[string]interface{}{
@ -357,7 +358,7 @@ func HandleV1DocumentDelete(c echo.Context) error {
} }
if !allowed { if !allowed {
return c.JSON(http.StatusNotFound, models.NotFoundResponse()) return c.JSON(models.NotFoundResponse())
} }
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
@ -378,14 +379,14 @@ func HandleV1DocumentDelete(c echo.Context) error {
} }
if !bucket_exists { 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{}) document_info, err := mediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{})
if err != nil { if err != nil {
if err.Error() == "The specified key does not exist." { 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{}{ return c.JSON(http.StatusInternalServerError, map[string]interface{}{

View file

@ -24,8 +24,8 @@ func (r SimpleResponse) Respond() Responder {
return r return r
} }
func NotFoundResponse() SimpleResponse { func NotFoundResponse() (int, SimpleResponse) {
return SimpleResponse{ return http.StatusNotFound, SimpleResponse{
Message: "Not Found", Message: "Not Found",
} }
} }

View file

@ -130,7 +130,7 @@ func HandleFormulaires(c echo.Context) error {
func HandlePublicDocumentation(c echo.Context) error { func HandlePublicDocumentation(c echo.Context) error {
client, err := api.NewApiClientFromViper() client, err := api.NewApiClientFromViper()
if err != nil { if err != nil {
return c.JSON(http.StatusNotFound, models.NotFoundResponse()) return c.JSON(models.NotFoundResponse())
} }
bucket := c.Param("bucket") 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)) result, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s/%s", bucket, document))
if err != nil { 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 // Check if result can fit inside a map containing a message
@ -259,7 +259,7 @@ func HandleAdminDocumentsUploadPOST(c echo.Context) error {
// Format response // Format response
var info, status string 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 status = uploadDocumentResponse.Message