2017-04-21 18:20:35 +08:00
|
|
|
package models
|
|
|
|
|
2017-04-22 17:24:17 +08:00
|
|
|
import (
|
|
|
|
"time"
|
2017-04-27 18:19:37 +08:00
|
|
|
|
2017-05-13 12:12:37 +08:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2017-05-19 17:20:33 +08:00
|
|
|
"github.com/astaxie/beego"
|
|
|
|
"github.com/astaxie/beego/orm"
|
2017-06-14 09:23:29 +08:00
|
|
|
"github.com/lifei6671/mindoc/conf"
|
2018-01-25 19:18:59 +08:00
|
|
|
"strings"
|
2018-01-26 17:17:38 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2018-01-27 13:56:45 +08:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
2017-04-22 17:24:17 +08:00
|
|
|
)
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
|
|
// Document struct.
|
|
|
|
type Document struct {
|
2017-05-19 17:20:33 +08:00
|
|
|
DocumentId int `orm:"pk;auto;unique;column(document_id)" json:"doc_id"`
|
|
|
|
DocumentName string `orm:"column(document_name);size(500)" json:"doc_name"`
|
2017-04-21 18:20:35 +08:00
|
|
|
// Identify 文档唯一标识
|
2017-05-19 17:20:33 +08:00
|
|
|
Identify string `orm:"column(identify);size(100);index;null;default(null)" json:"identify"`
|
|
|
|
BookId int `orm:"column(book_id);type(int);index" json:"book_id"`
|
|
|
|
ParentId int `orm:"column(parent_id);type(int);index;default(0)" json:"parent_id"`
|
|
|
|
OrderSort int `orm:"column(order_sort);default(0);type(int);index" json:"order_sort"`
|
2017-04-21 18:20:35 +08:00
|
|
|
// Markdown markdown格式文档.
|
2017-05-19 17:20:33 +08:00
|
|
|
Markdown string `orm:"column(markdown);type(text);null" json:"markdown"`
|
2017-04-21 18:20:35 +08:00
|
|
|
// Release 发布后的Html格式内容.
|
2017-05-19 17:20:33 +08:00
|
|
|
Release string `orm:"column(release);type(text);null" json:"release"`
|
2017-04-21 18:20:35 +08:00
|
|
|
// Content 未发布的 Html 格式内容.
|
2017-05-19 17:20:33 +08:00
|
|
|
Content string `orm:"column(content);type(text);null" json:"content"`
|
|
|
|
CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add" json:"create_time"`
|
|
|
|
MemberId int `orm:"column(member_id);type(int)" json:"member_id"`
|
|
|
|
ModifyTime time.Time `orm:"column(modify_time);type(datetime);auto_now" json:"modify_time"`
|
|
|
|
ModifyAt int `orm:"column(modify_at);type(int)" json:"-"`
|
|
|
|
Version int64 `orm:"type(bigint);column(version)" json:"version"`
|
2017-05-12 19:14:29 +08:00
|
|
|
AttachList []*Attachment `orm:"-" json:"attach"`
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TableName 获取对应数据库表名.
|
|
|
|
func (m *Document) TableName() string {
|
|
|
|
return "documents"
|
|
|
|
}
|
2017-05-19 17:20:33 +08:00
|
|
|
|
2017-04-21 18:20:35 +08:00
|
|
|
// TableEngine 获取数据使用的引擎.
|
|
|
|
func (m *Document) TableEngine() string {
|
|
|
|
return "INNODB"
|
|
|
|
}
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2017-05-19 17:20:33 +08:00
|
|
|
func (m *Document) TableNameWithPrefix() string {
|
2017-04-22 17:24:17 +08:00
|
|
|
return conf.GetDatabasePrefix() + m.TableName()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-05-15 14:59:23 +08:00
|
|
|
//根据文档ID查询指定文档.
|
2017-05-19 17:20:33 +08:00
|
|
|
func (m *Document) Find(id int) (*Document, error) {
|
2017-04-23 20:33:21 +08:00
|
|
|
if id <= 0 {
|
2017-05-19 17:20:33 +08:00
|
|
|
return m, ErrInvalidParameter
|
2017-04-23 20:33:21 +08:00
|
|
|
}
|
|
|
|
o := orm.NewOrm()
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2017-05-19 17:20:33 +08:00
|
|
|
err := o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", id).One(m)
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2017-05-19 17:20:33 +08:00
|
|
|
if err == orm.ErrNoRows {
|
|
|
|
return m, ErrDataNotExist
|
2017-04-23 20:33:21 +08:00
|
|
|
}
|
2017-05-19 17:20:33 +08:00
|
|
|
return m, nil
|
2017-04-23 20:33:21 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 10:00:21 +08:00
|
|
|
//插入和更新文档.
|
2017-05-19 17:20:33 +08:00
|
|
|
func (m *Document) InsertOrUpdate(cols ...string) error {
|
2017-04-27 18:19:37 +08:00
|
|
|
o := orm.NewOrm()
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2017-04-27 18:19:37 +08:00
|
|
|
if m.DocumentId > 0 {
|
2017-05-19 17:20:33 +08:00
|
|
|
_, err := o.Update(m)
|
2017-04-27 18:19:37 +08:00
|
|
|
return err
|
2017-05-19 17:20:33 +08:00
|
|
|
} else {
|
|
|
|
_, err := o.Insert(m)
|
2017-05-02 10:00:21 +08:00
|
|
|
NewBook().ResetDocumentNumber(m.BookId)
|
2017-04-27 18:19:37 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 10:00:21 +08:00
|
|
|
//根据指定字段查询一条文档.
|
2017-05-19 17:20:33 +08:00
|
|
|
func (m *Document) FindByFieldFirst(field string, v interface{}) (*Document, error) {
|
2017-04-27 18:19:37 +08:00
|
|
|
o := orm.NewOrm()
|
|
|
|
|
2017-05-19 17:20:33 +08:00
|
|
|
err := o.QueryTable(m.TableNameWithPrefix()).Filter(field, v).One(m)
|
2017-04-27 18:19:37 +08:00
|
|
|
|
2017-05-19 17:20:33 +08:00
|
|
|
return m, err
|
2017-04-27 18:19:37 +08:00
|
|
|
}
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2017-04-28 18:08:01 +08:00
|
|
|
//递归删除一个文档.
|
|
|
|
func (m *Document) RecursiveDocument(doc_id int) error {
|
|
|
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
2017-05-19 17:20:33 +08:00
|
|
|
if doc, err := m.Find(doc_id); 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
|
|
|
}
|
|
|
|
|
2017-04-28 18:08:01 +08:00
|
|
|
var docs []*Document
|
|
|
|
|
2017-05-19 17:20:33 +08:00
|
|
|
_, err := o.QueryTable(m.TableNameWithPrefix()).Filter("parent_id", doc_id).All(&docs)
|
2017-04-28 18:08:01 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2017-05-19 17:20:33 +08:00
|
|
|
beego.Error("RecursiveDocument => ", err)
|
2017-04-28 18:08:01 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-19 17:20:33 +08:00
|
|
|
for _, item := range docs {
|
2017-04-28 18:08:01 +08:00
|
|
|
doc_id := item.DocumentId
|
2017-05-19 17:20:33 +08:00
|
|
|
o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", doc_id).Delete()
|
2017-04-28 18:08:01 +08:00
|
|
|
m.RecursiveDocument(doc_id)
|
|
|
|
}
|
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
|
|
|
|
2017-05-02 10:00:21 +08:00
|
|
|
//发布文档
|
2018-01-26 17:17:38 +08:00
|
|
|
func (m *Document) ReleaseContent(bookId int) {
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2017-04-30 22:13:12 +08:00
|
|
|
o := orm.NewOrm()
|
|
|
|
|
2017-05-13 12:12:37 +08:00
|
|
|
var docs []*Document
|
2018-01-26 17:17:38 +08:00
|
|
|
_, err := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", bookId).All(&docs, "document_id", "content")
|
2017-04-30 22:13:12 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2017-05-19 17:20:33 +08:00
|
|
|
beego.Error("发布失败 => ", err)
|
2017-05-13 12:12:37 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, item := range docs {
|
2018-01-27 13:56:45 +08:00
|
|
|
if item.Content != "" {
|
|
|
|
item.Release = item.Content
|
|
|
|
bufio := bytes.NewReader([]byte(item.Content))
|
|
|
|
//解析文档中非本站的链接,并设置为新窗口打开
|
|
|
|
if content, err := goquery.NewDocumentFromReader(bufio);err == nil {
|
|
|
|
|
|
|
|
content.Find("a").Each(func(i int, contentSelection *goquery.Selection) {
|
|
|
|
if src, ok := contentSelection.Attr("href"); ok{
|
|
|
|
if strings.HasPrefix(src, "http://") || strings.HasPrefix(src,"https://") {
|
2018-01-27 14:23:00 +08:00
|
|
|
//beego.Info(src,conf.BaseUrl,strings.HasPrefix(src,conf.BaseUrl))
|
2018-01-27 14:22:44 +08:00
|
|
|
if conf.BaseUrl != "" && !strings.HasPrefix(src,conf.BaseUrl) {
|
2018-01-27 13:56:45 +08:00
|
|
|
contentSelection.SetAttr("target", "_blank")
|
|
|
|
if html, err := content.Html();err == nil {
|
|
|
|
item.Release = html
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
attachList, err := NewAttachment().FindListByDocumentId(item.DocumentId)
|
|
|
|
if err == nil && len(attachList) > 0 {
|
2017-05-13 12:12:37 +08:00
|
|
|
content := bytes.NewBufferString("<div class=\"attach-list\"><strong>附件</strong><ul>")
|
2018-01-26 17:17:38 +08:00
|
|
|
for _, attach := range attachList {
|
|
|
|
if strings.HasPrefix(attach.HttpPath, "/") {
|
2018-01-27 14:22:44 +08:00
|
|
|
attach.HttpPath = strings.TrimSuffix(conf.BaseUrl, "/") + attach.HttpPath
|
2018-01-25 19:18:59 +08:00
|
|
|
}
|
2017-05-19 17:20:33 +08:00
|
|
|
li := fmt.Sprintf("<li><a href=\"%s\" target=\"_blank\" title=\"%s\">%s</a></li>", attach.HttpPath, attach.FileName, attach.FileName)
|
2017-05-13 12:12:37 +08:00
|
|
|
|
|
|
|
content.WriteString(li)
|
|
|
|
}
|
|
|
|
content.WriteString("</ul></div>")
|
2017-05-19 17:20:33 +08:00
|
|
|
item.Release += content.String()
|
2017-05-13 12:12:37 +08:00
|
|
|
}
|
2017-05-19 17:20:33 +08:00
|
|
|
_, err = o.Update(item, "release")
|
2017-05-13 12:12:37 +08:00
|
|
|
if err != nil {
|
2017-05-19 17:20:33 +08:00
|
|
|
beego.Error(fmt.Sprintf("发布失败 => %+v", item), err)
|
2018-01-26 17:17:38 +08:00
|
|
|
}else {
|
|
|
|
os.RemoveAll(filepath.Join(conf.WorkingDirectory,"uploads","books",strconv.Itoa(bookId)))
|
2017-05-13 12:12:37 +08:00
|
|
|
}
|
2017-04-30 22:13:12 +08:00
|
|
|
}
|
|
|
|
}
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2017-05-15 14:59:23 +08:00
|
|
|
//根据项目ID查询文档列表.
|
2017-05-19 17:20:33 +08:00
|
|
|
func (m *Document) FindListByBookId(book_id int) (docs []*Document, err error) {
|
2017-05-06 16:16:27 +08:00
|
|
|
o := orm.NewOrm()
|
|
|
|
|
2017-07-07 16:20:55 +08:00
|
|
|
_, err = o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", book_id).OrderBy("order_sort").All(&docs)
|
2017-04-22 17:24:17 +08:00
|
|
|
|
2017-05-06 16:16:27 +08:00
|
|
|
return
|
|
|
|
}
|