Skip to content

Commit 59abe62

Browse files
committed
improve filename sanitization using a library
1 parent bf53f1c commit 59abe62

4 files changed

Lines changed: 32 additions & 10 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
output
1+
test

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module sourcerer
33
go 1.17
44

55
require (
6-
github.com/golang-demos/chalk v1.1.1
7-
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd
6+
github.com/golang-demos/chalk v1.1.3
7+
golang.org/x/net v0.7.0
88
)
9+
10+
require github.com/flytam/filenamify v1.1.2

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1+
github.com/flytam/filenamify v1.1.2 h1:dGlfWU4zrhDlsmvob4IFcfgjG5vIjfo4UwLyec6Wx94=
2+
github.com/flytam/filenamify v1.1.2/go.mod h1:Dzf9kVycwcsBlr2ATg6uxjqiFgKGH+5SKFuhdeP5zu8=
13
github.com/golang-demos/chalk v1.1.1 h1:5TzT+8d8jqmTDhZhCBM2pGKeZ0j8HJtuUzBoYVLg518=
24
github.com/golang-demos/chalk v1.1.1/go.mod h1:6oOi3j1Ghmt/Vnfr/xTkVRwOt3tx+oU/aQvIG0gbQHA=
5+
github.com/golang-demos/chalk v1.1.3 h1:1WV1SHjKQ4C14z5dkBxwK17eBX8tSrMy7ljHy4G9o8c=
6+
github.com/golang-demos/chalk v1.1.3/go.mod h1:6oOi3j1Ghmt/Vnfr/xTkVRwOt3tx+oU/aQvIG0gbQHA=
37
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
48
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
9+
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
10+
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
11+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12+
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
14+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
15+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

sourceMap.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"os"
77
"path/filepath"
88
"strings"
9+
10+
"github.com/flytam/filenamify"
911
)
1012

1113
type SourceMap struct {
@@ -37,13 +39,20 @@ func fromSourceMap(ctx Context, res *http.Response) {
3739
source = source[:queryIndex]
3840
}
3941

42+
hashIndex := strings.Index(source, "?")
43+
if hashIndex != -1 {
44+
source = source[:hashIndex]
45+
}
46+
4047
// remove reserved characters
41-
// https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
42-
source = strings.ReplaceAll(source, "<", "")
43-
source = strings.ReplaceAll(source, ">", "")
44-
source = strings.ReplaceAll(source, ":", "")
45-
source = strings.ReplaceAll(source, "\"", "")
46-
source = strings.ReplaceAll(source, "<", "")
48+
source, err := filenamify.FilenamifyV2(source, func(options *filenamify.Options) {
49+
options.Replacement = "_"
50+
})
51+
52+
if err != nil {
53+
error(ctx.Depth, "Failed to filenamify source:", err)
54+
continue
55+
}
4756

4857
path := filepath.Join(filepath.Clean(ctx.Dir), source)
4958
dir := filepath.Dir(path)
@@ -59,4 +68,4 @@ func fromSourceMap(ctx Context, res *http.Response) {
5968
continue
6069
}
6170
}
62-
}
71+
}

0 commit comments

Comments
 (0)