Ajouter et implémenter models.V1DocumentCreateResponse

This commit is contained in:
Victor Lacasse-Beaudoin 2023-08-23 15:26:01 -04:00
parent 89291a24ba
commit 5612b593e2
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
func HandleV1DocumentCreate(c echo.Context) error {
var response models.V1DocumentCreateResponse
var cfg config.Config
if err := viper.Unmarshal(&cfg); err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"error": err.Error(),
})
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during HandleV1DocumentCreate's echo#Context.FormFile"
response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
bucket := c.Param("bucket")
form_file, err := c.FormFile("document")
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": "Error during HandleV1DocumentCreate's echo#Context.FormFile",
"error": err,
})
response.StatusCode = http.StatusBadRequest
response.Message = "Error during HandleV1DocumentCreate's echo#Context.FormFile"
response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
allowed := false
@ -212,15 +217,20 @@ func HandleV1DocumentCreate(c echo.Context) error {
mediaClient, err := media.NewMediaClientFromViper()
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Error during media.NewMediaClientFromViper()",
"error": err.Error(),
})
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during media.NewMediaClientFromViper()"
response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
src, err := form_file.Open()
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()
@ -237,19 +247,18 @@ func HandleV1DocumentCreate(c echo.Context) error {
ContentType: form_file.Header.Get("Content-Type"),
})
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Error during minio#PutObject",
})
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during minio#PutObject"
//response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
return c.JSON(http.StatusOK, map[string]interface{}{
"message": "ok",
"info": map[string]interface{}{
"bucket": info.Bucket,
"key": info.Key,
"size": info.Size,
},
})
response.StatusCode = http.StatusOK
response.Data.Bucket = info.Bucket
response.Data.Key = info.Key
response.Data.Size = info.Size
return c.JSON(response.StatusCode, response)
}
// 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
}
}
type V1DocumentCreateResponse struct {
Response
Data struct {
Bucket string
Key string
Size int64
}
}