package apihandler import ( "context" "net/http" "codeberg.org/vlbeaudoin/voki/v3" "git.agecem.com/agecem/agecem-org/apirequest" "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 request apirequest.V1BucketsGET var response apiresponse.V1BucketsGET if !request.Complete() { response.Message = "Incomplete V1BucketsGET request received" response.SetStatusCode(http.StatusBadRequest) return c.JSON(response.StatusCode(), response) } 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.Message = "Error during minio#BucketExists" response.SetStatusCode(http.StatusInternalServerError) return c.JSON(response.StatusCode(), response) } if exists { buckets[bucket_name] = bucket_display_name } } response.SetStatusCode(http.StatusOK) response.Message = "Buckets list successful" response.Data.Buckets = buckets return c.JSON(response.StatusCode(), response) } func (h *V1Handler) V1BucketGET(c echo.Context) error { var request apirequest.V1BucketGET var response apiresponse.V1BucketGET request.Params.Bucket = c.Param("bucket") if !request.Complete() { response.Message = "Incomplete V1BucketGET request received" response.SetStatusCode(http.StatusBadRequest) return c.JSON(response.StatusCode(), response) } allowed := false for bucket_allowed := range h.Config.Server.Documents.Buckets { if request.Params.Bucket == bucket_allowed { allowed = true } } if !allowed { response := voki.ResponseNotFound{} return c.JSON(response.StatusCode(), response) } ctx, cancel := context.WithCancel(context.Background()) defer cancel() exists, err := h.MediaClient.MinioClient.BucketExists(ctx, request.Params.Bucket) if err != nil { response.Message = "Error during minio#BucketExists" response.SetStatusCode(http.StatusInternalServerError) response.Error = err.Error() return c.JSON(response.StatusCode(), response) } if !exists { response := voki.ResponseNotFound{} return c.JSON(response.StatusCode(), response) } objectCh := h.MediaClient.MinioClient.ListObjects(ctx, request.Params.Bucket, minio.ListObjectsOptions{}) for object := range objectCh { if object.Err != nil { response.Message = "Error during minio#ListObjects" response.SetStatusCode(http.StatusInternalServerError) //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.Message = "V1BucketRead ok" response.SetStatusCode(http.StatusOK) return c.JSON(response.StatusCode(), response) }