BlogController.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. package controllers
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "html/template"
  7. "net/http"
  8. "net/url"
  9. "os"
  10. "path/filepath"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/beego/beego/v2/adapter/orm"
  15. "github.com/beego/beego/v2/core/logs"
  16. "github.com/beego/beego/v2/server/web"
  17. "github.com/mindoc-org/mindoc/conf"
  18. "github.com/mindoc-org/mindoc/models"
  19. "github.com/mindoc-org/mindoc/utils"
  20. "github.com/mindoc-org/mindoc/utils/pagination"
  21. )
  22. type BlogController struct {
  23. BaseController
  24. }
  25. func (c *BlogController) Prepare() {
  26. c.BaseController.Prepare()
  27. if !c.EnableAnonymous && c.Member == nil {
  28. c.Redirect(conf.URLFor("AccountController.Login")+"?url="+url.PathEscape(conf.BaseUrl+c.Ctx.Request.URL.RequestURI()), 302)
  29. }
  30. }
  31. //文章阅读
  32. func (c *BlogController) Index() {
  33. c.Prepare()
  34. c.TplName = "blog/index.tpl"
  35. blogId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  36. if blogId <= 0 {
  37. c.ShowErrorPage(404, "页面不存在")
  38. }
  39. blogReadSession := fmt.Sprintf("blog:read:%d", blogId)
  40. blog, err := models.NewBlog().FindFromCache(blogId)
  41. if err != nil {
  42. c.ShowErrorPage(404, "文章不存在")
  43. }
  44. if c.Ctx.Input.IsPost() {
  45. password := c.GetString("password")
  46. if blog.BlogStatus == "password" && password != blog.Password {
  47. c.JsonResult(6001, "文章密码不正确")
  48. } else if blog.BlogStatus == "password" && password == blog.Password {
  49. //如果密码输入正确,则存入session中
  50. _ = c.CruSession.Set(context.TODO(), blogReadSession, blogId)
  51. c.JsonResult(0, "OK")
  52. }
  53. c.JsonResult(0, "OK")
  54. } else if blog.BlogStatus == "password" && (c.CruSession.Get(context.TODO(), blogReadSession) == nil || (c.Member != nil && blog.MemberId != c.Member.MemberId && !c.Member.IsAdministrator())) {
  55. //如果不存在已输入密码的标记
  56. c.TplName = "blog/index_password.tpl"
  57. }
  58. if blog.BlogType != 1 {
  59. //加载文章附件
  60. _ = blog.LinkAttach()
  61. }
  62. c.Data["Model"] = blog
  63. c.Data["Content"] = template.HTML(blog.BlogRelease)
  64. if blog.BlogExcerpt == "" {
  65. c.Data["Description"] = utils.AutoSummary(blog.BlogRelease, 120)
  66. } else {
  67. c.Data["Description"] = blog.BlogExcerpt
  68. }
  69. if nextBlog, err := models.NewBlog().QueryNext(blogId); err == nil {
  70. c.Data["Next"] = nextBlog
  71. }
  72. if preBlog, err := models.NewBlog().QueryPrevious(blogId); err == nil {
  73. c.Data["Previous"] = preBlog
  74. }
  75. }
  76. //文章列表
  77. func (c *BlogController) List() {
  78. c.Prepare()
  79. c.TplName = "blog/list.tpl"
  80. pageIndex, _ := c.GetInt("page", 1)
  81. var blogList []*models.Blog
  82. var totalCount int
  83. var err error
  84. blogList, totalCount, err = models.NewBlog().FindToPager(pageIndex, conf.PageSize, 0, "")
  85. if err != nil && err != orm.ErrNoRows {
  86. c.ShowErrorPage(500, err.Error())
  87. }
  88. if totalCount > 0 {
  89. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  90. c.Data["PageHtml"] = pager.HtmlPages()
  91. for _, blog := range blogList {
  92. //如果没有添加文章摘要,则自动提取
  93. if blog.BlogExcerpt == "" {
  94. blog.BlogExcerpt = utils.AutoSummary(blog.BlogRelease, 120)
  95. }
  96. blog.Link()
  97. }
  98. } else {
  99. c.Data["PageHtml"] = ""
  100. }
  101. c.Data["Lists"] = blogList
  102. }
  103. //管理后台文章列表
  104. func (c *BlogController) ManageList() {
  105. c.Prepare()
  106. c.TplName = "blog/manage_list.tpl"
  107. pageIndex, _ := c.GetInt("page", 1)
  108. blogList, totalCount, err := models.NewBlog().FindToPager(pageIndex, conf.PageSize, c.Member.MemberId, "")
  109. if err != nil {
  110. c.ShowErrorPage(500, err.Error())
  111. }
  112. if totalCount > 0 {
  113. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  114. c.Data["PageHtml"] = pager.HtmlPages()
  115. } else {
  116. c.Data["PageHtml"] = ""
  117. }
  118. c.Data["ModelList"] = blogList
  119. }
  120. //文章设置
  121. func (c *BlogController) ManageSetting() {
  122. c.Prepare()
  123. c.TplName = "blog/manage_setting.tpl"
  124. //如果是post请求
  125. if c.Ctx.Input.IsPost() {
  126. blogId, _ := c.GetInt("id", 0)
  127. blogTitle := c.GetString("title")
  128. blogIdentify := c.GetString("identify")
  129. orderIndex, _ := c.GetInt("order_index", 0)
  130. blogType, _ := c.GetInt("blog_type", 0)
  131. blogExcerpt := c.GetString("excerpt", "")
  132. blogStatus := c.GetString("status", "publish")
  133. blogPassword := c.GetString("password", "")
  134. documentIdentify := strings.TrimSpace(c.GetString("documentIdentify"))
  135. bookIdentify := strings.TrimSpace(c.GetString("bookIdentify"))
  136. documentId := 0
  137. if blogTitle == "" {
  138. c.JsonResult(6001, "文章标题不能为空")
  139. }
  140. if strings.Count(blogExcerpt, "") > 500 {
  141. c.JsonResult(6008, "文章摘要必须小于500字符")
  142. }
  143. if blogStatus != "public" && blogStatus != "password" && blogStatus != "draft" {
  144. blogStatus = "public"
  145. }
  146. if blogStatus == "password" && blogPassword == "" {
  147. c.JsonResult(6010, "加密文章请设置密码")
  148. }
  149. if blogType != 0 && blogType != 1 {
  150. c.JsonResult(6005, "未知的文章类型")
  151. }
  152. if strings.Count(blogTitle, "") > 200 {
  153. c.JsonResult(6002, "文章标题不能大于200个字符")
  154. }
  155. //如果是关联文章,需要同步关联的文档
  156. if blogType == 1 {
  157. book, err := models.NewBook().FindByIdentify(bookIdentify)
  158. if err != nil {
  159. c.JsonResult(6011, "关联文档的项目不存在")
  160. }
  161. doc, err := models.NewDocument().FindByIdentityFirst(documentIdentify, book.BookId)
  162. if err != nil {
  163. c.JsonResult(6003, "查询关联项目文档时出错")
  164. }
  165. documentId = doc.DocumentId
  166. // 如果不是超级管理员,则校验权限
  167. if !c.Member.IsAdministrator() {
  168. bookResult, err := models.NewBookResult().FindByIdentify(book.Identify, c.Member.MemberId)
  169. if err != nil || bookResult.RoleId == conf.BookObserver {
  170. c.JsonResult(6002, "关联文档不存在或权限不足")
  171. }
  172. }
  173. }
  174. var blog *models.Blog
  175. var err error
  176. //如果文章ID存在,则从数据库中查询文章
  177. if blogId > 0 {
  178. if c.Member.IsAdministrator() {
  179. blog, err = models.NewBlog().Find(blogId)
  180. } else {
  181. blog, err = models.NewBlog().FindByIdAndMemberId(blogId, c.Member.MemberId)
  182. }
  183. if err != nil {
  184. c.JsonResult(6003, "文章不存在")
  185. }
  186. //如果设置了文章标识
  187. if blogIdentify != "" {
  188. //如果查询到的文章标识存在并且不是当前文章的id
  189. if b, err := models.NewBlog().FindByIdentify(blogIdentify); err == nil && b.BlogId != blogId {
  190. c.JsonResult(6004, "文章标识已存在")
  191. }
  192. }
  193. blog.Modified = time.Now()
  194. blog.ModifyAt = c.Member.MemberId
  195. } else {
  196. //如果设置了文章标识
  197. if blogIdentify != "" {
  198. if models.NewBlog().IsExist(blogIdentify) {
  199. c.JsonResult(6004, "文章标识已存在")
  200. }
  201. }
  202. blog = models.NewBlog()
  203. blog.MemberId = c.Member.MemberId
  204. blog.Created = time.Now()
  205. }
  206. if blogIdentify == "" {
  207. blog.BlogIdentify = fmt.Sprintf("%s-%d", "post", time.Now().UnixNano())
  208. } else {
  209. blog.BlogIdentify = blogIdentify
  210. }
  211. blog.BlogTitle = blogTitle
  212. blog.OrderIndex = orderIndex
  213. blog.BlogType = blogType
  214. if blogType == 1 {
  215. blog.DocumentId = documentId
  216. }
  217. blog.BlogExcerpt = blogExcerpt
  218. blog.BlogStatus = blogStatus
  219. blog.Password = blogPassword
  220. if err := blog.Save(); err != nil {
  221. logs.Error("保存文章失败 -> ", err)
  222. c.JsonResult(6011, "保存文章失败")
  223. } else {
  224. c.JsonResult(0, "ok", blog)
  225. }
  226. }
  227. if c.Ctx.Input.Referer() == "" {
  228. c.Data["Referer"] = "javascript:history.back();"
  229. } else {
  230. c.Data["Referer"] = c.Ctx.Input.Referer()
  231. }
  232. blogId, err := strconv.Atoi(c.Ctx.Input.Param(":id"))
  233. c.Data["DocumentIdentify"] = ""
  234. if err == nil {
  235. blog, err := models.NewBlog().FindByIdAndMemberId(blogId, c.Member.MemberId)
  236. if err != nil {
  237. c.ShowErrorPage(500, err.Error())
  238. }
  239. c.Data["Model"] = blog
  240. } else {
  241. c.Data["Model"] = models.NewBlog()
  242. }
  243. }
  244. //文章创建或编辑
  245. func (c *BlogController) ManageEdit() {
  246. c.Prepare()
  247. c.TplName = "blog/manage_edit.tpl"
  248. if c.Ctx.Input.IsPost() {
  249. blogId, _ := c.GetInt("blogId", 0)
  250. if blogId <= 0 {
  251. c.JsonResult(6001, "文章参数错误")
  252. }
  253. blogContent := c.GetString("content", "")
  254. blogHtml := c.GetString("htmlContent", "")
  255. version, _ := c.GetInt64("version", 0)
  256. cover := c.GetString("cover")
  257. var blog *models.Blog
  258. var err error
  259. if c.Member.IsAdministrator() {
  260. blog, err = models.NewBlog().Find(blogId)
  261. } else {
  262. blog, err = models.NewBlog().FindByIdAndMemberId(blogId, c.Member.MemberId)
  263. }
  264. if err != nil {
  265. logs.Error("查询文章失败 ->", err)
  266. c.JsonResult(6002, "查询文章失败")
  267. }
  268. if version > 0 && blog.Version != version && cover != "yes" {
  269. c.JsonResult(6005, "文章已被修改")
  270. }
  271. //如果是关联文章,需要同步关联的文档
  272. if blog.BlogType == 1 {
  273. doc, err := models.NewDocument().Find(blog.DocumentId)
  274. if err != nil {
  275. logs.Error("查询关联项目文档时出错 ->", err)
  276. c.JsonResult(6003, "查询关联项目文档时出错")
  277. }
  278. book, err := models.NewBook().Find(doc.BookId)
  279. if err != nil {
  280. c.JsonResult(6002, "项目不存在或权限不足")
  281. }
  282. // 如果不是超级管理员,则校验权限
  283. if !c.Member.IsAdministrator() {
  284. bookResult, err := models.NewBookResult().FindByIdentify(book.Identify, c.Member.MemberId)
  285. if err != nil || bookResult.RoleId == conf.BookObserver {
  286. logs.Error("FindByIdentify => ", err)
  287. c.JsonResult(6002, "关联文档不存在或权限不足")
  288. }
  289. }
  290. doc.Markdown = blogContent
  291. doc.Release = blogHtml
  292. doc.Content = blogHtml
  293. doc.ModifyTime = time.Now()
  294. doc.ModifyAt = c.Member.MemberId
  295. if err := doc.InsertOrUpdate("markdown", "release", "content", "modify_time", "modify_at"); err != nil {
  296. logs.Error("保存关联文档时出错 ->", err)
  297. c.JsonResult(6004, "保存关联文档时出错")
  298. }
  299. }
  300. blog.BlogContent = blogContent
  301. blog.BlogRelease = blogHtml
  302. blog.ModifyAt = c.Member.MemberId
  303. blog.Modified = time.Now()
  304. if err := blog.Save("blog_content", "blog_release", "modify_at", "modify_time", "version"); err != nil {
  305. logs.Error("保存文章失败 -> ", err)
  306. c.JsonResult(6011, "保存文章失败")
  307. } else {
  308. c.JsonResult(0, "ok", blog)
  309. }
  310. }
  311. blogId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  312. if blogId <= 0 {
  313. c.ShowErrorPage(500, "参数错误")
  314. }
  315. var blog *models.Blog
  316. var err error
  317. if c.Member.IsAdministrator() {
  318. blog, err = models.NewBlog().Find(blogId)
  319. } else {
  320. blog, err = models.NewBlog().FindByIdAndMemberId(blogId, c.Member.MemberId)
  321. }
  322. if err != nil {
  323. c.ShowErrorPage(404, "文章不存在或已删除")
  324. }
  325. blog.LinkAttach()
  326. if len(blog.AttachList) > 0 {
  327. returnJSON, err := json.Marshal(blog.AttachList)
  328. if err != nil {
  329. logs.Error("序列化文章附件时出错 ->", err)
  330. } else {
  331. c.Data["AttachList"] = template.JS(string(returnJSON))
  332. }
  333. } else {
  334. c.Data["AttachList"] = template.JS("[]")
  335. }
  336. if conf.GetUploadFileSize() > 0 {
  337. c.Data["UploadFileSize"] = conf.GetUploadFileSize()
  338. } else {
  339. c.Data["UploadFileSize"] = "undefined"
  340. }
  341. c.Data["Model"] = blog
  342. }
  343. //删除文章
  344. func (c *BlogController) ManageDelete() {
  345. c.Prepare()
  346. blogId, _ := c.GetInt("blog_id", 0)
  347. if blogId <= 0 {
  348. c.JsonResult(6001, "参数错误")
  349. }
  350. var blog *models.Blog
  351. var err error
  352. if c.Member.IsAdministrator() {
  353. blog, err = models.NewBlog().Find(blogId)
  354. } else {
  355. blog, err = models.NewBlog().FindByIdAndMemberId(blogId, c.Member.MemberId)
  356. }
  357. if err != nil {
  358. c.JsonResult(6002, "文章不存在或已删除")
  359. }
  360. if err := blog.Delete(blogId); err != nil {
  361. c.JsonResult(6003, "删除失败")
  362. } else {
  363. c.JsonResult(0, "删除成功")
  364. }
  365. }
  366. // 上传附件或图片
  367. func (c *BlogController) Upload() {
  368. c.Prepare()
  369. blogId, _ := c.GetInt("blogId")
  370. if blogId <= 0 {
  371. c.JsonResult(6001, "参数错误")
  372. }
  373. blog, err := models.NewBlog().Find(blogId)
  374. if err != nil {
  375. c.JsonResult(6010, "文章不存在")
  376. }
  377. if !c.Member.IsAdministrator() && blog.MemberId != c.Member.MemberId {
  378. c.JsonResult(6011, "没有文章的访问权限")
  379. }
  380. name := "editormd-file-file"
  381. file, moreFile, err := c.GetFile(name)
  382. if err == http.ErrMissingFile {
  383. name = "editormd-image-file"
  384. file, moreFile, err = c.GetFile(name)
  385. if err == http.ErrMissingFile {
  386. c.JsonResult(6003, "没有发现需要上传的图片")
  387. }
  388. }
  389. if err != nil {
  390. c.JsonResult(6002, err.Error())
  391. }
  392. defer file.Close()
  393. type Size interface {
  394. Size() int64
  395. }
  396. if conf.GetUploadFileSize() > 0 && moreFile.Size > conf.GetUploadFileSize() {
  397. c.JsonResult(6009, "查过文件允许的上传最大值")
  398. }
  399. ext := filepath.Ext(moreFile.Filename)
  400. if ext == "" {
  401. c.JsonResult(6003, "无法解析文件的格式")
  402. }
  403. //如果文件类型设置为 * 标识不限制文件类型
  404. if web.AppConfig.DefaultString("upload_file_ext", "") != "*" {
  405. if !conf.IsAllowUploadFileExt(ext) {
  406. c.JsonResult(6004, "不允许的文件类型")
  407. }
  408. }
  409. // 如果是超级管理员,则不判断权限
  410. if c.Member.IsAdministrator() {
  411. _, err := models.NewBlog().Find(blogId)
  412. if err != nil {
  413. c.JsonResult(6006, "文档不存在或权限不足")
  414. }
  415. } else {
  416. _, err := models.NewBlog().FindByIdAndMemberId(blogId, c.Member.MemberId)
  417. if err != nil {
  418. logs.Error("查询文章时出错 -> ", err)
  419. if err == orm.ErrNoRows {
  420. c.JsonResult(6006, "权限不足")
  421. }
  422. c.JsonResult(6001, err.Error())
  423. }
  424. }
  425. fileName := "attach_" + strconv.FormatInt(time.Now().UnixNano(), 16)
  426. filePath := filepath.Join(conf.WorkingDirectory, "uploads", "blog", time.Now().Format("200601"), fileName+ext)
  427. path := filepath.Dir(filePath)
  428. os.MkdirAll(path, os.ModePerm)
  429. err = c.SaveToFile(name, filePath)
  430. if err != nil {
  431. logs.Error("SaveToFile => ", err)
  432. c.JsonResult(6005, "保存文件失败")
  433. }
  434. var httpPath string
  435. result := make(map[string]interface{})
  436. //如果是图片,则当做内置图片处理,否则当做附件处理
  437. if strings.EqualFold(ext, ".jpg") || strings.EqualFold(ext, ".jpeg") || strings.EqualFold(ext, ".png") || strings.EqualFold(ext, ".gif") {
  438. httpPath = "/" + strings.Replace(strings.TrimPrefix(filePath, conf.WorkingDirectory), "\\", "/", -1)
  439. if strings.HasPrefix(httpPath, "//") {
  440. httpPath = conf.URLForWithCdnImage(string(httpPath[1:]))
  441. }
  442. } else {
  443. attachment := models.NewAttachment()
  444. attachment.BookId = 0
  445. attachment.FileName = moreFile.Filename
  446. attachment.CreateAt = c.Member.MemberId
  447. attachment.FileExt = ext
  448. attachment.FilePath = strings.TrimPrefix(filePath, conf.WorkingDirectory)
  449. attachment.DocumentId = blogId
  450. //如果是关联文章,则将附件设置为关联文档的文档上
  451. if blog.BlogType == 1 {
  452. attachment.BookId = blog.BookId
  453. attachment.DocumentId = blog.DocumentId
  454. }
  455. if fileInfo, err := os.Stat(filePath); err == nil {
  456. attachment.FileSize = float64(fileInfo.Size())
  457. }
  458. attachment.HttpPath = httpPath
  459. if err := attachment.Insert(); err != nil {
  460. os.Remove(filePath)
  461. logs.Error("保存文件附件失败 -> ", err)
  462. c.JsonResult(6006, "文件保存失败")
  463. }
  464. if attachment.HttpPath == "" {
  465. attachment.HttpPath = conf.URLForNotHost("BlogController.Download", ":id", blogId, ":attach_id", attachment.AttachmentId)
  466. if err := attachment.Update(); err != nil {
  467. logs.Error("保存文件失败 -> ", attachment.FilePath, err)
  468. c.JsonResult(6005, "保存文件失败")
  469. }
  470. }
  471. result["attach"] = attachment
  472. }
  473. result["errcode"] = 0
  474. result["success"] = 1
  475. result["message"] = "ok"
  476. result["url"] = httpPath
  477. result["alt"] = fileName
  478. c.Ctx.Output.JSON(result, true, false)
  479. c.StopRun()
  480. }
  481. // 删除附件
  482. func (c *BlogController) RemoveAttachment() {
  483. c.Prepare()
  484. attachId, _ := c.GetInt("attach_id")
  485. blogId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  486. if attachId <= 0 {
  487. c.JsonResult(6001, "参数错误")
  488. }
  489. blog, err := models.NewBlog().Find(blogId)
  490. if err != nil {
  491. if err == orm.ErrNoRows {
  492. c.ShowErrorPage(500, "文档不存在")
  493. } else {
  494. c.ShowErrorPage(500, "查询文章时异常")
  495. }
  496. }
  497. attach, err := models.NewAttachment().Find(attachId)
  498. if err != nil {
  499. logs.Error(err)
  500. c.JsonResult(6002, "附件不存在")
  501. }
  502. if !c.Member.IsAdministrator() {
  503. _, err := models.NewBlog().FindByIdAndMemberId(attach.DocumentId, c.Member.MemberId)
  504. if err != nil {
  505. logs.Error(err)
  506. c.JsonResult(6003, "文档不存在")
  507. }
  508. }
  509. if blog.BlogType == 1 && attach.BookId != blog.BookId && attach.DocumentId != blog.DocumentId {
  510. c.ShowErrorPage(404, "附件不存在")
  511. } else if attach.BookId != 0 || attach.DocumentId != blogId {
  512. c.ShowErrorPage(404, "附件不存在")
  513. }
  514. if err := attach.Delete(); err != nil {
  515. logs.Error(err)
  516. c.JsonResult(6005, "删除失败")
  517. }
  518. os.Remove(filepath.Join(conf.WorkingDirectory, attach.FilePath))
  519. c.JsonResult(0, "ok", attach)
  520. }
  521. //下载附件
  522. func (c *BlogController) Download() {
  523. c.Prepare()
  524. blogId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  525. attachId, _ := strconv.Atoi(c.Ctx.Input.Param(":attach_id"))
  526. password := c.GetString("password")
  527. blog, err := models.NewBlog().Find(blogId)
  528. if err != nil {
  529. if err == orm.ErrNoRows {
  530. c.ShowErrorPage(500, "文档不存在")
  531. } else {
  532. c.ShowErrorPage(500, "查询文章时异常")
  533. }
  534. }
  535. blogReadSession := fmt.Sprintf("blog:read:%d", blogId)
  536. //如果没有启动匿名访问,或者设置了访问密码
  537. if (c.Member == nil && !c.EnableAnonymous) || (blog.BlogStatus == "password" && password != blog.Password && c.CruSession.Get(context.TODO(), blogReadSession) == nil) {
  538. c.ShowErrorPage(403, "没有下载权限")
  539. }
  540. // 查找附件
  541. attachment, err := models.NewAttachment().Find(attachId)
  542. if err != nil {
  543. if err == orm.ErrNoRows {
  544. c.ShowErrorPage(404, "附件不存在")
  545. } else {
  546. logs.Error("查询附件时出现异常 -> ", err)
  547. c.ShowErrorPage(500, "查询附件时出现异常")
  548. }
  549. }
  550. //如果是链接的文章,需要校验文档ID是否一致,如果不是,需要保证附件的项目ID为0且文档的ID等于博文ID
  551. if blog.BlogType == 1 && attachment.DocumentId != blog.DocumentId {
  552. c.ShowErrorPage(404, "附件不存在")
  553. } else if blog.BlogType != 1 && (attachment.BookId != 0 || attachment.DocumentId != blogId) {
  554. c.ShowErrorPage(404, "附件不存在")
  555. }
  556. c.Ctx.Output.Download(filepath.Join(conf.WorkingDirectory, attachment.FilePath), attachment.FileName)
  557. c.StopRun()
  558. }