Implémenter models.V1BucketReadResponse

This commit is contained in:
Victor Lacasse-Beaudoin 2023-08-22 15:08:10 -04:00
parent cd8c2d4955
commit a6ba62fd62
3 changed files with 44 additions and 23 deletions

View file

@ -107,11 +107,14 @@ func HandleV1BucketList(c echo.Context) error {
}
func HandleV1BucketRead(c echo.Context) error {
var response models.V1BucketReadResponse
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.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
bucket := c.Param("bucket")
@ -124,10 +127,11 @@ func HandleV1BucketRead(c echo.Context) error {
}
if !allowed {
/*
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "Bucket is not allowed in server.documents.buckets",
})
/* TODO models.NotFoundResponse
response.StatusCode = http.StatusNotFound
response.Message = "Not Found"
return c.JSON(response.StatusCode, response)
*/
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
}
@ -138,35 +142,45 @@ func HandleV1BucketRead(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)
}
exists, err := mediaClient.MinioClient.BucketExists(ctx, bucket)
if err != nil {
return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists")
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during minio#BucketExists"
response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
if !exists {
// TODO models.NotFoundResponse
return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"})
}
var keys []string
objectCh := mediaClient.MinioClient.ListObjects(ctx, bucket, minio.ListObjectsOptions{})
for object := range objectCh {
if object.Err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Error during minio#ListObjects",
})
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during minio#ListObjects"
//TODO make sure this is safe
//response.Error = object.Err.Error()
return c.JSON(response.StatusCode, response)
}
keys = append(keys, object.Key)
response.Data.Keys = append(response.Data.Keys, object.Key)
}
return c.JSON(http.StatusOK, keys)
response.StatusCode = http.StatusOK
response.Message = "V1BucketRead ok"
return c.JSON(response.StatusCode, response)
}
// HandleV1DocumentCreate permet d'ajouter un object dans un bucket, par multipart/form-data

View file

@ -52,3 +52,10 @@ type V1BucketListResponse struct {
Buckets map[string]string
}
}
type V1BucketReadResponse struct {
Response
Data struct {
Keys []string
}
}

View file

@ -68,7 +68,6 @@ func HandleDocumentation(c echo.Context) error {
for bucket, displayName := range v1BucketListResponse.Data.Buckets {
// TODO move call to dedicated API client method
// TODO add Response type
content, err := client.Call(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket))
if err != nil {
response.StatusCode = http.StatusInternalServerError
@ -78,9 +77,9 @@ func HandleDocumentation(c echo.Context) error {
return c.Render(response.StatusCode, "documentation-html", response)
}
var documents []string
var v1BucketReadResponse models.V1BucketReadResponse
err = json.Unmarshal(content, &documents)
err = json.Unmarshal(content, &v1BucketReadResponse)
if err != nil {
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during json.Unmarshal /v1/bucket/:bucket"
@ -110,13 +109,14 @@ func HandleDocumentation(c echo.Context) error {
response.Data.Buckets = append(response.Data.Buckets, models.Bucket{
Name: bucket,
DisplayName: displayName,
Documents: documents,
Documents: v1BucketReadResponse.Data.Keys,
})
}
sort.SliceStable(response.Data.Buckets, func(i, j int) bool { return response.Data.Buckets[i].Name < response.Data.Buckets[j].Name })
response.StatusCode = http.StatusOK
//response.Message = "HandleDocumentation ok"
// TODO render .Message
return c.Render(response.StatusCode, "documentation-html", response)