document.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. package controllers
  2. import (
  3. "os"
  4. "time"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "net/http"
  9. "path/filepath"
  10. "encoding/json"
  11. "html/template"
  12. "github.com/lifei6671/godoc/models"
  13. "github.com/lifei6671/godoc/conf"
  14. "github.com/astaxie/beego"
  15. "github.com/astaxie/beego/orm"
  16. )
  17. type DocumentController struct {
  18. BaseController
  19. }
  20. func isReadable (identify,token string,c *DocumentController) *models.BookResult {
  21. book,err := models.NewBook().FindByFieldFirst("identify",identify)
  22. if err != nil {
  23. beego.Error(err)
  24. c.Abort("500")
  25. }
  26. //如果文档是私有的
  27. if book.PrivatelyOwned == 1 {
  28. is_ok := false
  29. if c.Member != nil{
  30. _, err := models.NewRelationship().FindForRoleId(book.BookId, c.Member.MemberId)
  31. if err == nil {
  32. is_ok = true
  33. }
  34. }
  35. if book.PrivateToken != "" && !is_ok {
  36. //如果有访问的Token,并且该项目设置了访问Token,并且和用户提供的相匹配,则记录到Session中.
  37. //如果用户未提供Token且用户登录了,则判断用户是否参与了该项目.
  38. //如果用户未登录,则从Session中读取Token.
  39. if token != "" && strings.EqualFold(token, book.PrivateToken) {
  40. c.SetSession(identify, token)
  41. } else if token, ok := c.GetSession(identify).(string); !ok || !strings.EqualFold(token, book.PrivateToken) {
  42. c.Abort("403")
  43. }
  44. }else{
  45. c.Abort("403")
  46. }
  47. }
  48. bookResult := book.ToBookResult()
  49. if c.Member != nil {
  50. rel ,err := models.NewRelationship().FindByBookIdAndMemberId(bookResult.BookId,c.Member.MemberId)
  51. if err == nil {
  52. bookResult.MemberId = rel.MemberId
  53. bookResult.RoleId = rel.RoleId
  54. bookResult.RelationshipId = rel.RelationshipId
  55. }
  56. }
  57. return bookResult
  58. }
  59. func (c *DocumentController) Index() {
  60. c.Prepare()
  61. identify := c.Ctx.Input.Param(":key")
  62. token := c.GetString("token")
  63. if identify == "" {
  64. c.Abort("404")
  65. }
  66. bookResult := isReadable(identify,token,c)
  67. c.TplName = "document/" + bookResult.Theme + "_read.tpl"
  68. tree,err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId,0)
  69. if err != nil {
  70. beego.Error(err)
  71. c.Abort("500")
  72. }
  73. c.Data["Model"] = bookResult
  74. c.Data["Result"] = template.HTML(tree)
  75. c.Data["Title"] = "概要"
  76. c.Data["Content"] = bookResult.Description
  77. }
  78. func (c *DocumentController) Read() {
  79. c.Prepare()
  80. identify := c.Ctx.Input.Param(":key")
  81. token := c.GetString("token")
  82. id := c.GetString(":id")
  83. if identify == "" || id == ""{
  84. c.Abort("404")
  85. }
  86. bookResult := isReadable(identify,token,c)
  87. c.TplName = "document/" + bookResult.Theme + "_read.tpl"
  88. doc := models.NewDocument()
  89. if doc_id,err := strconv.Atoi(id);err == nil {
  90. doc,err = doc.Find(doc_id)
  91. if err != nil {
  92. beego.Error(err)
  93. c.Abort("500")
  94. }
  95. }else{
  96. doc,err = doc.FindByFieldFirst("identify",id)
  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. if c.IsAjax() {
  106. var data struct{
  107. DocTitle string `json:"doc_title"`
  108. Body string `json:"body"`
  109. Title string `json:"title"`
  110. }
  111. data.DocTitle = doc.DocumentName
  112. data.Body = doc.Release
  113. data.Title = doc.DocumentName + " - Powered by MinDoc"
  114. c.JsonResult(0,"ok",data)
  115. }
  116. tree,err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId,doc.DocumentId)
  117. if err != nil {
  118. beego.Error(err)
  119. c.Abort("500")
  120. }
  121. c.Data["Model"] = bookResult
  122. c.Data["Result"] = template.HTML(tree)
  123. c.Data["Title"] = doc.DocumentName
  124. c.Data["Content"] = template.HTML(doc.Release)
  125. }
  126. func (c *DocumentController) Edit() {
  127. c.Prepare()
  128. identify := c.Ctx.Input.Param(":key")
  129. if identify == "" {
  130. c.Abort("404")
  131. }
  132. bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  133. if err != nil {
  134. beego.Error("DocumentController.Edit => ",err)
  135. c.Abort("403")
  136. }
  137. if bookResult.RoleId == conf.BookObserver {
  138. c.JsonResult(6002,"项目不存在或权限不足")
  139. }
  140. //根据不同编辑器类型加载编辑器
  141. if bookResult.Editor == "markdown" {
  142. c.TplName = "document/markdown_edit_template.tpl"
  143. }else if bookResult.Editor == "html"{
  144. c.TplName = "document/html_edit_template.tpl"
  145. }else{
  146. c.TplName = "document/" + bookResult.Editor + "_edit_template.tpl"
  147. }
  148. c.Data["Model"] = bookResult
  149. r,_ := json.Marshal(bookResult)
  150. c.Data["ModelResult"] = template.JS(string(r))
  151. c.Data["Result"] = template.JS("[]")
  152. trees ,err := models.NewDocument().FindDocumentTree(bookResult.BookId)
  153. if err != nil {
  154. beego.Error("FindDocumentTree => ", err)
  155. }else{
  156. if len(trees) > 0 {
  157. if jtree, err := json.Marshal(trees); err == nil {
  158. c.Data["Result"] = template.JS(string(jtree))
  159. }
  160. }else{
  161. c.Data["Result"] = template.JS("[]")
  162. }
  163. }
  164. }
  165. //创建一个文档.
  166. func (c *DocumentController) Create() {
  167. identify := c.GetString("identify")
  168. doc_identify := c.GetString("doc_identify")
  169. doc_name := c.GetString("doc_name")
  170. parent_id,_ := c.GetInt("parent_id",0)
  171. doc_id,_ := c.GetInt("doc_id",0)
  172. if identify == "" {
  173. c.JsonResult(6001,"参数错误")
  174. }
  175. if doc_name == "" {
  176. c.JsonResult(6004,"文档名称不能为空")
  177. }
  178. if doc_identify != "" {
  179. if ok, err := regexp.MatchString(`^[a-z]+[a-zA-Z0-9_\-]*$`, doc_identify); !ok || err != nil {
  180. c.JsonResult(6003, "文档标识只能包含小写字母、数字,以及“-”和“_”符号,并且只能小写字母开头")
  181. }
  182. d,_ := models.NewDocument().FindByFieldFirst("identify",doc_identify);
  183. if d.DocumentId > 0 && d.DocumentId != doc_id{
  184. c.JsonResult(6006,"文档标识已被使用")
  185. }
  186. }
  187. bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  188. if err != nil || bookResult.RoleId == conf.BookObserver {
  189. beego.Error("FindByIdentify => ",err)
  190. c.JsonResult(6002,"项目不存在或权限不足")
  191. }
  192. if parent_id > 0 {
  193. doc,err := models.NewDocument().Find(parent_id)
  194. if err != nil || doc.BookId != bookResult.BookId{
  195. c.JsonResult(6003,"父分类不存在")
  196. }
  197. }
  198. document,_ := models.NewDocument().Find(doc_id)
  199. document.MemberId = c.Member.MemberId
  200. document.BookId = bookResult.BookId
  201. if doc_identify != ""{
  202. document.Identify = doc_identify
  203. }
  204. document.Version = time.Now().Unix()
  205. document.DocumentName = doc_name
  206. document.ParentId = parent_id
  207. if err := document.InsertOrUpdate();err != nil {
  208. beego.Error("InsertOrUpdate => ",err)
  209. c.JsonResult(6005,"保存失败")
  210. }else{
  211. beego.Info("",document)
  212. c.JsonResult(0,"ok",document)
  213. }
  214. }
  215. //上传附件或图片.
  216. func (c *DocumentController) Upload() {
  217. identify := c.GetString("identify")
  218. doc_id,_ := c.GetInt("doc_id")
  219. if identify == "" {
  220. c.JsonResult(6001,"参数错误")
  221. }
  222. name := "editormd-file-file"
  223. file,moreFile,err := c.GetFile(name)
  224. if err == http.ErrMissingFile {
  225. name = "editormd-image-file"
  226. file,moreFile,err = c.GetFile(name);
  227. if err == http.ErrMissingFile {
  228. c.JsonResult(6003,"没有发现需要上传的文件")
  229. }
  230. }
  231. if err != nil {
  232. c.JsonResult(6002,err.Error())
  233. }
  234. defer file.Close()
  235. ext := filepath.Ext(moreFile.Filename)
  236. if ext == "" {
  237. c.JsonResult(6003,"无法解析文件的格式")
  238. }
  239. if !conf.IsAllowUploadFileExt(ext) {
  240. c.JsonResult(6004,"不允许的文件类型")
  241. }
  242. book,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  243. if err != nil {
  244. beego.Error("DocumentController.Edit => ",err)
  245. if err == orm.ErrNoRows {
  246. c.JsonResult(6006,"权限不足")
  247. }
  248. c.JsonResult(6001,err.Error())
  249. }
  250. //如果没有编辑权限
  251. if book.RoleId != conf.BookEditor && book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder {
  252. c.JsonResult(6006,"权限不足")
  253. }
  254. if doc_id > 0 {
  255. doc,err := models.NewDocument().Find(doc_id);
  256. if err != nil {
  257. c.JsonResult(6007,"文档不存在")
  258. }
  259. if doc.BookId != book.BookId {
  260. c.JsonResult(6008,"文档不属于指定的项目")
  261. }
  262. }
  263. fileName := "attachment_" + strconv.FormatInt(time.Now().UnixNano(), 16)
  264. filePath := "uploads/" + time.Now().Format("200601") + "/" + fileName + ext
  265. path := filepath.Dir(filePath)
  266. os.MkdirAll(path, os.ModePerm)
  267. err = c.SaveToFile(name,filePath)
  268. if err != nil {
  269. beego.Error("SaveToFile => ",err)
  270. c.JsonResult(6005,"保存文件失败")
  271. }
  272. attachment := models.NewAttachment()
  273. attachment.BookId = book.BookId
  274. attachment.FileName = moreFile.Filename
  275. attachment.CreateAt = c.Member.MemberId
  276. attachment.FileExt = ext
  277. attachment.FilePath = filePath
  278. if doc_id > 0{
  279. attachment.DocumentId = doc_id
  280. }
  281. if strings.EqualFold(ext,".jpg") || strings.EqualFold(ext,".jpeg") || strings.EqualFold(ext,"png") || strings.EqualFold(ext,"gif") {
  282. attachment.HttpPath = c.BaseUrl() + "/" + filePath
  283. }
  284. err = attachment.Insert();
  285. if err != nil {
  286. os.Remove(filePath)
  287. beego.Error("Attachment Insert => ",err)
  288. c.JsonResult(6006,"文件保存失败")
  289. }
  290. if attachment.HttpPath == "" {
  291. attachment.HttpPath = c.BaseUrl() + beego.URLFor("DocumentController.DownloadAttachment",":key", identify, ":attach_id", attachment.AttachmentId)
  292. if err := attachment.Update();err != nil {
  293. beego.Error("SaveToFile => ",err)
  294. c.JsonResult(6005,"保存文件失败")
  295. }
  296. }
  297. result := map[string]interface{}{
  298. "errcode" : 0,
  299. "success" : 1,
  300. "message" :"ok",
  301. "url" : attachment.HttpPath,
  302. "alt" : attachment.FileName,
  303. }
  304. c.Data["json"] = result
  305. c.ServeJSON(true)
  306. c.StopRun()
  307. }
  308. //DownloadAttachment 下载附件.
  309. func (c *DocumentController) DownloadAttachment() {
  310. c.Prepare()
  311. identify := c.Ctx.Input.Param(":key")
  312. attach_id,_ := strconv.Atoi(c.Ctx.Input.Param(":attach_id"))
  313. token := c.GetString("token")
  314. member_id := 0
  315. if c.Member != nil {
  316. member_id = c.Member.MemberId
  317. }
  318. book_id := 0
  319. //判断用户是否参与了项目
  320. bookResult,err := models.NewBookResult().FindByIdentify(identify,member_id)
  321. if err != nil {
  322. //判断项目公开状态
  323. book,err := models.NewBook().FindByFieldFirst("identify",identify)
  324. if err != nil {
  325. c.Abort("404")
  326. }
  327. //如果项目是私有的,并且token不正确
  328. if (book.PrivatelyOwned == 1 && token == "" ) || ( book.PrivatelyOwned == 1 && book.PrivateToken != token ){
  329. c.Abort("403")
  330. }
  331. book_id = book.BookId
  332. }else{
  333. book_id = bookResult.BookId
  334. }
  335. attachment,err := models.NewAttachment().Find(attach_id)
  336. if err != nil {
  337. beego.Error("DownloadAttachment => ", err)
  338. if err == orm.ErrNoRows {
  339. c.Abort("404")
  340. } else {
  341. c.Abort("500")
  342. }
  343. }
  344. if attachment.BookId != book_id {
  345. c.Abort("404")
  346. }
  347. c.Ctx.Output.Download(attachment.FilePath,attachment.FileName)
  348. c.StopRun()
  349. }
  350. func (c *DocumentController) Delete() {
  351. c.Prepare()
  352. identify := c.GetString("identify")
  353. doc_id,err := c.GetInt("doc_id",0)
  354. bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  355. if err != nil || bookResult.RoleId == conf.BookObserver {
  356. beego.Error("FindByIdentify => ",err)
  357. c.JsonResult(6002,"项目不存在或权限不足")
  358. }
  359. if doc_id <= 0 {
  360. c.JsonResult(6001,"参数错误")
  361. }
  362. doc,err := models.NewDocument().Find(doc_id)
  363. if err != nil {
  364. beego.Error("Delete => ",err)
  365. c.JsonResult(6003,"删除失败")
  366. }
  367. if doc.BookId != bookResult.BookId {
  368. c.JsonResult(6004,"参数错误")
  369. }
  370. err = doc.RecursiveDocument(doc.DocumentId)
  371. if err != nil {
  372. c.JsonResult(6005,"删除失败")
  373. }
  374. c.JsonResult(0,"ok")
  375. }
  376. func (c *DocumentController) Content() {
  377. c.Prepare()
  378. identify := c.Ctx.Input.Param(":key")
  379. doc_id,err := c.GetInt("doc_id")
  380. if err != nil {
  381. doc_id,_ = strconv.Atoi(c.Ctx.Input.Param(":id"))
  382. }
  383. bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  384. if err != nil || bookResult.RoleId == conf.BookObserver {
  385. beego.Error("FindByIdentify => ",err)
  386. c.JsonResult(6002,"项目不存在或权限不足")
  387. }
  388. if doc_id <= 0 {
  389. c.JsonResult(6001,"参数错误")
  390. }
  391. if c.Ctx.Input.IsPost() {
  392. markdown := strings.TrimSpace(c.GetString("markdown",""))
  393. content := c.GetString("html")
  394. version,_ := c.GetInt64("version",0)
  395. is_cover := c.GetString("cover")
  396. doc ,err := models.NewDocument().Find(doc_id);
  397. if err != nil {
  398. c.JsonResult(6003,"读取文档错误")
  399. }
  400. if doc.BookId != bookResult.BookId {
  401. c.JsonResult(6004,"保存的文档不属于指定项目")
  402. }
  403. if doc.Version != version && !strings.EqualFold(is_cover,"yes"){
  404. beego.Info("%d|",version,doc.Version)
  405. c.JsonResult(6005,"文档已被修改确定要覆盖吗?")
  406. }
  407. if markdown == "" && content != ""{
  408. doc.Markdown = content
  409. }else{
  410. doc.Markdown = markdown
  411. }
  412. doc.Version = time.Now().Unix()
  413. doc.Content = content
  414. if err := doc.InsertOrUpdate();err != nil {
  415. beego.Error("InsertOrUpdate => ",err)
  416. c.JsonResult(6006,"保存失败")
  417. }
  418. c.JsonResult(0,"ok",doc)
  419. }
  420. doc,err := models.NewDocument().Find(doc_id)
  421. if err != nil {
  422. c.JsonResult(6003,"文档不存在")
  423. }
  424. c.JsonResult(0,"ok",doc)
  425. }