From 6a095ef55b59db402ed95bb922f512270aa736cd Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 24 Apr 2023 17:04:02 -0400 Subject: [PATCH 1/6] Fix typo --- cmd/server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/server.go b/cmd/server.go index 3a01df0..52dba15 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -277,7 +277,7 @@ func handleV1BucketRead(c echo.Context) error { } if !exists { - return c.JSON(http.StatusNotFound, map[string]string{"message": "Not found"}) + return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) } var keys []string From 2373dfb68122fb19664460997da02e22faeb44f3 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 24 Apr 2023 17:05:18 -0400 Subject: [PATCH 2/6] Ajouter skelette de routes CRUD documents --- cmd/server.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/cmd/server.go b/cmd/server.go index 52dba15..0f513d6 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -296,6 +296,30 @@ func handleV1BucketRead(c echo.Context) error { return c.JSON(http.StatusOK, keys) } +func handleV1DocumentCreate(c echo.Context) error { + return c.JSON(http.StatusNotImplemented, map[string]string{ + "message": "Not Implemented", + }) +} + +func handleV1DocumentRead(c echo.Context) error { + return c.JSON(http.StatusNotImplemented, map[string]string{ + "message": "Not Implemented", + }) +} + +func handleV1DocumentUpdate(c echo.Context) error { + return c.JSON(http.StatusNotImplemented, map[string]string{ + "message": "Not Implemented", + }) +} + +func handleV1DocumentDelete(c echo.Context) error { + return c.JSON(http.StatusNotImplemented, map[string]string{ + "message": "Not Implemented", + }) +} + // HTML Handlers func handleIndex(c echo.Context) error { From 902e57cb1f9291d7a1c77002c548867a60089abf Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 24 Apr 2023 17:19:52 -0400 Subject: [PATCH 3/6] Ajouter routes vers handlers skelettes documents --- cmd/server.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd/server.go b/cmd/server.go index 0f513d6..1ba060d 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -116,6 +116,14 @@ func RunServer() { groupV1.GET("/bucket/:bucket", handleV1BucketRead) + groupV1.POST("/bucket/:bucket/:document", handleV1DocumentCreate) + + groupV1.GET("/bucket/:bucket/:document", handleV1DocumentRead) + + groupV1.PUT("/bucket/:bucket/:document", handleV1DocumentUpdate) + + groupV1.DELETE("/bucket/:bucket/:document", handleV1DocumentDelete) + // Static Routes e.GET("/static/general.css", handleStaticCSSGeneral) From e21fe3be118137fa29c060ac4e7d959e1fe69a57 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 24 Apr 2023 18:29:05 -0400 Subject: [PATCH 4/6] =?UTF-8?q?Impl=C3=A9menter=20upload=20de=20document?= =?UTF-8?q?=20par=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `POST /bucket/:bucket` permet d'upload un FormFile 'file' dans le ':bucket' spécifié. Le nom de fichier sera utilisé comme descriptif alors il est de bonne pratique de donner un nom descriptif. Comme le reste du API, doit être authentifié par key-auth --- cmd/server.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index 1ba060d..5a75c0d 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -116,7 +116,7 @@ func RunServer() { groupV1.GET("/bucket/:bucket", handleV1BucketRead) - groupV1.POST("/bucket/:bucket/:document", handleV1DocumentCreate) + groupV1.POST("/bucket/:bucket", handleV1DocumentCreate) groupV1.GET("/bucket/:bucket/:document", handleV1DocumentRead) @@ -305,8 +305,59 @@ func handleV1BucketRead(c echo.Context) error { } func handleV1DocumentCreate(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.FormValue("bucket") + bucket := c.Param("bucket") + + form_file, err := c.FormFile("file") + if err != nil { + return err + } + + 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", + }) + } + + src, err := form_file.Open() + if err != nil { + return err + } + defer src.Close() + + info, err := client.PutObject(ctx, bucket, form_file.Filename, src, form_file.Size, minio.PutObjectOptions{ + ContentType: form_file.Header.Get("Content-Type"), + }) + if err != nil { + //TODO + log.Println(err) + + return c.JSON(http.StatusInternalServerError, map[string]string{ + "message": "Error during minio#PutObject", + }) + } + + return c.JSON(http.StatusOK, map[string]interface{}{ + "message": "ok", + "info": map[string]interface{}{ + "bucket": info.Bucket, + "key": info.Key, + "size": info.Size, + }, }) } From 2732d36066c78fd0d3fd933c5a34462230b59a43 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 24 Apr 2023 18:33:42 -0400 Subject: [PATCH 5/6] Cleanup handleV1DocumentCreate --- cmd/server.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index 5a75c0d..ac210fd 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -310,7 +310,6 @@ func handleV1DocumentCreate(c echo.Context) error { documents_secret_access_key := viper.GetString("server.documents.secret_access_key") documents_use_ssl := viper.GetBool("server.documents.use_ssl") - //bucket := c.FormValue("bucket") bucket := c.Param("bucket") form_file, err := c.FormFile("file") @@ -343,9 +342,6 @@ func handleV1DocumentCreate(c echo.Context) error { ContentType: form_file.Header.Get("Content-Type"), }) if err != nil { - //TODO - log.Println(err) - return c.JSON(http.StatusInternalServerError, map[string]string{ "message": "Error during minio#PutObject", }) From c4b85e77adebcc9c996442cbedac218ee5953df6 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Mon, 24 Apr 2023 18:40:17 -0400 Subject: [PATCH 6/6] Ajouter docstrings pour routes Document --- cmd/server.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/server.go b/cmd/server.go index ac210fd..c65041c 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -304,6 +304,7 @@ func handleV1BucketRead(c echo.Context) error { return c.JSON(http.StatusOK, keys) } +// handleV1DocumentCreate permet d'ajouter un object dans un bucket, par multipart/form-data func handleV1DocumentCreate(c echo.Context) error { documents_endpoint := viper.GetString("server.documents.endpoint") documents_access_key_id := viper.GetString("server.documents.access_key_id") @@ -357,18 +358,21 @@ 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", }) } +// handleV1DocumentUpdate permet de mettre à jour certains champs d'un object, comme le Content-Type ou le Filename func handleV1DocumentUpdate(c echo.Context) error { return c.JSON(http.StatusNotImplemented, map[string]string{ "message": "Not Implemented", }) } +// handleV1DocumentDelete permet de supprimer un object func handleV1DocumentDelete(c echo.Context) error { return c.JSON(http.StatusNotImplemented, map[string]string{ "message": "Not Implemented",