From 97254e6fa1a64b6d4ad1a0562eaa5dec1024a8b4 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Fri, 14 Jul 2023 20:43:40 -0400 Subject: [PATCH] =?UTF-8?q?Fix=20noms=20de=20fichiers=20avec=20symboles=20?= =?UTF-8?q?sp=C3=A9ciaux?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Utiliser `url.QueryUnescape()` pour s'assurer que les noms de fichiers soient bien affichés --- api/api.go | 18 ++++++++++++++---- cmd/server.go | 42 +++++++++++++++++++----------------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/api/api.go b/api/api.go index 552d7b6..42211b6 100644 --- a/api/api.go +++ b/api/api.go @@ -9,6 +9,7 @@ import ( "io/ioutil" "mime/multipart" "net/http" + "net/url" "git.agecem.com/agecem/agecem-org/config" "github.com/spf13/viper" @@ -77,7 +78,11 @@ func (a *API) Call(method, route string) ([]byte, error) { a.Host, a.Port, ) - request := fmt.Sprintf("%s%s", endpoint, route) + prerequest := fmt.Sprintf("%s%s", endpoint, route) + request, err := url.QueryUnescape(prerequest) + if err != nil { + return nil, err + } switch method { case http.MethodGet: @@ -148,7 +153,7 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) ( a.Port, ) - url := fmt.Sprintf("%s/v1/bucket/%s", endpoint, bucket) + current_url := fmt.Sprintf("%s/v1/bucket/%s", endpoint, bucket) // Create a new multipart writer body := &bytes.Buffer{} @@ -161,7 +166,12 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) ( } defer file.Close() - part, err := writer.CreateFormFile("document", file_header.Filename) + filename_processed, err := url.QueryUnescape(file_header.Filename) + if err != nil { + return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#url.QueryUnescape: %s", err) + } + + part, err := writer.CreateFormFile("document", filename_processed) if err != nil { return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#writer.CreateFormFile: %s", err) } @@ -177,7 +187,7 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) ( } // Create a new HTTP request with the multipart body - req, err := http.NewRequest(http.MethodPost, url, body) + req, err := http.NewRequest(http.MethodPost, current_url, body) if err != nil { return UploadDocumentResponse{}, fmt.Errorf("UploadDocument#http.NewRequest: %s", err) } diff --git a/cmd/server.go b/cmd/server.go index 8c0f41a..cef05fc 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -9,7 +9,6 @@ import ( "encoding/json" "fmt" "log" - "regexp" "embed" "html/template" @@ -417,14 +416,16 @@ func handleV1DocumentCreate(c echo.Context) error { } defer src.Close() - reg, err := regexp.Compile("[^.a-zA-Z0-9_-]+") - if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-html", nil) - } + /* + reg, err := regexp.Compile("[^.a-zA-Z0-9_-]+") + if err != nil { + return c.Render(http.StatusInternalServerError, "documentation-html", nil) + } - filename_processed := reg.ReplaceAllString(form_file.Filename, "") + filename_processed := reg.ReplaceAllString(form_file.Filename, "") + */ - info, err := mediaClient.MinioClient.PutObject(ctx, bucket, filename_processed, src, form_file.Size, minio.PutObjectOptions{ + info, err := mediaClient.MinioClient.PutObject(ctx, bucket, form_file.Filename, src, form_file.Size, minio.PutObjectOptions{ ContentType: form_file.Header.Get("Content-Type"), }) if err != nil { @@ -456,11 +457,6 @@ func handleV1DocumentRead(c echo.Context) error { } if !allowed { - /* - return c.JSON(http.StatusBadRequest, map[string]string{ - "message": "Bucket is not allowed in server.documents.buckets", - }) - */ return c.JSON(http.StatusNotFound, map[string]string{"message": "Not Found"}) } @@ -658,23 +654,23 @@ func handleDocumentation(c echo.Context) error { // N'est pas présentement activé, car les fichiers sont processed // à la création de toute façon. /* - reg, err := regexp.Compile("[^.a-zA-Z0-9_-]+") - if err != nil { - return c.Render(http.StatusInternalServerError, "documentation-html", nil) - } + reg, err := regexp.Compile("[^.a-zA-Z0-9_-]+") + if err != nil { + return c.Render(http.StatusInternalServerError, "documentation-html", nil) + } - var documents_processed []string + var documents_processed []string - for _, document := range documents { - document_processed := reg.ReplaceAllString(document, "") - documents_processed = append(documents_processed, document_processed) - } + for _, document := range documents { + document_processed := reg.ReplaceAllString(document, "") + documents_processed = append(documents_processed, document_processed) + } + documents_processed := documents */ - documents_processed := documents data = append(data, Bucket{ Name: bucket, - Documents: documents_processed, + Documents: documents, }) } -- 2.45.2