Merge pull request 'Ajouter et implémenter models.V1DocumentCreateResponse' (#137) from refactor/v1-document-create-response into main

Reviewed-on: #137
This commit is contained in:
Victor Lacasse-Beaudoin 2023-08-23 14:26:45 -05:00
commit 742cf9999b
2 changed files with 41 additions and 23 deletions

View file

@ -178,21 +178,26 @@ func HandleV1BucketRead(c echo.Context) error {
// HandleV1DocumentCreate permet d'ajouter un object dans un bucket, par multipart/form-data // HandleV1DocumentCreate permet d'ajouter un object dans un bucket, par multipart/form-data
func HandleV1DocumentCreate(c echo.Context) error { func HandleV1DocumentCreate(c echo.Context) error {
var response models.V1DocumentCreateResponse
var cfg config.Config var cfg config.Config
if err := viper.Unmarshal(&cfg); err != nil { if err := viper.Unmarshal(&cfg); err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{ response.StatusCode = http.StatusInternalServerError
"error": err.Error(), response.Message = "Error during HandleV1DocumentCreate's echo#Context.FormFile"
}) response.Error = err.Error()
return c.JSON(response.StatusCode, response)
} }
bucket := c.Param("bucket") bucket := c.Param("bucket")
form_file, err := c.FormFile("document") form_file, err := c.FormFile("document")
if err != nil { if err != nil {
return c.JSON(http.StatusBadRequest, map[string]interface{}{ response.StatusCode = http.StatusBadRequest
"message": "Error during HandleV1DocumentCreate's echo#Context.FormFile", response.Message = "Error during HandleV1DocumentCreate's echo#Context.FormFile"
"error": err, response.Error = err.Error()
})
return c.JSON(response.StatusCode, response)
} }
allowed := false allowed := false
@ -212,15 +217,20 @@ func HandleV1DocumentCreate(c echo.Context) error {
mediaClient, err := media.NewMediaClientFromViper() mediaClient, err := media.NewMediaClientFromViper()
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{ response.StatusCode = http.StatusInternalServerError
"message": "Error during media.NewMediaClientFromViper()", response.Message = "Error during media.NewMediaClientFromViper()"
"error": err.Error(), response.Error = err.Error()
})
return c.JSON(response.StatusCode, response)
} }
src, err := form_file.Open() src, err := form_file.Open()
if err != nil { if err != nil {
return err response.StatusCode = http.StatusBadRequest
response.Message = "Error during form_file.Open()"
response.Error = err.Error()
return c.JSON(response.StatusCode, response)
} }
defer src.Close() defer src.Close()
@ -237,19 +247,18 @@ func HandleV1DocumentCreate(c echo.Context) error {
ContentType: form_file.Header.Get("Content-Type"), ContentType: form_file.Header.Get("Content-Type"),
}) })
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{ response.StatusCode = http.StatusInternalServerError
"message": "Error during minio#PutObject", response.Message = "Error during minio#PutObject"
}) //response.Error = err.Error()
return c.JSON(response.StatusCode, response)
} }
return c.JSON(http.StatusOK, map[string]interface{}{ response.StatusCode = http.StatusOK
"message": "ok", response.Data.Bucket = info.Bucket
"info": map[string]interface{}{ response.Data.Key = info.Key
"bucket": info.Bucket, response.Data.Size = info.Size
"key": info.Key, return c.JSON(response.StatusCode, response)
"size": info.Size,
},
})
} }
// HandleV1DocumentRead permet de lire le contenu d'un fichier et protentiellement de le télécharger // HandleV1DocumentRead permet de lire le contenu d'un fichier et protentiellement de le télécharger

View file

@ -73,3 +73,12 @@ type V1BucketReadResponse struct {
Keys []string Keys []string
} }
} }
type V1DocumentCreateResponse struct {
Response
Data struct {
Bucket string
Key string
Size int64
}
}