DocumentController.go 36 KB

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