DocumentController.go 36 KB

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