DocumentController.go 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. package controllers
  2. import (
  3. "container/list"
  4. "encoding/json"
  5. "html/template"
  6. "net/http"
  7. "os"
  8. "path/filepath"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "net/url"
  14. "image/png"
  15. "bytes"
  16. "github.com/PuerkitoBio/goquery"
  17. "github.com/astaxie/beego"
  18. "github.com/astaxie/beego/orm"
  19. "github.com/boombuler/barcode"
  20. "github.com/boombuler/barcode/qr"
  21. "github.com/lifei6671/mindoc/conf"
  22. "github.com/lifei6671/mindoc/models"
  23. "github.com/lifei6671/mindoc/utils"
  24. "github.com/lifei6671/mindoc/utils/pagination"
  25. "gopkg.in/russross/blackfriday.v2"
  26. "github.com/lifei6671/mindoc/utils/cryptil"
  27. "fmt"
  28. "github.com/lifei6671/mindoc/utils/filetil"
  29. "github.com/lifei6671/mindoc/utils/gopool"
  30. )
  31. // DocumentController struct
  32. type DocumentController struct {
  33. BaseController
  34. }
  35. // 文档首页
  36. func (c *DocumentController) Index() {
  37. c.Prepare()
  38. identify := c.Ctx.Input.Param(":key")
  39. token := c.GetString("token")
  40. if identify == "" {
  41. c.Abort("404")
  42. }
  43. // 如果没有开启匿名访问则跳转到登录
  44. if !c.EnableAnonymous && !isUserLoggedIn(c) {
  45. promptUserToLogIn(c)
  46. return
  47. }
  48. bookResult := isReadable(identify, token, c)
  49. c.TplName = "document/" + bookResult.Theme + "_read.tpl"
  50. selected := 0
  51. if bookResult.IsUseFirstDocument {
  52. doc,err := bookResult.FindFirstDocumentByBookId(bookResult.BookId)
  53. if err == nil {
  54. if strings.TrimSpace(doc.Release) != "" {
  55. doc.Release += "<div class=\"wiki-bottom\">文档更新时间: " + doc.ModifyTime.Local().Format("2006-01-02 15:04") + "</div>";
  56. }
  57. selected = doc.DocumentId
  58. c.Data["Title"] = doc.DocumentName
  59. c.Data["Content"] = template.HTML(doc.Release)
  60. c.Data["Description"] = utils.AutoSummary(doc.Release,120)
  61. }
  62. }else {
  63. c.Data["Title"] = "概要"
  64. c.Data["Content"] = template.HTML(blackfriday.Run([]byte(bookResult.Description)))
  65. }
  66. tree, err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId, selected)
  67. if err != nil {
  68. beego.Error(err)
  69. c.Abort("500")
  70. }
  71. c.Data["Model"] = bookResult
  72. c.Data["Result"] = template.HTML(tree)
  73. }
  74. // 阅读文档
  75. func (c *DocumentController) Read() {
  76. c.Prepare()
  77. identify := c.Ctx.Input.Param(":key")
  78. token := c.GetString("token")
  79. id := c.GetString(":id")
  80. c.Data["DocumentId"] = id
  81. if identify == "" || id == "" {
  82. c.Abort("404")
  83. }
  84. // 如果没有开启匿名访问则跳转到登录
  85. if !c.EnableAnonymous && !isUserLoggedIn(c) {
  86. promptUserToLogIn(c)
  87. return
  88. }
  89. bookResult := isReadable(identify, token, c)
  90. c.TplName = fmt.Sprintf("document/%s_read.tpl",bookResult.Theme)
  91. doc := models.NewDocument()
  92. if docId, err := strconv.Atoi(id); err == nil {
  93. doc, err = doc.FromCacheById(docId)
  94. if err != nil {
  95. beego.Error(err)
  96. c.Abort("500")
  97. }
  98. } else {
  99. doc, err = doc.FromCacheByIdentify(id,bookResult.BookId)
  100. if err != nil {
  101. beego.Error(err)
  102. c.Abort("500")
  103. }
  104. }
  105. if doc.BookId != bookResult.BookId {
  106. c.Abort("403")
  107. }
  108. attach, err := models.NewAttachment().FindListByDocumentId(doc.DocumentId)
  109. if err == nil {
  110. doc.AttachList = attach
  111. }
  112. cdnimg := beego.AppConfig.String("cdnimg")
  113. if doc.Release != "" && cdnimg != "" {
  114. query, err := goquery.NewDocumentFromReader(bytes.NewBufferString(doc.Release))
  115. if err != nil {
  116. beego.Error(err)
  117. } else {
  118. query.Find("img").Each(func(i int, contentSelection *goquery.Selection) {
  119. if src, ok := contentSelection.Attr("src"); ok && strings.HasPrefix(src, "/uploads/") {
  120. contentSelection.SetAttr("src", utils.JoinURI(cdnimg, src))
  121. }
  122. })
  123. html, err := query.Html()
  124. if err != nil {
  125. beego.Error(err)
  126. } else {
  127. doc.Release = html
  128. }
  129. }
  130. }
  131. // assemble doc info, added by dandycheung, 2017-12-20
  132. docInfo := ""
  133. docCreator, err := models.NewMember().Find(doc.MemberId)
  134. if err == nil {
  135. docInfo += docCreator.Account
  136. }
  137. docInfo += " 创建于 "
  138. docInfo += doc.CreateTime.Local().Format("2006-01-02 15:04")
  139. if doc.ModifyTime != doc.CreateTime {
  140. docInfo += ";更新于 "
  141. docInfo += doc.ModifyTime.Local().Format("2006-01-02 15:04")
  142. if strings.TrimSpace(doc.Release) != "" {
  143. doc.Release += "<div class=\"wiki-bottom\">文档更新时间: " + doc.ModifyTime.Local().Format("2006-01-02 15:04") +" &nbsp;&nbsp;作者:";
  144. if docCreator != nil {
  145. if docCreator.RealName != "" {
  146. doc.Release += docCreator.RealName
  147. }else{
  148. doc.Release += docCreator.Account
  149. }
  150. }
  151. doc.Release += "</div>"
  152. }
  153. }
  154. if c.IsAjax() {
  155. var data struct {
  156. DocTitle string `json:"doc_title"`
  157. Body string `json:"body"`
  158. Title string `json:"title"`
  159. DocInfo string `json:"doc_info"`
  160. }
  161. data.DocTitle = doc.DocumentName
  162. data.Body = doc.Release
  163. data.Title = doc.DocumentName + " - Powered by MinDoc"
  164. data.DocInfo = docInfo
  165. c.JsonResult(0, "ok", data)
  166. }
  167. tree, err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId, doc.DocumentId)
  168. if err != nil {
  169. beego.Error(err)
  170. c.Abort("500")
  171. }
  172. c.Data["Description"] = utils.AutoSummary(doc.Release,120)
  173. c.Data["Model"] = bookResult
  174. c.Data["Result"] = template.HTML(tree)
  175. c.Data["Title"] = doc.DocumentName
  176. c.Data["Info"] = docInfo
  177. c.Data["Content"] = template.HTML(doc.Release)
  178. }
  179. // 编辑文档
  180. func (c *DocumentController) Edit() {
  181. c.Prepare()
  182. identify := c.Ctx.Input.Param(":key")
  183. if identify == "" {
  184. c.Abort("404")
  185. }
  186. bookResult := models.NewBookResult()
  187. var err error
  188. // 如果是超级管理者,则不判断权限
  189. if c.Member.IsAdministrator() {
  190. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  191. if err != nil {
  192. c.JsonResult(6002, "项目不存在或权限不足")
  193. }
  194. bookResult = models.NewBookResult().ToBookResult(*book)
  195. } else {
  196. bookResult, err = models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  197. if err != nil {
  198. beego.Error("DocumentController.Edit => ", err)
  199. c.Abort("403")
  200. }
  201. if bookResult.RoleId == conf.BookObserver {
  202. c.JsonResult(6002, "项目不存在或权限不足")
  203. }
  204. }
  205. // 根据不同编辑器类型加载编辑器
  206. if bookResult.Editor == "markdown" {
  207. c.TplName = "document/markdown_edit_template.tpl"
  208. } else if bookResult.Editor == "html" {
  209. c.TplName = "document/new_html_edit_template.tpl"
  210. } else {
  211. c.TplName = "document/" + bookResult.Editor + "_edit_template.tpl"
  212. }
  213. c.Data["Model"] = bookResult
  214. r, _ := json.Marshal(bookResult)
  215. c.Data["ModelResult"] = template.JS(string(r))
  216. c.Data["Result"] = template.JS("[]")
  217. trees, err := models.NewDocument().FindDocumentTree(bookResult.BookId)
  218. if err != nil {
  219. beego.Error("FindDocumentTree => ", err)
  220. } else {
  221. if len(trees) > 0 {
  222. if jtree, err := json.Marshal(trees); err == nil {
  223. c.Data["Result"] = template.JS(string(jtree))
  224. }
  225. } else {
  226. c.Data["Result"] = template.JS("[]")
  227. }
  228. }
  229. c.Data["BaiDuMapKey"] = beego.AppConfig.DefaultString("baidumapkey", "")
  230. }
  231. // 创建一个文档
  232. func (c *DocumentController) Create() {
  233. identify := c.GetString("identify")
  234. docIdentify := c.GetString("doc_identify")
  235. docName := c.GetString("doc_name")
  236. parentId, _ := c.GetInt("parent_id", 0)
  237. docId, _ := c.GetInt("doc_id", 0)
  238. if identify == "" {
  239. c.JsonResult(6001, "参数错误")
  240. }
  241. if docName == "" {
  242. c.JsonResult(6004, "文档名称不能为空")
  243. }
  244. bookId := 0
  245. // 如果是超级管理员则不判断权限
  246. if c.Member.IsAdministrator() {
  247. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  248. if err != nil {
  249. beego.Error(err)
  250. c.JsonResult(6002, "项目不存在或权限不足")
  251. }
  252. bookId = book.BookId
  253. } else {
  254. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  255. if err != nil || bookResult.RoleId == conf.BookObserver {
  256. beego.Error("FindByIdentify => ", err)
  257. c.JsonResult(6002, "项目不存在或权限不足")
  258. }
  259. bookId = bookResult.BookId
  260. }
  261. if docIdentify != "" {
  262. if ok, err := regexp.MatchString(`[a-z]+[a-zA-Z0-9_.\-]*$`, docIdentify); !ok || err != nil {
  263. c.JsonResult(6003, "文档标识只能包含小写字母、数字,以及“-”、“.”和“_”符号")
  264. }
  265. d, _ := models.NewDocument().FindByIdentityFirst(docIdentify,bookId)
  266. if d.DocumentId > 0 && d.DocumentId != docId {
  267. c.JsonResult(6006, "文档标识已被使用")
  268. }
  269. }
  270. if parentId > 0 {
  271. doc, err := models.NewDocument().Find(parentId)
  272. if err != nil || doc.BookId != bookId {
  273. c.JsonResult(6003, "父分类不存在")
  274. }
  275. }
  276. document, _ := models.NewDocument().Find(docId)
  277. document.MemberId = c.Member.MemberId
  278. document.BookId = bookId
  279. document.Identify = docIdentify
  280. document.Version = time.Now().Unix()
  281. document.DocumentName = docName
  282. document.ParentId = parentId
  283. if err := document.InsertOrUpdate(); err != nil {
  284. beego.Error("InsertOrUpdate => ", err)
  285. c.JsonResult(6005, "保存失败")
  286. } else {
  287. c.JsonResult(0, "ok", document)
  288. }
  289. }
  290. // 上传附件或图片
  291. func (c *DocumentController) Upload() {
  292. identify := c.GetString("identify")
  293. doc_id, _ := c.GetInt("doc_id")
  294. is_attach := true
  295. if identify == "" {
  296. c.JsonResult(6001, "参数错误")
  297. }
  298. name := "editormd-file-file"
  299. file, moreFile, err := c.GetFile(name)
  300. if err == http.ErrMissingFile {
  301. name = "editormd-image-file"
  302. file, moreFile, err = c.GetFile(name)
  303. if err == http.ErrMissingFile {
  304. c.JsonResult(6003, "没有发现需要上传的文件")
  305. }
  306. }
  307. if err != nil {
  308. c.JsonResult(6002, err.Error())
  309. }
  310. defer file.Close()
  311. type Size interface {
  312. Size() int64
  313. }
  314. if conf.GetUploadFileSize() > 0 && moreFile.Size > conf.GetUploadFileSize() {
  315. c.JsonResult(6009, "查过文件允许的上传最大值")
  316. }
  317. ext := filepath.Ext(moreFile.Filename)
  318. if ext == "" {
  319. c.JsonResult(6003, "无法解析文件的格式")
  320. }
  321. //如果文件类型设置为 * 标识不限制文件类型
  322. if beego.AppConfig.DefaultString("upload_file_ext", "") != "*" {
  323. if !conf.IsAllowUploadFileExt(ext) {
  324. c.JsonResult(6004, "不允许的文件类型")
  325. }
  326. }
  327. bookId := 0
  328. // 如果是超级管理员,则不判断权限
  329. if c.Member.IsAdministrator() {
  330. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  331. if err != nil {
  332. c.JsonResult(6006, "文档不存在或权限不足")
  333. }
  334. bookId = book.BookId
  335. } else {
  336. book, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  337. if err != nil {
  338. beego.Error("DocumentController.Edit => ", err)
  339. if err == orm.ErrNoRows {
  340. c.JsonResult(6006, "权限不足")
  341. }
  342. c.JsonResult(6001, err.Error())
  343. }
  344. // 如果没有编辑权限
  345. if book.RoleId != conf.BookEditor && book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder {
  346. c.JsonResult(6006, "权限不足")
  347. }
  348. bookId = book.BookId
  349. }
  350. if doc_id > 0 {
  351. doc, err := models.NewDocument().Find(doc_id)
  352. if err != nil {
  353. c.JsonResult(6007, "文档不存在")
  354. }
  355. if doc.BookId != bookId {
  356. c.JsonResult(6008, "文档不属于指定的项目")
  357. }
  358. }
  359. fileName := "attach_" + strconv.FormatInt(time.Now().UnixNano(), 16)
  360. filePath := filepath.Join(conf.WorkingDirectory, "uploads", time.Now().Format("200601"),identify, fileName+ext)
  361. path := filepath.Dir(filePath)
  362. os.MkdirAll(path, os.ModePerm)
  363. err = c.SaveToFile(name, filePath)
  364. if err != nil {
  365. beego.Error("SaveToFile => ", err)
  366. c.JsonResult(6005, "保存文件失败")
  367. }
  368. attachment := models.NewAttachment()
  369. attachment.BookId = bookId
  370. attachment.FileName = moreFile.Filename
  371. attachment.CreateAt = c.Member.MemberId
  372. attachment.FileExt = ext
  373. attachment.FilePath = strings.TrimPrefix(filePath, conf.WorkingDirectory)
  374. attachment.DocumentId = doc_id
  375. if fileInfo, err := os.Stat(filePath); err == nil {
  376. attachment.FileSize = float64(fileInfo.Size())
  377. }
  378. if doc_id > 0 {
  379. attachment.DocumentId = doc_id
  380. }
  381. if strings.EqualFold(ext, ".jpg") || strings.EqualFold(ext, ".jpeg") || strings.EqualFold(ext, ".png") || strings.EqualFold(ext, ".gif") {
  382. attachment.HttpPath = "/" + strings.Replace(strings.TrimPrefix(filePath, conf.WorkingDirectory), "\\", "/", -1)
  383. if strings.HasPrefix(attachment.HttpPath, "//") {
  384. attachment.HttpPath = conf.URLForWithCdnImage(string(attachment.HttpPath[1:]))
  385. }
  386. is_attach = false
  387. }
  388. err = attachment.Insert()
  389. if err != nil {
  390. os.Remove(filePath)
  391. beego.Error("Attachment Insert => ", err)
  392. c.JsonResult(6006, "文件保存失败")
  393. }
  394. if attachment.HttpPath == "" {
  395. attachment.HttpPath = conf.URLFor("DocumentController.DownloadAttachment", ":key", identify, ":attach_id", attachment.AttachmentId)
  396. if err := attachment.Update(); err != nil {
  397. beego.Error("SaveToFile => ", err)
  398. c.JsonResult(6005, "保存文件失败")
  399. }
  400. }
  401. result := map[string]interface{}{
  402. "errcode": 0,
  403. "success": 1,
  404. "message": "ok",
  405. "url": attachment.HttpPath,
  406. "alt": attachment.FileName,
  407. "is_attach": is_attach,
  408. "attach": attachment,
  409. }
  410. c.Ctx.Output.JSON(result, true, false)
  411. c.StopRun()
  412. }
  413. // 下载附件
  414. func (c *DocumentController) DownloadAttachment() {
  415. c.Prepare()
  416. identify := c.Ctx.Input.Param(":key")
  417. attachId, _ := strconv.Atoi(c.Ctx.Input.Param(":attach_id"))
  418. token := c.GetString("token")
  419. memberId := 0
  420. if c.Member != nil {
  421. memberId = c.Member.MemberId
  422. }
  423. bookId := 0
  424. // 判断用户是否参与了项目
  425. bookResult, err := models.NewBookResult().FindByIdentify(identify, memberId)
  426. if err != nil {
  427. // 判断项目公开状态
  428. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  429. if err != nil {
  430. c.Abort("404")
  431. }
  432. // 如果不是超级管理员则判断权限
  433. if c.Member == nil || c.Member.Role != conf.MemberSuperRole {
  434. // 如果项目是私有的,并且 token 不正确
  435. if (book.PrivatelyOwned == 1 && token == "") || (book.PrivatelyOwned == 1 && book.PrivateToken != token) {
  436. c.Abort("403")
  437. }
  438. }
  439. bookId = book.BookId
  440. } else {
  441. bookId = bookResult.BookId
  442. }
  443. // 查找附件
  444. attachment, err := models.NewAttachment().Find(attachId)
  445. if err != nil {
  446. beego.Error("DownloadAttachment => ", err)
  447. if err == orm.ErrNoRows {
  448. c.Abort("404")
  449. } else {
  450. c.Abort("500")
  451. }
  452. }
  453. if attachment.BookId != bookId {
  454. c.Abort("404")
  455. }
  456. c.Ctx.Output.Download(filepath.Join(conf.WorkingDirectory, attachment.FilePath), attachment.FileName)
  457. c.StopRun()
  458. }
  459. // 删除附件
  460. func (c *DocumentController) RemoveAttachment() {
  461. c.Prepare()
  462. attachId, _ := c.GetInt("attach_id")
  463. if attachId <= 0 {
  464. c.JsonResult(6001, "参数错误")
  465. }
  466. attach, err := models.NewAttachment().Find(attachId)
  467. if err != nil {
  468. beego.Error(err)
  469. c.JsonResult(6002, "附件不存在")
  470. }
  471. document, err := models.NewDocument().Find(attach.DocumentId)
  472. if err != nil {
  473. beego.Error(err)
  474. c.JsonResult(6003, "文档不存在")
  475. }
  476. if c.Member.Role != conf.MemberSuperRole {
  477. rel, err := models.NewRelationship().FindByBookIdAndMemberId(document.BookId, c.Member.MemberId)
  478. if err != nil {
  479. beego.Error(err)
  480. c.JsonResult(6004, "权限不足")
  481. }
  482. if rel.RoleId == conf.BookObserver {
  483. c.JsonResult(6004, "权限不足")
  484. }
  485. }
  486. err = attach.Delete()
  487. if err != nil {
  488. beego.Error(err)
  489. c.JsonResult(6005, "删除失败")
  490. }
  491. os.Remove(filepath.Join(conf.WorkingDirectory, attach.FilePath))
  492. c.JsonResult(0, "ok", attach)
  493. }
  494. // 删除文档
  495. func (c *DocumentController) Delete() {
  496. c.Prepare()
  497. identify := c.GetString("identify")
  498. doc_id, err := c.GetInt("doc_id", 0)
  499. book_id := 0
  500. // 如果是超级管理员则忽略权限判断
  501. if c.Member.IsAdministrator() {
  502. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  503. if err != nil {
  504. beego.Error("FindByIdentify => ", err)
  505. c.JsonResult(6002, "项目不存在或权限不足")
  506. }
  507. book_id = book.BookId
  508. } else {
  509. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  510. if err != nil || bookResult.RoleId == conf.BookObserver {
  511. beego.Error("FindByIdentify => ", err)
  512. c.JsonResult(6002, "项目不存在或权限不足")
  513. }
  514. book_id = bookResult.BookId
  515. }
  516. if doc_id <= 0 {
  517. c.JsonResult(6001, "参数错误")
  518. }
  519. doc, err := models.NewDocument().Find(doc_id)
  520. if err != nil {
  521. beego.Error("Delete => ", err)
  522. c.JsonResult(6003, "删除失败")
  523. }
  524. // 如果文档所属项目错误
  525. if doc.BookId != book_id {
  526. c.JsonResult(6004, "参数错误")
  527. }
  528. // 递归删除项目下的文档以及子文档
  529. err = doc.RecursiveDocument(doc.DocumentId)
  530. if err != nil {
  531. c.JsonResult(6005, "删除失败")
  532. }
  533. // 重置文档数量统计
  534. models.NewBook().ResetDocumentNumber(doc.BookId)
  535. c.JsonResult(0, "ok")
  536. }
  537. // 获取文档内容
  538. func (c *DocumentController) Content() {
  539. c.Prepare()
  540. identify := c.Ctx.Input.Param(":key")
  541. docId, err := c.GetInt("doc_id")
  542. if err != nil {
  543. docId, _ = strconv.Atoi(c.Ctx.Input.Param(":id"))
  544. }
  545. bookId := 0
  546. autoRelease := false
  547. // 如果是超级管理员,则忽略权限
  548. if c.Member.IsAdministrator() {
  549. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  550. if err != nil {
  551. c.JsonResult(6002, "项目不存在或权限不足")
  552. }
  553. bookId = book.BookId
  554. autoRelease = book.AutoRelease == 1
  555. } else {
  556. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  557. if err != nil || bookResult.RoleId == conf.BookObserver {
  558. beego.Error("FindByIdentify => ", err)
  559. c.JsonResult(6002, "项目不存在或权限不足")
  560. }
  561. bookId = bookResult.BookId
  562. autoRelease = bookResult.AutoRelease
  563. }
  564. if docId <= 0 {
  565. c.JsonResult(6001, "参数错误")
  566. }
  567. if c.Ctx.Input.IsPost() {
  568. markdown := strings.TrimSpace(c.GetString("markdown", ""))
  569. content := c.GetString("html")
  570. version, _ := c.GetInt64("version", 0)
  571. isCover := c.GetString("cover")
  572. doc, err := models.NewDocument().Find(docId)
  573. if err != nil {
  574. c.JsonResult(6003, "读取文档错误")
  575. }
  576. if doc.BookId != bookId {
  577. c.JsonResult(6004, "保存的文档不属于指定项目")
  578. }
  579. if doc.Version != version && !strings.EqualFold(isCover, "yes") {
  580. beego.Info("%d|", version, doc.Version)
  581. c.JsonResult(6005, "文档已被修改确定要覆盖吗?")
  582. }
  583. history := models.NewDocumentHistory()
  584. history.DocumentId = docId
  585. history.Content = doc.Content
  586. history.Markdown = doc.Markdown
  587. history.DocumentName = doc.DocumentName
  588. history.ModifyAt = c.Member.MemberId
  589. history.MemberId = doc.MemberId
  590. history.ParentId = doc.ParentId
  591. history.Version = time.Now().Unix()
  592. history.Action = "modify"
  593. history.ActionName = "修改文档"
  594. if markdown == "" && content != "" {
  595. doc.Markdown = content
  596. } else {
  597. doc.Markdown = markdown
  598. }
  599. doc.Version = time.Now().Unix()
  600. doc.Content = content
  601. if err := doc.InsertOrUpdate(); err != nil {
  602. beego.Error("InsertOrUpdate => ", err)
  603. c.JsonResult(6006, "保存失败")
  604. }
  605. // 如果启用了文档历史,则添加历史文档
  606. ///如果两次保存的MD5值不同则保存为历史,否则忽略
  607. go func(history *models.DocumentHistory) {
  608. if c.EnableDocumentHistory && cryptil.Md5Crypt(history.Markdown) != cryptil.Md5Crypt(doc.Markdown) {
  609. _, err = history.InsertOrUpdate()
  610. if err != nil {
  611. beego.Error("DocumentHistory InsertOrUpdate => ", err)
  612. }
  613. }
  614. }(history)
  615. //如果启用了自动发布
  616. if autoRelease {
  617. go func(identify string) {
  618. models.NewBook().ReleaseContent(bookId)
  619. }(identify)
  620. }
  621. c.JsonResult(0, "ok", doc)
  622. }
  623. doc, err := models.NewDocument().Find(docId)
  624. if err != nil {
  625. c.JsonResult(6003, "文档不存在")
  626. }
  627. attach, err := models.NewAttachment().FindListByDocumentId(doc.DocumentId)
  628. if err == nil {
  629. doc.AttachList = attach
  630. }
  631. c.JsonResult(0, "ok", doc)
  632. }
  633. //
  634. //func (c *DocumentController) GetDocumentById(id string) (doc *models.Document, err error) {
  635. // doc = models.NewDocument()
  636. // if doc_id, err := strconv.Atoi(id); err == nil {
  637. // doc, err = doc.Find(doc_id)
  638. // if err != nil {
  639. // return nil, err
  640. // }
  641. // } else {
  642. // doc, err = doc.FindByFieldFirst("identify", id)
  643. // if err != nil {
  644. // return nil, err
  645. // }
  646. // }
  647. //
  648. // return doc, nil
  649. //}
  650. // 导出
  651. func (c *DocumentController) Export() {
  652. c.Prepare()
  653. identify := c.Ctx.Input.Param(":key")
  654. if identify == "" {
  655. c.Abort("404")
  656. }
  657. output := c.GetString("output")
  658. token := c.GetString("token")
  659. // 如果没有开启匿名访问则跳转到登录
  660. if !c.EnableAnonymous && !isUserLoggedIn(c) {
  661. promptUserToLogIn(c)
  662. return
  663. }
  664. if !conf.GetEnableExport() {
  665. c.ShowErrorPage(500,"系统没有开启导出功能")
  666. }
  667. bookResult := models.NewBookResult()
  668. if c.Member != nil && c.Member.IsAdministrator() {
  669. book, err := models.NewBook().FindByIdentify(identify)
  670. if err != nil {
  671. beego.Error(err)
  672. c.Abort("500")
  673. }
  674. bookResult = models.NewBookResult().ToBookResult(*book)
  675. } else {
  676. bookResult = isReadable(identify, token, c)
  677. }
  678. if !bookResult.IsDownload {
  679. c.ShowErrorPage(200,"当前项目没有开启导出功能")
  680. }
  681. if !strings.HasPrefix(bookResult.Cover, "http:://") && !strings.HasPrefix(bookResult.Cover, "https:://") {
  682. bookResult.Cover = conf.URLForWithCdnImage(bookResult.Cover)
  683. }
  684. if output == "markdown" {
  685. if bookResult.Editor != "markdown"{
  686. c.ShowErrorPage(500,"当前项目不支持Markdown编辑器")
  687. }
  688. p,err := bookResult.ExportMarkdown(c.CruSession.SessionID())
  689. if err != nil {
  690. c.ShowErrorPage(500,"导出文档失败")
  691. }
  692. c.Ctx.Output.Download(p, bookResult.BookName+".zip")
  693. c.StopRun()
  694. return
  695. }
  696. outputPath := filepath.Join(conf.GetExportOutputPath(), strconv.Itoa(bookResult.BookId))
  697. pdfpath := filepath.Join(outputPath, "book.pdf")
  698. epubpath := filepath.Join(outputPath, "book.epub")
  699. mobipath := filepath.Join(outputPath, "book.mobi")
  700. docxpath := filepath.Join(outputPath, "book.docx")
  701. if output == "pdf" && filetil.FileExists(pdfpath){
  702. c.Ctx.Output.Download(pdfpath, bookResult.BookName+".pdf")
  703. c.Abort("200")
  704. } else if output == "epub" && filetil.FileExists(epubpath){
  705. c.Ctx.Output.Download(epubpath, bookResult.BookName+".epub")
  706. c.Abort("200")
  707. } else if output == "mobi" && filetil.FileExists(mobipath) {
  708. c.Ctx.Output.Download(mobipath, bookResult.BookName+".mobi")
  709. c.Abort("200")
  710. } else if output == "docx" && filetil.FileExists(docxpath){
  711. c.Ctx.Output.Download(docxpath, bookResult.BookName+".docx")
  712. c.Abort("200")
  713. }else if output == "pdf" || output == "epub" || output == "docx" || output == "mobi"{
  714. if err := models.BackgroupConvert(c.CruSession.SessionID(),bookResult);err != nil && err != gopool.ErrHandlerIsExist{
  715. c.ShowErrorPage(500,"导出失败,请查看系统日志")
  716. }
  717. c.ShowErrorPage(200,"文档正在后台转换,请稍后再下载")
  718. }else{
  719. c.ShowErrorPage(200,"不支持的文件格式")
  720. }
  721. c.Abort("404")
  722. }
  723. // 生成项目访问的二维码
  724. func (c *DocumentController) QrCode() {
  725. c.Prepare()
  726. identify := c.GetString(":key")
  727. book, err := models.NewBook().FindByIdentify(identify)
  728. if err != nil || book.BookId <= 0 {
  729. c.Abort("404")
  730. }
  731. uri := conf.URLFor("DocumentController.Index", ":key", identify)
  732. code, err := qr.Encode(uri, qr.L, qr.Unicode)
  733. if err != nil {
  734. beego.Error(err)
  735. c.Abort("500")
  736. }
  737. code, err = barcode.Scale(code, 150, 150)
  738. if err != nil {
  739. beego.Error(err)
  740. c.Abort("500")
  741. }
  742. c.Ctx.ResponseWriter.Header().Set("Content-Type", "image/png")
  743. // imgpath := filepath.Join("cache","qrcode",identify + ".png")
  744. err = png.Encode(c.Ctx.ResponseWriter, code)
  745. if err != nil {
  746. beego.Error(err)
  747. c.Abort("500")
  748. }
  749. }
  750. // 项目内搜索
  751. func (c *DocumentController) Search() {
  752. c.Prepare()
  753. identify := c.Ctx.Input.Param(":key")
  754. token := c.GetString("token")
  755. keyword := strings.TrimSpace(c.GetString("keyword"))
  756. if identify == "" {
  757. c.JsonResult(6001, "参数错误")
  758. }
  759. if !c.EnableAnonymous && !isUserLoggedIn(c) {
  760. promptUserToLogIn(c)
  761. return
  762. }
  763. bookResult := isReadable(identify, token, c)
  764. docs, err := models.NewDocumentSearchResult().SearchDocument(keyword, bookResult.BookId)
  765. if err != nil {
  766. beego.Error(err)
  767. c.JsonResult(6002, "搜索结果错误")
  768. }
  769. if len(docs) < 0 {
  770. c.JsonResult(404, "没有数据库")
  771. }
  772. for _, doc := range docs {
  773. doc.BookId = bookResult.BookId
  774. doc.BookName = bookResult.BookName
  775. doc.Description = bookResult.Description
  776. doc.BookIdentify = bookResult.Identify
  777. }
  778. c.JsonResult(0, "ok", docs)
  779. }
  780. // 文档历史列表
  781. func (c *DocumentController) History() {
  782. c.Prepare()
  783. c.TplName = "document/history.tpl"
  784. identify := c.GetString("identify")
  785. docId, err := c.GetInt("doc_id", 0)
  786. pageIndex, _ := c.GetInt("page", 1)
  787. book_id := 0
  788. // 如果是超级管理员则忽略权限判断
  789. if c.Member.IsAdministrator() {
  790. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  791. if err != nil {
  792. beego.Error("FindByIdentify => ", err)
  793. c.Data["ErrorMessage"] = "项目不存在或权限不足"
  794. return
  795. }
  796. book_id = book.BookId
  797. c.Data["Model"] = book
  798. } else {
  799. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  800. if err != nil || bookResult.RoleId == conf.BookObserver {
  801. beego.Error("FindByIdentify => ", err)
  802. c.Data["ErrorMessage"] = "项目不存在或权限不足"
  803. return
  804. }
  805. book_id = bookResult.BookId
  806. c.Data["Model"] = bookResult
  807. }
  808. if docId <= 0 {
  809. c.Data["ErrorMessage"] = "参数错误"
  810. return
  811. }
  812. doc, err := models.NewDocument().Find(docId)
  813. if err != nil {
  814. beego.Error("Delete => ", err)
  815. c.Data["ErrorMessage"] = "获取历史失败"
  816. return
  817. }
  818. // 如果文档所属项目错误
  819. if doc.BookId != book_id {
  820. c.Data["ErrorMessage"] = "参数错误"
  821. return
  822. }
  823. historis, totalCount, err := models.NewDocumentHistory().FindToPager(docId, pageIndex, conf.PageSize)
  824. if err != nil {
  825. beego.Error("FindToPager => ", err)
  826. c.Data["ErrorMessage"] = "获取历史失败"
  827. return
  828. }
  829. c.Data["List"] = historis
  830. c.Data["PageHtml"] = ""
  831. c.Data["Document"] = doc
  832. if totalCount > 0 {
  833. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  834. c.Data["PageHtml"] = pager.HtmlPages()
  835. }
  836. }
  837. func (c *DocumentController) DeleteHistory() {
  838. c.Prepare()
  839. c.TplName = "document/history.tpl"
  840. identify := c.GetString("identify")
  841. docId, err := c.GetInt("doc_id", 0)
  842. historyId, _ := c.GetInt("history_id", 0)
  843. if historyId <= 0 {
  844. c.JsonResult(6001, "参数错误")
  845. }
  846. bookId := 0
  847. // 如果是超级管理员则忽略权限判断
  848. if c.Member.IsAdministrator() {
  849. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  850. if err != nil {
  851. beego.Error("FindByIdentify => ", err)
  852. c.JsonResult(6002, "项目不存在或权限不足")
  853. }
  854. bookId = book.BookId
  855. } else {
  856. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  857. if err != nil || bookResult.RoleId == conf.BookObserver {
  858. beego.Error("FindByIdentify => ", err)
  859. c.JsonResult(6002, "项目不存在或权限不足")
  860. }
  861. bookId = bookResult.BookId
  862. }
  863. if docId <= 0 {
  864. c.JsonResult(6001, "参数错误")
  865. }
  866. doc, err := models.NewDocument().Find(docId)
  867. if err != nil {
  868. beego.Error("Delete => ", err)
  869. c.JsonResult(6001, "获取历史失败")
  870. }
  871. // 如果文档所属项目错误
  872. if doc.BookId != bookId {
  873. c.JsonResult(6001, "参数错误")
  874. }
  875. err = models.NewDocumentHistory().Delete(historyId, docId)
  876. if err != nil {
  877. beego.Error(err)
  878. c.JsonResult(6002, "删除失败")
  879. }
  880. c.JsonResult(0, "ok")
  881. }
  882. //通过文档历史恢复文档
  883. func (c *DocumentController) RestoreHistory() {
  884. c.Prepare()
  885. c.TplName = "document/history.tpl"
  886. identify := c.GetString("identify")
  887. docId, err := c.GetInt("doc_id", 0)
  888. historyId, _ := c.GetInt("history_id", 0)
  889. if historyId <= 0 {
  890. c.JsonResult(6001, "参数错误")
  891. }
  892. bookId := 0
  893. // 如果是超级管理员则忽略权限判断
  894. if c.Member.IsAdministrator() {
  895. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  896. if err != nil {
  897. beego.Error("FindByIdentify => ", err)
  898. c.JsonResult(6002, "项目不存在或权限不足")
  899. }
  900. bookId = book.BookId
  901. } else {
  902. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  903. if err != nil || bookResult.RoleId == conf.BookObserver {
  904. beego.Error("FindByIdentify => ", err)
  905. c.JsonResult(6002, "项目不存在或权限不足")
  906. }
  907. bookId = bookResult.BookId
  908. }
  909. if docId <= 0 {
  910. c.JsonResult(6001, "参数错误")
  911. }
  912. doc, err := models.NewDocument().Find(docId)
  913. if err != nil {
  914. beego.Error("Delete => ", err)
  915. c.JsonResult(6001, "获取历史失败")
  916. }
  917. // 如果文档所属项目错误
  918. if doc.BookId != bookId {
  919. c.JsonResult(6001, "参数错误")
  920. }
  921. err = models.NewDocumentHistory().Restore(historyId, docId, c.Member.MemberId)
  922. if err != nil {
  923. beego.Error(err)
  924. c.JsonResult(6002, "删除失败")
  925. }
  926. c.JsonResult(0, "ok", doc)
  927. }
  928. func (c *DocumentController) Compare() {
  929. c.Prepare()
  930. c.TplName = "document/compare.tpl"
  931. historyId, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
  932. identify := c.Ctx.Input.Param(":key")
  933. bookId := 0
  934. editor := "markdown"
  935. // 如果是超级管理员则忽略权限判断
  936. if c.Member.IsAdministrator() {
  937. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  938. if err != nil {
  939. beego.Error("DocumentController.Compare => ", err)
  940. c.Abort("403")
  941. return
  942. }
  943. bookId = book.BookId
  944. c.Data["Model"] = book
  945. editor = book.Editor
  946. } else {
  947. bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
  948. if err != nil || bookResult.RoleId == conf.BookObserver {
  949. beego.Error("FindByIdentify => ", err)
  950. c.Abort("403")
  951. return
  952. }
  953. bookId = bookResult.BookId
  954. c.Data["Model"] = bookResult
  955. editor = bookResult.Editor
  956. }
  957. if historyId <= 0 {
  958. c.ShowErrorPage(60002, "参数错误")
  959. }
  960. history, err := models.NewDocumentHistory().Find(historyId)
  961. if err != nil {
  962. beego.Error("DocumentController.Compare => ", err)
  963. c.ShowErrorPage(60003, err.Error())
  964. }
  965. doc, err := models.NewDocument().Find(history.DocumentId)
  966. if doc.BookId != bookId {
  967. c.ShowErrorPage(60002, "参数错误")
  968. }
  969. c.Data["HistoryId"] = historyId
  970. c.Data["DocumentId"] = doc.DocumentId
  971. if editor == "markdown" {
  972. c.Data["HistoryContent"] = history.Markdown
  973. c.Data["Content"] = doc.Markdown
  974. } else {
  975. c.Data["HistoryContent"] = template.HTML(history.Content)
  976. c.Data["Content"] = template.HTML(doc.Content)
  977. }
  978. }
  979. // 递归生成文档序列数组
  980. func RecursiveFun(parentId int, prefix, dpath string, c *DocumentController, book *models.BookResult, docs []*models.Document, paths *list.List) {
  981. for _, item := range docs {
  982. if item.ParentId == parentId {
  983. EachFun(prefix, dpath, c, book, item, paths)
  984. for _, sub := range docs {
  985. if sub.ParentId == item.DocumentId {
  986. prefix += strconv.Itoa(item.ParentId) + strconv.Itoa(item.OrderSort) + strconv.Itoa(item.DocumentId)
  987. RecursiveFun(item.DocumentId, prefix, dpath, c, book, docs, paths)
  988. break
  989. }
  990. }
  991. }
  992. }
  993. }
  994. func EachFun(prefix, dpath string, c *DocumentController, book *models.BookResult, item *models.Document, paths *list.List) {
  995. name := prefix + strconv.Itoa(item.ParentId) + strconv.Itoa(item.OrderSort) + strconv.Itoa(item.DocumentId)
  996. fpath := dpath + "/" + name + ".html"
  997. paths.PushBack(fpath)
  998. f, err := os.OpenFile(fpath, os.O_CREATE|os.O_RDWR, 0777)
  999. if err != nil {
  1000. beego.Error(err)
  1001. c.Abort("500")
  1002. }
  1003. html, err := c.ExecuteViewPathTemplate("document/export.tpl", map[string]interface{}{"Model": book, "Lists": item, "BaseUrl": c.BaseUrl()})
  1004. if err != nil {
  1005. f.Close()
  1006. beego.Error(err)
  1007. c.Abort("500")
  1008. }
  1009. buf := bytes.NewReader([]byte(html))
  1010. doc, err := goquery.NewDocumentFromReader(buf)
  1011. doc.Find("img").Each(func(i int, contentSelection *goquery.Selection) {
  1012. if src, ok := contentSelection.Attr("src"); ok && strings.HasPrefix(src, "/uploads/") {
  1013. contentSelection.SetAttr("src", c.BaseUrl()+src)
  1014. }
  1015. })
  1016. html, err = doc.Html()
  1017. if err != nil {
  1018. f.Close()
  1019. beego.Error(err)
  1020. c.Abort("500")
  1021. }
  1022. // html = strings.Replace(html, "<img src=\"/uploads", "<img src=\"" + c.BaseUrl() + "/uploads", -1)
  1023. f.WriteString(html)
  1024. f.Close()
  1025. }
  1026. // 判断用户是否可以阅读文档
  1027. func isReadable(identify, token string, c *DocumentController) *models.BookResult {
  1028. book, err := models.NewBook().FindByFieldFirst("identify", identify)
  1029. if err != nil {
  1030. beego.Error(err)
  1031. c.Abort("500")
  1032. }
  1033. // 如果文档是私有的
  1034. if book.PrivatelyOwned == 1 && !c.Member.IsAdministrator() {
  1035. is_ok := false
  1036. if c.Member != nil {
  1037. _, err := models.NewRelationship().FindForRoleId(book.BookId, c.Member.MemberId)
  1038. if err == nil {
  1039. is_ok = true
  1040. }
  1041. }
  1042. if book.PrivateToken != "" && !is_ok {
  1043. // 如果有访问的 Token,并且该项目设置了访问 Token,并且和用户提供的相匹配,则记录到 Session 中。
  1044. // 如果用户未提供 Token 且用户登录了,则判断用户是否参与了该项目。
  1045. // 如果用户未登录,则从 Session 中读取 Token。
  1046. if token != "" && strings.EqualFold(token, book.PrivateToken) {
  1047. c.SetSession(identify, token)
  1048. } else if token, ok := c.GetSession(identify).(string); !ok || !strings.EqualFold(token, book.PrivateToken) {
  1049. c.Abort("403")
  1050. }
  1051. } else if !is_ok {
  1052. c.Abort("403")
  1053. }
  1054. }
  1055. bookResult := models.NewBookResult().ToBookResult(*book)
  1056. if c.Member != nil {
  1057. rel, err := models.NewRelationship().FindByBookIdAndMemberId(bookResult.BookId, c.Member.MemberId)
  1058. if err == nil {
  1059. bookResult.MemberId = rel.MemberId
  1060. bookResult.RoleId = rel.RoleId
  1061. bookResult.RelationshipId = rel.RelationshipId
  1062. }
  1063. }
  1064. // 判断是否需要显示评论框
  1065. if bookResult.CommentStatus == "closed" {
  1066. bookResult.IsDisplayComment = false
  1067. } else if bookResult.CommentStatus == "open" {
  1068. bookResult.IsDisplayComment = true
  1069. } else if bookResult.CommentStatus == "group_only" {
  1070. bookResult.IsDisplayComment = bookResult.RelationshipId > 0
  1071. } else if bookResult.CommentStatus == "registered_only" {
  1072. bookResult.IsDisplayComment = true
  1073. }
  1074. return bookResult
  1075. }
  1076. func isUserLoggedIn(c *DocumentController) bool {
  1077. return c.Member != nil && c.Member.MemberId > 0
  1078. }
  1079. func promptUserToLogIn(c *DocumentController) {
  1080. beego.Info("Access " + c.Ctx.Request.URL.RequestURI() + " not permitted.")
  1081. beego.Info(" Access will be redirected to login page(SessionId: " + c.CruSession.SessionID() + ").")
  1082. if c.IsAjax() {
  1083. c.JsonResult(6000, "请重新登录。")
  1084. } else {
  1085. c.Redirect(conf.URLFor("AccountController.Login")+ "?url=" + url.PathEscape(conf.BaseUrl+ c.Ctx.Request.URL.RequestURI()), 302)
  1086. }
  1087. }