diff --git a/biz/document.go b/biz/document.go index 2f8250d..e1bd5a9 100644 --- a/biz/document.go +++ b/biz/document.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/url" + "os" "path/filepath" "strings" "time" @@ -220,16 +221,36 @@ func (s *DocumentAPIService) GetDocument(ctx context.Context, req *pb.GetDocumen // 查找文档相关联的附件。对于列表,只返回hash和id,不返回其他字段 attchment := s.dbModel.GetAttachmentByTypeAndTypeId(model.AttachmentTypeDocument, doc.Id, "hash", "path") + fixedData := make(map[string]interface{}) if pbDoc.Width == 0 || pbDoc.Height == 0 { bigCover := strings.TrimLeft(strings.TrimSuffix(attchment.Path, filepath.Ext(attchment.Path)), "./") + "/cover.big.png" doc.Width, doc.Height, _ = util.GetImageSize(bigCover) if doc.Width*doc.Height > 0 { pbDoc.Width = int32(doc.Width) pbDoc.Height = int32(doc.Height) - s.dbModel.UpdateDocumentField(doc.Id, map[string]interface{}{"width": doc.Width, "height": doc.Height}) + fixedData = map[string]interface{}{"width": doc.Width, "height": doc.Height} } } + if desc := strings.TrimSpace(pbDoc.Description); desc == "" { + desc = doc.Title // 默认 + textFile := strings.TrimLeft(strings.TrimSuffix(attchment.Path, filepath.Ext(attchment.Path)), "./") + "/content.txt" + if content, errRead := os.ReadFile(textFile); errRead == nil { // 读取文本内容,以提取关键字和摘要 + contentStr := string(content) + replacer := strings.NewReplacer("\r", " ", "\n", " ", "\t", " ") + contentStr = strings.TrimSpace(replacer.Replace(util.Substr(contentStr, 255))) + if contentStr != "" { + desc = contentStr + } + } + doc.Description = desc + fixedData["description"] = desc + } + + if len(fixedData) > 0 { + s.dbModel.UpdateDocumentField(doc.Id, fixedData) + } + pbDoc.Attachment = &pb.Attachment{Hash: attchment.Hash} return pbDoc, nil }