2017-04-21 18:20:35 +08:00
|
|
|
|
package models
|
|
|
|
|
|
2017-04-22 17:24:17 +08:00
|
|
|
|
import (
|
|
|
|
|
"time"
|
2018-03-25 14:59:42 +08:00
|
|
|
|
|
2021-04-12 21:12:26 +08:00
|
|
|
|
"github.com/beego/i18n"
|
|
|
|
|
|
2017-05-13 12:12:37 +08:00
|
|
|
|
"fmt"
|
2018-03-25 14:59:42 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
|
2019-05-20 12:08:14 +08:00
|
|
|
|
"bytes"
|
2018-08-13 19:05:49 +08:00
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2018-09-13 18:19:26 +08:00
|
|
|
|
"strings"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
2021-03-23 21:55:50 +08:00
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
2021-03-24 00:28:13 +08:00
|
|
|
|
"github.com/beego/beego/v2/core/logs"
|
2021-03-25 10:42:18 +08:00
|
|
|
|
"github.com/beego/beego/v2/server/web"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"github.com/mindoc-org/mindoc/cache"
|
|
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
|
|
|
|
"github.com/mindoc-org/mindoc/utils"
|
2017-04-22 17:24:17 +08:00
|
|
|
|
)
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
|
|
|
|
// Document struct.
|
|
|
|
|
type Document struct {
|
2023-07-03 09:41:27 +08:00
|
|
|
|
DocumentId int `orm:"pk;auto;unique;column(document_id)" json:"doc_id"`
|
|
|
|
|
DocumentName string `orm:"column(document_name);size(500);description(文档名称)" json:"doc_name"`
|
|
|
|
|
Identify string `orm:"column(identify);size(100);index;null;default(null);description(唯一标识)" json:"identify"` // Identify 文档唯一标识
|
|
|
|
|
BookId int `orm:"column(book_id);type(int);index;description(关联bools表主键)" json:"book_id"`
|
|
|
|
|
ParentId int `orm:"column(parent_id);type(int);index;default(0);description(父级文档)" json:"parent_id"`
|
|
|
|
|
OrderSort int `orm:"column(order_sort);default(0);type(int);index;description(排序从小到大排序)" json:"order_sort"`
|
|
|
|
|
Markdown string `orm:"column(markdown);type(text);null;description(markdown内容)" json:"markdown"` // Markdown markdown格式文档.
|
|
|
|
|
MarkdownTheme string `orm:"column(markdown_theme);size(50);default(theme__light);description(markdown主题)" json:"markdown_theme"`
|
|
|
|
|
Release string `orm:"column(release);type(text);null;description(文章内容)" json:"release"` // Release 发布后的Html格式内容.
|
|
|
|
|
Content string `orm:"column(content);type(text);null;description(文章内容)" json:"content"` // Content 未发布的 Html 格式内容.
|
|
|
|
|
CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add;description(创建时间)" json:"create_time"`
|
|
|
|
|
MemberId int `orm:"column(member_id);type(int);description(关系用户id)" json:"member_id"`
|
|
|
|
|
ModifyTime time.Time `orm:"column(modify_time);type(datetime);auto_now;description(修改时间)" json:"modify_time"`
|
|
|
|
|
ModifyAt int `orm:"column(modify_at);type(int);description(修改人id)" json:"-"`
|
|
|
|
|
Version int64 `orm:"column(version);type(bigint);description(版本,关联历史文档里的version)" json:"version"`
|
|
|
|
|
IsOpen int `orm:"column(is_open);type(int);default(0);description(是否展开子目录 0:阅读时关闭节点 1:阅读时展开节点 2:空目录 单击时会展开下级节点)" json:"is_open"` //是否展开子目录:0 否/1 是 /2 空间节点,单击时展开下一级
|
|
|
|
|
ViewCount int `orm:"column(view_count);type(int);description(浏览量)" json:"view_count"`
|
|
|
|
|
AttachList []*Attachment `orm:"-" json:"attach"`
|
2021-04-12 21:12:26 +08:00
|
|
|
|
//i18n
|
|
|
|
|
Lang string `orm:"-"`
|
2017-04-21 18:20:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
|
// 多字段唯一键
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func (item *Document) TableUnique() [][]string {
|
2023-07-03 09:41:27 +08:00
|
|
|
|
return [][]string{{"book_id", "identify"}}
|
2018-03-24 17:24:02 +08:00
|
|
|
|
}
|
2018-03-25 14:59:42 +08:00
|
|
|
|
|
2017-04-21 18:20:35 +08:00
|
|
|
|
// TableName 获取对应数据库表名.
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func (item *Document) TableName() string {
|
2017-04-21 18:20:35 +08:00
|
|
|
|
return "documents"
|
|
|
|
|
}
|
2017-05-19 17:20:33 +08:00
|
|
|
|
|
2017-04-21 18:20:35 +08:00
|
|
|
|
// TableEngine 获取数据使用的引擎.
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func (item *Document) TableEngine() string {
|
2017-04-21 18:20:35 +08:00
|
|
|
|
return "INNODB"
|
|
|
|
|
}
|
2017-04-22 17:24:17 +08:00
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func (item *Document) TableNameWithPrefix() string {
|
|
|
|
|
return conf.GetDatabasePrefix() + item.TableName()
|
2017-04-22 17:24:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-19 17:20:33 +08:00
|
|
|
|
func NewDocument() *Document {
|
2017-04-29 21:28:09 +08:00
|
|
|
|
return &Document{
|
|
|
|
|
Version: time.Now().Unix(),
|
|
|
|
|
}
|
2017-04-22 17:24:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 09:41:27 +08:00
|
|
|
|
// 根据文档ID查询指定文档.
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func (item *Document) Find(id int) (*Document, error) {
|
2017-04-23 20:33:21 +08:00
|
|
|
|
if id <= 0 {
|
2018-09-13 18:19:26 +08:00
|
|
|
|
return item, ErrInvalidParameter
|
2017-04-23 20:33:21 +08:00
|
|
|
|
}
|
2018-02-28 15:47:00 +08:00
|
|
|
|
|
2017-04-23 20:33:21 +08:00
|
|
|
|
o := orm.NewOrm()
|
2017-04-22 17:24:17 +08:00
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
err := o.QueryTable(item.TableNameWithPrefix()).Filter("document_id", id).One(item)
|
2017-04-22 17:24:17 +08:00
|
|
|
|
|
2017-05-19 17:20:33 +08:00
|
|
|
|
if err == orm.ErrNoRows {
|
2018-09-13 18:19:26 +08:00
|
|
|
|
return item, ErrDataNotExist
|
2017-04-23 20:33:21 +08:00
|
|
|
|
}
|
2018-02-28 15:47:00 +08:00
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
return item, nil
|
2017-04-23 20:33:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 09:41:27 +08:00
|
|
|
|
// 插入和更新文档.
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func (item *Document) InsertOrUpdate(cols ...string) error {
|
2017-04-27 18:19:37 +08:00
|
|
|
|
o := orm.NewOrm()
|
2018-11-08 20:08:30 +08:00
|
|
|
|
item.DocumentName = utils.StripTags(item.DocumentName)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
var err error
|
2018-09-13 18:19:26 +08:00
|
|
|
|
if item.DocumentId > 0 {
|
|
|
|
|
_, err = o.Update(item, cols...)
|
2017-05-19 17:20:33 +08:00
|
|
|
|
} else {
|
2018-09-13 18:19:26 +08:00
|
|
|
|
if item.Identify == "" {
|
2018-03-30 17:21:16 +08:00
|
|
|
|
book := NewBook()
|
|
|
|
|
identify := "docs"
|
2018-09-13 18:19:26 +08:00
|
|
|
|
if err := o.QueryTable(book.TableNameWithPrefix()).Filter("book_id", item.BookId).One(book, "identify"); err == nil {
|
2018-03-30 17:21:16 +08:00
|
|
|
|
identify = book.Identify
|
|
|
|
|
}
|
2018-06-06 20:45:24 +08:00
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
item.Identify = fmt.Sprintf("%s-%s", identify, strconv.FormatInt(time.Now().UnixNano(), 32))
|
2018-06-06 20:45:24 +08:00
|
|
|
|
}
|
2018-07-10 18:53:41 +08:00
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
if item.OrderSort == 0 {
|
|
|
|
|
sort, _ := o.QueryTable(item.TableNameWithPrefix()).Filter("book_id", item.BookId).Filter("parent_id", item.ParentId).Count()
|
|
|
|
|
item.OrderSort = int(sort) + 1
|
2018-03-30 17:21:16 +08:00
|
|
|
|
}
|
2018-09-13 18:19:26 +08:00
|
|
|
|
_, err = o.Insert(item)
|
|
|
|
|
NewBook().ResetDocumentNumber(item.BookId)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
2017-04-27 18:19:37 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
2018-02-27 17:20:42 +08:00
|
|
|
|
|
|
|
|
|
return nil
|
2017-04-27 18:19:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 09:41:27 +08:00
|
|
|
|
// 根据文档识别编号和项目id获取一篇文档
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func (item *Document) FindByIdentityFirst(identify string, bookId int) (*Document, error) {
|
2017-04-27 18:19:37 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
err := o.QueryTable(item.TableNameWithPrefix()).Filter("book_id", bookId).Filter("identify", identify).One(item)
|
2017-04-27 18:19:37 +08:00
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
return item, err
|
2017-04-27 18:19:37 +08:00
|
|
|
|
}
|
2017-04-22 17:24:17 +08:00
|
|
|
|
|
2023-07-03 09:41:27 +08:00
|
|
|
|
// 递归删除一个文档.
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func (item *Document) RecursiveDocument(docId int) error {
|
2017-04-28 18:08:01 +08:00
|
|
|
|
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
if doc, err := item.Find(docId); err == nil {
|
2017-04-29 21:28:09 +08:00
|
|
|
|
o.Delete(doc)
|
2017-05-20 15:27:03 +08:00
|
|
|
|
NewDocumentHistory().Clear(doc.DocumentId)
|
2017-04-29 21:28:09 +08:00
|
|
|
|
}
|
2018-02-27 17:20:42 +08:00
|
|
|
|
var maps []orm.Params
|
2017-04-28 18:08:01 +08:00
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
_, err := o.Raw("SELECT document_id FROM " + item.TableNameWithPrefix() + " WHERE parent_id=" + strconv.Itoa(docId)).Values(&maps)
|
2017-04-28 18:08:01 +08:00
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("RecursiveDocument => ", err)
|
2017-04-28 18:08:01 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
for _, param := range maps {
|
|
|
|
|
if docId, ok := param["document_id"].(string); ok {
|
2018-03-25 14:59:42 +08:00
|
|
|
|
id, _ := strconv.Atoi(docId)
|
2018-09-13 18:19:26 +08:00
|
|
|
|
o.QueryTable(item.TableNameWithPrefix()).Filter("document_id", id).Delete()
|
|
|
|
|
item.RecursiveDocument(id)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
}
|
2017-04-28 18:08:01 +08:00
|
|
|
|
}
|
2017-04-29 21:28:09 +08:00
|
|
|
|
|
2017-04-28 18:08:01 +08:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-04-22 17:24:17 +08:00
|
|
|
|
|
2023-07-03 09:41:27 +08:00
|
|
|
|
// 将文档写入缓存
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func (item *Document) PutToCache() {
|
2018-02-28 15:47:00 +08:00
|
|
|
|
go func(m Document) {
|
2018-07-25 14:46:56 +08:00
|
|
|
|
|
2018-08-13 15:04:52 +08:00
|
|
|
|
if m.Identify == "" {
|
2018-02-28 15:47:00 +08:00
|
|
|
|
|
2018-08-13 15:04:52 +08:00
|
|
|
|
if err := cache.Put("Document.Id."+strconv.Itoa(m.DocumentId), m, time.Second*3600); err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Info("文档缓存失败:", m.DocumentId)
|
2018-08-13 15:04:52 +08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if err := cache.Put(fmt.Sprintf("Document.BookId.%d.Identify.%s", m.BookId, m.Identify), m, time.Second*3600); err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Info("文档缓存失败:", m.DocumentId)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
}
|
2018-08-13 15:04:52 +08:00
|
|
|
|
}
|
2018-07-25 14:46:56 +08:00
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
}(*item)
|
2018-02-28 15:47:00 +08:00
|
|
|
|
}
|
2018-03-25 14:59:42 +08:00
|
|
|
|
|
2023-07-03 09:41:27 +08:00
|
|
|
|
// 清除缓存
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func (item *Document) RemoveCache() {
|
2018-02-28 15:47:00 +08:00
|
|
|
|
go func(m Document) {
|
2018-03-25 14:59:42 +08:00
|
|
|
|
cache.Put("Document.Id."+strconv.Itoa(m.DocumentId), m, time.Second*3600)
|
2018-02-28 15:47:00 +08:00
|
|
|
|
|
|
|
|
|
if m.Identify != "" {
|
2018-03-25 14:59:42 +08:00
|
|
|
|
cache.Put(fmt.Sprintf("Document.BookId.%d.Identify.%s", m.BookId, m.Identify), m, time.Second*3600)
|
2018-02-28 15:47:00 +08:00
|
|
|
|
}
|
2018-09-13 18:19:26 +08:00
|
|
|
|
}(*item)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
}
|
2018-02-28 15:47:00 +08:00
|
|
|
|
|
2023-07-03 09:41:27 +08:00
|
|
|
|
// 从缓存获取
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func (item *Document) FromCacheById(id int) (*Document, error) {
|
2018-02-28 15:47:00 +08:00
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
if err := cache.Get("Document.Id."+strconv.Itoa(id), &item); err == nil && item.DocumentId > 0 {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Info("从缓存中获取文档信息成功 ->", item.DocumentId)
|
2018-09-13 18:19:26 +08:00
|
|
|
|
return item, nil
|
2018-02-27 17:20:42 +08:00
|
|
|
|
}
|
2018-07-25 14:46:56 +08:00
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
if item.DocumentId > 0 {
|
|
|
|
|
item.PutToCache()
|
2018-07-25 14:46:56 +08:00
|
|
|
|
}
|
2018-09-13 18:19:26 +08:00
|
|
|
|
item, err := item.Find(id)
|
2018-07-25 14:46:56 +08:00
|
|
|
|
|
|
|
|
|
if err == nil {
|
2018-09-13 18:19:26 +08:00
|
|
|
|
item.PutToCache()
|
2018-07-25 14:46:56 +08:00
|
|
|
|
}
|
2018-09-13 18:19:26 +08:00
|
|
|
|
return item, err
|
2018-02-27 17:20:42 +08:00
|
|
|
|
}
|
2018-03-24 17:24:02 +08:00
|
|
|
|
|
2023-07-03 09:41:27 +08:00
|
|
|
|
// 根据文档标识从缓存中查询文档
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func (item *Document) FromCacheByIdentify(identify string, bookId int) (*Document, error) {
|
2018-07-25 14:46:56 +08:00
|
|
|
|
|
|
|
|
|
key := fmt.Sprintf("Document.BookId.%d.Identify.%s", bookId, identify)
|
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
if err := cache.Get(key, item); err == nil && item.DocumentId > 0 {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Info("从缓存中获取文档信息成功 ->", key)
|
2018-09-13 18:19:26 +08:00
|
|
|
|
return item, nil
|
2018-02-27 17:20:42 +08:00
|
|
|
|
}
|
2018-07-25 14:46:56 +08:00
|
|
|
|
|
2018-03-01 10:12:31 +08:00
|
|
|
|
defer func() {
|
2018-09-13 18:19:26 +08:00
|
|
|
|
if item.DocumentId > 0 {
|
|
|
|
|
item.PutToCache()
|
2018-03-01 10:12:31 +08:00
|
|
|
|
}
|
|
|
|
|
}()
|
2018-09-13 18:19:26 +08:00
|
|
|
|
return item.FindByIdentityFirst(identify, bookId)
|
2018-02-27 17:20:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 09:41:27 +08:00
|
|
|
|
// 根据项目ID查询文档列表.
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func (item *Document) FindListByBookId(bookId int) (docs []*Document, err error) {
|
2017-05-06 16:16:27 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
_, err = o.QueryTable(item.TableNameWithPrefix()).Filter("book_id", bookId).OrderBy("order_sort").All(&docs)
|
2017-04-22 17:24:17 +08:00
|
|
|
|
|
2017-05-06 16:16:27 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2018-07-18 17:10:24 +08:00
|
|
|
|
|
2023-07-03 09:41:27 +08:00
|
|
|
|
// 判断文章是否存在
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func (item *Document) IsExist(documentId int) bool {
|
2018-07-13 19:04:51 +08:00
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
return o.QueryTable(item.TableNameWithPrefix()).Filter("document_id", documentId).Exist()
|
2018-08-13 15:04:52 +08:00
|
|
|
|
}
|
2018-08-13 19:05:49 +08:00
|
|
|
|
|
2023-07-03 09:41:27 +08:00
|
|
|
|
// 发布单篇文档
|
2018-08-13 19:05:49 +08:00
|
|
|
|
func (item *Document) ReleaseContent() error {
|
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
item.Release = strings.TrimSpace(item.Content)
|
2018-08-13 19:05:49 +08:00
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
err := item.Processor().InsertOrUpdate("release")
|
2018-08-13 19:05:49 +08:00
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error(fmt.Sprintf("发布失败 -> %+v", item), err)
|
2018-09-13 18:19:26 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
//当文档发布后,需要清除已缓存的转换文档和文档缓存
|
|
|
|
|
item.RemoveCache()
|
2018-09-12 15:08:16 +08:00
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
if err := os.RemoveAll(filepath.Join(conf.WorkingDirectory, "uploads", "books", strconv.Itoa(item.BookId))); err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("删除已缓存的文档目录失败 -> ", filepath.Join(conf.WorkingDirectory, "uploads", "books", strconv.Itoa(item.BookId)))
|
2018-09-13 18:19:26 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 10:04:11 +08:00
|
|
|
|
// Processor 调用位置两处:
|
|
|
|
|
// 1. 项目发布和文档发布: 处理文档的外链,附件,底部编辑信息等;
|
|
|
|
|
// 2. 文档阅读:可以修复存在问题的文档,使其能正常显示附件下载和文档作者信息等。
|
2018-09-13 18:19:26 +08:00
|
|
|
|
func (item *Document) Processor() *Document {
|
|
|
|
|
if item.Release != "" {
|
|
|
|
|
item.Release = utils.SafetyProcessor(item.Release)
|
2023-06-07 10:04:11 +08:00
|
|
|
|
} else {
|
|
|
|
|
// Release内容为空,直接赋值文档标签,保证附件下载正常
|
|
|
|
|
item.Release = "<div class=\"whole-article-wrap\"></div>"
|
|
|
|
|
}
|
2018-09-13 18:19:26 +08:00
|
|
|
|
|
2023-06-07 10:04:11 +08:00
|
|
|
|
// Next: 生成文档的一些附加信息
|
|
|
|
|
if docQuery, err := goquery.NewDocumentFromReader(bytes.NewBufferString(item.Release)); err == nil {
|
2018-09-13 18:19:26 +08:00
|
|
|
|
|
2023-06-07 10:04:11 +08:00
|
|
|
|
//处理附件
|
|
|
|
|
if selector := docQuery.Find("div.attach-list").First(); selector.Size() <= 0 {
|
2018-09-13 18:19:26 +08:00
|
|
|
|
//处理附件
|
2023-06-07 10:04:11 +08:00
|
|
|
|
attachList, err := NewAttachment().FindListByDocumentId(item.DocumentId)
|
|
|
|
|
if err == nil && len(attachList) > 0 {
|
|
|
|
|
content := bytes.NewBufferString("<div class=\"attach-list\"><strong>" + i18n.Tr(item.Lang, "doc.attachment") + "</strong><ul>")
|
|
|
|
|
for _, attach := range attachList {
|
|
|
|
|
if strings.HasPrefix(attach.HttpPath, "/") {
|
|
|
|
|
attach.HttpPath = strings.TrimSuffix(conf.BaseUrl, "/") + attach.HttpPath
|
2018-09-13 18:19:26 +08:00
|
|
|
|
}
|
2023-06-07 10:04:11 +08:00
|
|
|
|
li := fmt.Sprintf("<li><a href=\"%s\" target=\"_blank\" title=\"%s\">%s</a></li>", attach.HttpPath, attach.FileName, attach.FileName)
|
|
|
|
|
|
|
|
|
|
content.WriteString(li)
|
|
|
|
|
}
|
|
|
|
|
content.WriteString("</ul></div>")
|
|
|
|
|
if docQuery == nil {
|
|
|
|
|
docQuery, err = goquery.NewDocumentFromReader(content)
|
2023-07-03 09:41:27 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
logs.Error("goquery->NewDocumentFromReader err:%+v", err)
|
|
|
|
|
}
|
2023-06-07 10:04:11 +08:00
|
|
|
|
} else {
|
|
|
|
|
if selector := docQuery.Find("div.wiki-bottom").First(); selector.Size() > 0 {
|
|
|
|
|
selector.BeforeHtml(content.String()) //This branch should be a compatible branch.
|
|
|
|
|
} else if selector := docQuery.Find("div.markdown-article").First(); selector.Size() > 0 {
|
|
|
|
|
selector.AppendHtml(content.String()) //The document produced by the editor of Markdown will have this tag.class.
|
|
|
|
|
} else if selector := docQuery.Find("div.whole-article-wrap").First(); selector.Size() > 0 {
|
|
|
|
|
selector.AppendHtml(content.String()) //All documents should have this tag.
|
2018-08-13 19:05:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-07 10:04:11 +08:00
|
|
|
|
}
|
2018-08-13 19:05:49 +08:00
|
|
|
|
|
2023-06-07 10:04:11 +08:00
|
|
|
|
//处理了文档底部信息
|
|
|
|
|
if selector := docQuery.Find("div.wiki-bottom").First(); selector.Size() <= 0 && item.MemberId > 0 {
|
|
|
|
|
//处理文档结尾信息
|
|
|
|
|
docCreator, err := NewMember().Find(item.MemberId, "real_name", "account")
|
|
|
|
|
release := "<div class=\"wiki-bottom\">"
|
|
|
|
|
|
|
|
|
|
release += i18n.Tr(item.Lang, "doc.ft_author")
|
|
|
|
|
if err == nil && docCreator != nil {
|
|
|
|
|
if docCreator.RealName != "" {
|
|
|
|
|
release += docCreator.RealName
|
|
|
|
|
} else {
|
|
|
|
|
release += docCreator.Account
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
release += " " + i18n.Tr(item.Lang, "doc.ft_create_time") + item.CreateTime.Local().Format("2006-01-02 15:04") + "<br>"
|
2021-03-26 08:43:33 +08:00
|
|
|
|
|
2023-06-07 10:04:11 +08:00
|
|
|
|
if item.ModifyAt > 0 {
|
|
|
|
|
docModify, err := NewMember().Find(item.ModifyAt, "real_name", "account")
|
|
|
|
|
if err == nil {
|
|
|
|
|
if docModify.RealName != "" {
|
|
|
|
|
release += i18n.Tr(item.Lang, "doc.ft_last_editor") + docModify.RealName
|
2021-03-26 08:43:33 +08:00
|
|
|
|
} else {
|
2023-06-07 10:04:11 +08:00
|
|
|
|
release += i18n.Tr(item.Lang, "doc.ft_last_editor") + docModify.Account
|
2019-05-20 17:38:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-07 10:04:11 +08:00
|
|
|
|
}
|
|
|
|
|
release += " " + i18n.Tr(item.Lang, "doc.ft_update_time") + item.ModifyTime.Local().Format("2006-01-02 15:04") + "<br>"
|
|
|
|
|
release += "</div>"
|
2018-09-13 18:19:26 +08:00
|
|
|
|
|
2023-06-07 10:04:11 +08:00
|
|
|
|
if selector := docQuery.Find("div.markdown-article").First(); selector.Size() > 0 {
|
|
|
|
|
selector.AppendHtml(release)
|
|
|
|
|
} else if selector := docQuery.Find("div.whole-article-wrap").First(); selector.Size() > 0 {
|
|
|
|
|
selector.AppendHtml(release)
|
2018-09-12 15:08:16 +08:00
|
|
|
|
}
|
2023-06-07 10:04:11 +08:00
|
|
|
|
}
|
|
|
|
|
cdnimg, _ := web.AppConfig.String("cdnimg")
|
2018-09-12 15:08:16 +08:00
|
|
|
|
|
2023-06-07 10:04:11 +08:00
|
|
|
|
docQuery.Find("img").Each(func(i int, selection *goquery.Selection) {
|
2019-05-20 12:08:14 +08:00
|
|
|
|
|
2023-06-07 10:04:11 +08:00
|
|
|
|
if src, ok := selection.Attr("src"); ok {
|
|
|
|
|
src = strings.TrimSpace(strings.ToLower(src))
|
|
|
|
|
//过滤掉没有链接的图片标签
|
|
|
|
|
if src == "" || strings.HasPrefix(src, "data:text/html") {
|
|
|
|
|
selection.Remove()
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-05-20 12:08:14 +08:00
|
|
|
|
|
2023-06-07 10:04:11 +08:00
|
|
|
|
//设置图片为CDN地址
|
|
|
|
|
if cdnimg != "" && strings.HasPrefix(src, "/uploads/") {
|
|
|
|
|
selection.SetAttr("src", utils.JoinURI(cdnimg, src))
|
|
|
|
|
}
|
2019-05-20 12:08:14 +08:00
|
|
|
|
|
2023-06-07 10:04:11 +08:00
|
|
|
|
}
|
|
|
|
|
selection.RemoveAttr("onerror").RemoveAttr("onload")
|
|
|
|
|
})
|
|
|
|
|
//过滤A标签的非法连接
|
|
|
|
|
docQuery.Find("a").Each(func(i int, selection *goquery.Selection) {
|
|
|
|
|
if val, exists := selection.Attr("href"); exists {
|
|
|
|
|
if val == "" {
|
|
|
|
|
selection.SetAttr("href", "#")
|
|
|
|
|
return
|
2019-05-20 12:08:14 +08:00
|
|
|
|
}
|
2023-06-07 10:04:11 +08:00
|
|
|
|
val = strings.Replace(strings.ToLower(val), " ", "", -1)
|
|
|
|
|
//移除危险脚本链接
|
|
|
|
|
if strings.HasPrefix(val, "data:text/html") ||
|
|
|
|
|
strings.HasPrefix(val, "vbscript:") ||
|
|
|
|
|
strings.HasPrefix(val, "javascript:") ||
|
|
|
|
|
strings.HasPrefix(val, "javascript:") {
|
|
|
|
|
selection.SetAttr("href", "#")
|
2019-05-20 12:08:14 +08:00
|
|
|
|
}
|
2023-06-07 10:04:11 +08:00
|
|
|
|
}
|
|
|
|
|
//移除所有 onerror 属性
|
|
|
|
|
selection.RemoveAttr("onerror").RemoveAttr("onload").RemoveAttr("onclick")
|
|
|
|
|
})
|
2019-05-20 12:08:14 +08:00
|
|
|
|
|
2023-06-07 10:04:11 +08:00
|
|
|
|
docQuery.Find("script").Remove()
|
|
|
|
|
docQuery.Find("link").Remove()
|
|
|
|
|
docQuery.Find("vbscript").Remove()
|
2018-09-12 15:08:16 +08:00
|
|
|
|
|
2023-06-07 10:04:11 +08:00
|
|
|
|
if html, err := docQuery.Html(); err == nil {
|
|
|
|
|
item.Release = strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(html), "<html><head></head><body>"), "</body></html>")
|
2018-08-13 19:05:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-07 10:04:11 +08:00
|
|
|
|
|
2018-09-13 18:19:26 +08:00
|
|
|
|
return item
|
|
|
|
|
}
|
2021-04-01 09:23:55 +08:00
|
|
|
|
|
|
|
|
|
// 增加阅读次数
|
|
|
|
|
func (item *Document) IncrViewCount(id int) {
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
o.QueryTable(item.TableNameWithPrefix()).Filter("document_id", id).Update(orm.Params{
|
|
|
|
|
"view_count": orm.ColValue(orm.ColAdd, 1),
|
|
|
|
|
})
|
|
|
|
|
}
|