mindoc/controllers/DocumentController.go

1296 lines
33 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 (
"context"
2017-05-13 13:04:53 +08:00
"encoding/json"
"fmt"
"html/template"
"image/png"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/beego/beego/v2/client/orm"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
"github.com/mindoc-org/mindoc/conf"
"github.com/mindoc-org/mindoc/models"
"github.com/mindoc-org/mindoc/utils"
"github.com/mindoc-org/mindoc/utils/cryptil"
"github.com/mindoc-org/mindoc/utils/filetil"
"github.com/mindoc-org/mindoc/utils/gopool"
"github.com/mindoc-org/mindoc/utils/pagination"
"github.com/russross/blackfriday/v2"
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 (c *DocumentController) Index() {
c.Prepare()
identify := c.Ctx.Input.Param(":key")
token := c.GetString("token")
if identify == "" {
c.ShowErrorPage(404, "项目不存在或已删除")
}
// 如果没有开启匿名访问则跳转到登录
2018-11-14 15:57:55 +08:00
if !c.EnableAnonymous && !c.isUserLoggedIn() {
promptUserToLogIn(c)
return
}
2018-11-14 15:57:55 +08:00
bookResult := c.isReadable(identify, token)
c.TplName = "document/" + bookResult.Theme + "_read.tpl"
selected := 0
if bookResult.IsUseFirstDocument {
doc, err := bookResult.FindFirstDocumentByBookId(bookResult.BookId)
if err == nil {
selected = doc.DocumentId
c.Data["Title"] = doc.DocumentName
c.Data["Content"] = template.HTML(doc.Release)
c.Data["Description"] = utils.AutoSummary(doc.Release, 120)
}
} else {
c.Data["Title"] = "概要"
c.Data["Content"] = template.HTML(blackfriday.Run([]byte(bookResult.Description)))
}
tree, err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId, selected)
if err != nil {
2018-07-25 17:47:06 +08:00
if err == orm.ErrNoRows {
2018-08-29 10:23:43 +08:00
c.ShowErrorPage(404, "当前项目没有文档")
2018-07-25 17:47:06 +08:00
} else {
logs.Error("生成项目文档树时出错 -> ", err)
c.ShowErrorPage(500, "生成项目文档树时出错")
2018-07-25 17:47:06 +08:00
}
}
c.Data["Model"] = bookResult
c.Data["Result"] = template.HTML(tree)
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")
c.Data["DocumentId"] = id
2017-05-13 13:04:53 +08:00
if identify == "" || id == "" {
c.ShowErrorPage(404, "项目不存或已删除")
}
// 如果没有开启匿名访问则跳转到登录
2018-11-14 15:57:55 +08:00
if !c.EnableAnonymous && !c.isUserLoggedIn() {
promptUserToLogIn(c)
return
}
2018-11-14 15:57:55 +08:00
bookResult := c.isReadable(identify, token)
c.TplName = fmt.Sprintf("document/%s_read.tpl", bookResult.Theme)
doc := models.NewDocument()
2018-03-23 11:17:52 +08:00
if docId, err := strconv.Atoi(id); err == nil {
doc, err = doc.FromCacheById(docId)
if err != nil || doc == nil {
logs.Error("从缓存中读取文档时失败 ->", err)
c.ShowErrorPage(404, "文档不存在或已删除")
return
}
2017-05-13 13:04:53 +08:00
} else {
doc, err = doc.FromCacheByIdentify(id, bookResult.BookId)
if err != nil || doc == nil {
2018-07-25 17:47:06 +08:00
if err == orm.ErrNoRows {
c.ShowErrorPage(404, "文档不存在或已删除")
} else {
logs.Error("从缓存查询文档时出错 ->", err)
c.ShowErrorPage(500, "未知异常")
2018-07-25 17:47:06 +08:00
}
return
}
}
if doc.BookId != bookResult.BookId {
c.ShowErrorPage(404, "文档不存在或已删除")
}
doc.Processor()
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
}
2021-04-01 09:23:55 +08:00
doc.IncrViewCount(doc.DocumentId)
doc.ViewCount = doc.ViewCount + 1
doc.PutToCache()
2021-03-31 19:32:18 +08:00
if c.IsAjax() {
2017-05-13 13:04:53 +08:00
var data struct {
DocTitle string `json:"doc_title"`
Body string `json:"body"`
Title string `json:"title"`
Version int64 `json:"version"`
ViewCount int `json:"view_count"`
}
data.DocTitle = doc.DocumentName
data.Body = doc.Release
data.Title = doc.DocumentName + " - Powered by MinDoc"
data.Version = doc.Version
data.ViewCount = doc.ViewCount
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)
2018-07-25 17:47:06 +08:00
if err != nil && err != orm.ErrNoRows {
logs.Error("生成项目文档树时出错 ->", err)
2018-07-25 17:47:06 +08:00
c.ShowErrorPage(500, "生成项目文档树时出错")
}
c.Data["Description"] = utils.AutoSummary(doc.Release, 120)
c.Data["Model"] = bookResult
c.Data["Result"] = template.HTML(tree)
c.Data["Title"] = doc.DocumentName
c.Data["Content"] = template.HTML(doc.Release)
c.Data["ViewCount"] = doc.ViewCount
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.ShowErrorPage(404, "无法解析项目标识")
2017-04-27 18:19:37 +08:00
}
2017-04-26 18:17:38 +08:00
bookResult := models.NewBookResult()
2017-05-12 11:41:59 +08:00
var err error
2018-11-26 18:54:50 +08:00
// 如果是管理者,则不判断权限
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, "项目不存在或权限不足")
}
2017-04-26 18:17:38 +08:00
bookResult = models.NewBookResult().ToBookResult(*book)
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 {
if err == orm.ErrNoRows || err == models.ErrPermissionDenied {
2018-07-25 17:47:06 +08:00
c.ShowErrorPage(403, "项目不存在或没有权限")
} else {
logs.Error("查询项目时出错 -> ", err)
c.ShowErrorPage(500, "查询项目时出错")
2018-07-25 17:47:06 +08:00
}
return
}
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" {
c.TplName = "document/new_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 {
logs.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("[]")
}
}
c.Data["BaiDuMapKey"] = web.AppConfig.DefaultString("baidumapkey", "")
if conf.GetUploadFileSize() > 0 {
c.Data["UploadFileSize"] = conf.GetUploadFileSize()
2018-11-14 15:57:55 +08:00
} else {
c.Data["UploadFileSize"] = "undefined"
}
2017-04-27 18:19:37 +08:00
}
// 创建一个文档
2017-04-27 18:19:37 +08:00
func (c *DocumentController) Create() {
identify := c.GetString("identify")
2018-03-23 11:17:52 +08:00
docIdentify := c.GetString("doc_identify")
docName := c.GetString("doc_name")
parentId, _ := c.GetInt("parent_id", 0)
docId, _ := c.GetInt("doc_id", 0)
isOpen, _ := c.GetInt("is_open", 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
}
2018-03-23 11:17:52 +08:00
if docName == "" {
2017-05-13 13:04:53 +08:00
c.JsonResult(6004, "文档名称不能为空")
2017-04-27 18:19:37 +08:00
}
2018-03-23 11:17:52 +08:00
bookId := 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 {
logs.Error(err)
c.JsonResult(6002, "项目不存在或权限不足")
}
2018-03-23 11:17:52 +08:00
bookId = 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 {
logs.Error("FindByIdentify => ", err)
c.JsonResult(6002, "项目不存在或权限不足")
}
2018-03-23 11:17:52 +08:00
bookId = bookResult.BookId
2017-04-27 18:19:37 +08:00
}
2018-03-23 11:17:52 +08:00
if docIdentify != "" {
if ok, err := regexp.MatchString(`[a-z]+[a-zA-Z0-9_.\-]*$`, docIdentify); !ok || err != nil {
c.JsonResult(6003, "文档标识只能包含小写字母、数字,以及“-”、“.”和“_”符号")
}
d, _ := models.NewDocument().FindByIdentityFirst(docIdentify, bookId)
2018-03-23 11:17:52 +08:00
if d.DocumentId > 0 && d.DocumentId != docId {
c.JsonResult(6006, "文档标识已被使用")
}
}
if parentId > 0 {
doc, err := models.NewDocument().Find(parentId)
if err != nil || doc.BookId != bookId {
2017-05-13 13:04:53 +08:00
c.JsonResult(6003, "父分类不存在")
2017-04-27 18:19:37 +08:00
}
}
2018-03-23 11:17:52 +08:00
document, _ := models.NewDocument().Find(docId)
2017-04-28 18:08:01 +08:00
2017-04-27 18:19:37 +08:00
document.MemberId = c.Member.MemberId
2018-03-23 11:17:52 +08:00
document.BookId = bookId
2018-03-23 11:17:52 +08:00
document.Identify = docIdentify
2017-04-28 18:08:01 +08:00
document.Version = time.Now().Unix()
2018-03-23 11:17:52 +08:00
document.DocumentName = docName
document.ParentId = parentId
2017-04-28 18:08:01 +08:00
if isOpen == 1 {
document.IsOpen = 1
} else if isOpen == 2 {
document.IsOpen = 2
} else {
document.IsOpen = 0
}
2017-05-13 13:04:53 +08:00
if err := document.InsertOrUpdate(); err != nil {
logs.Error("添加或更新文档时出错 -> ", err)
2017-05-13 13:04:53 +08:00
c.JsonResult(6005, "保存失败")
} else {
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")
docId, _ := c.GetInt("doc_id")
isAttach := 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)
if err == http.ErrMissingFile || moreFile == nil {
2017-04-27 18:19:37 +08:00
name = "editormd-image-file"
2017-05-13 13:04:53 +08:00
file, moreFile, err = c.GetFile(name)
if err == http.ErrMissingFile || moreFile == nil {
2017-05-13 13:04:53 +08:00
c.JsonResult(6003, "没有发现需要上传的文件")
return
2017-04-27 18:19:37 +08:00
}
}
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()
type Size interface {
Size() int64
}
if conf.GetUploadFileSize() > 0 && moreFile.Size > conf.GetUploadFileSize() {
c.JsonResult(6009, "文件大小超过了限定的最大值")
}
2017-04-27 18:19:37 +08:00
ext := filepath.Ext(moreFile.Filename)
//文件必须带有后缀名
2017-04-27 18:19:37 +08:00
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) == false {
c.JsonResult(6004, "不允许的文件类型")
2017-04-27 18:19:37 +08:00
}
bookId := 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, "文档不存在或权限不足")
}
2017-04-27 18:19:37 +08:00
bookId = book.BookId
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 {
logs.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, "权限不足")
}
bookId = book.BookId
2017-04-27 18:19:37 +08:00
}
if docId > 0 {
doc, err := models.NewDocument().Find(docId)
if err != nil {
2017-05-13 13:04:53 +08:00
c.JsonResult(6007, "文档不存在")
}
if doc.BookId != bookId {
2017-05-13 13:04:53 +08:00
c.JsonResult(6008, "文档不属于指定的项目")
}
}
2017-04-27 18:19:37 +08:00
fileName := "m_" + cryptil.UniqueId() + "_r"
filePath := filepath.Join(conf.WorkingDirectory, "uploads", identify)
//将图片和文件分开存放
2019-02-27 17:32:06 +08:00
if filetil.IsImageExt(moreFile.Filename) {
filePath = filepath.Join(filePath, "images", fileName+ext)
} else {
filePath = filepath.Join(filePath, "files", fileName+ext)
}
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 {
logs.Error("保存文件失败 -> ", err)
2017-05-13 13:04:53 +08:00
c.JsonResult(6005, "保存文件失败")
2017-04-27 18:19:37 +08:00
}
2017-04-27 18:19:37 +08:00
attachment := models.NewAttachment()
attachment.BookId = bookId
2017-04-27 18:19:37 +08:00
attachment.FileName = moreFile.Filename
attachment.CreateAt = c.Member.MemberId
attachment.FileExt = ext
attachment.FilePath = strings.TrimPrefix(filePath, conf.WorkingDirectory)
attachment.DocumentId = docId
2017-05-12 19:14:29 +08:00
if fileInfo, err := os.Stat(filePath); err == nil {
attachment.FileSize = float64(fileInfo.Size())
}
if docId > 0 {
attachment.DocumentId = docId
}
2017-04-27 18:19:37 +08:00
2019-03-11 17:36:45 +08:00
if filetil.IsImageExt(moreFile.Filename) {
attachment.HttpPath = "/" + strings.Replace(strings.TrimPrefix(filePath, conf.WorkingDirectory), "\\", "/", -1)
2017-06-05 13:41:47 +08:00
if strings.HasPrefix(attachment.HttpPath, "//") {
2018-03-13 19:20:50 +08:00
attachment.HttpPath = conf.URLForWithCdnImage(string(attachment.HttpPath[1:]))
2017-06-05 13:38:06 +08:00
}
isAttach = 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)
logs.Error("文件保存失败 ->", err)
2017-05-13 13:04:53 +08:00
c.JsonResult(6006, "文件保存失败")
2017-04-27 18:19:37 +08:00
}
2017-04-27 18:19:37 +08:00
if attachment.HttpPath == "" {
attachment.HttpPath = conf.URLForNotHost("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 {
logs.Error("保存文件失败 ->", err)
2017-05-13 13:04:53 +08:00
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": isAttach,
2017-05-13 13:04:53 +08:00
"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()
}
// 下载附件
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")
2018-03-13 14:14:56 +08:00
attachId, _ := strconv.Atoi(c.Ctx.Input.Param(":attach_id"))
2017-04-27 18:19:37 +08:00
token := c.GetString("token")
2018-03-13 14:14:56 +08:00
memberId := 0
2017-04-27 18:19:37 +08:00
if c.Member != nil {
2018-03-13 14:14:56 +08:00
memberId = c.Member.MemberId
2017-04-27 18:19:37 +08:00
}
2018-03-13 14:14:56 +08:00
bookId := 0
2017-04-27 18:19:37 +08:00
// 判断用户是否参与了项目
2018-03-13 14:14:56 +08:00
bookResult, err := models.NewBookResult().FindByIdentify(identify, memberId)
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 {
2018-07-25 17:47:06 +08:00
if err == orm.ErrNoRows {
c.ShowErrorPage(404, "项目不存在或已删除")
} else {
logs.Error("查找项目时出错 ->", err)
c.ShowErrorPage(500, "系统错误")
2018-07-25 17:47:06 +08:00
}
2017-04-27 18:19:37 +08:00
}
// 如果不是超级管理员则判断权限
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.ShowErrorPage(403, "权限不足")
}
2017-04-27 18:19:37 +08:00
}
2018-03-13 14:14:56 +08:00
bookId = book.BookId
2017-05-13 13:04:53 +08:00
} else {
2018-03-13 14:14:56 +08:00
bookId = bookResult.BookId
2017-04-27 18:19:37 +08:00
}
// 查找附件
2018-03-13 14:14:56 +08:00
attachment, err := models.NewAttachment().Find(attachId)
2017-04-27 18:19:37 +08:00
if err != nil {
logs.Error("查找附件时出错 -> ", err)
2017-04-27 18:19:37 +08:00
if err == orm.ErrNoRows {
c.ShowErrorPage(404, "附件不存在或已删除")
2017-04-27 18:19:37 +08:00
} else {
c.ShowErrorPage(500, "查找附件时出错")
2017-04-27 18:19:37 +08:00
}
}
2018-03-13 14:14:56 +08:00
if attachment.BookId != bookId {
c.ShowErrorPage(404, "附件不存在或已删除")
2017-04-27 18:19:37 +08:00
}
c.Ctx.Output.Download(filepath.Join(conf.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-13 13:04:53 +08:00
func (c *DocumentController) RemoveAttachment() {
2017-05-12 19:14:29 +08:00
c.Prepare()
attachId, _ := c.GetInt("attach_id")
2017-05-12 19:14:29 +08:00
if attachId <= 0 {
2017-05-13 13:04:53 +08:00
c.JsonResult(6001, "参数错误")
2017-05-12 19:14:29 +08:00
}
attach, err := models.NewAttachment().Find(attachId)
2017-05-12 19:14:29 +08:00
if err != nil {
logs.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 {
logs.Error(err)
2017-05-13 13:04:53 +08:00
c.JsonResult(6003, "文档不存在")
2017-05-12 19:14:29 +08:00
}
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 {
logs.Error(err)
2017-05-13 13:04:53 +08:00
c.JsonResult(6004, "权限不足")
2017-05-12 19:14:29 +08:00
}
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()
2017-05-12 19:14:29 +08:00
if err != nil {
logs.Error(err)
2017-05-13 13:04:53 +08:00
c.JsonResult(6005, "删除失败")
2017-05-12 19:14:29 +08:00
}
os.Remove(filepath.Join(conf.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")
docId, err := c.GetInt("doc_id", 0)
2017-04-28 18:08:01 +08:00
bookId := 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 {
logs.Error("FindByIdentify => ", err)
c.JsonResult(6002, "项目不存在或权限不足")
}
bookId = 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 {
logs.Error("FindByIdentify => ", err)
c.JsonResult(6002, "项目不存在或权限不足")
}
bookId = bookResult.BookId
2017-04-28 18:08:01 +08:00
}
if docId <= 0 {
2017-05-13 13:04:53 +08:00
c.JsonResult(6001, "参数错误")
2017-04-28 18:08:01 +08:00
}
doc, err := models.NewDocument().Find(docId)
2017-04-28 18:08:01 +08:00
if err != nil {
logs.Error("Delete => ", err)
2017-05-13 13:04:53 +08:00
c.JsonResult(6003, "删除失败")
2017-04-28 18:08:01 +08:00
}
// 如果文档所属项目错误
if doc.BookId != bookId {
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")
docId, err := c.GetInt("doc_id")
2017-04-28 18:08:01 +08:00
if err != nil {
docId, _ = strconv.Atoi(c.Ctx.Input.Param(":id"))
2017-04-28 18:08:01 +08:00
}
bookId := 0
autoRelease := false
// 如果是超级管理员,则忽略权限
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 || book == nil {
c.JsonResult(6002, "项目不存在或权限不足")
return
}
bookId = book.BookId
autoRelease = book.AutoRelease == 1
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 {
logs.Error("项目不存在或权限不足 -> ", err)
c.JsonResult(6002, "项目不存在或权限不足")
}
bookId = bookResult.BookId
autoRelease = bookResult.AutoRelease
2017-04-28 18:08:01 +08:00
}
if docId <= 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)
isCover := c.GetString("cover")
2017-04-28 18:08:01 +08:00
doc, err := models.NewDocument().Find(docId)
2017-04-28 18:08:01 +08:00
if err != nil || doc == nil {
2017-05-13 13:04:53 +08:00
c.JsonResult(6003, "读取文档错误")
return
2017-04-28 18:08:01 +08:00
}
if doc.BookId != bookId {
2017-05-13 13:04:53 +08:00
c.JsonResult(6004, "保存的文档不属于指定项目")
2017-04-28 18:08:01 +08:00
}
if doc.Version != version && !strings.EqualFold(isCover, "yes") {
logs.Info("%d|", version, doc.Version)
2017-05-13 13:04:53 +08:00
c.JsonResult(6005, "文档已被修改确定要覆盖吗?")
2017-04-28 18:08:01 +08:00
}
2017-05-20 15:27:03 +08:00
history := models.NewDocumentHistory()
history.DocumentId = docId
2017-05-20 15:27:03 +08:00
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
}
2017-04-28 18:08:01 +08:00
doc.Version = time.Now().Unix()
doc.Content = content
doc.ModifyAt = c.Member.MemberId
2017-05-13 13:04:53 +08:00
if err := doc.InsertOrUpdate(); err != nil {
logs.Error("InsertOrUpdate => ", err)
2017-05-13 13:04:53 +08:00
c.JsonResult(6006, "保存失败")
2017-04-28 18:08:01 +08:00
}
// 如果启用了文档历史,则添加历史文档
///如果两次保存的MD5值不同则保存为历史否则忽略
go func(history *models.DocumentHistory) {
if c.EnableDocumentHistory && cryptil.Md5Crypt(history.Markdown) != cryptil.Md5Crypt(doc.Markdown) {
_, err = history.InsertOrUpdate()
if err != nil {
logs.Error("DocumentHistory InsertOrUpdate => ", err)
}
2017-05-20 15:27:03 +08:00
}
}(history)
//如果启用了自动发布
if autoRelease {
2018-08-13 19:05:49 +08:00
go func() {
err := doc.ReleaseContent()
if err == nil {
logs.Informational("文档自动发布成功 -> document_id=%d;document_name=%s", doc.DocumentId, doc.DocumentName)
2018-08-13 19:05:49 +08:00
}
}()
}
2017-05-13 13:04:53 +08:00
c.JsonResult(0, "ok", doc)
2017-04-28 18:08:01 +08:00
}
doc, err := models.NewDocument().Find(docId)
2017-04-28 18:08:01 +08:00
if err != nil {
2017-05-13 13:04:53 +08:00
c.JsonResult(6003, "文档不存在")
return
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
}
// Export 导出
func (c *DocumentController) Export() {
2017-05-06 16:16:27 +08:00
c.Prepare()
identify := c.Ctx.Input.Param(":key")
if identify == "" {
c.ShowErrorPage(500, "参数错误")
}
2017-05-09 10:04:56 +08:00
2017-05-06 16:16:27 +08:00
output := c.GetString("output")
token := c.GetString("token")
// 如果没有开启匿名访问则跳转到登录
2018-11-14 15:57:55 +08:00
if !c.EnableAnonymous && !c.isUserLoggedIn() {
promptUserToLogIn(c)
2017-05-06 16:16:27 +08:00
return
}
if !conf.GetEnableExport() {
c.ShowErrorPage(500, "系统没有开启导出功能")
}
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 {
2018-07-25 17:47:06 +08:00
if err == orm.ErrNoRows {
c.ShowErrorPage(404, "项目不存在")
2018-07-25 17:47:06 +08:00
} else {
logs.Error("查找项目时出错 ->", err)
c.ShowErrorPage(500, "查找项目时出错")
2018-07-25 17:47:06 +08:00
}
2017-05-12 14:21:29 +08:00
}
bookResult = models.NewBookResult().ToBookResult(*book)
2017-05-13 13:04:53 +08:00
} else {
2018-11-14 15:57:55 +08:00
bookResult = c.isReadable(identify, token)
2017-05-12 14:21:29 +08:00
}
if !bookResult.IsDownload {
c.ShowErrorPage(200, "当前项目没有开启导出功能")
}
if !strings.HasPrefix(bookResult.Cover, "http:://") && !strings.HasPrefix(bookResult.Cover, "https:://") {
2018-03-13 19:20:50 +08:00
bookResult.Cover = conf.URLForWithCdnImage(bookResult.Cover)
}
2018-03-12 18:24:58 +08:00
if output == "markdown" {
if bookResult.Editor != "markdown" {
c.ShowErrorPage(500, "当前项目不支持Markdown编辑器")
2018-03-12 18:24:58 +08:00
}
p, err := bookResult.ExportMarkdown(c.CruSession.SessionID(context.TODO()))
2018-03-12 18:24:58 +08:00
if err != nil {
c.ShowErrorPage(500, "导出文档失败")
2018-03-12 18:24:58 +08:00
}
c.Ctx.Output.Download(p, bookResult.BookName+".zip")
c.StopRun()
return
}
outputPath := filepath.Join(conf.GetExportOutputPath(), strconv.Itoa(bookResult.BookId))
pdfpath := filepath.Join(outputPath, "book.pdf")
epubpath := filepath.Join(outputPath, "book.epub")
mobipath := filepath.Join(outputPath, "book.mobi")
docxpath := filepath.Join(outputPath, "book.docx")
if output == "pdf" && filetil.FileExists(pdfpath) {
c.Ctx.Output.Download(pdfpath, bookResult.BookName+".pdf")
c.Abort("200")
} else if output == "epub" && filetil.FileExists(epubpath) {
c.Ctx.Output.Download(epubpath, bookResult.BookName+".epub")
c.Abort("200")
} else if output == "mobi" && filetil.FileExists(mobipath) {
c.Ctx.Output.Download(mobipath, bookResult.BookName+".mobi")
c.Abort("200")
} else if output == "docx" && filetil.FileExists(docxpath) {
c.Ctx.Output.Download(docxpath, bookResult.BookName+".docx")
c.Abort("200")
} else if output == "pdf" || output == "epub" || output == "docx" || output == "mobi" {
if err := models.BackgroundConvert(c.CruSession.SessionID(context.TODO()), bookResult); err != nil && err != gopool.ErrHandlerIsExist {
c.ShowErrorPage(500, "导出失败,请查看系统日志")
2018-07-11 14:18:27 +08:00
}
c.ShowErrorPage(200, "文档正在后台转换,请稍后再下载")
} else {
c.ShowErrorPage(200, "不支持的文件格式")
2017-05-06 16:16:27 +08:00
}
c.ShowErrorPage(404, "项目没有导出文件")
2017-05-06 16:16:27 +08:00
}
// 生成项目访问的二维码
2017-05-13 13:04:53 +08:00
func (c *DocumentController) QrCode() {
c.Prepare()
2017-05-13 13:04:53 +08:00
identify := c.GetString(":key")
book, err := models.NewBook().FindByIdentify(identify)
if err != nil || book.BookId <= 0 {
c.ShowErrorPage(404, "项目不存在")
2017-05-13 13:04:53 +08:00
}
2018-03-13 19:20:50 +08:00
uri := conf.URLFor("DocumentController.Index", ":key", identify)
2017-05-13 13:04:53 +08:00
code, err := qr.Encode(uri, qr.L, qr.Unicode)
if err != nil {
logs.Error("生成二维码失败 ->", err)
c.ShowErrorPage(500, "生成二维码失败")
2017-05-13 13:04:53 +08:00
}
code, err = barcode.Scale(code, 150, 150)
2017-05-13 13:04:53 +08:00
if err != nil {
logs.Error("生成二维码失败 ->", err)
c.ShowErrorPage(500, "生成二维码失败")
2017-05-13 13:04:53 +08:00
}
2017-05-13 13:04:53 +08:00
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:15:21 +08:00
2017-05-13 13:04:53 +08:00
err = png.Encode(c.Ctx.ResponseWriter, code)
if err != nil {
logs.Error("生成二维码失败 ->", err)
c.ShowErrorPage(500, "生成二维码失败")
2017-05-13 13:04:53 +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
}
2018-11-14 15:57:55 +08:00
if !c.EnableAnonymous && !c.isUserLoggedIn() {
promptUserToLogIn(c)
2017-05-15 14:59:23 +08:00
return
}
2018-11-14 15:57:55 +08:00
bookResult := c.isReadable(identify, token)
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 {
logs.Error(err)
2017-06-05 10:52:14 +08:00
c.JsonResult(6002, "搜索结果错误")
2017-05-15 14:59:23 +08:00
}
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")
2018-03-23 11:17:52 +08:00
docId, err := c.GetInt("doc_id", 0)
2017-05-20 15:27:03 +08:00
pageIndex, _ := c.GetInt("page", 1)
bookId := 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 {
logs.Error("查找项目失败 ->", err)
2017-05-25 15:19:17 +08:00
c.Data["ErrorMessage"] = "项目不存在或权限不足"
return
2017-05-20 15:27:03 +08:00
}
bookId = 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 {
logs.Error("查找项目失败 ->", err)
2017-05-25 15:19:17 +08:00
c.Data["ErrorMessage"] = "项目不存在或权限不足"
return
2017-05-20 15:27:03 +08:00
}
bookId = bookResult.BookId
2017-05-25 15:19:17 +08:00
c.Data["Model"] = bookResult
2017-05-20 15:27:03 +08:00
}
2018-03-23 11:17:52 +08:00
if docId <= 0 {
2017-05-25 15:19:17 +08:00
c.Data["ErrorMessage"] = "参数错误"
return
2017-05-20 15:27:03 +08:00
}
2018-03-23 11:17:52 +08:00
doc, err := models.NewDocument().Find(docId)
2017-05-20 15:27:03 +08:00
if err != nil {
logs.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 != bookId {
2017-05-25 15:19:17 +08:00
c.Data["ErrorMessage"] = "参数错误"
return
2017-05-20 15:27:03 +08:00
}
histories, totalCount, err := models.NewDocumentHistory().FindToPager(docId, pageIndex, conf.PageSize)
2017-05-20 15:27:03 +08:00
if err != nil {
logs.Error("分页查找文档历史失败 ->", 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"] = histories
2017-05-25 15:19:17 +08:00
c.Data["PageHtml"] = ""
c.Data["Document"] = doc
2017-05-20 15:27:03 +08:00
if totalCount > 0 {
2018-03-13 14:14:56 +08:00
pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
2018-01-26 18:07:55 +08:00
c.Data["PageHtml"] = pager.HtmlPages()
2017-05-25 15:19:17 +08:00
}
}
func (c *DocumentController) DeleteHistory() {
c.Prepare()
2017-05-25 15:19:17 +08:00
c.TplName = "document/history.tpl"
identify := c.GetString("identify")
2018-03-23 11:17:52 +08:00
docId, err := c.GetInt("doc_id", 0)
historyId, _ := c.GetInt("history_id", 0)
2017-05-25 15:19:17 +08:00
2018-03-23 11:17:52 +08:00
if historyId <= 0 {
2017-06-05 10:52:14 +08:00
c.JsonResult(6001, "参数错误")
2017-05-25 15:19:17 +08:00
}
2018-03-23 11:17:52 +08:00
bookId := 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 {
logs.Error("查找项目失败 ->", err)
2017-05-25 15:19:17 +08:00
c.JsonResult(6002, "项目不存在或权限不足")
}
2018-03-23 11:17:52 +08:00
bookId = book.BookId
2017-05-25 15:19:17 +08:00
} else {
bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
if err != nil || bookResult.RoleId == conf.BookObserver {
logs.Error("查找项目失败 ->", err)
2017-05-25 15:19:17 +08:00
c.JsonResult(6002, "项目不存在或权限不足")
}
2018-03-23 11:17:52 +08:00
bookId = bookResult.BookId
2017-05-25 15:19:17 +08:00
}
2018-03-23 11:17:52 +08:00
if docId <= 0 {
2017-06-05 10:52:14 +08:00
c.JsonResult(6001, "参数错误")
2017-05-25 15:19:17 +08:00
}
2018-03-23 11:17:52 +08:00
doc, err := models.NewDocument().Find(docId)
2017-05-25 15:19:17 +08:00
if err != nil {
logs.Error("Delete => ", err)
2017-06-05 10:52:14 +08:00
c.JsonResult(6001, "获取历史失败")
2017-05-25 15:19:17 +08:00
}
// 如果文档所属项目错误
2018-03-23 11:17:52 +08:00
if doc.BookId != bookId {
2017-06-05 10:52:14 +08:00
c.JsonResult(6001, "参数错误")
2017-05-25 15:19:17 +08:00
}
2018-03-23 11:17:52 +08:00
err = models.NewDocumentHistory().Delete(historyId, docId)
2017-05-25 15:19:17 +08:00
if err != nil {
logs.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()
2017-05-25 15:19:17 +08:00
c.TplName = "document/history.tpl"
identify := c.GetString("identify")
2018-03-23 11:17:52 +08:00
docId, err := c.GetInt("doc_id", 0)
historyId, _ := c.GetInt("history_id", 0)
2017-05-25 15:19:17 +08:00
2018-03-23 11:17:52 +08:00
if historyId <= 0 {
2017-06-05 10:52:14 +08:00
c.JsonResult(6001, "参数错误")
2017-05-25 15:19:17 +08:00
}
2018-03-23 11:17:52 +08:00
bookId := 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 {
logs.Error("FindByIdentify => ", err)
2017-05-25 15:19:17 +08:00
c.JsonResult(6002, "项目不存在或权限不足")
}
2018-03-23 11:17:52 +08:00
bookId = book.BookId
2017-05-25 15:19:17 +08:00
} else {
bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
if err != nil || bookResult.RoleId == conf.BookObserver {
logs.Error("FindByIdentify => ", err)
2017-05-25 15:19:17 +08:00
c.JsonResult(6002, "项目不存在或权限不足")
}
2018-03-23 11:17:52 +08:00
bookId = bookResult.BookId
2017-05-25 15:19:17 +08:00
}
2018-03-23 11:17:52 +08:00
if docId <= 0 {
2017-06-05 10:52:14 +08:00
c.JsonResult(6001, "参数错误")
2017-05-20 15:27:03 +08:00
}
2018-03-23 11:17:52 +08:00
doc, err := models.NewDocument().Find(docId)
2017-05-25 15:19:17 +08:00
if err != nil {
logs.Error("Delete => ", err)
2017-06-05 10:52:14 +08:00
c.JsonResult(6001, "获取历史失败")
2017-05-25 15:19:17 +08:00
}
// 如果文档所属项目错误
2018-03-23 11:17:52 +08:00
if doc.BookId != bookId {
2017-06-05 10:52:14 +08:00
c.JsonResult(6001, "参数错误")
2017-05-25 15:19:17 +08:00
}
2018-03-23 11:17:52 +08:00
err = models.NewDocumentHistory().Restore(historyId, docId, c.Member.MemberId)
2017-05-25 15:19:17 +08:00
if err != nil {
logs.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
}
func (c *DocumentController) Compare() {
2017-06-09 18:14:55 +08:00
c.Prepare()
2017-06-09 18:14:55 +08:00
c.TplName = "document/compare.tpl"
2018-03-23 11:17:52 +08:00
historyId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
identify := c.Ctx.Input.Param(":key")
2018-03-23 11:17:52 +08:00
bookId := 0
editor := "markdown"
// 如果是超级管理员则忽略权限判断
if c.Member.IsAdministrator() {
book, err := models.NewBook().FindByFieldFirst("identify", identify)
if err != nil {
logs.Error("DocumentController.Compare => ", err)
c.ShowErrorPage(403, "权限不足")
return
}
2018-03-23 11:17:52 +08:00
bookId = 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 {
logs.Error("FindByIdentify => ", err)
c.ShowErrorPage(403, "权限不足")
return
}
2018-03-23 11:17:52 +08:00
bookId = bookResult.BookId
c.Data["Model"] = bookResult
editor = bookResult.Editor
}
2018-03-23 11:17:52 +08:00
if historyId <= 0 {
c.ShowErrorPage(60002, "参数错误")
}
2018-03-23 11:17:52 +08:00
history, err := models.NewDocumentHistory().Find(historyId)
if err != nil {
logs.Error("DocumentController.Compare => ", err)
c.ShowErrorPage(60003, err.Error())
}
doc, err := models.NewDocument().Find(history.DocumentId)
if err != nil || doc == nil || doc.BookId != bookId {
c.ShowErrorPage(60002, "文档不存在或已删除")
return
}
2018-03-23 11:17:52 +08:00
c.Data["HistoryId"] = historyId
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
}
// 判断用户是否可以阅读文档
2018-11-14 15:57:55 +08:00
func (c *DocumentController) isReadable(identify, token string) *models.BookResult {
book, err := models.NewBook().FindByFieldFirst("identify", identify)
if err != nil {
logs.Error(err)
c.ShowErrorPage(500, "项目不存在")
}
2018-11-13 17:33:13 +08:00
bookResult := models.NewBookResult().ToBookResult(*book)
isOk := false
2018-11-14 15:57:55 +08:00
if c.isUserLoggedIn() {
2018-11-13 17:33:13 +08:00
roleId, err := models.NewBook().FindForRoleId(book.BookId, c.Member.MemberId)
if err == nil {
isOk = true
bookResult.MemberId = c.Member.MemberId
bookResult.RoleId = roleId
}
}
// 如果文档是私有的
2018-11-14 15:57:55 +08:00
if book.PrivatelyOwned == 1 && (!c.isUserLoggedIn() || !c.Member.IsAdministrator()) {
if s, ok := c.GetSession(identify).(string); !ok || (!strings.EqualFold(s, book.PrivateToken) && !strings.EqualFold(s, book.BookPassword)) {
if book.PrivateToken != "" && !isOk && token != "" {
// 如果有访问的 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) {
logs.Info("尝试访问文档但权限不足 ->", identify, token)
c.ShowErrorPage(403, "权限不足")
}
} else if password := c.GetString("bPassword", ""); !isOk && book.BookPassword != "" && password != "" {
//如果设置了密码,则判断密码是否正确
if book.BookPassword != password {
c.JsonResult(5001, "密码错误")
} else {
c.SetSession(identify, password)
c.JsonResult(0, "OK")
}
} else if !isOk {
//如果设置了密码,则显示密码输入页面
if book.BookPassword != "" {
//判断已存在的密码是否正确
if password, ok := c.GetSession(identify).(string); !ok || !strings.EqualFold(password, book.BookPassword) {
body, err := c.ExecuteViewPathTemplate("document/document_password.tpl", map[string]string{"Identify": book.Identify})
if err != nil {
logs.Error("显示密码页面失败 ->", err)
}
c.CustomAbort(200, body)
2018-11-14 15:57:55 +08:00
}
} else {
logs.Info("尝试访问文档但权限不足 ->", identify, token)
c.ShowErrorPage(403, "权限不足")
2018-11-14 15:57:55 +08:00
}
}
}
}
return bookResult
}
func promptUserToLogIn(c *DocumentController) {
logs.Info("Access " + c.Ctx.Request.URL.RequestURI() + " not permitted.")
logs.Info(" Access will be redirected to login page(SessionId: " + c.CruSession.SessionID(context.TODO()) + ").")
if c.IsAjax() {
c.JsonResult(6000, "请重新登录。")
} else {
c.Redirect(conf.URLFor("AccountController.Login")+"?url="+url.PathEscape(conf.BaseUrl+c.Ctx.Request.URL.RequestURI()), 302)
}
}