2018-07-13 16:46:40 +08:00
|
|
|
|
package controllers
|
|
|
|
|
|
2018-07-13 19:04:51 +08:00
|
|
|
|
import (
|
2021-03-23 21:55:50 +08:00
|
|
|
|
"context"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"encoding/json"
|
2018-07-17 19:13:11 +08:00
|
|
|
|
"fmt"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"html/template"
|
2018-07-17 19:13:11 +08:00
|
|
|
|
"net/http"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"net/url"
|
|
|
|
|
"os"
|
2018-07-17 19:13:11 +08:00
|
|
|
|
"path/filepath"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
2021-03-26 11:34:02 +08:00
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
2021-03-24 00:28:13 +08:00
|
|
|
|
"github.com/beego/beego/v2/core/logs"
|
|
|
|
|
"github.com/beego/beego/v2/server/web"
|
2021-04-19 11:24:12 +08:00
|
|
|
|
"github.com/beego/i18n"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
|
|
|
|
"github.com/mindoc-org/mindoc/models"
|
|
|
|
|
"github.com/mindoc-org/mindoc/utils"
|
|
|
|
|
"github.com/mindoc-org/mindoc/utils/pagination"
|
2018-07-13 19:04:51 +08:00
|
|
|
|
)
|
2018-07-13 16:46:40 +08:00
|
|
|
|
|
2018-09-19 11:32:29 +08:00
|
|
|
|
type BlogController struct {
|
2018-07-13 16:46:40 +08:00
|
|
|
|
BaseController
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-19 11:32:29 +08:00
|
|
|
|
func (c *BlogController) Prepare() {
|
|
|
|
|
c.BaseController.Prepare()
|
|
|
|
|
if !c.EnableAnonymous && c.Member == nil {
|
|
|
|
|
c.Redirect(conf.URLFor("AccountController.Login")+"?url="+url.PathEscape(conf.BaseUrl+c.Ctx.Request.URL.RequestURI()), 302)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 14:30:27 +08:00
|
|
|
|
// 文章阅读
|
2018-07-13 16:46:40 +08:00
|
|
|
|
func (c *BlogController) Index() {
|
|
|
|
|
c.Prepare()
|
2018-07-13 19:04:51 +08:00
|
|
|
|
c.TplName = "blog/index.tpl"
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blogId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
|
2018-07-18 17:10:24 +08:00
|
|
|
|
|
2018-09-19 11:32:29 +08:00
|
|
|
|
if blogId <= 0 {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.page_not_existed"))
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blogReadSession := fmt.Sprintf("blog:read:%d", blogId)
|
2018-07-18 17:10:24 +08:00
|
|
|
|
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blog, err := models.NewBlog().FindFromCache(blogId)
|
2018-07-18 17:10:24 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.blog_not_existed"))
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.Ctx.Input.IsPost() {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
password := c.GetString("password")
|
2018-07-18 17:10:24 +08:00
|
|
|
|
if blog.BlogStatus == "password" && password != blog.Password {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6001, i18n.Tr(c.Lang, "message.blog_pwd_incorrect"))
|
2018-09-19 11:32:29 +08:00
|
|
|
|
} else if blog.BlogStatus == "password" && password == blog.Password {
|
2023-06-19 09:30:17 +08:00
|
|
|
|
// Store the session value for the next GET request.
|
|
|
|
|
_ = c.CruSession.Set(context.TODO(), blogReadSession, blogId)
|
|
|
|
|
c.JsonResult(0, "OK")
|
2022-06-26 14:42:19 +08:00
|
|
|
|
} else {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
c.JsonResult(0, "OK")
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
2023-06-19 09:30:17 +08:00
|
|
|
|
} else if blog.BlogStatus == "password" && c.CruSession.Get(context.TODO(), blogReadSession) == nil && // Read session doesn't exist
|
|
|
|
|
(c.Member == nil || (blog.MemberId != c.Member.MemberId && !c.Member.IsAdministrator())) { // User isn't author or administrator
|
2018-07-18 17:10:24 +08:00
|
|
|
|
//如果不存在已输入密码的标记
|
|
|
|
|
c.TplName = "blog/index_password.tpl"
|
|
|
|
|
}
|
2019-03-11 18:09:19 +08:00
|
|
|
|
if blog.BlogType != 1 {
|
|
|
|
|
//加载文章附件
|
|
|
|
|
_ = blog.LinkAttach()
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-18 17:10:24 +08:00
|
|
|
|
c.Data["Model"] = blog
|
|
|
|
|
c.Data["Content"] = template.HTML(blog.BlogRelease)
|
|
|
|
|
|
2018-07-25 14:46:56 +08:00
|
|
|
|
if blog.BlogExcerpt == "" {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
c.Data["Description"] = utils.AutoSummary(blog.BlogRelease, 120)
|
|
|
|
|
} else {
|
2018-07-25 14:46:56 +08:00
|
|
|
|
c.Data["Description"] = blog.BlogExcerpt
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-19 11:32:29 +08:00
|
|
|
|
if nextBlog, err := models.NewBlog().QueryNext(blogId); err == nil {
|
2018-07-18 17:10:24 +08:00
|
|
|
|
c.Data["Next"] = nextBlog
|
|
|
|
|
}
|
2018-09-19 11:32:29 +08:00
|
|
|
|
if preBlog, err := models.NewBlog().QueryPrevious(blogId); err == nil {
|
2018-07-18 17:10:24 +08:00
|
|
|
|
c.Data["Previous"] = preBlog
|
|
|
|
|
}
|
2018-07-13 16:46:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 14:30:27 +08:00
|
|
|
|
// 文章列表
|
2018-07-13 16:46:40 +08:00
|
|
|
|
func (c *BlogController) List() {
|
|
|
|
|
c.Prepare()
|
2018-07-13 19:04:51 +08:00
|
|
|
|
c.TplName = "blog/list.tpl"
|
2018-07-18 17:10:24 +08:00
|
|
|
|
pageIndex, _ := c.GetInt("page", 1)
|
|
|
|
|
|
|
|
|
|
var blogList []*models.Blog
|
|
|
|
|
var totalCount int
|
2018-09-19 11:32:29 +08:00
|
|
|
|
var err error
|
2018-07-18 17:10:24 +08:00
|
|
|
|
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blogList, totalCount, err = models.NewBlog().FindToPager(pageIndex, conf.PageSize, 0, "")
|
2018-07-18 17:10:24 +08:00
|
|
|
|
|
2018-07-22 17:28:54 +08:00
|
|
|
|
if err != nil && err != orm.ErrNoRows {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
c.ShowErrorPage(500, err.Error())
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
if totalCount > 0 {
|
|
|
|
|
pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
|
|
|
|
|
c.Data["PageHtml"] = pager.HtmlPages()
|
2018-09-19 11:32:29 +08:00
|
|
|
|
for _, blog := range blogList {
|
2018-07-25 14:46:56 +08:00
|
|
|
|
//如果没有添加文章摘要,则自动提取
|
|
|
|
|
if blog.BlogExcerpt == "" {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blog.BlogExcerpt = utils.AutoSummary(blog.BlogRelease, 120)
|
2018-07-25 14:46:56 +08:00
|
|
|
|
}
|
2018-07-18 17:10:24 +08:00
|
|
|
|
blog.Link()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
c.Data["PageHtml"] = ""
|
|
|
|
|
}
|
2018-07-13 19:04:51 +08:00
|
|
|
|
|
2018-07-18 17:10:24 +08:00
|
|
|
|
c.Data["Lists"] = blogList
|
2018-07-13 16:46:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 14:30:27 +08:00
|
|
|
|
// 管理后台文章列表
|
2018-07-13 16:46:40 +08:00
|
|
|
|
func (c *BlogController) ManageList() {
|
|
|
|
|
c.Prepare()
|
2018-07-13 19:04:51 +08:00
|
|
|
|
c.TplName = "blog/manage_list.tpl"
|
2018-07-17 19:13:11 +08:00
|
|
|
|
|
|
|
|
|
pageIndex, _ := c.GetInt("page", 1)
|
|
|
|
|
|
2024-07-24 09:20:06 +08:00
|
|
|
|
blogList, totalCount, err := models.NewBlog().FindToPager(pageIndex, conf.PageSize, c.Member.MemberId, "all")
|
2018-07-17 19:13:11 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
c.ShowErrorPage(500, err.Error())
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
if totalCount > 0 {
|
|
|
|
|
pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
|
|
|
|
|
c.Data["PageHtml"] = pager.HtmlPages()
|
|
|
|
|
} else {
|
|
|
|
|
c.Data["PageHtml"] = ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Data["ModelList"] = blogList
|
|
|
|
|
|
2018-07-13 16:46:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 14:30:27 +08:00
|
|
|
|
// 文章设置
|
2018-07-13 16:46:40 +08:00
|
|
|
|
func (c *BlogController) ManageSetting() {
|
|
|
|
|
c.Prepare()
|
2018-07-13 19:04:51 +08:00
|
|
|
|
c.TplName = "blog/manage_setting.tpl"
|
|
|
|
|
//如果是post请求
|
|
|
|
|
if c.Ctx.Input.IsPost() {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blogId, _ := c.GetInt("id", 0)
|
2018-07-13 19:04:51 +08:00
|
|
|
|
blogTitle := c.GetString("title")
|
|
|
|
|
blogIdentify := c.GetString("identify")
|
2018-09-19 11:32:29 +08:00
|
|
|
|
orderIndex, _ := c.GetInt("order_index", 0)
|
|
|
|
|
blogType, _ := c.GetInt("blog_type", 0)
|
|
|
|
|
blogExcerpt := c.GetString("excerpt", "")
|
|
|
|
|
blogStatus := c.GetString("status", "publish")
|
|
|
|
|
blogPassword := c.GetString("password", "")
|
2018-07-18 17:10:24 +08:00
|
|
|
|
documentIdentify := strings.TrimSpace(c.GetString("documentIdentify"))
|
|
|
|
|
bookIdentify := strings.TrimSpace(c.GetString("bookIdentify"))
|
|
|
|
|
documentId := 0
|
|
|
|
|
|
2018-07-13 19:04:51 +08:00
|
|
|
|
if blogTitle == "" {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6001, i18n.Tr(c.Lang, "message.blog_title_empty"))
|
2018-07-13 19:04:51 +08:00
|
|
|
|
}
|
2018-09-19 11:32:29 +08:00
|
|
|
|
if strings.Count(blogExcerpt, "") > 500 {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6008, i18n.Tr(c.Lang, "message.blog_digest_tips"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
2024-07-24 09:20:06 +08:00
|
|
|
|
if blogStatus != "private" && blogStatus != "public" && blogStatus != "password" && blogStatus != "draft" {
|
2018-07-17 19:13:11 +08:00
|
|
|
|
blogStatus = "public"
|
|
|
|
|
}
|
|
|
|
|
if blogStatus == "password" && blogPassword == "" {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6010, i18n.Tr(c.Lang, "message.set_pwd_pls"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
2018-07-13 19:04:51 +08:00
|
|
|
|
if blogType != 0 && blogType != 1 {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6005, i18n.Tr(c.Lang, "message.unknown_blog_type"))
|
2018-07-13 19:04:51 +08:00
|
|
|
|
}
|
2018-09-19 11:32:29 +08:00
|
|
|
|
if strings.Count(blogTitle, "") > 200 {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6002, i18n.Tr(c.Lang, "message.blog_title_tips"))
|
2018-07-13 19:04:51 +08:00
|
|
|
|
}
|
2018-07-18 17:10:24 +08:00
|
|
|
|
//如果是关联文章,需要同步关联的文档
|
|
|
|
|
if blogType == 1 {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
book, err := models.NewBook().FindByIdentify(bookIdentify)
|
2018-07-18 17:10:24 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6011, i18n.Tr(c.Lang, "message.ref_doc_not_exist_or_no_permit"))
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
2018-09-19 11:32:29 +08:00
|
|
|
|
doc, err := models.NewDocument().FindByIdentityFirst(documentIdentify, book.BookId)
|
|
|
|
|
if err != nil {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6003, i18n.Tr(c.Lang, "message.query_failed"))
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
documentId = doc.DocumentId
|
|
|
|
|
|
|
|
|
|
// 如果不是超级管理员,则校验权限
|
|
|
|
|
if !c.Member.IsAdministrator() {
|
|
|
|
|
bookResult, err := models.NewBookResult().FindByIdentify(book.Identify, c.Member.MemberId)
|
|
|
|
|
|
|
|
|
|
if err != nil || bookResult.RoleId == conf.BookObserver {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6002, i18n.Tr(c.Lang, "message.ref_doc_not_exist_or_no_permit"))
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 19:04:51 +08:00
|
|
|
|
var blog *models.Blog
|
|
|
|
|
var err error
|
|
|
|
|
//如果文章ID存在,则从数据库中查询文章
|
|
|
|
|
if blogId > 0 {
|
|
|
|
|
if c.Member.IsAdministrator() {
|
|
|
|
|
blog, err = models.NewBlog().Find(blogId)
|
|
|
|
|
} else {
|
|
|
|
|
blog, err = models.NewBlog().FindByIdAndMemberId(blogId, c.Member.MemberId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6003, i18n.Tr(c.Lang, "message.blog_not_exist"))
|
2018-07-13 19:04:51 +08:00
|
|
|
|
}
|
|
|
|
|
//如果设置了文章标识
|
|
|
|
|
if blogIdentify != "" {
|
|
|
|
|
//如果查询到的文章标识存在并且不是当前文章的id
|
2018-09-19 11:32:29 +08:00
|
|
|
|
if b, err := models.NewBlog().FindByIdentify(blogIdentify); err == nil && b.BlogId != blogId {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6004, i18n.Tr(c.Lang, "message.blog_id_existed"))
|
2018-07-13 19:04:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-17 19:13:11 +08:00
|
|
|
|
blog.Modified = time.Now()
|
|
|
|
|
blog.ModifyAt = c.Member.MemberId
|
2018-09-19 11:32:29 +08:00
|
|
|
|
} else {
|
2018-07-13 19:04:51 +08:00
|
|
|
|
//如果设置了文章标识
|
|
|
|
|
if blogIdentify != "" {
|
|
|
|
|
if models.NewBlog().IsExist(blogIdentify) {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6004, i18n.Tr(c.Lang, "message.blog_id_existed"))
|
2018-07-13 19:04:51 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
blog = models.NewBlog()
|
|
|
|
|
blog.MemberId = c.Member.MemberId
|
2018-07-17 19:13:11 +08:00
|
|
|
|
blog.Created = time.Now()
|
|
|
|
|
}
|
|
|
|
|
if blogIdentify == "" {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blog.BlogIdentify = fmt.Sprintf("%s-%d", "post", time.Now().UnixNano())
|
|
|
|
|
} else {
|
2018-07-17 19:13:11 +08:00
|
|
|
|
blog.BlogIdentify = blogIdentify
|
2018-07-13 19:04:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
blog.BlogTitle = blogTitle
|
2018-07-17 19:13:11 +08:00
|
|
|
|
|
2018-07-13 19:04:51 +08:00
|
|
|
|
blog.OrderIndex = orderIndex
|
|
|
|
|
blog.BlogType = blogType
|
|
|
|
|
if blogType == 1 {
|
|
|
|
|
blog.DocumentId = documentId
|
|
|
|
|
}
|
2018-07-17 19:13:11 +08:00
|
|
|
|
blog.BlogExcerpt = blogExcerpt
|
|
|
|
|
blog.BlogStatus = blogStatus
|
|
|
|
|
blog.Password = blogPassword
|
2018-07-13 19:04:51 +08:00
|
|
|
|
|
2018-09-19 11:32:29 +08:00
|
|
|
|
if err := blog.Save(); err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("保存文章失败 -> ", err)
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6011, i18n.Tr(c.Lang, "message.failed"))
|
2018-09-19 11:32:29 +08:00
|
|
|
|
} else {
|
|
|
|
|
c.JsonResult(0, "ok", blog)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if c.Ctx.Input.Referer() == "" {
|
|
|
|
|
c.Data["Referer"] = "javascript:history.back();"
|
2018-09-19 11:32:29 +08:00
|
|
|
|
} else {
|
2018-07-17 19:13:11 +08:00
|
|
|
|
c.Data["Referer"] = c.Ctx.Input.Referer()
|
|
|
|
|
}
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blogId, err := strconv.Atoi(c.Ctx.Input.Param(":id"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
|
2021-03-30 14:24:14 +08:00
|
|
|
|
c.Data["DocumentIdentify"] = ""
|
2018-07-17 19:13:11 +08:00
|
|
|
|
if err == nil {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blog, err := models.NewBlog().FindByIdAndMemberId(blogId, c.Member.MemberId)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
if err != nil {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
c.ShowErrorPage(500, err.Error())
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
2018-07-18 17:10:24 +08:00
|
|
|
|
|
2018-07-17 19:13:11 +08:00
|
|
|
|
c.Data["Model"] = blog
|
2018-09-19 11:32:29 +08:00
|
|
|
|
} else {
|
2018-07-17 19:13:11 +08:00
|
|
|
|
c.Data["Model"] = models.NewBlog()
|
2018-07-13 19:04:51 +08:00
|
|
|
|
}
|
2018-07-13 16:46:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 14:30:27 +08:00
|
|
|
|
// 文章创建或编辑
|
2018-07-13 16:46:40 +08:00
|
|
|
|
func (c *BlogController) ManageEdit() {
|
|
|
|
|
c.Prepare()
|
2018-07-13 19:04:51 +08:00
|
|
|
|
c.TplName = "blog/manage_edit.tpl"
|
2018-07-17 19:13:11 +08:00
|
|
|
|
|
|
|
|
|
if c.Ctx.Input.IsPost() {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blogId, _ := c.GetInt("blogId", 0)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
|
|
|
|
|
if blogId <= 0 {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blogContent := c.GetString("content", "")
|
|
|
|
|
blogHtml := c.GetString("htmlContent", "")
|
|
|
|
|
version, _ := c.GetInt64("version", 0)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
cover := c.GetString("cover")
|
|
|
|
|
|
|
|
|
|
var blog *models.Blog
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
if c.Member.IsAdministrator() {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blog, err = models.NewBlog().Find(blogId)
|
|
|
|
|
} else {
|
|
|
|
|
blog, err = models.NewBlog().FindByIdAndMemberId(blogId, c.Member.MemberId)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("查询文章失败 ->", err)
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6002, i18n.Tr(c.Lang, "message.query_failed"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
2018-09-19 11:32:29 +08:00
|
|
|
|
if version > 0 && blog.Version != version && cover != "yes" {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6005, i18n.Tr(c.Lang, "message.blog_has_modified"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
2018-07-18 17:10:24 +08:00
|
|
|
|
//如果是关联文章,需要同步关联的文档
|
2018-07-17 19:13:11 +08:00
|
|
|
|
if blog.BlogType == 1 {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
doc, err := models.NewDocument().Find(blog.DocumentId)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("查询关联项目文档时出错 ->", err)
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6003, i18n.Tr(c.Lang, "message.query_failed"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
2018-07-18 17:10:24 +08:00
|
|
|
|
book, err := models.NewBook().Find(doc.BookId)
|
|
|
|
|
if err != nil {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6002, i18n.Tr(c.Lang, "message.item_not_exist_or_no_permit"))
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果不是超级管理员,则校验权限
|
|
|
|
|
if !c.Member.IsAdministrator() {
|
|
|
|
|
bookResult, err := models.NewBookResult().FindByIdentify(book.Identify, c.Member.MemberId)
|
|
|
|
|
|
|
|
|
|
if err != nil || bookResult.RoleId == conf.BookObserver {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("FindByIdentify => ", err)
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6002, i18n.Tr(c.Lang, "message.ref_doc_not_exist_or_no_permit"))
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-17 19:13:11 +08:00
|
|
|
|
doc.Markdown = blogContent
|
|
|
|
|
doc.Release = blogHtml
|
|
|
|
|
doc.Content = blogHtml
|
|
|
|
|
doc.ModifyTime = time.Now()
|
|
|
|
|
doc.ModifyAt = c.Member.MemberId
|
2018-09-19 11:32:29 +08:00
|
|
|
|
if err := doc.InsertOrUpdate("markdown", "release", "content", "modify_time", "modify_at"); err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("保存关联文档时出错 ->", err)
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6004, i18n.Tr(c.Lang, "message.failed"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
blog.BlogContent = blogContent
|
|
|
|
|
blog.BlogRelease = blogHtml
|
|
|
|
|
blog.ModifyAt = c.Member.MemberId
|
|
|
|
|
blog.Modified = time.Now()
|
|
|
|
|
|
2018-09-19 11:32:29 +08:00
|
|
|
|
if err := blog.Save("blog_content", "blog_release", "modify_at", "modify_time", "version"); err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("保存文章失败 -> ", err)
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6011, i18n.Tr(c.Lang, "message.failed"))
|
2018-09-19 11:32:29 +08:00
|
|
|
|
} else {
|
|
|
|
|
c.JsonResult(0, "ok", blog)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blogId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
|
|
|
|
|
if blogId <= 0 {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.param_error"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
var blog *models.Blog
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
if c.Member.IsAdministrator() {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blog, err = models.NewBlog().Find(blogId)
|
|
|
|
|
} else {
|
|
|
|
|
blog, err = models.NewBlog().FindByIdAndMemberId(blogId, c.Member.MemberId)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.blog_not_exist"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
2018-07-18 17:10:24 +08:00
|
|
|
|
blog.LinkAttach()
|
|
|
|
|
|
|
|
|
|
if len(blog.AttachList) > 0 {
|
|
|
|
|
returnJSON, err := json.Marshal(blog.AttachList)
|
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("序列化文章附件时出错 ->", err)
|
2018-09-19 11:32:29 +08:00
|
|
|
|
} else {
|
2018-07-18 17:10:24 +08:00
|
|
|
|
c.Data["AttachList"] = template.JS(string(returnJSON))
|
|
|
|
|
}
|
2018-09-19 11:32:29 +08:00
|
|
|
|
} else {
|
2018-07-18 17:10:24 +08:00
|
|
|
|
c.Data["AttachList"] = template.JS("[]")
|
|
|
|
|
}
|
2018-09-11 15:57:03 +08:00
|
|
|
|
if conf.GetUploadFileSize() > 0 {
|
|
|
|
|
c.Data["UploadFileSize"] = conf.GetUploadFileSize()
|
2018-09-19 11:32:29 +08:00
|
|
|
|
} else {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
c.Data["UploadFileSize"] = "undefined"
|
2018-09-11 15:57:03 +08:00
|
|
|
|
}
|
2018-07-17 19:13:11 +08:00
|
|
|
|
c.Data["Model"] = blog
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 14:30:27 +08:00
|
|
|
|
// 删除文章
|
2018-09-19 11:32:29 +08:00
|
|
|
|
func (c *BlogController) ManageDelete() {
|
2018-07-17 19:13:11 +08:00
|
|
|
|
c.Prepare()
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blogId, _ := c.GetInt("blog_id", 0)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
|
|
|
|
|
if blogId <= 0 {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
var blog *models.Blog
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
if c.Member.IsAdministrator() {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blog, err = models.NewBlog().Find(blogId)
|
|
|
|
|
} else {
|
|
|
|
|
blog, err = models.NewBlog().FindByIdAndMemberId(blogId, c.Member.MemberId)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6002, i18n.Tr(c.Lang, "message.blog_not_exist"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := blog.Delete(blogId); err != nil {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6003, i18n.Tr(c.Lang, "message.failed"))
|
2018-09-19 11:32:29 +08:00
|
|
|
|
} else {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(0, i18n.Tr(c.Lang, "message.success"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 上传附件或图片
|
|
|
|
|
func (c *BlogController) Upload() {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
c.Prepare()
|
2018-07-17 19:13:11 +08:00
|
|
|
|
blogId, _ := c.GetInt("blogId")
|
|
|
|
|
|
|
|
|
|
if blogId <= 0 {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blog, err := models.NewBlog().Find(blogId)
|
2018-07-18 17:10:24 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6010, i18n.Tr(c.Lang, "message.blog_not_exist"))
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
if !c.Member.IsAdministrator() && blog.MemberId != c.Member.MemberId {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6011, i18n.Tr(c.Lang, "message.no_permission"))
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-17 19:13:11 +08:00
|
|
|
|
name := "editormd-file-file"
|
|
|
|
|
|
|
|
|
|
file, moreFile, err := c.GetFile(name)
|
|
|
|
|
if err == http.ErrMissingFile {
|
|
|
|
|
name = "editormd-image-file"
|
|
|
|
|
file, moreFile, err = c.GetFile(name)
|
|
|
|
|
if err == http.ErrMissingFile {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6003, i18n.Tr(c.Lang, "message.upload_file_empty"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JsonResult(6002, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
type Size interface {
|
|
|
|
|
Size() int64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if conf.GetUploadFileSize() > 0 && moreFile.Size > conf.GetUploadFileSize() {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6009, i18n.Tr(c.Lang, "message.upload_file_size_limit"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ext := filepath.Ext(moreFile.Filename)
|
|
|
|
|
|
|
|
|
|
if ext == "" {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6003, i18n.Tr(c.Lang, "message.upload_file_type_error"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
//如果文件类型设置为 * 标识不限制文件类型
|
2021-03-24 00:28:13 +08:00
|
|
|
|
if web.AppConfig.DefaultString("upload_file_ext", "") != "*" {
|
2018-07-17 19:13:11 +08:00
|
|
|
|
if !conf.IsAllowUploadFileExt(ext) {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6004, i18n.Tr(c.Lang, "message.upload_file_type_error"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果是超级管理员,则不判断权限
|
|
|
|
|
if c.Member.IsAdministrator() {
|
|
|
|
|
_, err := models.NewBlog().Find(blogId)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6006, i18n.Tr(c.Lang, "message.doc_not_exist_or_no_permit"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
_, err := models.NewBlog().FindByIdAndMemberId(blogId, c.Member.MemberId)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("查询文章时出错 -> ", err)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
if err == orm.ErrNoRows {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6006, i18n.Tr(c.Lang, "message.no_permission"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JsonResult(6001, err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileName := "attach_" + strconv.FormatInt(time.Now().UnixNano(), 16)
|
|
|
|
|
|
|
|
|
|
filePath := filepath.Join(conf.WorkingDirectory, "uploads", "blog", time.Now().Format("200601"), fileName+ext)
|
|
|
|
|
|
|
|
|
|
path := filepath.Dir(filePath)
|
|
|
|
|
|
|
|
|
|
os.MkdirAll(path, os.ModePerm)
|
|
|
|
|
|
|
|
|
|
err = c.SaveToFile(name, filePath)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("SaveToFile => ", err)
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6005, i18n.Tr(c.Lang, "message.failed"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var httpPath string
|
|
|
|
|
result := make(map[string]interface{})
|
2018-07-18 17:10:24 +08:00
|
|
|
|
//如果是图片,则当做内置图片处理,否则当做附件处理
|
2018-07-17 19:13:11 +08:00
|
|
|
|
if strings.EqualFold(ext, ".jpg") || strings.EqualFold(ext, ".jpeg") || strings.EqualFold(ext, ".png") || strings.EqualFold(ext, ".gif") {
|
|
|
|
|
httpPath = "/" + strings.Replace(strings.TrimPrefix(filePath, conf.WorkingDirectory), "\\", "/", -1)
|
|
|
|
|
if strings.HasPrefix(httpPath, "//") {
|
|
|
|
|
httpPath = conf.URLForWithCdnImage(string(httpPath[1:]))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
attachment := models.NewAttachment()
|
|
|
|
|
attachment.BookId = 0
|
|
|
|
|
attachment.FileName = moreFile.Filename
|
|
|
|
|
attachment.CreateAt = c.Member.MemberId
|
|
|
|
|
attachment.FileExt = ext
|
|
|
|
|
attachment.FilePath = strings.TrimPrefix(filePath, conf.WorkingDirectory)
|
|
|
|
|
attachment.DocumentId = blogId
|
2018-07-18 17:10:24 +08:00
|
|
|
|
//如果是关联文章,则将附件设置为关联文档的文档上
|
|
|
|
|
if blog.BlogType == 1 {
|
|
|
|
|
attachment.BookId = blog.BookId
|
|
|
|
|
attachment.DocumentId = blog.DocumentId
|
|
|
|
|
}
|
2018-07-17 19:13:11 +08:00
|
|
|
|
if fileInfo, err := os.Stat(filePath); err == nil {
|
|
|
|
|
attachment.FileSize = float64(fileInfo.Size())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attachment.HttpPath = httpPath
|
|
|
|
|
|
|
|
|
|
if err := attachment.Insert(); err != nil {
|
|
|
|
|
os.Remove(filePath)
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("保存文件附件失败 -> ", err)
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6006, i18n.Tr(c.Lang, "message.failed"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
if attachment.HttpPath == "" {
|
2018-11-15 19:54:25 +08:00
|
|
|
|
attachment.HttpPath = conf.URLForNotHost("BlogController.Download", ":id", blogId, ":attach_id", attachment.AttachmentId)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
|
|
|
|
|
if err := attachment.Update(); err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("保存文件失败 -> ", attachment.FilePath, err)
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6005, i18n.Tr(c.Lang, "message.failed"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result["attach"] = attachment
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result["errcode"] = 0
|
|
|
|
|
result["success"] = 1
|
|
|
|
|
result["message"] = "ok"
|
|
|
|
|
result["url"] = httpPath
|
|
|
|
|
result["alt"] = fileName
|
|
|
|
|
|
|
|
|
|
c.Ctx.Output.JSON(result, true, false)
|
|
|
|
|
c.StopRun()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除附件
|
|
|
|
|
func (c *BlogController) RemoveAttachment() {
|
|
|
|
|
c.Prepare()
|
|
|
|
|
attachId, _ := c.GetInt("attach_id")
|
2018-07-18 17:10:24 +08:00
|
|
|
|
blogId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
|
|
|
|
|
if attachId <= 0 {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6001, i18n.Tr(c.Lang, "message.param_error"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
2018-07-18 17:10:24 +08:00
|
|
|
|
blog, err := models.NewBlog().Find(blogId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == orm.ErrNoRows {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.doc_not_exist"))
|
2018-07-18 17:10:24 +08:00
|
|
|
|
} else {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.query_failed"))
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-17 19:13:11 +08:00
|
|
|
|
attach, err := models.NewAttachment().Find(attachId)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error(err)
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6002, i18n.Tr(c.Lang, "message.attachment_not_exist"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !c.Member.IsAdministrator() {
|
2018-09-19 11:32:29 +08:00
|
|
|
|
_, err := models.NewBlog().FindByIdAndMemberId(attach.DocumentId, c.Member.MemberId)
|
2018-07-17 19:13:11 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error(err)
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6003, i18n.Tr(c.Lang, "message.doc_not_exist"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-18 17:10:24 +08:00
|
|
|
|
if blog.BlogType == 1 && attach.BookId != blog.BookId && attach.DocumentId != blog.DocumentId {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.attachment_not_exist"))
|
2018-09-19 11:32:29 +08:00
|
|
|
|
} else if attach.BookId != 0 || attach.DocumentId != blogId {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.attachment_not_exist"))
|
2018-07-18 17:10:24 +08:00
|
|
|
|
}
|
2018-07-17 19:13:11 +08:00
|
|
|
|
|
2018-09-19 11:32:29 +08:00
|
|
|
|
if err := attach.Delete(); err != nil {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error(err)
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.JsonResult(6005, i18n.Tr(c.Lang, "message.failed"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
os.Remove(filepath.Join(conf.WorkingDirectory, attach.FilePath))
|
|
|
|
|
|
|
|
|
|
c.JsonResult(0, "ok", attach)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-20 14:30:27 +08:00
|
|
|
|
// 下载附件
|
2018-07-17 19:13:11 +08:00
|
|
|
|
func (c *BlogController) Download() {
|
|
|
|
|
c.Prepare()
|
|
|
|
|
|
|
|
|
|
blogId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
|
|
|
|
|
attachId, _ := strconv.Atoi(c.Ctx.Input.Param(":attach_id"))
|
|
|
|
|
password := c.GetString("password")
|
|
|
|
|
|
|
|
|
|
blog, err := models.NewBlog().Find(blogId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == orm.ErrNoRows {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.doc_not_exist"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
} else {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.query_failed"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-19 11:32:29 +08:00
|
|
|
|
blogReadSession := fmt.Sprintf("blog:read:%d", blogId)
|
2018-08-29 10:11:30 +08:00
|
|
|
|
//如果没有启动匿名访问,或者设置了访问密码
|
2021-03-23 21:55:50 +08:00
|
|
|
|
if (c.Member == nil && !c.EnableAnonymous) || (blog.BlogStatus == "password" && password != blog.Password && c.CruSession.Get(context.TODO(), blogReadSession) == nil) {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.ShowErrorPage(403, i18n.Tr(c.Lang, "message.no_permission"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查找附件
|
|
|
|
|
attachment, err := models.NewAttachment().Find(attachId)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == orm.ErrNoRows {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.attachment_not_exist"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
} else {
|
2021-03-30 14:24:14 +08:00
|
|
|
|
logs.Error("查询附件时出现异常 -> ", err)
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.ShowErrorPage(500, i18n.Tr(c.Lang, "message.query_failed"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-22 18:51:39 +08:00
|
|
|
|
|
|
|
|
|
//如果是链接的文章,需要校验文档ID是否一致,如果不是,需要保证附件的项目ID为0且文档的ID等于博文ID
|
|
|
|
|
if blog.BlogType == 1 && attachment.DocumentId != blog.DocumentId {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.attachment_not_exist"))
|
2021-03-30 14:24:14 +08:00
|
|
|
|
} else if blog.BlogType != 1 && (attachment.BookId != 0 || attachment.DocumentId != blogId) {
|
2021-04-19 11:24:12 +08:00
|
|
|
|
c.ShowErrorPage(404, i18n.Tr(c.Lang, "message.attachment_not_exist"))
|
2018-07-17 19:13:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.Ctx.Output.Download(filepath.Join(conf.WorkingDirectory, attachment.FilePath), attachment.FileName)
|
|
|
|
|
c.StopRun()
|
2018-07-13 16:46:40 +08:00
|
|
|
|
}
|