document.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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/astaxie/beego/logs"
  14. "github.com/lifei6671/godoc/conf"
  15. "github.com/astaxie/beego"
  16. "github.com/astaxie/beego/orm"
  17. )
  18. type DocumentController struct {
  19. BaseController
  20. }
  21. func (p *DocumentController) Index() {
  22. p.TplName = "document/index.tpl"
  23. }
  24. func (p *DocumentController) Read() {
  25. p.TplName = "document/kancloud.tpl"
  26. }
  27. func (c *DocumentController) Edit() {
  28. c.Prepare()
  29. identify := c.Ctx.Input.Param(":key")
  30. if identify == "" {
  31. c.Abort("404")
  32. }
  33. book,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  34. if err != nil {
  35. logs.Error("DocumentController.Edit => ",err)
  36. c.Abort("403")
  37. }
  38. if book.Editor == "markdown" {
  39. c.TplName = "document/markdown_edit_template.tpl"
  40. }else{
  41. c.TplName = "document/html_edit_template.tpl"
  42. }
  43. c.Data["Model"] = book
  44. r,_ := json.Marshal(book)
  45. c.Data["ModelResult"] = template.JS(string(r))
  46. c.Data["Result"] = template.JS("[]")
  47. trees ,err := models.NewDocument().FindDocumentTree(book.BookId)
  48. logs.Info("",trees)
  49. if err != nil {
  50. logs.Error("FindDocumentTree => ", err)
  51. }else{
  52. if len(trees) > 0 {
  53. if jtree, err := json.Marshal(trees); err == nil {
  54. c.Data["Result"] = template.JS(string(jtree))
  55. }
  56. }else{
  57. c.Data["Result"] = template.JS("[]")
  58. }
  59. }
  60. }
  61. //创建一个文档
  62. func (c *DocumentController) Create() {
  63. identify := c.GetString("identify")
  64. doc_identify := c.GetString("doc_identify")
  65. doc_name := c.GetString("doc_name")
  66. parent_id,_ := c.GetInt("parent_id",0)
  67. if identify == "" {
  68. c.JsonResult(6001,"参数错误")
  69. }
  70. if doc_name == "" {
  71. c.JsonResult(6004,"文档名称不能为空")
  72. }
  73. if doc_identify != "" {
  74. if ok, err := regexp.MatchString(`^[a-z]+[a-zA-Z0-9_\-]*$`, doc_identify); !ok || err != nil {
  75. c.JsonResult(6003, "文档标识只能包含小写字母、数字,以及“-”和“_”符号,并且只能小写字母开头")
  76. }
  77. d,_ := models.NewDocument().FindByFieldFirst("identify",doc_identify);
  78. if d.DocumentId > 0{
  79. c.JsonResult(6006,"文档标识已被使用")
  80. }
  81. }
  82. bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  83. if err != nil || bookResult.RoleId == conf.BookObserver {
  84. logs.Error("FindByIdentify => ",err)
  85. c.JsonResult(6002,"项目不存在或权限不足")
  86. }
  87. if parent_id > 0 {
  88. doc,err := models.NewDocument().Find(parent_id)
  89. if err != nil || doc.BookId != bookResult.BookId{
  90. c.JsonResult(6003,"父分类不存在")
  91. }
  92. }
  93. document := models.NewDocument()
  94. document.MemberId = c.Member.MemberId
  95. document.BookId = bookResult.BookId
  96. if doc_identify != ""{
  97. document.Identify = doc_identify
  98. }
  99. document.Version = time.Now().UnixNano()
  100. document.DocumentName = doc_name
  101. document.ParentId = parent_id
  102. logs.Info("%+v",document)
  103. if err := document.InsertOrUpdate();err != nil {
  104. logs.Error("InsertOrUpdate => ",err)
  105. c.JsonResult(6005,"保存失败")
  106. }else{
  107. logs.Info("",document)
  108. c.JsonResult(0,"ok",document)
  109. }
  110. }
  111. //上传附件或图片
  112. func (c *DocumentController) Upload() {
  113. identify := c.GetString("identify")
  114. if identify == "" {
  115. c.JsonResult(6001,"参数错误")
  116. }
  117. name := "editormd-file-file"
  118. file,moreFile,err := c.GetFile(name)
  119. if err == http.ErrMissingFile {
  120. name = "editormd-image-file"
  121. file,moreFile,err = c.GetFile(name);
  122. if err == http.ErrMissingFile {
  123. c.JsonResult(6003,"没有发现需要上传的文件")
  124. }
  125. }
  126. if err != nil {
  127. c.JsonResult(6002,err.Error())
  128. }
  129. defer file.Close()
  130. ext := filepath.Ext(moreFile.Filename)
  131. if ext == "" {
  132. c.JsonResult(6003,"无法解析文件的格式")
  133. }
  134. if !conf.IsAllowUploadFileExt(ext) {
  135. c.JsonResult(6004,"不允许的文件类型")
  136. }
  137. book,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  138. if err != nil {
  139. logs.Error("DocumentController.Edit => ",err)
  140. if err == orm.ErrNoRows {
  141. c.JsonResult(6006,"权限不足")
  142. }
  143. c.JsonResult(6001,err.Error())
  144. }
  145. //如果没有编辑权限
  146. if book.RoleId != conf.BookEditor && book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder {
  147. c.JsonResult(6006,"权限不足")
  148. }
  149. fileName := "attachment_" + strconv.FormatInt(time.Now().UnixNano(), 16)
  150. filePath := "uploads/" + time.Now().Format("200601") + "/" + fileName + ext
  151. err = c.SaveToFile(name,filePath)
  152. if err != nil {
  153. logs.Error("SaveToFile => ",err)
  154. c.JsonResult(6005,"保存文件失败")
  155. }
  156. attachment := models.NewAttachment()
  157. attachment.BookId = book.BookId
  158. attachment.FileName = moreFile.Filename
  159. attachment.CreateAt = c.Member.MemberId
  160. attachment.FileExt = ext
  161. attachment.FilePath = filePath
  162. if strings.EqualFold(ext,".jpg") || strings.EqualFold(ext,".jpeg") || strings.EqualFold(ext,"png") || strings.EqualFold(ext,"gif") {
  163. attachment.HttpPath = c.BaseUrl() + "/" + filePath
  164. }
  165. err = attachment.Insert();
  166. if err != nil {
  167. os.Remove(filePath)
  168. logs.Error("Attachment Insert => ",err)
  169. c.JsonResult(6006,"文件保存失败")
  170. }
  171. if attachment.HttpPath == "" {
  172. attachment.HttpPath = c.BaseUrl() + beego.URLFor("DocumentController.DownloadAttachment",":key", identify, ":attach_id", attachment.AttachmentId)
  173. if err := attachment.Update();err != nil {
  174. logs.Error("SaveToFile => ",err)
  175. c.JsonResult(6005,"保存文件失败")
  176. }
  177. }
  178. result := map[string]interface{}{
  179. "success" : 1,
  180. "message" :"ok",
  181. "url" : attachment.HttpPath,
  182. "alt" : attachment.FileName,
  183. }
  184. c.Data["json"] = result
  185. c.ServeJSON(true)
  186. c.StopRun()
  187. }
  188. //DownloadAttachment 下载附件.
  189. func (c *DocumentController) DownloadAttachment() {
  190. c.Prepare()
  191. identify := c.Ctx.Input.Param(":key")
  192. attach_id,_ := strconv.Atoi(c.Ctx.Input.Param(":attach_id"))
  193. token := c.GetString("token")
  194. member_id := 0
  195. if c.Member != nil {
  196. member_id = c.Member.MemberId
  197. }
  198. book_id := 0
  199. //判断用户是否参与了项目
  200. bookResult,err := models.NewBookResult().FindByIdentify(identify,member_id)
  201. if err != nil {
  202. //判断项目公开状态
  203. book,err := models.NewBook().FindByFieldFirst("identify",identify)
  204. if err != nil {
  205. c.Abort("404")
  206. }
  207. //如果项目是私有的,并且token不正确
  208. if (book.PrivatelyOwned == 1 && token == "" ) || ( book.PrivatelyOwned == 1 && book.PrivateToken != token ){
  209. c.Abort("403")
  210. }
  211. book_id = book.BookId
  212. }else{
  213. book_id = bookResult.BookId
  214. }
  215. attachment,err := models.NewAttachment().Find(attach_id)
  216. if err != nil {
  217. logs.Error("DownloadAttachment => ", err)
  218. if err == orm.ErrNoRows {
  219. c.Abort("404")
  220. } else {
  221. c.Abort("500")
  222. }
  223. }
  224. if attachment.BookId != book_id {
  225. c.Abort("404")
  226. }
  227. c.Ctx.Output.Download(attachment.FilePath,attachment.FileName)
  228. c.StopRun()
  229. }