From c123119ad8b6f83701030b1c28519ae5c6dd1f96 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Wed, 26 Apr 2023 13:23:53 -0400 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9menter=20route=20API=20de=20lecture?= =?UTF-8?q?=20de=20document?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/server.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index d6074c5..9dd3a7b 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -360,9 +360,57 @@ func handleV1DocumentCreate(c echo.Context) error { // handleV1DocumentRead permet de lire le contenu d'un fichier et protentiellement de le télécharger func handleV1DocumentRead(c echo.Context) error { - return c.JSON(http.StatusNotImplemented, map[string]string{ - "message": "Not Implemented", + documents_endpoint := viper.GetString("server.documents.endpoint") + documents_access_key_id := viper.GetString("server.documents.access_key_id") + documents_secret_access_key := viper.GetString("server.documents.secret_access_key") + documents_use_ssl := viper.GetBool("server.documents.use_ssl") + + bucket := c.Param("bucket") + document := c.Param("document") + + ctx, cancel := context.WithCancel(context.Background()) + + defer cancel() + + // Initialize minio client object + client, err := minio.New(documents_endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(documents_access_key_id, documents_secret_access_key, ""), + Secure: documents_use_ssl, }) + if err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{ + "message": "Error during minio#New", + }) + } + + bucket_exists, err := client.BucketExists(ctx, bucket) + if err != nil { + return c.JSON(http.StatusInternalServerError, "Error during minio#BucketExists") + } + + if !bucket_exists { + return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + } + + document_info, err := client.StatObject(ctx, bucket, document, minio.StatObjectOptions{}) + if err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{ + "message": "Error during minio#StatObject", + }) + } + + _ = document_info + + document_object, err := client.GetObject(ctx, bucket, document, minio.GetObjectOptions{}) + if err != nil { + return c.JSON(http.StatusInternalServerError, map[string]string{ + "message": "Error during minio#GetObject", + }) + } + + defer document_object.Close() + + return c.Stream(http.StatusOK, document_info.ContentType, document_object) } // handleV1DocumentUpdate permet de mettre à jour certains champs d'un object, comme le Content-Type ou le Filename -- 2.45.2