-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
161 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
# yiwen-api | ||
# yiwen-api | ||
|
||
yiwen.ai 是一个由人和 AI 共同驱动的知识网络平台,它基于 AI 能力能将知识文档翻译成大部分国家主流语言版本,也能对知识文档内容进行跨语言的语义搜索。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package util | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/gabriel-vasile/mimetype" | ||
"github.com/saintfish/chardet" | ||
"github.com/teambition/gear" | ||
"golang.org/x/text/encoding/ianaindex" | ||
"golang.org/x/text/encoding/unicode" | ||
) | ||
|
||
func init() { | ||
mimetype.SetLimit(1024 * 1024) // 1MB | ||
} | ||
|
||
func NormalizeFileEncodingAndType(buf []byte, mtype string) ([]byte, string, error) { | ||
mt := mimetype.Detect(buf) | ||
|
||
var de *chardet.Detector | ||
switch { | ||
case mtype == "application/pdf" && mt.Is("application/pdf"): | ||
return buf, mtype, nil | ||
case mtype == "text/html" && mt.Is("text/html"): | ||
de = chardet.NewHtmlDetector() | ||
case mtype == "text/markdown" && mt.Is("text/plain"): | ||
de = chardet.NewHtmlDetector() | ||
case mtype == "text/plain" && mt.Is("text/plain"): | ||
de = chardet.NewTextDetector() | ||
default: | ||
return nil, "", gear.ErrUnsupportedMediaType.WithMsgf("unsupported media type: %s", mt.String()) | ||
} | ||
|
||
rt, err := de.DetectBest(buf) | ||
if err != nil { | ||
return nil, "", gear.ErrUnsupportedMediaType.From(err) | ||
} | ||
|
||
enc, err := ianaindex.IANA.Encoding(rt.Charset) | ||
if err != nil { | ||
enc, err = ianaindex.IANA.Encoding(strings.ReplaceAll(rt.Charset, "-", "")) | ||
} | ||
|
||
if err != nil { | ||
return nil, "", gear.ErrUnsupportedMediaType.From(err) | ||
} | ||
|
||
if enc != unicode.UTF8 { | ||
decoder := enc.NewDecoder() | ||
buf, err = decoder.Bytes(buf) | ||
if err != nil { | ||
return nil, "", gear.ErrUnsupportedMediaType.From(err) | ||
} | ||
} | ||
|
||
return buf, mtype, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package util | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFile(t *testing.T) { | ||
t.Run("NormalizeFileEncodingAndType", func(t *testing.T) { | ||
file1, err := os.ReadFile("./file_test_gb18030.md") | ||
require.NoError(t, err) | ||
|
||
file2, err := os.ReadFile("./file_test_utf8.md") | ||
require.NoError(t, err) | ||
|
||
buf, mt, err := NormalizeFileEncodingAndType(file1, "text/markdown") | ||
require.NoError(t, err) | ||
assert.Equal(t, "text/markdown", mt) | ||
assert.Equal(t, file2, buf) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# yiwen-api | ||
|
||
yiwen.ai 是一个由人和 AI 共同驱动的知识网络平台,它基于 AI 能力能将知识文档翻译成大部分国家主流语言版本,也能对知识文档内容进行跨语言的语义搜索。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# yiwen-api | ||
|
||
yiwen.ai 是一个由人和 AI 共同驱动的知识网络平台,它基于 AI 能力能将知识文档翻译成大部分国家主流语言版本,也能对知识文档内容进行跨语言的语义搜索。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters