document.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. doc_id,_ := c.GetInt("doc_id",0)
  68. if identify == "" {
  69. c.JsonResult(6001,"参数错误")
  70. }
  71. if doc_name == "" {
  72. c.JsonResult(6004,"文档名称不能为空")
  73. }
  74. if doc_identify != "" {
  75. if ok, err := regexp.MatchString(`^[a-z]+[a-zA-Z0-9_\-]*$`, doc_identify); !ok || err != nil {
  76. c.JsonResult(6003, "文档标识只能包含小写字母、数字,以及“-”和“_”符号,并且只能小写字母开头")
  77. }
  78. d,_ := models.NewDocument().FindByFieldFirst("identify",doc_identify);
  79. if d.DocumentId > 0 && d.DocumentId != doc_id{
  80. c.JsonResult(6006,"文档标识已被使用")
  81. }
  82. }
  83. bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  84. if err != nil || bookResult.RoleId == conf.BookObserver {
  85. logs.Error("FindByIdentify => ",err)
  86. c.JsonResult(6002,"项目不存在或权限不足")
  87. }
  88. if parent_id > 0 {
  89. doc,err := models.NewDocument().Find(parent_id)
  90. if err != nil || doc.BookId != bookResult.BookId{
  91. c.JsonResult(6003,"父分类不存在")
  92. }
  93. }
  94. document,_ := models.NewDocument().Find(doc_id)
  95. document.MemberId = c.Member.MemberId
  96. document.BookId = bookResult.BookId
  97. if doc_identify != ""{
  98. document.Identify = doc_identify
  99. }
  100. document.Version = time.Now().Unix()
  101. document.DocumentName = doc_name
  102. document.ParentId = parent_id
  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. }
  230. func (c *DocumentController) Delete() {
  231. c.Prepare()
  232. identify := c.GetString("identify")
  233. doc_id,err := c.GetInt("doc_id",0)
  234. bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  235. if err != nil || bookResult.RoleId == conf.BookObserver {
  236. logs.Error("FindByIdentify => ",err)
  237. c.JsonResult(6002,"项目不存在或权限不足")
  238. }
  239. if doc_id <= 0 {
  240. c.JsonResult(6001,"参数错误")
  241. }
  242. doc,err := models.NewDocument().Find(doc_id)
  243. if err != nil {
  244. logs.Error("Delete => ",err)
  245. c.JsonResult(6003,"删除失败")
  246. }
  247. if doc.BookId != bookResult.BookId {
  248. c.JsonResult(6004,"参数错误")
  249. }
  250. err = doc.RecursiveDocument(doc.DocumentId)
  251. if err != nil {
  252. c.JsonResult(6005,"删除失败")
  253. }
  254. c.JsonResult(0,"ok")
  255. }
  256. func (c *DocumentController) Content() {
  257. c.Prepare()
  258. identify := c.Ctx.Input.Param(":key")
  259. doc_id,err := c.GetInt("doc_id")
  260. if err != nil {
  261. doc_id,_ = strconv.Atoi(c.Ctx.Input.Param(":id"))
  262. }
  263. bookResult,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  264. if err != nil || bookResult.RoleId == conf.BookObserver {
  265. logs.Error("FindByIdentify => ",err)
  266. c.JsonResult(6002,"项目不存在或权限不足")
  267. }
  268. if doc_id <= 0 {
  269. c.JsonResult(6001,"参数错误")
  270. }
  271. if c.Ctx.Input.IsPost() {
  272. markdown := strings.TrimSpace(c.GetString("markdown",""))
  273. content := c.GetString("html")
  274. version,_ := c.GetInt64("version",0)
  275. is_cover := c.GetString("cover")
  276. doc ,err := models.NewDocument().Find(doc_id);
  277. if err != nil {
  278. c.JsonResult(6003,"读取文档错误")
  279. }
  280. if doc.BookId != bookResult.BookId {
  281. c.JsonResult(6004,"保存的文档不属于指定项目")
  282. }
  283. if doc.Version != version && !strings.EqualFold(is_cover,"yes"){
  284. logs.Info("%d|",version,doc.Version)
  285. c.JsonResult(6005,"文档已被修改确定要覆盖吗?")
  286. }
  287. if markdown == "" && content != ""{
  288. doc.Markdown = content
  289. }else{
  290. doc.Markdown = markdown
  291. }
  292. doc.Version = time.Now().Unix()
  293. doc.Content = content
  294. if err := doc.InsertOrUpdate();err != nil {
  295. logs.Error("InsertOrUpdate => ",err)
  296. c.JsonResult(6006,"保存失败")
  297. }
  298. c.JsonResult(0,"ok",doc)
  299. }
  300. doc,err := models.NewDocument().Find(doc_id)
  301. if err != nil {
  302. c.JsonResult(6003,"文档不存在")
  303. }
  304. c.JsonResult(0,"ok",doc)
  305. }