|
|
@ -12,6 +12,7 @@ import ( |
|
|
|
"runtime" |
|
|
|
"runtime" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
"time" |
|
|
|
|
|
|
|
"unicode/utf8" |
|
|
|
|
|
|
|
|
|
|
|
"golang.org/x/net/html/charset" |
|
|
|
"golang.org/x/net/html/charset" |
|
|
|
"golang.org/x/text/transform" |
|
|
|
"golang.org/x/text/transform" |
|
|
@ -130,20 +131,19 @@ func Sha1(str string) string { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func ToUtf8WithErr(content []byte) (error, string) { |
|
|
|
func ToUtf8WithErr(content []byte) (error, string) { |
|
|
|
charsetLabel := base.DetectEncoding(content) |
|
|
|
if utf8.Valid(content[:1024]) { |
|
|
|
if charsetLabel == "UTF-8" { |
|
|
|
|
|
|
|
return nil, string(content) |
|
|
|
return nil, string(content) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
charsetLabel := base.DetectEncoding(content) |
|
|
|
encoding, _ := charset.Lookup(charsetLabel) |
|
|
|
encoding, _ := charset.Lookup(charsetLabel) |
|
|
|
if encoding == nil { |
|
|
|
if encoding == nil { |
|
|
|
return fmt.Errorf("unknown char decoder %s", charsetLabel), string(content) |
|
|
|
return fmt.Errorf("Unknown encoding: %s", charsetLabel), string(content) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
result, n, err := transform.String(encoding.NewDecoder(), string(content)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If there is an error, we concatenate the nicely decoded part and the
|
|
|
|
// If there is an error, we concatenate the nicely decoded part and the
|
|
|
|
// original left over. This way we won't loose data.
|
|
|
|
// original left over. This way we won't loose data.
|
|
|
|
|
|
|
|
result, n, err := transform.String(encoding.NewDecoder(), string(content)) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
result = result + string(content[n:]) |
|
|
|
result = result + string(content[n:]) |
|
|
|
} |
|
|
|
} |
|
|
|