document.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 (p *DocumentController) Index() {
  21. p.TplName = "document/index.tpl"
  22. }
  23. func (p *DocumentController) Read() {
  24. p.TplName = "document/kancloud.tpl"
  25. }
  26. func (c *DocumentController) Edit() {
  27. c.Prepare()
  28. identify := c.Ctx.Input.Param(":key")
  29. if identify == "" {
  30. c.Abort("404")
  31. }
  32. bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  33. if err != nil {
  34. beego.Error("DocumentController.Edit => ",err)
  35. c.Abort("403")
  36. }
  37. if bookResult.RoleId == conf.BookObserver {
  38. c.JsonResult(6002,"项目不存在或权限不足")
  39. }
  40. //根据不同编辑器类型加载编辑器
  41. if bookResult.Editor == "markdown" {
  42. c.TplName = "document/markdown_edit_template.tpl"
  43. }else if bookResult.Editor == "html"{
  44. c.TplName = "document/html_edit_template.tpl"
  45. }else{
  46. c.TplName = "document/" + bookResult.Editor + "_edit_template.tpl"
  47. }
  48. c.Data["Model"] = bookResult
  49. r,_ := json.Marshal(bookResult)
  50. c.Data["ModelResult"] = template.JS(string(r))
  51. c.Data["Result"] = template.JS("[]")
  52. trees ,err := models.NewDocument().FindDocumentTree(bookResult.BookId)
  53. beego.Info("",trees)
  54. if err != nil {
  55. beego.Error("FindDocumentTree => ", err)
  56. }else{
  57. if len(trees) > 0 {
  58. if jtree, err := json.Marshal(trees); err == nil {
  59. c.Data["Result"] = template.JS(string(jtree))
  60. }
  61. }else{
  62. c.Data["Result"] = template.JS("[]")
  63. }
  64. }
  65. }
  66. //创建一个文档.
  67. func (c *DocumentController) Create() {
  68. identify := c.GetString("identify")
  69. doc_identify := c.GetString("doc_identify")
  70. doc_name := c.GetString("doc_name")
  71. parent_id,_ := c.GetInt("parent_id",0)
  72. doc_id,_ := c.GetInt("doc_id",0)
  73. if identify == "" {
  74. c.JsonResult(6001,"参数错误")
  75. }
  76. if doc_name == "" {
  77. c.JsonResult(6004,"文档名称不能为空")
  78. }
  79. if doc_identify != "" {
  80. if ok, err := regexp.MatchString(`^[a-z]+[a-zA-Z0-9_\-]*$`, doc_identify); !ok || err != nil {
  81. c.JsonResult(6003, "文档标识只能包含小写字母、数字,以及“-”和“_”符号,并且只能小写字母开头")
  82. }
  83. d,_ := models.NewDocument().FindByFieldFirst("identify",doc_identify);
  84. if d.DocumentId > 0 && d.DocumentId != doc_id{
  85. c.JsonResult(6006,"文档标识已被使用")
  86. }
  87. }
  88. bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  89. if err != nil || bookResult.RoleId == conf.BookObserver {
  90. beego.Error("FindByIdentify => ",err)
  91. c.JsonResult(6002,"项目不存在或权限不足")
  92. }
  93. if parent_id > 0 {
  94. doc,err := models.NewDocument().Find(parent_id)
  95. if err != nil || doc.BookId != bookResult.BookId{
  96. c.JsonResult(6003,"父分类不存在")
  97. }
  98. }
  99. document,_ := models.NewDocument().Find(doc_id)
  100. document.MemberId = c.Member.MemberId
  101. document.BookId = bookResult.BookId
  102. if doc_identify != ""{
  103. document.Identify = doc_identify
  104. }
  105. document.Version = time.Now().Unix()
  106. document.DocumentName = doc_name
  107. document.ParentId = parent_id
  108. if err := document.InsertOrUpdate();err != nil {
  109. beego.Error("InsertOrUpdate => ",err)
  110. c.JsonResult(6005,"保存失败")
  111. }else{
  112. beego.Info("",document)
  113. c.JsonResult(0,"ok",document)
  114. }
  115. }
  116. //上传附件或图片.
  117. func (c *DocumentController) Upload() {
  118. identify := c.GetString("identify")
  119. doc_id,_ := c.GetInt("doc_id")
  120. if identify == "" {
  121. c.JsonResult(6001,"参数错误")
  122. }
  123. name := "editormd-file-file"
  124. file,moreFile,err := c.GetFile(name)
  125. if err == http.ErrMissingFile {
  126. name = "editormd-image-file"
  127. file,moreFile,err = c.GetFile(name);
  128. if err == http.ErrMissingFile {
  129. c.JsonResult(6003,"没有发现需要上传的文件")
  130. }
  131. }
  132. if err != nil {
  133. c.JsonResult(6002,err.Error())
  134. }
  135. defer file.Close()
  136. ext := filepath.Ext(moreFile.Filename)
  137. if ext == "" {
  138. c.JsonResult(6003,"无法解析文件的格式")
  139. }
  140. if !conf.IsAllowUploadFileExt(ext) {
  141. c.JsonResult(6004,"不允许的文件类型")
  142. }
  143. book,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  144. if err != nil {
  145. beego.Error("DocumentController.Edit => ",err)
  146. if err == orm.ErrNoRows {
  147. c.JsonResult(6006,"权限不足")
  148. }
  149. c.JsonResult(6001,err.Error())
  150. }
  151. //如果没有编辑权限
  152. if book.RoleId != conf.BookEditor && book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder {
  153. c.JsonResult(6006,"权限不足")
  154. }
  155. if doc_id > 0 {
  156. doc,err := models.NewDocument().Find(doc_id);
  157. if err != nil {
  158. c.JsonResult(6007,"文档不存在")
  159. }
  160. if doc.BookId != book.BookId {
  161. c.JsonResult(6008,"文档不属于指定的项目")
  162. }
  163. }
  164. fileName := "attachment_" + strconv.FormatInt(time.Now().UnixNano(), 16)
  165. filePath := "uploads/" + time.Now().Format("200601") + "/" + fileName + ext
  166. path := filepath.Dir(filePath)
  167. os.MkdirAll(path, os.ModePerm)
  168. err = c.SaveToFile(name,filePath)
  169. if err != nil {
  170. beego.Error("SaveToFile => ",err)
  171. c.JsonResult(6005,"保存文件失败")
  172. }
  173. attachment := models.NewAttachment()
  174. attachment.BookId = book.BookId
  175. attachment.FileName = moreFile.Filename
  176. attachment.CreateAt = c.Member.MemberId
  177. attachment.FileExt = ext
  178. attachment.FilePath = filePath
  179. if doc_id > 0{
  180. attachment.DocumentId = doc_id
  181. }
  182. if strings.EqualFold(ext,".jpg") || strings.EqualFold(ext,".jpeg") || strings.EqualFold(ext,"png") || strings.EqualFold(ext,"gif") {
  183. attachment.HttpPath = c.BaseUrl() + "/" + filePath
  184. }
  185. err = attachment.Insert();
  186. if err != nil {
  187. os.Remove(filePath)
  188. beego.Error("Attachment Insert => ",err)
  189. c.JsonResult(6006,"文件保存失败")
  190. }
  191. if attachment.HttpPath == "" {
  192. attachment.HttpPath = c.BaseUrl() + beego.URLFor("DocumentController.DownloadAttachment",":key", identify, ":attach_id", attachment.AttachmentId)
  193. if err := attachment.Update();err != nil {
  194. beego.Error("SaveToFile => ",err)
  195. c.JsonResult(6005,"保存文件失败")
  196. }
  197. }
  198. result := map[string]interface{}{
  199. "errcode" : 0,
  200. "success" : 1,
  201. "message" :"ok",
  202. "url" : attachment.HttpPath,
  203. "alt" : attachment.FileName,
  204. }
  205. c.Data["json"] = result
  206. c.ServeJSON(true)
  207. c.StopRun()
  208. }
  209. //DownloadAttachment 下载附件.
  210. func (c *DocumentController) DownloadAttachment() {
  211. c.Prepare()
  212. identify := c.Ctx.Input.Param(":key")
  213. attach_id,_ := strconv.Atoi(c.Ctx.Input.Param(":attach_id"))
  214. token := c.GetString("token")
  215. member_id := 0
  216. if c.Member != nil {
  217. member_id = c.Member.MemberId
  218. }
  219. book_id := 0
  220. //判断用户是否参与了项目
  221. bookResult,err := models.NewBookResult().FindByIdentify(identify,member_id)
  222. if err != nil {
  223. //判断项目公开状态
  224. book,err := models.NewBook().FindByFieldFirst("identify",identify)
  225. if err != nil {
  226. c.Abort("404")
  227. }
  228. //如果项目是私有的,并且token不正确
  229. if (book.PrivatelyOwned == 1 && token == "" ) || ( book.PrivatelyOwned == 1 && book.PrivateToken != token ){
  230. c.Abort("403")
  231. }
  232. book_id = book.BookId
  233. }else{
  234. book_id = bookResult.BookId
  235. }
  236. attachment,err := models.NewAttachment().Find(attach_id)
  237. if err != nil {
  238. beego.Error("DownloadAttachment => ", err)
  239. if err == orm.ErrNoRows {
  240. c.Abort("404")
  241. } else {
  242. c.Abort("500")
  243. }
  244. }
  245. if attachment.BookId != book_id {
  246. c.Abort("404")
  247. }
  248. c.Ctx.Output.Download(attachment.FilePath,attachment.FileName)
  249. c.StopRun()
  250. }
  251. func (c *DocumentController) Delete() {
  252. c.Prepare()
  253. identify := c.GetString("identify")
  254. doc_id,err := c.GetInt("doc_id",0)
  255. bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  256. if err != nil || bookResult.RoleId == conf.BookObserver {
  257. beego.Error("FindByIdentify => ",err)
  258. c.JsonResult(6002,"项目不存在或权限不足")
  259. }
  260. if doc_id <= 0 {
  261. c.JsonResult(6001,"参数错误")
  262. }
  263. doc,err := models.NewDocument().Find(doc_id)
  264. if err != nil {
  265. beego.Error("Delete => ",err)
  266. c.JsonResult(6003,"删除失败")
  267. }
  268. if doc.BookId != bookResult.BookId {
  269. c.JsonResult(6004,"参数错误")
  270. }
  271. err = doc.RecursiveDocument(doc.DocumentId)
  272. if err != nil {
  273. c.JsonResult(6005,"删除失败")
  274. }
  275. c.JsonResult(0,"ok")
  276. }
  277. func (c *DocumentController) Content() {
  278. c.Prepare()
  279. identify := c.Ctx.Input.Param(":key")
  280. doc_id,err := c.GetInt("doc_id")
  281. if err != nil {
  282. doc_id,_ = strconv.Atoi(c.Ctx.Input.Param(":id"))
  283. }
  284. bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  285. if err != nil || bookResult.RoleId == conf.BookObserver {
  286. beego.Error("FindByIdentify => ",err)
  287. c.JsonResult(6002,"项目不存在或权限不足")
  288. }
  289. if doc_id <= 0 {
  290. c.JsonResult(6001,"参数错误")
  291. }
  292. if c.Ctx.Input.IsPost() {
  293. markdown := strings.TrimSpace(c.GetString("markdown",""))
  294. content := c.GetString("html")
  295. version,_ := c.GetInt64("version",0)
  296. is_cover := c.GetString("cover")
  297. doc ,err := models.NewDocument().Find(doc_id);
  298. if err != nil {
  299. c.JsonResult(6003,"读取文档错误")
  300. }
  301. if doc.BookId != bookResult.BookId {
  302. c.JsonResult(6004,"保存的文档不属于指定项目")
  303. }
  304. if doc.Version != version && !strings.EqualFold(is_cover,"yes"){
  305. beego.Info("%d|",version,doc.Version)
  306. c.JsonResult(6005,"文档已被修改确定要覆盖吗?")
  307. }
  308. if markdown == "" && content != ""{
  309. doc.Markdown = content
  310. }else{
  311. doc.Markdown = markdown
  312. }
  313. doc.Version = time.Now().Unix()
  314. doc.Content = content
  315. if err := doc.InsertOrUpdate();err != nil {
  316. beego.Error("InsertOrUpdate => ",err)
  317. c.JsonResult(6006,"保存失败")
  318. }
  319. c.JsonResult(0,"ok",doc)
  320. }
  321. doc,err := models.NewDocument().Find(doc_id)
  322. if err != nil {
  323. c.JsonResult(6003,"文档不存在")
  324. }
  325. c.JsonResult(0,"ok",doc)
  326. }