From 289f92f249d1c9d4f698a0ef12591f6c9b5745a7 Mon Sep 17 00:00:00 2001 From: truthhun <1272881215@qq.com> Date: Tue, 7 Feb 2023 17:33:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- biz/document.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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 }