From f46dc54aafcb648e557b15c1d3ba032c67d78055 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Tue, 19 Dec 2023 19:44:25 -0500 Subject: [PATCH] =?UTF-8?q?feat(web):=20permettre=20=C3=A0=20`admin-upload?= =?UTF-8?q?`=20de=20t=C3=A9l=C3=A9verser=20plusieurs=20fichiers=20=C3=A0?= =?UTF-8?q?=20la=20fois?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Seuls les fichiers pdf, markdown et plain text sont permis. --- templates/html/admin-upload.html | 7 +++- webhandler/webhandler.go | 66 +++++++++++++++++++++++++++----- 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/templates/html/admin-upload.html b/templates/html/admin-upload.html index aee3a6c..11b45a7 100644 --- a/templates/html/admin-upload.html +++ b/templates/html/admin-upload.html @@ -15,7 +15,12 @@

Document à téléverser

- +
diff --git a/webhandler/webhandler.go b/webhandler/webhandler.go index 6d945c7..bf6ba3b 100644 --- a/webhandler/webhandler.go +++ b/webhandler/webhandler.go @@ -8,6 +8,7 @@ import ( "sort" "git.agecem.com/agecem/agecem-org/api" + "git.agecem.com/agecem/agecem-org/apirequest" "git.agecem.com/agecem/agecem-org/apiresponse" "git.agecem.com/agecem/agecem-org/models" "git.agecem.com/agecem/agecem-org/webresponse" @@ -155,6 +156,7 @@ func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error { } func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error { + var request apirequest.V1DocumentsPOST var response webresponse.HandleAdminDocumentsUploadResponse v1BucketsGET, err := h.ApiClient.ListBuckets() @@ -173,35 +175,79 @@ func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error { }) } - bucket := c.FormValue("bucket") + request.Params.Bucket = c.FormValue("bucket") - document, err := c.FormFile("document") + form, err := c.MultipartForm() if err != nil { response.StatusCode = http.StatusBadRequest - response.Message = "Formulaire invalide" + response.Message = "Impossible de téléverser" response.Error = err.Error() return c.Render(response.StatusCode, "admin-upload-html", response) } + if form == nil { + response.StatusCode = http.StatusInternalServerError + response.Message = "Formulaire pointe vers une addresse mémoire nulle" + response.Error = "Formulaire pointe vers une addresse mémoire nulle" - uploadDocumentResponse, err := h.ApiClient.UploadDocument(bucket, document) + return c.Render(response.StatusCode, "admin-upload-html", response) + } + + if len(form.File) == 0 { + response.StatusCode = http.StatusBadRequest + response.Message = "Veuillez sélectionner au moins 1 fichier à téléverser" + response.Error = "Input 'documents' ne contient aucun fichier" + + return c.Render(response.StatusCode, "admin-upload-html", response) + } + + for inputName, inputFileHeaders := range form.File { + if inputName == "documents" { + request.Data.Documents = inputFileHeaders + break + } + } + + if request.Data.Documents == nil { + response.StatusCode = http.StatusBadRequest + response.Message = "Impossible d'obtenir les documents depuis le formulaire" + response.Error = "Impossible d'obtenir les documents depuis le formulaire" + + return c.Render(response.StatusCode, "admin-upload-html", response) + } + + uploadDocumentsResponse, err := h.ApiClient.UploadDocuments(request.Params.Bucket, request.Data.Documents...) if err != nil { - response.StatusCode = uploadDocumentResponse.StatusCode - response.Message = uploadDocumentResponse.Message - response.Error = err.Error() + //TODO figure out pourquoi `err` n'est jamais `nil` + response.StatusCode = uploadDocumentsResponse.StatusCode + response.Message = uploadDocumentsResponse.Message + response.Error = fmt.Sprintf("%s. Détails: %s", err.Error(), uploadDocumentsResponse.Error) + /* + response.StatusCode = http.StatusInternalServerError + response.Message = fmt.Sprintf("api.(*API).UploadDocuments: %s", err) + response.Error = err.Error() + */ return c.Render(response.StatusCode, "admin-upload-html", response) } + //TODO figure out pourquoi on se rend jamais ici + // Format response var info, status string - info = fmt.Sprintf("[%d] /public/documentation/%s/%s", uploadDocumentResponse.Data.Size, uploadDocumentResponse.Data.Bucket, uploadDocumentResponse.Data.Key) + for i, document := range uploadDocumentsResponse.Data.Documents { + info = fmt.Sprintf("%s[%d] /public/documentation/%s/%s (%dk) ok\n", + info, i, uploadDocumentsResponse.Data.Bucket, document.Key, document.Size) + } - status = uploadDocumentResponse.Message + status = uploadDocumentsResponse.Message + if errMsg := uploadDocumentsResponse.Error; errMsg != "" { + status = fmt.Sprintf("%s. Erreur: %s", status, errMsg) + } response.StatusCode = http.StatusOK - response.Message = fmt.Sprintf("%s - %s", status, info) + response.Message = fmt.Sprintf("%s \n %s", status, info) return c.Render(response.StatusCode, "admin-upload-html", response) }