From bcf06f0d692adb908b1089de09bce4fd16267e7f Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Sun, 18 Aug 2019 20:03:47 +0200 Subject: [PATCH] fixes #104 --- go.mod | 1 + internal/app/xhttp/pkg/context/context.go | 7 ++++++- internal/pkg/normalize/doc.go | 9 +++++++++ internal/pkg/normalize/normalize.go | 21 +++++++++++++++++++++ internal/pkg/normalize/normalize_test.go | 17 +++++++++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 internal/pkg/normalize/doc.go create mode 100644 internal/pkg/normalize/normalize.go create mode 100644 internal/pkg/normalize/normalize_test.go diff --git a/go.mod b/go.mod index a5a208f9..7fc1e345 100644 --- a/go.mod +++ b/go.mod @@ -19,4 +19,5 @@ require ( golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect golang.org/x/sync v0.0.0-20190423024810-112230192c58 golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 // indirect + golang.org/x/text v0.3.2 ) diff --git a/internal/app/xhttp/pkg/context/context.go b/internal/app/xhttp/pkg/context/context.go index 9eb5abaf..d29b9321 100644 --- a/internal/app/xhttp/pkg/context/context.go +++ b/internal/app/xhttp/pkg/context/context.go @@ -10,6 +10,7 @@ import ( "github.com/labstack/echo/v4" "github.com/thecodingmachine/gotenberg/internal/app/xhttp/pkg/resource" "github.com/thecodingmachine/gotenberg/internal/pkg/conf" + "github.com/thecodingmachine/gotenberg/internal/pkg/normalize" "github.com/thecodingmachine/gotenberg/internal/pkg/pm2" "github.com/thecodingmachine/gotenberg/internal/pkg/xerror" "github.com/thecodingmachine/gotenberg/internal/pkg/xlog" @@ -110,7 +111,11 @@ func (ctx *Context) WithResource(directoryName string) error { return r, err } defer in.Close() // nolint: errcheck - if err := r.WithFile(fh.Filename, in); err != nil { + filename, err := normalize.String(fh.Filename) + if err != nil { + return r, err + } + if err := r.WithFile(filename, in); err != nil { return r, err } } diff --git a/internal/pkg/normalize/doc.go b/internal/pkg/normalize/doc.go new file mode 100644 index 00000000..f5e6f4ab --- /dev/null +++ b/internal/pkg/normalize/doc.go @@ -0,0 +1,9 @@ +/* +Package normalize helps removing special +characters from a string. + +Fixes: https://github.com/thecodingmachine/gotenberg/issues/104 + +Credits: https://medium.com/@swdream/golang-remove-all-accents-in-string-319abf6a7f5b +*/ +package normalize diff --git a/internal/pkg/normalize/normalize.go b/internal/pkg/normalize/normalize.go new file mode 100644 index 00000000..5b58c55b --- /dev/null +++ b/internal/pkg/normalize/normalize.go @@ -0,0 +1,21 @@ +package normalize + +import ( + "unicode" + + "github.com/thecodingmachine/gotenberg/internal/pkg/xerror" + "golang.org/x/text/runes" + "golang.org/x/text/transform" + "golang.org/x/text/unicode/norm" +) + +// String normalizes given string. +func String(str string) (string, error) { + const op string = "normalize.String" + t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC) + result, _, err := transform.String(t, str) + if err != nil { + return "", xerror.New(op, err) + } + return result, nil +} diff --git a/internal/pkg/normalize/normalize_test.go b/internal/pkg/normalize/normalize_test.go new file mode 100644 index 00000000..087ad509 --- /dev/null +++ b/internal/pkg/normalize/normalize_test.go @@ -0,0 +1,17 @@ +package normalize + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestString(t *testing.T) { + const ( + toNormalize string = "analise da aplicação" + expected string = "analise da aplicacao" + ) + v, err := String(toNormalize) + assert.Nil(t, err) + assert.Equal(t, expected, v) +}