refactor: déplacer handlers bucket vers apihandler/bucket.go

This commit is contained in:
Victor Lacasse-Beaudoin 2023-11-20 15:30:08 -05:00
parent ffd8109d27
commit c40f7eb9dc
2 changed files with 91 additions and 81 deletions

View file

@ -56,87 +56,6 @@ func (h *V1Handler) HandleV1Seed(c echo.Context) error {
return c.JSON(response.StatusCode, response) return c.JSON(response.StatusCode, response)
} }
// V1BucketsGET affiche les buckets permis par server.documents.buckets, qui existent.
func (h *V1Handler) V1BucketsGET(c echo.Context) error {
var response apiresponse.V1BucketsGET
var buckets = make(map[string]string)
for bucket_name, bucket_display_name := range h.Config.Server.Documents.Buckets {
exists, err := h.MediaClient.MinioClient.BucketExists(context.Background(), bucket_name)
if err != nil {
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during minio#BucketExists"
// response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
if exists {
buckets[bucket_name] = bucket_display_name
}
}
response.StatusCode = http.StatusOK
response.Message = "Buckets list successful"
response.Data.Buckets = buckets
return c.JSON(response.StatusCode, response)
}
func (h *V1Handler) HandleV1BucketRead(c echo.Context) error {
var response apiresponse.V1BucketRead
bucket := c.Param("bucket")
allowed := false
for bucket_allowed := range h.Config.Server.Documents.Buckets {
if bucket == bucket_allowed {
allowed = true
}
}
if !allowed {
return c.JSON(apiresponse.NotFoundResponse())
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
exists, err := h.MediaClient.MinioClient.BucketExists(ctx, bucket)
if err != nil {
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during minio#BucketExists"
response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
if !exists {
return c.JSON(apiresponse.NotFoundResponse())
}
objectCh := h.MediaClient.MinioClient.ListObjects(ctx, bucket, minio.ListObjectsOptions{})
for object := range objectCh {
if object.Err != nil {
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)
}
response.Data.Keys = append(response.Data.Keys, object.Key)
}
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 // HandleV1DocumentCreate permet d'ajouter un object dans un bucket, par multipart/form-data
func (h *V1Handler) HandleV1DocumentCreate(c echo.Context) error { func (h *V1Handler) HandleV1DocumentCreate(c echo.Context) error {
var response apiresponse.V1DocumentCreate var response apiresponse.V1DocumentCreate

91
apihandler/bucket.go Normal file
View file

@ -0,0 +1,91 @@
package apihandler
import (
"context"
"net/http"
"git.agecem.com/agecem/agecem-org/apiresponse"
"github.com/labstack/echo/v4"
"github.com/minio/minio-go/v7"
)
// V1BucketsGET affiche les buckets permis par server.documents.buckets, qui existent.
func (h *V1Handler) V1BucketsGET(c echo.Context) error {
var response apiresponse.V1BucketsGET
var buckets = make(map[string]string)
for bucket_name, bucket_display_name := range h.Config.Server.Documents.Buckets {
exists, err := h.MediaClient.MinioClient.BucketExists(context.Background(), bucket_name)
if err != nil {
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during minio#BucketExists"
// response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
if exists {
buckets[bucket_name] = bucket_display_name
}
}
response.StatusCode = http.StatusOK
response.Message = "Buckets list successful"
response.Data.Buckets = buckets
return c.JSON(response.StatusCode, response)
}
func (h *V1Handler) HandleV1BucketRead(c echo.Context) error {
var response apiresponse.V1BucketRead
bucket := c.Param("bucket")
allowed := false
for bucket_allowed := range h.Config.Server.Documents.Buckets {
if bucket == bucket_allowed {
allowed = true
}
}
if !allowed {
return c.JSON(apiresponse.NotFoundResponse())
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
exists, err := h.MediaClient.MinioClient.BucketExists(ctx, bucket)
if err != nil {
response.StatusCode = http.StatusInternalServerError
response.Message = "Error during minio#BucketExists"
response.Error = err.Error()
return c.JSON(response.StatusCode, response)
}
if !exists {
return c.JSON(apiresponse.NotFoundResponse())
}
objectCh := h.MediaClient.MinioClient.ListObjects(ctx, bucket, minio.ListObjectsOptions{})
for object := range objectCh {
if object.Err != nil {
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)
}
response.Data.Keys = append(response.Data.Keys, object.Key)
}
response.StatusCode = http.StatusOK
response.Message = "V1BucketRead ok"
return c.JSON(response.StatusCode, response)
}