From 5d984ccacb977997f7df8b50e2362ddfa838fde9 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 28 Apr 2023 17:30:08 -0400 Subject: [PATCH] =?UTF-8?q?Retirer=20JSON=20de=20form=20cr=C3=A9ation=20de?= =?UTF-8?q?=20document?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Utiliser context#Render pour afficher une string Message à l'écran. --- api/api.go | 16 ++++++++-------- cmd/server.go | 36 +++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/api/api.go b/api/api.go index 6cbe09d..2de03f8 100644 --- a/api/api.go +++ b/api/api.go @@ -109,7 +109,7 @@ func (a *API) Call(method, route string) ([]byte, error) { return nil, errors.New(fmt.Sprintf("method must be 'GET' or 'DELETE', got '%s'", method)) } -func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (string, error) { +func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (map[string]interface{}, error) { endpoint := fmt.Sprintf("%s://%s:%d", a.Protocol, a.Host, @@ -125,29 +125,29 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) ( // Add the file to the request file, err := file_header.Open() if err != nil { - return "", fmt.Errorf("UploadDocument#file_header.Open: %s", err) + return nil, fmt.Errorf("UploadDocument#file_header.Open: %s", err) } defer file.Close() part, err := writer.CreateFormFile("document", file_header.Filename) if err != nil { - return "", fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err) + return nil, fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err) } _, err = io.Copy(part, file) if err != nil { - return "", fmt.Errorf("UploadDocument#io.Copy: %s", err) + return nil, fmt.Errorf("UploadDocument#io.Copy: %s", err) } err = writer.Close() if err != nil { - return "", fmt.Errorf("UploadDocument#writer.Close: %s", err) + return nil, fmt.Errorf("UploadDocument#writer.Close: %s", err) } // Create a new HTTP request with the multipart body req, err := http.NewRequest(http.MethodPost, url, body) if err != nil { - return "", fmt.Errorf("UploadDocument#http.NewRequest: %s", err) + return nil, fmt.Errorf("UploadDocument#http.NewRequest: %s", err) } req.Header.Set("Content-Type", writer.FormDataContentType()) @@ -160,7 +160,7 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) ( client := &http.Client{} resp, err := client.Do(req) if err != nil { - return "", fmt.Errorf("UploadDocument#client.Do: %s", err) + return nil, fmt.Errorf("UploadDocument#client.Do: %s", err) } defer resp.Body.Close() @@ -168,7 +168,7 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) ( var res map[string]interface{} json.NewDecoder(resp.Body).Decode(&res) - return fmt.Sprintf("%s", res), nil + return res, nil } // CallWithData takes data and returns a string representing a response body. diff --git a/cmd/server.go b/cmd/server.go index b0d55eb..d3f0ca8 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -741,33 +741,39 @@ func handleAdminDocumentsUploadPOST(c echo.Context) error { Password: viper.GetString("server.admin.password"), }) if err != nil { - return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + return c.Render(http.StatusInternalServerError, "admin-upload-html", struct{ Message string }{Message: fmt.Sprintf("handleAdminDocumentsUploadPOST#api.New: %s", err)}) } bucket := c.FormValue("bucket") document, err := c.FormFile("document") if err != nil { - //return c.JSON(http.StatusBadRequest, map[string]string{"message": "Error during file parse"}) - return c.JSON(http.StatusNotFound, map[string]string{"message": "Error during echo#Context.FormFile", "error": err.Error()}) + return c.Render(http.StatusBadRequest, "admin-upload-html", struct{ Message string }{Message: fmt.Sprintf("handleAdminDocumentsUploadPOST#c.FormFile: %s", err)}) } - /* - file, err := document.Open() - if err != nil { - //return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) - return c.JSON(http.StatusNotFound, map[string]string{"message": "Error during file.Open()", "error": err.Error()}) - } - defer file.Close() - */ - response, err := client.UploadDocument(bucket, document) if err != nil { - return c.JSON(http.StatusNotFound, map[string]string{"message": "Error duing api#client.UploadDocument", "error": err.Error()}) - //return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) + return c.Render(http.StatusInternalServerError, "admin-upload-html", struct{ Message string }{Message: fmt.Sprintf("handleAdminDocumentsUploadPOST#client.UploadDocument: %s", err)}) } - return c.Render(http.StatusOK, "admin-upload-html", struct{ Message string }{Message: response}) + // Format response + var message, info, status string + + for key, value := range response { + switch key { + case "info": + info_map, ok := value.(map[string]interface{}) + if ok { + info = fmt.Sprintf("/public/documentation/%s/%s [%.2f]", info_map["bucket"], info_map["key"], info_map["size"]) + } + case "message": + status = fmt.Sprint(value) + } + } + + message = fmt.Sprintf("%s - %s", status, info) + + return c.Render(http.StatusOK, "admin-upload-html", struct{ Message string }{Message: message}) } // CSS Handlers