diff --git a/apihandler/apihandler.go b/apihandler/apihandler.go index 78c3fc2..b810cc9 100644 --- a/apihandler/apihandler.go +++ b/apihandler/apihandler.go @@ -56,87 +56,6 @@ func (h *V1Handler) HandleV1Seed(c echo.Context) error { 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 func (h *V1Handler) HandleV1DocumentCreate(c echo.Context) error { var response apiresponse.V1DocumentCreate diff --git a/apihandler/bucket.go b/apihandler/bucket.go new file mode 100644 index 0000000..d6540db --- /dev/null +++ b/apihandler/bucket.go @@ -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) +}