This commit is contained in:
Julien Neuhart
2019-08-18 20:03:47 +02:00
parent 4f3fe28905
commit bcf06f0d69
5 changed files with 54 additions and 1 deletions

1
go.mod
View File

@@ -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
)

View File

@@ -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
}
}

View File

@@ -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

View File

@@ -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
}

View File

@@ -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)
}