1
0

Sanizize mailbox name for exporting

This commit is contained in:
Michal Horejsek
2020-10-16 08:33:52 +02:00
parent cfd8e56277
commit 26fb1fc34d
5 changed files with 50 additions and 3 deletions

View File

@ -24,6 +24,7 @@ import (
"net/mail"
"net/textproto"
"path/filepath"
"runtime"
"sort"
"strings"
@ -139,3 +140,24 @@ func getMessageHeader(body []byte) (mail.Header, error) {
}
return mail.Header(header), nil
}
// sanitizeFileName replaces problematic special characters with underscore.
func sanitizeFileName(fileName string) string {
if len(fileName) == 0 {
return fileName
}
if runtime.GOOS != "windows" && (fileName[0] == '-' || fileName[0] == '.') { //nolint[goconst]
fileName = "_" + fileName[1:]
}
return strings.Map(func(r rune) rune {
switch r {
case '\\', '/', ':', '*', '?', '"', '<', '>', '|':
return '_'
case '[', ']', '(', ')', '{', '}', '^', '#', '%', '&', '!', '@', '+', '=', '\'', '~':
if runtime.GOOS != "windows" {
return '_'
}
}
return r
}, fileName)
}