From c74bfe153b81083e9ab29db5258ae50304721303 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 15 Aug 2023 16:19:37 -0400 Subject: [PATCH] Remplir dropdown admin upload documents avec types de document Utilise Config.Server.Documents.Buckets pour le bucketName et le displayName --- cmd/server.go | 83 +++++++++++++++++++++++++++--- templates/html/admin-upload.gohtml | 5 +- 2 files changed, 79 insertions(+), 9 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index 499e7ba..84a2b25 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -388,10 +388,56 @@ func handleAdmin(c echo.Context) error { } func handleAdminDocumentsUpload(c echo.Context) error { - return c.Render(http.StatusOK, "admin-upload-html", nil) + client, err := api.NewApiClientFromViper() + if err != nil { + return c.Render(http.StatusInternalServerError, "documentation-html", nil) + } + + result, err := client.Call(http.MethodGet, "/v1/bucket") + if err != nil { + return c.Render(http.StatusInternalServerError, "documentation-html", nil) + } + + var buckets map[string]string + + err = json.Unmarshal(result, &buckets) + if err != nil { + return c.Render(http.StatusInternalServerError, "documentation-html", nil) + } + + type Bucket struct { + Name string + DisplayName string + Documents []string + } + + var data struct { + Buckets []Bucket + Message string + } + + for bucketName, displayName := range buckets { + data.Buckets = append(data.Buckets, Bucket{ + Name: bucketName, + DisplayName: displayName, + }) + } + + return c.Render(http.StatusOK, "admin-upload-html", data) } func handleAdminDocumentsUploadPOST(c echo.Context) error { + type Bucket struct { + Name string + DisplayName string + Documents []string + } + + var data struct { + Buckets []Bucket + Message string + } + client, err := api.New(cfg.Server.Api.Protocol, cfg.Server.Api.Host, cfg.Server.Port, api.APIOptions{ KeyAuth: cfg.Server.Api.Auth, Key: cfg.Server.Api.Key, @@ -400,29 +446,52 @@ func handleAdminDocumentsUploadPOST(c echo.Context) error { Password: cfg.Server.Admin.Password, }) if err != nil { - return c.Render(http.StatusInternalServerError, "admin-upload-html", struct{ Message string }{Message: fmt.Sprintf("handleAdminDocumentsUploadPOST#api.New: %s", err)}) + data.Message = fmt.Sprintf("handleAdminDocumentsUploadPOST#api.New: %s", err) + return c.Render(http.StatusInternalServerError, "admin-upload-html", data) + } + + result, err := client.Call(http.MethodGet, "/v1/bucket") + if err != nil { + data.Message = "Error during GET /v1/bucket" + return c.Render(http.StatusInternalServerError, "documentation-html", data) + } + + var buckets map[string]string + + err = json.Unmarshal(result, &buckets) + if err != nil { + return c.Render(http.StatusInternalServerError, "documentation-html", nil) + } + + for bucketName, displayName := range buckets { + data.Buckets = append(data.Buckets, Bucket{ + Name: bucketName, + DisplayName: displayName, + }) } bucket := c.FormValue("bucket") document, err := c.FormFile("document") if err != nil { - return c.Render(http.StatusBadRequest, "admin-upload-html", struct{ Message string }{Message: fmt.Sprintf("handleAdminDocumentsUploadPOST#c.FormFile: %s", err)}) + data.Message = fmt.Sprintf("handleAdminDocumentsUploadPOST#c.FormFile: %s", err) + return c.Render(http.StatusBadRequest, "admin-upload-html", data) } response, err := client.UploadDocument(bucket, document) if err != nil { - return c.Render(http.StatusInternalServerError, "admin-upload-html", struct{ Message string }{Message: fmt.Sprintf("handleAdminDocumentsUploadPOST#client.UploadDocument: %s", err)}) + data.Message = fmt.Sprintf("handleAdminDocumentsUploadPOST#client.UploadDocument: %s", err) + return c.Render(http.StatusInternalServerError, "admin-upload-html", data) } // Format response - var message, info, status string + var info, status string info = fmt.Sprintf("[%.0f] /public/documentation/%s/%s", response.Info.Size, response.Info.Bucket, response.Info.Object) status = response.Message - message = fmt.Sprintf("%s - %s", status, info) + data.Message = fmt.Sprintf("%s - %s", status, info) - return c.Render(http.StatusOK, "admin-upload-html", struct{ Message string }{Message: message}) + return c.Render(http.StatusOK, "admin-upload-html", data) } diff --git a/templates/html/admin-upload.gohtml b/templates/html/admin-upload.gohtml index 3c145fb..9200b03 100644 --- a/templates/html/admin-upload.gohtml +++ b/templates/html/admin-upload.gohtml @@ -13,8 +13,9 @@
Document: