Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
71 lines
2.8 KiB
Go
71 lines
2.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
"unicode/utf8"
|
|
|
|
"github.com/Tencent/WeKnora/internal/infrastructure/docparser"
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
)
|
|
|
|
const DigitalHumanFileLimit = 20 << 20
|
|
const DigitalHumanUploadLimit = 50 << 20
|
|
const DigitalHumanFileCountLimit = 99
|
|
|
|
type DigitalHumanUpload struct {
|
|
Name string
|
|
Data []byte
|
|
}
|
|
|
|
func DigitalHumanUploadSupported(name string) bool {
|
|
switch strings.ToLower(filepath.Ext(name)) {
|
|
case ".md", ".markdown", ".txt", ".srt", ".vtt", ".doc", ".docx", ".pdf", ".rtf", ".odt", ".ppt", ".pptx", ".csv", ".json", ".html", ".htm":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (s *DigitalHumanService) readDigitalHumanUpload(ctx context.Context, file DigitalHumanUpload) (types.DigitalHumanSource, error) {
|
|
name := filepath.Base(file.Name)
|
|
source := types.DigitalHumanSource{Name: name}
|
|
if !DigitalHumanUploadSupported(name) || len(file.Data) == 0 || len(file.Data) > DigitalHumanFileLimit {
|
|
return source, digitalHumanInputError("材料格式不支持、内容为空或单份超过20MB:" + name)
|
|
}
|
|
fileType := strings.TrimPrefix(strings.ToLower(filepath.Ext(name)), ".")
|
|
if fileType == "srt" || fileType == "vtt" || fileType == "txt" || fileType == "md" || fileType == "markdown" {
|
|
if !utf8.Valid(file.Data) {
|
|
return source, digitalHumanInputError("文本或字幕文件须采用 UTF-8 编码:" + name)
|
|
}
|
|
source.Text = strings.TrimSpace(strings.TrimPrefix(string(file.Data), "\ufeff"))
|
|
} else {
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Minute)
|
|
defer cancel()
|
|
engine := types.DefaultParserEngine(fileType)
|
|
overrides := map[string]string{"anydoc_extract_images": "false"}
|
|
reader, err := docparser.NewReader(ctx, engine, fileType, false, docparser.ReaderDeps{Remote: s.reader, Overrides: overrides})
|
|
if err != nil {
|
|
return source, digitalHumanInputError("文档解析器不可用,请检查文档解析服务:" + name)
|
|
}
|
|
result, err := reader.Read(ctx, &types.ReadRequest{FileName: name, FileType: fileType, FileContent: file.Data,
|
|
ParserEngine: engine, ParserEngineOverrides: overrides})
|
|
if err != nil || result == nil || result.Error != "" {
|
|
if ctx.Err() != nil {
|
|
return source, digitalHumanInputError("文档解析超时,请拆分文档后重试:" + name)
|
|
}
|
|
return source, digitalHumanInputError("无法解析文档,请检查文件是否损坏;扫描 PDF 需要可用的 OCR 解析器:" + name)
|
|
}
|
|
source.Text = strings.TrimSpace(result.MarkdownContent)
|
|
}
|
|
if source.Text == "" {
|
|
return source, digitalHumanInputError("文档没有可读取的文字:" + name)
|
|
}
|
|
if !utf8.ValidString(source.Text) || utf8.RuneCountInString(source.Text) > digitalHumanSourceLimit {
|
|
return source, digitalHumanInputError(fmt.Sprintf("文档文字超过%d字或编码无效:%s", digitalHumanSourceLimit, name))
|
|
}
|
|
return source, nil
|
|
}
|