BlogController.go 18 KB

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