mindoc/controllers/document.go

1190 lines
30 KiB
Go
Raw Normal View History

2017-04-20 18:19:32 +08:00
package controllers
2017-04-26 18:17:38 +08:00
import (
2017-05-13 13:04:53 +08:00
"container/list"
"encoding/json"
"html/template"
"net/http"
2017-04-27 18:19:37 +08:00
"os"
2017-05-13 13:04:53 +08:00
"path/filepath"
2017-04-27 18:19:37 +08:00
"regexp"
"strconv"
"strings"
2017-05-13 13:04:53 +08:00
"time"
"image/png"
2017-04-27 18:19:37 +08:00
2017-06-05 10:52:14 +08:00
"bytes"
"github.com/PuerkitoBio/goquery"
2017-04-27 18:19:37 +08:00
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
2017-05-13 13:04:53 +08:00
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
2017-06-14 09:23:29 +08:00
"github.com/lifei6671/mindoc/commands"
"github.com/lifei6671/mindoc/conf"
"github.com/lifei6671/mindoc/models"
"github.com/lifei6671/mindoc/utils"
"github.com/lifei6671/mindoc/utils/wkhtmltopdf"
2017-04-26 18:17:38 +08:00
)
//DocumentController struct.
2017-04-20 18:19:32 +08:00
type DocumentController struct {
BaseController
}
//判断用户是否可以阅读文档.
2017-05-13 13:04:53 +08:00
func isReadable(identify, token string, c *DocumentController) *models.BookResult {
book, err := models.NewBook().FindByFieldFirst("identify", identify)
if err != nil {
beego.Error(err)
c.Abort("500")
}
2017-06-05 10:52:14 +08:00
if c.Member != nil && c.Member.IsAdministrator() {
bookResult := book.ToBookResult()
return bookResult
}
//如果文档是私有的
if book.PrivatelyOwned == 1 {
is_ok := false
if c.Member != nil {
_, err := models.NewRelationship().FindForRoleId(book.BookId, c.Member.MemberId)
if err == nil {
is_ok = true
}
}
if book.PrivateToken != "" && !is_ok {
//如果有访问的Token并且该项目设置了访问Token并且和用户提供的相匹配则记录到Session中.
//如果用户未提供Token且用户登录了则判断用户是否参与了该项目.
//如果用户未登录则从Session中读取Token.
if token != "" && strings.EqualFold(token, book.PrivateToken) {
c.SetSession(identify, token)
} else if token, ok := c.GetSession(identify).(string); !ok || !strings.EqualFold(token, book.PrivateToken) {
c.Abort("403")
}
2017-05-05 11:09:17 +08:00
} else if !is_ok {
c.Abort("403")
}
}
bookResult := book.ToBookResult()
if c.Member != nil {
rel, err := models.NewRelationship().FindByBookIdAndMemberId(bookResult.BookId, c.Member.MemberId)
if err == nil {
bookResult.MemberId = rel.MemberId
bookResult.RoleId = rel.RoleId
bookResult.RelationshipId = rel.RelationshipId
}
}
//判断是否需要显示评论框
if bookResult.CommentStatus == "closed" {
bookResult.IsDisplayComment = false
} else if bookResult.CommentStatus == "open" {
bookResult.IsDisplayComment = true
} else if bookResult.CommentStatus == "group_only" {
bookResult.IsDisplayComment = bookResult.RelationshipId > 0
} else if bookResult.CommentStatus == "registered_only" {
bookResult.IsDisplayComment = true
}
return bookResult
}
//文档首页.
2017-05-13 13:04:53 +08:00
func (c *DocumentController) Index() {
c.Prepare()
identify := c.Ctx.Input.Param(":key")
token := c.GetString("token")
if identify == "" {
c.Abort("404")
}
//如果没有开启你们访问则跳转到登录
if !c.EnableAnonymous && c.Member == nil {
2017-05-13 13:04:53 +08:00
c.Redirect(beego.URLFor("AccountController.Login"), 302)
return
}
2017-05-13 13:04:53 +08:00
bookResult := isReadable(identify, token, c)
c.TplName = "document/" + bookResult.Theme + "_read.tpl"
2017-05-13 13:04:53 +08:00
tree, err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId, 0)
if err != nil {
beego.Error(err)
c.Abort("500")
}
c.Data["Model"] = bookResult
c.Data["Result"] = template.HTML(tree)
c.Data["Title"] = "概要"
c.Data["Content"] = bookResult.Description
2017-04-20 18:19:32 +08:00
}
2017-05-12 11:41:59 +08:00
//阅读文档.
func (c *DocumentController) Read() {
c.Prepare()
identify := c.Ctx.Input.Param(":key")
token := c.GetString("token")
2017-05-13 13:04:53 +08:00
id := c.GetString(":id")
2017-05-13 13:04:53 +08:00
if identify == "" || id == "" {
c.Abort("404")
}
//如果没有开启你们访问则跳转到登录
if !c.EnableAnonymous && c.Member == nil {
2017-05-13 13:04:53 +08:00
c.Redirect(beego.URLFor("AccountController.Login"), 302)
return
}
2017-05-13 13:04:53 +08:00
bookResult := isReadable(identify, token, c)
c.TplName = "document/" + bookResult.Theme + "_read.tpl"
doc := models.NewDocument()
2017-05-13 13:04:53 +08:00
if doc_id, err := strconv.Atoi(id); err == nil {
doc, err = doc.Find(doc_id)
if err != nil {
beego.Error(err)
c.Abort("500")
}
2017-05-13 13:04:53 +08:00
} else {
doc, err = doc.FindByFieldFirst("identify", id)
if err != nil {
beego.Error(err)
c.Abort("500")
}
}
if doc.BookId != bookResult.BookId {
c.Abort("403")
}
2017-05-13 13:04:53 +08:00
attach, err := models.NewAttachment().FindListByDocumentId(doc.DocumentId)
2017-05-13 12:12:37 +08:00
if err == nil {
doc.AttachList = attach
}
2017-06-05 10:52:14 +08:00
cdnimg := beego.AppConfig.String("cdnimg")
if doc.Release != "" && cdnimg != "" {
query, err := goquery.NewDocumentFromReader(bytes.NewBufferString(doc.Release))
if err != nil {
beego.Error(err)
} else {
query.Find("img").Each(func(i int, contentSelection *goquery.Selection) {
if src, ok := contentSelection.Attr("src"); ok && strings.HasPrefix(src, "/uploads/") {
contentSelection.SetAttr("src", utils.JoinURI(cdnimg, src))
}
})
html, err := query.Html()
if err != nil {
beego.Error(err)
} else {
doc.Release = html
}
}
}
if c.IsAjax() {
2017-05-13 13:04:53 +08:00
var data struct {
DocTitle string `json:"doc_title"`
2017-05-13 13:04:53 +08:00
Body string `json:"body"`
Title string `json:"title"`
}
data.DocTitle = doc.DocumentName
data.Body = doc.Release
data.Title = doc.DocumentName + " - Powered by MinDoc"
2017-05-13 13:04:53 +08:00
c.JsonResult(0, "ok", data)
}
2017-05-13 13:04:53 +08:00
tree, err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId, doc.DocumentId)
if err != nil {
beego.Error(err)
c.Abort("500")
}
c.Data["Model"] = bookResult
c.Data["Result"] = template.HTML(tree)
c.Data["Title"] = doc.DocumentName
c.Data["Content"] = template.HTML(doc.Release)
2017-04-20 18:19:32 +08:00
}
2017-04-26 18:17:38 +08:00
//编辑文档.
2017-05-13 13:04:53 +08:00
func (c *DocumentController) Edit() {
2017-04-26 18:17:38 +08:00
c.Prepare()
identify := c.Ctx.Input.Param(":key")
2017-04-27 18:19:37 +08:00
if identify == "" {
c.Abort("404")
}
2017-04-26 18:17:38 +08:00
bookResult := models.NewBookResult()
2017-05-12 11:41:59 +08:00
var err error
//如果是超级管理者,则不判断权限
2017-06-05 10:52:14 +08:00
if c.Member.IsAdministrator() {
2017-05-13 13:04:53 +08:00
book, err := models.NewBook().FindByFieldFirst("identify", identify)
if err != nil {
c.JsonResult(6002, "项目不存在或权限不足")
}
bookResult = book.ToBookResult()
2017-04-26 18:17:38 +08:00
2017-05-13 13:04:53 +08:00
} else {
2017-05-12 11:41:59 +08:00
bookResult, err = models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
2017-04-27 18:19:37 +08:00
if err != nil {
beego.Error("DocumentController.Edit => ", err)
c.Abort("403")
}
if bookResult.RoleId == conf.BookObserver {
c.JsonResult(6002, "项目不存在或权限不足")
}
}
//根据不同编辑器类型加载编辑器
if bookResult.Editor == "markdown" {
2017-04-26 18:17:38 +08:00
c.TplName = "document/markdown_edit_template.tpl"
2017-05-13 13:04:53 +08:00
} else if bookResult.Editor == "html" {
2017-04-26 18:17:38 +08:00
c.TplName = "document/html_edit_template.tpl"
2017-05-13 13:04:53 +08:00
} else {
c.TplName = "document/" + bookResult.Editor + "_edit_template.tpl"
2017-04-26 18:17:38 +08:00
}
c.Data["Model"] = bookResult
2017-04-27 18:19:37 +08:00
2017-05-13 13:04:53 +08:00
r, _ := json.Marshal(bookResult)
2017-04-27 18:19:37 +08:00
c.Data["ModelResult"] = template.JS(string(r))
c.Data["Result"] = template.JS("[]")
2017-05-13 13:04:53 +08:00
trees, err := models.NewDocument().FindDocumentTree(bookResult.BookId)
2017-04-27 18:19:37 +08:00
if err != nil {
beego.Error("FindDocumentTree => ", err)
2017-05-13 13:04:53 +08:00
} else {
2017-04-27 18:19:37 +08:00
if len(trees) > 0 {
if jtree, err := json.Marshal(trees); err == nil {
c.Data["Result"] = template.JS(string(jtree))
}
2017-05-13 13:04:53 +08:00
} else {
2017-04-27 18:19:37 +08:00
c.Data["Result"] = template.JS("[]")
}
}
2017-05-13 13:04:53 +08:00
c.Data["BaiDuMapKey"] = beego.AppConfig.DefaultString("baidumapkey", "")
2017-04-27 18:19:37 +08:00
}
//创建一个文档.
2017-04-27 18:19:37 +08:00
func (c *DocumentController) Create() {
identify := c.GetString("identify")
doc_identify := c.GetString("doc_identify")
doc_name := c.GetString("doc_name")
2017-05-13 13:04:53 +08:00
parent_id, _ := c.GetInt("parent_id", 0)
doc_id, _ := c.GetInt("doc_id", 0)
2017-04-27 18:19:37 +08:00
if identify == "" {
2017-05-13 13:04:53 +08:00
c.JsonResult(6001, "参数错误")
2017-04-27 18:19:37 +08:00
}
if doc_name == "" {
2017-05-13 13:04:53 +08:00
c.JsonResult(6004, "文档名称不能为空")
2017-04-27 18:19:37 +08:00
}
if doc_identify != "" {
if ok, err := regexp.MatchString(`^[a-z]+[a-zA-Z0-9_\-]*$`, doc_identify); !ok || err != nil {
c.JsonResult(6003, "文档标识只能包含小写字母、数字,以及“-”和“_”符号,并且只能小写字母开头")
}
2017-05-13 13:04:53 +08:00
d, _ := models.NewDocument().FindByFieldFirst("identify", doc_identify)
if d.DocumentId > 0 && d.DocumentId != doc_id {
c.JsonResult(6006, "文档标识已被使用")
2017-04-27 18:19:37 +08:00
}
}
book_id := 0
//如果是超级管理员则不判断权限
2017-06-05 10:52:14 +08:00
if c.Member.IsAdministrator() {
2017-05-13 13:04:53 +08:00
book, err := models.NewBook().FindByFieldFirst("identify", identify)
if err != nil {
beego.Error(err)
c.JsonResult(6002, "项目不存在或权限不足")
}
book_id = book.BookId
2017-05-13 13:04:53 +08:00
} else {
bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
2017-04-27 18:19:37 +08:00
if err != nil || bookResult.RoleId == conf.BookObserver {
beego.Error("FindByIdentify => ", err)
c.JsonResult(6002, "项目不存在或权限不足")
}
book_id = bookResult.BookId
2017-04-27 18:19:37 +08:00
}
if parent_id > 0 {
2017-05-13 13:04:53 +08:00
doc, err := models.NewDocument().Find(parent_id)
if err != nil || doc.BookId != book_id {
2017-05-13 13:04:53 +08:00
c.JsonResult(6003, "父分类不存在")
2017-04-27 18:19:37 +08:00
}
}
2017-05-13 13:04:53 +08:00
document, _ := models.NewDocument().Find(doc_id)
2017-04-28 18:08:01 +08:00
2017-04-27 18:19:37 +08:00
document.MemberId = c.Member.MemberId
document.BookId = book_id
2017-05-13 13:04:53 +08:00
if doc_identify != "" {
2017-04-27 18:19:37 +08:00
document.Identify = doc_identify
}
2017-04-28 18:08:01 +08:00
document.Version = time.Now().Unix()
2017-04-27 18:19:37 +08:00
document.DocumentName = doc_name
document.ParentId = parent_id
2017-04-28 18:08:01 +08:00
2017-05-13 13:04:53 +08:00
if err := document.InsertOrUpdate(); err != nil {
beego.Error("InsertOrUpdate => ", err)
c.JsonResult(6005, "保存失败")
} else {
2017-05-13 13:04:53 +08:00
c.JsonResult(0, "ok", document)
2017-04-27 18:19:37 +08:00
}
}
//上传附件或图片.
2017-05-13 13:04:53 +08:00
func (c *DocumentController) Upload() {
2017-04-27 18:19:37 +08:00
identify := c.GetString("identify")
2017-05-13 13:04:53 +08:00
doc_id, _ := c.GetInt("doc_id")
2017-05-12 19:14:29 +08:00
is_attach := true
2017-04-27 18:19:37 +08:00
if identify == "" {
2017-05-13 13:04:53 +08:00
c.JsonResult(6001, "参数错误")
2017-04-27 18:19:37 +08:00
}
name := "editormd-file-file"
2017-05-13 13:04:53 +08:00
file, moreFile, err := c.GetFile(name)
2017-04-27 18:19:37 +08:00
if err == http.ErrMissingFile {
name = "editormd-image-file"
2017-05-13 13:04:53 +08:00
file, moreFile, err = c.GetFile(name)
2017-04-27 18:19:37 +08:00
if err == http.ErrMissingFile {
2017-05-13 13:04:53 +08:00
c.JsonResult(6003, "没有发现需要上传的文件")
2017-04-27 18:19:37 +08:00
}
}
if err != nil {
2017-05-13 13:04:53 +08:00
c.JsonResult(6002, err.Error())
2017-04-27 18:19:37 +08:00
}
defer file.Close()
ext := filepath.Ext(moreFile.Filename)
if ext == "" {
2017-05-13 13:04:53 +08:00
c.JsonResult(6003, "无法解析文件的格式")
2017-04-27 18:19:37 +08:00
}
if !conf.IsAllowUploadFileExt(ext) {
2017-05-13 13:04:53 +08:00
c.JsonResult(6004, "不允许的文件类型")
2017-04-27 18:19:37 +08:00
}
book_id := 0
//如果是超级管理员,则不判断权限
2017-06-05 10:52:14 +08:00
if c.Member.IsAdministrator() {
2017-05-13 13:04:53 +08:00
book, err := models.NewBook().FindByFieldFirst("identify", identify)
if err != nil {
c.JsonResult(6006, "文档不存在或权限不足")
}
book_id = book.BookId
2017-04-27 18:19:37 +08:00
2017-05-13 13:04:53 +08:00
} else {
book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
2017-04-27 18:19:37 +08:00
if err != nil {
beego.Error("DocumentController.Edit => ", err)
if err == orm.ErrNoRows {
c.JsonResult(6006, "权限不足")
}
c.JsonResult(6001, err.Error())
2017-04-27 18:19:37 +08:00
}
//如果没有编辑权限
if book.RoleId != conf.BookEditor && book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder {
c.JsonResult(6006, "权限不足")
}
book_id = book.BookId
2017-04-27 18:19:37 +08:00
}
if doc_id > 0 {
2017-05-13 13:04:53 +08:00
doc, err := models.NewDocument().Find(doc_id)
if err != nil {
2017-05-13 13:04:53 +08:00
c.JsonResult(6007, "文档不存在")
}
if doc.BookId != book_id {
2017-05-13 13:04:53 +08:00
c.JsonResult(6008, "文档不属于指定的项目")
}
}
2017-04-27 18:19:37 +08:00
2017-05-13 13:04:53 +08:00
fileName := "attach_" + strconv.FormatInt(time.Now().UnixNano(), 16)
2017-04-27 18:19:37 +08:00
2017-06-05 13:41:47 +08:00
filePath := filepath.Join(commands.WorkingDirectory, "uploads", time.Now().Format("200601"), fileName+ext)
2017-04-27 18:19:37 +08:00
path := filepath.Dir(filePath)
os.MkdirAll(path, os.ModePerm)
2017-05-13 13:04:53 +08:00
err = c.SaveToFile(name, filePath)
2017-04-27 18:19:37 +08:00
if err != nil {
2017-05-13 13:04:53 +08:00
beego.Error("SaveToFile => ", err)
c.JsonResult(6005, "保存文件失败")
2017-04-27 18:19:37 +08:00
}
attachment := models.NewAttachment()
attachment.BookId = book_id
2017-04-27 18:19:37 +08:00
attachment.FileName = moreFile.Filename
attachment.CreateAt = c.Member.MemberId
attachment.FileExt = ext
2017-06-05 13:41:47 +08:00
attachment.FilePath = strings.TrimPrefix(filePath, commands.WorkingDirectory)
2017-05-12 19:14:29 +08:00
attachment.DocumentId = doc_id
if fileInfo, err := os.Stat(filePath); err == nil {
attachment.FileSize = float64(fileInfo.Size())
}
2017-05-13 13:04:53 +08:00
if doc_id > 0 {
attachment.DocumentId = doc_id
}
2017-04-27 18:19:37 +08:00
2017-06-09 18:14:55 +08:00
if strings.EqualFold(ext, ".jpg") || strings.EqualFold(ext, ".jpeg") || strings.EqualFold(ext, ".png") || strings.EqualFold(ext, ".gif") {
2017-06-05 13:38:06 +08:00
2017-06-05 13:41:47 +08:00
attachment.HttpPath = "/" + strings.Replace(strings.TrimPrefix(filePath, commands.WorkingDirectory), "\\", "/", -1)
if strings.HasPrefix(attachment.HttpPath, "//") {
2017-06-05 13:38:06 +08:00
attachment.HttpPath = string(attachment.HttpPath[1:])
}
2017-05-12 19:14:29 +08:00
is_attach = false
2017-04-27 18:19:37 +08:00
}
2017-05-13 13:04:53 +08:00
err = attachment.Insert()
2017-04-27 18:19:37 +08:00
if err != nil {
os.Remove(filePath)
2017-05-13 13:04:53 +08:00
beego.Error("Attachment Insert => ", err)
c.JsonResult(6006, "文件保存失败")
2017-04-27 18:19:37 +08:00
}
if attachment.HttpPath == "" {
2017-05-13 13:04:53 +08:00
attachment.HttpPath = beego.URLFor("DocumentController.DownloadAttachment", ":key", identify, ":attach_id", attachment.AttachmentId)
2017-04-27 18:19:37 +08:00
2017-05-13 13:04:53 +08:00
if err := attachment.Update(); err != nil {
beego.Error("SaveToFile => ", err)
c.JsonResult(6005, "保存文件失败")
2017-04-27 18:19:37 +08:00
}
}
2017-04-27 18:19:37 +08:00
result := map[string]interface{}{
2017-05-13 13:04:53 +08:00
"errcode": 0,
"success": 1,
"message": "ok",
"url": attachment.HttpPath,
"alt": attachment.FileName,
"is_attach": is_attach,
"attach": attachment,
2017-04-27 18:19:37 +08:00
}
2017-05-13 13:04:53 +08:00
c.Ctx.Output.JSON(result, true, false)
2017-04-27 18:19:37 +08:00
c.StopRun()
}
//DownloadAttachment 下载附件.
2017-05-13 13:04:53 +08:00
func (c *DocumentController) DownloadAttachment() {
2017-04-27 18:19:37 +08:00
c.Prepare()
identify := c.Ctx.Input.Param(":key")
2017-05-13 13:04:53 +08:00
attach_id, _ := strconv.Atoi(c.Ctx.Input.Param(":attach_id"))
2017-04-27 18:19:37 +08:00
token := c.GetString("token")
member_id := 0
if c.Member != nil {
member_id = c.Member.MemberId
}
book_id := 0
//判断用户是否参与了项目
2017-05-13 13:04:53 +08:00
bookResult, err := models.NewBookResult().FindByIdentify(identify, member_id)
2017-04-27 18:19:37 +08:00
if err != nil {
//判断项目公开状态
book, err := models.NewBook().FindByFieldFirst("identify", identify)
2017-04-27 18:19:37 +08:00
if err != nil {
c.Abort("404")
}
//如果不是超级管理员则判断权限
if c.Member == nil || c.Member.Role != conf.MemberSuperRole {
//如果项目是私有的并且token不正确
2017-05-13 13:04:53 +08:00
if (book.PrivatelyOwned == 1 && token == "") || (book.PrivatelyOwned == 1 && book.PrivateToken != token) {
c.Abort("403")
}
2017-04-27 18:19:37 +08:00
}
2017-04-27 18:19:37 +08:00
book_id = book.BookId
2017-05-13 13:04:53 +08:00
} else {
2017-04-27 18:19:37 +08:00
book_id = bookResult.BookId
}
//查找附件
2017-05-13 13:04:53 +08:00
attachment, err := models.NewAttachment().Find(attach_id)
2017-04-27 18:19:37 +08:00
if err != nil {
beego.Error("DownloadAttachment => ", err)
2017-04-27 18:19:37 +08:00
if err == orm.ErrNoRows {
c.Abort("404")
} else {
c.Abort("500")
}
}
if attachment.BookId != book_id {
c.Abort("404")
}
2017-06-05 13:41:47 +08:00
c.Ctx.Output.Download(filepath.Join(commands.WorkingDirectory, attachment.FilePath), attachment.FileName)
2017-04-27 18:19:37 +08:00
c.StopRun()
}
2017-04-28 18:08:01 +08:00
2017-05-12 19:14:29 +08:00
//删除附件.
2017-05-13 13:04:53 +08:00
func (c *DocumentController) RemoveAttachment() {
2017-05-12 19:14:29 +08:00
c.Prepare()
2017-05-13 13:04:53 +08:00
attach_id, _ := c.GetInt("attach_id")
2017-05-12 19:14:29 +08:00
if attach_id <= 0 {
2017-05-13 13:04:53 +08:00
c.JsonResult(6001, "参数错误")
2017-05-12 19:14:29 +08:00
}
2017-05-13 13:04:53 +08:00
attach, err := models.NewAttachment().Find(attach_id)
2017-05-12 19:14:29 +08:00
if err != nil {
beego.Error(err)
2017-05-13 13:04:53 +08:00
c.JsonResult(6002, "附件不存在")
2017-05-12 19:14:29 +08:00
}
2017-05-13 13:04:53 +08:00
document, err := models.NewDocument().Find(attach.DocumentId)
2017-05-12 19:14:29 +08:00
if err != nil {
beego.Error(err)
2017-05-13 13:04:53 +08:00
c.JsonResult(6003, "文档不存在")
2017-05-12 19:14:29 +08:00
}
if c.Member.Role != conf.MemberSuperRole {
2017-05-13 13:04:53 +08:00
rel, err := models.NewRelationship().FindByBookIdAndMemberId(document.BookId, c.Member.MemberId)
2017-05-12 19:14:29 +08:00
if err != nil {
beego.Error(err)
2017-05-13 13:04:53 +08:00
c.JsonResult(6004, "权限不足")
2017-05-12 19:14:29 +08:00
}
if rel.RoleId == conf.BookObserver {
2017-05-13 13:04:53 +08:00
c.JsonResult(6004, "权限不足")
2017-05-12 19:14:29 +08:00
}
}
err = attach.Delete()
if err != nil {
beego.Error(err)
2017-05-13 13:04:53 +08:00
c.JsonResult(6005, "删除失败")
2017-05-12 19:14:29 +08:00
}
2017-06-05 13:41:47 +08:00
os.Remove(filepath.Join(commands.WorkingDirectory, attach.FilePath))
2017-06-05 13:38:06 +08:00
2017-05-13 13:04:53 +08:00
c.JsonResult(0, "ok", attach)
2017-05-12 19:14:29 +08:00
}
2017-05-13 13:04:53 +08:00
//删除文档.
2017-04-28 18:08:01 +08:00
func (c *DocumentController) Delete() {
c.Prepare()
identify := c.GetString("identify")
2017-05-13 13:04:53 +08:00
doc_id, err := c.GetInt("doc_id", 0)
2017-04-28 18:08:01 +08:00
book_id := 0
//如果是超级管理员则忽略权限判断
2017-06-05 10:52:14 +08:00
if c.Member.IsAdministrator() {
2017-05-13 13:04:53 +08:00
book, err := models.NewBook().FindByFieldFirst("identify", identify)
if err != nil {
beego.Error("FindByIdentify => ", err)
c.JsonResult(6002, "项目不存在或权限不足")
}
book_id = book.BookId
2017-05-13 13:04:53 +08:00
} else {
bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
2017-04-28 18:08:01 +08:00
if err != nil || bookResult.RoleId == conf.BookObserver {
beego.Error("FindByIdentify => ", err)
c.JsonResult(6002, "项目不存在或权限不足")
}
book_id = bookResult.BookId
2017-04-28 18:08:01 +08:00
}
if doc_id <= 0 {
2017-05-13 13:04:53 +08:00
c.JsonResult(6001, "参数错误")
2017-04-28 18:08:01 +08:00
}
2017-05-13 13:04:53 +08:00
doc, err := models.NewDocument().Find(doc_id)
2017-04-28 18:08:01 +08:00
if err != nil {
2017-05-13 13:04:53 +08:00
beego.Error("Delete => ", err)
c.JsonResult(6003, "删除失败")
2017-04-28 18:08:01 +08:00
}
//如果文档所属项目错误
if doc.BookId != book_id {
2017-05-13 13:04:53 +08:00
c.JsonResult(6004, "参数错误")
2017-04-28 18:08:01 +08:00
}
//递归删除项目下的文档以及子文档
2017-04-28 18:08:01 +08:00
err = doc.RecursiveDocument(doc.DocumentId)
if err != nil {
2017-05-13 13:04:53 +08:00
c.JsonResult(6005, "删除失败")
2017-04-28 18:08:01 +08:00
}
//重置文档数量统计
models.NewBook().ResetDocumentNumber(doc.BookId)
2017-05-13 13:04:53 +08:00
c.JsonResult(0, "ok")
2017-04-28 18:08:01 +08:00
}
//获取文档内容.
2017-05-13 13:04:53 +08:00
func (c *DocumentController) Content() {
2017-04-28 18:08:01 +08:00
c.Prepare()
identify := c.Ctx.Input.Param(":key")
2017-05-13 13:04:53 +08:00
doc_id, err := c.GetInt("doc_id")
2017-04-28 18:08:01 +08:00
if err != nil {
2017-05-13 13:04:53 +08:00
doc_id, _ = strconv.Atoi(c.Ctx.Input.Param(":id"))
2017-04-28 18:08:01 +08:00
}
book_id := 0
//如果是超级管理员,则忽略权限
2017-05-26 14:19:27 +08:00
if c.Member.IsAdministrator() {
2017-05-13 13:04:53 +08:00
book, err := models.NewBook().FindByFieldFirst("identify", identify)
if err != nil {
c.JsonResult(6002, "项目不存在或权限不足")
}
book_id = book.BookId
2017-05-13 13:04:53 +08:00
} else {
bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
2017-04-28 18:08:01 +08:00
if err != nil || bookResult.RoleId == conf.BookObserver {
beego.Error("FindByIdentify => ", err)
c.JsonResult(6002, "项目不存在或权限不足")
}
book_id = bookResult.BookId
2017-04-28 18:08:01 +08:00
}
if doc_id <= 0 {
2017-05-13 13:04:53 +08:00
c.JsonResult(6001, "参数错误")
2017-04-28 18:08:01 +08:00
}
if c.Ctx.Input.IsPost() {
2017-05-13 13:04:53 +08:00
markdown := strings.TrimSpace(c.GetString("markdown", ""))
2017-04-28 18:08:01 +08:00
content := c.GetString("html")
2017-05-13 13:04:53 +08:00
version, _ := c.GetInt64("version", 0)
2017-04-28 18:08:01 +08:00
is_cover := c.GetString("cover")
2017-05-13 13:04:53 +08:00
doc, err := models.NewDocument().Find(doc_id)
2017-04-28 18:08:01 +08:00
if err != nil {
2017-05-13 13:04:53 +08:00
c.JsonResult(6003, "读取文档错误")
2017-04-28 18:08:01 +08:00
}
if doc.BookId != book_id {
2017-05-13 13:04:53 +08:00
c.JsonResult(6004, "保存的文档不属于指定项目")
2017-04-28 18:08:01 +08:00
}
2017-05-13 13:04:53 +08:00
if doc.Version != version && !strings.EqualFold(is_cover, "yes") {
beego.Info("%d|", version, doc.Version)
c.JsonResult(6005, "文档已被修改确定要覆盖吗?")
2017-04-28 18:08:01 +08:00
}
2017-05-20 15:27:03 +08:00
history := models.NewDocumentHistory()
history.DocumentId = doc_id
history.Content = doc.Content
history.Markdown = doc.Markdown
history.DocumentName = doc.DocumentName
history.ModifyAt = c.Member.MemberId
history.MemberId = doc.MemberId
history.ParentId = doc.ParentId
history.Version = time.Now().Unix()
history.Action = "modify"
history.ActionName = "修改文档"
2017-05-13 13:04:53 +08:00
if markdown == "" && content != "" {
2017-04-28 18:08:01 +08:00
doc.Markdown = content
2017-05-13 13:04:53 +08:00
} else {
2017-04-28 18:08:01 +08:00
doc.Markdown = markdown
}
doc.Version = time.Now().Unix()
doc.Content = content
2017-05-13 13:04:53 +08:00
if err := doc.InsertOrUpdate(); err != nil {
beego.Error("InsertOrUpdate => ", err)
c.JsonResult(6006, "保存失败")
2017-04-28 18:08:01 +08:00
}
2017-05-20 15:27:03 +08:00
//如果启用了文档历史,则添加历史文档
if c.EnableDocumentHistory {
2017-06-05 10:52:14 +08:00
_, err = history.InsertOrUpdate()
2017-05-20 15:27:03 +08:00
if err != nil {
2017-06-05 10:52:14 +08:00
beego.Error("DocumentHistory InsertOrUpdate => ", err)
2017-05-20 15:27:03 +08:00
}
}
2017-04-28 18:08:01 +08:00
2017-05-13 13:04:53 +08:00
c.JsonResult(0, "ok", doc)
2017-04-28 18:08:01 +08:00
}
2017-05-13 13:04:53 +08:00
doc, err := models.NewDocument().Find(doc_id)
2017-04-28 18:08:01 +08:00
if err != nil {
2017-05-13 13:04:53 +08:00
c.JsonResult(6003, "文档不存在")
2017-04-28 18:08:01 +08:00
}
2017-05-13 13:04:53 +08:00
attach, err := models.NewAttachment().FindListByDocumentId(doc.DocumentId)
2017-05-12 19:14:29 +08:00
if err == nil {
doc.AttachList = attach
}
2017-05-13 13:04:53 +08:00
c.JsonResult(0, "ok", doc)
2017-04-28 18:08:01 +08:00
}
2017-05-06 16:16:27 +08:00
//导出文件
func (c *DocumentController) Export() {
c.Prepare()
c.TplName = "document/export.tpl"
identify := c.Ctx.Input.Param(":key")
2017-05-09 10:04:56 +08:00
2017-05-06 16:16:27 +08:00
output := c.GetString("output")
2017-05-09 10:04:56 +08:00
2017-05-06 16:16:27 +08:00
token := c.GetString("token")
if identify == "" {
c.Abort("404")
}
//如果没有开启你们访问则跳转到登录
if !c.EnableAnonymous && c.Member == nil {
2017-05-13 13:04:53 +08:00
c.Redirect(beego.URLFor("AccountController.Login"), 302)
2017-05-06 16:16:27 +08:00
return
}
2017-05-12 14:21:29 +08:00
bookResult := models.NewBookResult()
2017-06-05 10:52:14 +08:00
if c.Member != nil && c.Member.IsAdministrator() {
2017-05-13 13:04:53 +08:00
book, err := models.NewBook().FindByIdentify(identify)
2017-05-12 14:21:29 +08:00
if err != nil {
beego.Error(err)
c.Abort("500")
}
bookResult = book.ToBookResult()
2017-05-13 13:04:53 +08:00
} else {
2017-05-12 14:21:29 +08:00
bookResult = isReadable(identify, token, c)
}
docs, err := models.NewDocument().FindListByBookId(bookResult.BookId)
2017-05-06 16:16:27 +08:00
if err != nil {
beego.Error(err)
c.Abort("500")
}
if output == "pdf" {
2017-05-10 18:06:54 +08:00
exe := beego.AppConfig.String("wkhtmltopdf")
if exe == "" {
2017-05-13 13:04:53 +08:00
c.TplName = "errors/error.tpl"
2017-05-10 18:06:54 +08:00
c.Data["ErrorMessage"] = "没有配置PDF导出程序"
c.Data["ErrorCode"] = 50010
return
}
2017-05-12 14:21:29 +08:00
dpath := "cache/" + bookResult.Identify
2017-05-06 16:16:27 +08:00
os.MkdirAll(dpath, 0766)
2017-04-28 18:08:01 +08:00
2017-05-06 16:16:27 +08:00
pathList := list.New()
2017-04-28 18:08:01 +08:00
2017-05-12 14:21:29 +08:00
RecursiveFun(0, "", dpath, c, bookResult, docs, pathList)
2017-04-28 18:08:01 +08:00
2017-05-12 14:21:29 +08:00
defer os.RemoveAll(dpath)
2017-04-28 18:08:01 +08:00
2017-05-06 16:16:27 +08:00
os.MkdirAll("./cache", 0766)
2017-06-05 10:52:14 +08:00
pdfpath := filepath.Join("cache", identify+"_"+c.CruSession.SessionID()+".pdf")
2017-05-06 16:16:27 +08:00
2017-05-13 13:04:53 +08:00
if _, err := os.Stat(pdfpath); os.IsNotExist(err) {
2017-05-13 12:12:37 +08:00
2017-05-13 12:15:55 +08:00
wkhtmltopdf.SetPath(beego.AppConfig.String("wkhtmltopdf"))
2017-05-13 12:12:37 +08:00
pdfg, err := wkhtmltopdf.NewPDFGenerator()
2017-05-13 13:04:53 +08:00
pdfg.MarginBottom.Set(35)
2017-05-13 12:12:37 +08:00
if err != nil {
beego.Error(err)
c.Abort("500")
2017-05-10 18:06:54 +08:00
}
2017-05-06 16:16:27 +08:00
2017-05-13 12:12:37 +08:00
for e := pathList.Front(); e != nil; e = e.Next() {
2017-05-13 13:04:53 +08:00
if page, ok := e.Value.(string); ok {
2017-05-13 12:15:55 +08:00
pdfg.AddPage(wkhtmltopdf.NewPage(page))
}
2017-05-13 12:12:37 +08:00
}
err = pdfg.Create()
if err != nil {
beego.Error(err)
c.Abort("500")
}
2017-05-06 16:16:27 +08:00
2017-05-13 12:12:37 +08:00
err = pdfg.WriteFile(pdfpath)
if err != nil {
beego.Error(err)
}
2017-05-10 18:06:54 +08:00
}
2017-05-06 16:16:27 +08:00
2017-05-13 13:04:53 +08:00
c.Ctx.Output.Download(pdfpath, identify+".pdf")
2017-05-06 16:16:27 +08:00
2017-05-12 14:21:29 +08:00
defer os.Remove(pdfpath)
2017-05-06 16:16:27 +08:00
c.StopRun()
}
2017-05-12 14:21:29 +08:00
c.Abort("404")
2017-05-06 16:16:27 +08:00
}
2017-05-13 13:04:53 +08:00
//生成项目访问的二维码.
func (c *DocumentController) QrCode() {
c.Prepare()
identify := c.GetString(":key")
book, err := models.NewBook().FindByIdentify(identify)
if err != nil || book.BookId <= 0 {
c.Abort("404")
}
uri := c.BaseUrl() + beego.URLFor("DocumentController.Index", ":key", identify)
code, err := qr.Encode(uri, qr.L, qr.Unicode)
if err != nil {
beego.Error(err)
c.Abort("500")
}
code, err = barcode.Scale(code, 150, 150)
if err != nil {
beego.Error(err)
c.Abort("500")
}
c.Ctx.ResponseWriter.Header().Set("Content-Type", "image/png")
2017-05-13 13:15:21 +08:00
//imgpath := filepath.Join("cache","qrcode",identify + ".png")
2017-05-13 13:04:53 +08:00
err = png.Encode(c.Ctx.ResponseWriter, code)
if err != nil {
beego.Error(err)
c.Abort("500")
}
}
2017-05-15 14:59:23 +08:00
//项目内搜索.
2017-06-05 10:52:14 +08:00
func (c *DocumentController) Search() {
2017-05-13 17:41:17 +08:00
c.Prepare()
2017-05-15 14:59:23 +08:00
identify := c.Ctx.Input.Param(":key")
token := c.GetString("token")
keyword := strings.TrimSpace(c.GetString("keyword"))
2017-06-05 10:52:14 +08:00
if identify == "" {
c.JsonResult(6001, "参数错误")
2017-05-15 14:59:23 +08:00
}
if !c.EnableAnonymous && c.Member == nil {
c.Redirect(beego.URLFor("AccountController.Login"), 302)
return
}
2017-06-05 10:52:14 +08:00
bookResult := isReadable(identify, token, c)
2017-05-15 14:59:23 +08:00
2017-06-05 10:52:14 +08:00
docs, err := models.NewDocumentSearchResult().SearchDocument(keyword, bookResult.BookId)
2017-05-15 14:59:23 +08:00
if err != nil {
beego.Error(err)
2017-06-05 10:52:14 +08:00
c.JsonResult(6002, "搜索结果错误")
2017-05-15 14:59:23 +08:00
}
if len(docs) < 0 {
2017-06-05 10:52:14 +08:00
c.JsonResult(404, "没有数据库")
2017-05-15 14:59:23 +08:00
}
2017-06-05 10:52:14 +08:00
for _, doc := range docs {
2017-05-15 14:59:23 +08:00
doc.BookId = bookResult.BookId
doc.BookName = bookResult.BookName
doc.Description = bookResult.Description
doc.BookIdentify = bookResult.Identify
}
2017-06-05 10:52:14 +08:00
c.JsonResult(0, "ok", docs)
2017-05-13 17:41:17 +08:00
}
2017-05-20 15:27:03 +08:00
//文档历史列表.
func (c *DocumentController) History() {
c.Prepare()
2017-05-25 15:19:17 +08:00
c.TplName = "document/history.tpl"
2017-05-20 15:27:03 +08:00
identify := c.GetString("identify")
doc_id, err := c.GetInt("doc_id", 0)
pageIndex, _ := c.GetInt("page", 1)
book_id := 0
//如果是超级管理员则忽略权限判断
2017-06-05 10:52:14 +08:00
if c.Member.IsAdministrator() {
2017-05-20 15:27:03 +08:00
book, err := models.NewBook().FindByFieldFirst("identify", identify)
if err != nil {
beego.Error("FindByIdentify => ", err)
2017-05-25 15:19:17 +08:00
c.Data["ErrorMessage"] = "项目不存在或权限不足"
return
2017-05-20 15:27:03 +08:00
}
book_id = book.BookId
2017-05-25 15:19:17 +08:00
c.Data["Model"] = book
2017-05-20 15:27:03 +08:00
} else {
bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
if err != nil || bookResult.RoleId == conf.BookObserver {
beego.Error("FindByIdentify => ", err)
2017-05-25 15:19:17 +08:00
c.Data["ErrorMessage"] = "项目不存在或权限不足"
return
2017-05-20 15:27:03 +08:00
}
book_id = bookResult.BookId
2017-05-25 15:19:17 +08:00
c.Data["Model"] = bookResult
2017-05-20 15:27:03 +08:00
}
if doc_id <= 0 {
2017-05-25 15:19:17 +08:00
c.Data["ErrorMessage"] = "参数错误"
return
2017-05-20 15:27:03 +08:00
}
doc, err := models.NewDocument().Find(doc_id)
if err != nil {
beego.Error("Delete => ", err)
2017-05-25 15:19:17 +08:00
c.Data["ErrorMessage"] = "获取历史失败"
return
2017-05-20 15:27:03 +08:00
}
//如果文档所属项目错误
if doc.BookId != book_id {
2017-05-25 15:19:17 +08:00
c.Data["ErrorMessage"] = "参数错误"
return
2017-05-20 15:27:03 +08:00
}
2017-06-05 10:52:14 +08:00
historis, totalCount, err := models.NewDocumentHistory().FindToPager(doc_id, pageIndex, conf.PageSize)
2017-05-20 15:27:03 +08:00
if err != nil {
2017-06-05 10:52:14 +08:00
beego.Error("FindToPager => ", err)
2017-05-25 15:19:17 +08:00
c.Data["ErrorMessage"] = "获取历史失败"
return
2017-05-20 15:27:03 +08:00
}
2017-05-25 15:19:17 +08:00
c.Data["List"] = historis
c.Data["PageHtml"] = ""
c.Data["Document"] = doc
2017-05-20 15:27:03 +08:00
if totalCount > 0 {
html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, conf.PageSize, totalCount)
2017-06-05 10:52:14 +08:00
c.Data["PageHtml"] = html
2017-05-25 15:19:17 +08:00
}
}
func (c *DocumentController) DeleteHistory() {
c.Prepare()
c.TplName = "document/history.tpl"
identify := c.GetString("identify")
doc_id, err := c.GetInt("doc_id", 0)
2017-06-05 10:52:14 +08:00
history_id, _ := c.GetInt("history_id", 0)
2017-05-25 15:19:17 +08:00
if history_id <= 0 {
2017-06-05 10:52:14 +08:00
c.JsonResult(6001, "参数错误")
2017-05-25 15:19:17 +08:00
}
book_id := 0
//如果是超级管理员则忽略权限判断
2017-06-05 10:52:14 +08:00
if c.Member.IsAdministrator() {
2017-05-25 15:19:17 +08:00
book, err := models.NewBook().FindByFieldFirst("identify", identify)
if err != nil {
beego.Error("FindByIdentify => ", err)
c.JsonResult(6002, "项目不存在或权限不足")
}
book_id = book.BookId
} else {
bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
if err != nil || bookResult.RoleId == conf.BookObserver {
beego.Error("FindByIdentify => ", err)
c.JsonResult(6002, "项目不存在或权限不足")
}
book_id = bookResult.BookId
}
if doc_id <= 0 {
2017-06-05 10:52:14 +08:00
c.JsonResult(6001, "参数错误")
2017-05-25 15:19:17 +08:00
}
doc, err := models.NewDocument().Find(doc_id)
if err != nil {
beego.Error("Delete => ", err)
2017-06-05 10:52:14 +08:00
c.JsonResult(6001, "获取历史失败")
2017-05-25 15:19:17 +08:00
}
//如果文档所属项目错误
if doc.BookId != book_id {
2017-06-05 10:52:14 +08:00
c.JsonResult(6001, "参数错误")
2017-05-25 15:19:17 +08:00
}
2017-06-05 10:52:14 +08:00
err = models.NewDocumentHistory().Delete(history_id, doc_id)
2017-05-25 15:19:17 +08:00
if err != nil {
beego.Error(err)
2017-06-05 10:52:14 +08:00
c.JsonResult(6002, "删除失败")
2017-05-25 15:19:17 +08:00
}
2017-06-05 10:52:14 +08:00
c.JsonResult(0, "ok")
2017-05-25 15:19:17 +08:00
}
2017-06-05 10:52:14 +08:00
func (c *DocumentController) RestoreHistory() {
2017-05-25 15:19:17 +08:00
c.Prepare()
c.TplName = "document/history.tpl"
identify := c.GetString("identify")
doc_id, err := c.GetInt("doc_id", 0)
2017-06-05 10:52:14 +08:00
history_id, _ := c.GetInt("history_id", 0)
2017-05-25 15:19:17 +08:00
if history_id <= 0 {
2017-06-05 10:52:14 +08:00
c.JsonResult(6001, "参数错误")
2017-05-25 15:19:17 +08:00
}
book_id := 0
//如果是超级管理员则忽略权限判断
2017-06-05 10:52:14 +08:00
if c.Member.IsAdministrator() {
2017-05-25 15:19:17 +08:00
book, err := models.NewBook().FindByFieldFirst("identify", identify)
if err != nil {
beego.Error("FindByIdentify => ", err)
c.JsonResult(6002, "项目不存在或权限不足")
}
book_id = book.BookId
} else {
bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
if err != nil || bookResult.RoleId == conf.BookObserver {
beego.Error("FindByIdentify => ", err)
c.JsonResult(6002, "项目不存在或权限不足")
}
book_id = bookResult.BookId
}
if doc_id <= 0 {
2017-06-05 10:52:14 +08:00
c.JsonResult(6001, "参数错误")
2017-05-20 15:27:03 +08:00
}
2017-05-25 15:19:17 +08:00
doc, err := models.NewDocument().Find(doc_id)
if err != nil {
beego.Error("Delete => ", err)
2017-06-05 10:52:14 +08:00
c.JsonResult(6001, "获取历史失败")
2017-05-25 15:19:17 +08:00
}
//如果文档所属项目错误
if doc.BookId != book_id {
2017-06-05 10:52:14 +08:00
c.JsonResult(6001, "参数错误")
2017-05-25 15:19:17 +08:00
}
2017-06-05 10:52:14 +08:00
err = models.NewDocumentHistory().Restore(history_id, doc_id, c.Member.MemberId)
2017-05-25 15:19:17 +08:00
if err != nil {
beego.Error(err)
2017-06-05 10:52:14 +08:00
c.JsonResult(6002, "删除失败")
2017-05-25 15:19:17 +08:00
}
2017-06-05 10:52:14 +08:00
c.JsonResult(0, "ok", doc)
2017-05-20 15:27:03 +08:00
}
2017-06-09 18:14:55 +08:00
func (c *DocumentController) Compare() {
c.Prepare()
c.TplName = "document/compare.tpl"
history_id ,_ := strconv.Atoi(c.Ctx.Input.Param(":id"))
identify := c.Ctx.Input.Param(":key")
book_id := 0
editor := "markdown"
//如果是超级管理员则忽略权限判断
if c.Member.IsAdministrator() {
book, err := models.NewBook().FindByFieldFirst("identify", identify)
if err != nil {
beego.Error("DocumentController.Compare => ", err)
c.Abort("403")
return
}
book_id = book.BookId
c.Data["Model"] = book
editor = book.Editor
} else {
bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
if err != nil || bookResult.RoleId == conf.BookObserver {
beego.Error("FindByIdentify => ", err)
c.Abort("403")
return
}
book_id = bookResult.BookId
c.Data["Model"] = bookResult
editor = bookResult.Editor
}
if history_id <= 0 {
c.ShowErrorPage(60002,"参数错误")
}
history,err := models.NewDocumentHistory().Find(history_id)
if err != nil {
beego.Error("DocumentController.Compare => ",err)
c.ShowErrorPage(60003,err.Error())
}
doc,err := models.NewDocument().Find(history.DocumentId)
if doc.BookId != book_id {
c.ShowErrorPage(60002,"参数错误")
}
c.Data["HistoryId"] = history_id
c.Data["DocumentId"] = doc.DocumentId
if editor == "markdown" {
c.Data["HistoryContent"] = history.Markdown
c.Data["Content"] = doc.Markdown
}else{
c.Data["HistoryContent"] = template.HTML(history.Content)
c.Data["Content"] = template.HTML(doc.Content)
}
2017-06-09 18:14:55 +08:00
}
2017-05-09 10:04:56 +08:00
//递归生成文档序列数组.
2017-05-13 13:04:53 +08:00
func RecursiveFun(parent_id int, prefix, dpath string, c *DocumentController, book *models.BookResult, docs []*models.Document, paths *list.List) {
2017-05-06 16:16:27 +08:00
for _, item := range docs {
if item.ParentId == parent_id {
name := prefix + strconv.Itoa(item.ParentId) + strconv.Itoa(item.OrderSort) + strconv.Itoa(item.DocumentId)
2017-05-13 13:04:53 +08:00
fpath := dpath + "/" + name + ".html"
2017-05-06 16:16:27 +08:00
paths.PushBack(fpath)
2017-05-11 17:15:56 +08:00
f, err := os.OpenFile(fpath, os.O_CREATE|os.O_RDWR, 0777)
2017-05-06 16:16:27 +08:00
if err != nil {
beego.Error(err)
c.Abort("500")
}
2017-05-13 13:04:53 +08:00
html, err := c.ExecuteViewPathTemplate("document/export.tpl", map[string]interface{}{"Model": book, "Lists": item, "BaseUrl": c.BaseUrl()})
2017-05-06 16:16:27 +08:00
if err != nil {
f.Close()
beego.Error(err)
c.Abort("500")
}
2017-06-05 10:52:14 +08:00
buf := bytes.NewReader([]byte(html))
doc, err := goquery.NewDocumentFromReader(buf)
doc.Find("img").Each(func(i int, contentSelection *goquery.Selection) {
if src, ok := contentSelection.Attr("src"); ok && strings.HasPrefix(src, "/uploads/") {
contentSelection.SetAttr("src", c.BaseUrl()+src)
}
})
html, err = doc.Html()
if err != nil {
f.Close()
beego.Error(err)
c.Abort("500")
}
//html = strings.Replace(html,"<img src=\"/uploads","<img src=\""+ c.BaseUrl() +"/uploads",-1)
2017-05-06 16:16:27 +08:00
f.WriteString(html)
f.Close()
for _, sub := range docs {
if sub.ParentId == item.DocumentId {
2017-05-13 13:04:53 +08:00
RecursiveFun(item.DocumentId, name, dpath, c, book, docs, paths)
break
2017-05-06 16:16:27 +08:00
}
}
}
}
}