BlogController.go 18 KB

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