book.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. package controllers
  2. import (
  3. "strings"
  4. "regexp"
  5. "strconv"
  6. "time"
  7. "encoding/json"
  8. "html/template"
  9. "errors"
  10. "fmt"
  11. "path/filepath"
  12. "os"
  13. "github.com/lifei6671/godoc/models"
  14. "github.com/lifei6671/godoc/utils"
  15. "github.com/astaxie/beego"
  16. "github.com/astaxie/beego/orm"
  17. "github.com/astaxie/beego/logs"
  18. "github.com/lifei6671/godoc/conf"
  19. "github.com/lifei6671/godoc/graphics"
  20. )
  21. type BookController struct {
  22. BaseController
  23. }
  24. func (c *BookController) Index() {
  25. c.Prepare()
  26. c.TplName = "book/index.tpl"
  27. pageIndex, _ := c.GetInt("page", 1)
  28. books,totalCount,err := models.NewBook().FindToPager(pageIndex,conf.PageSize,c.Member.MemberId)
  29. if err != nil {
  30. c.Abort("500")
  31. }
  32. if totalCount > 0 {
  33. html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, conf.PageSize, totalCount)
  34. c.Data["PageHtml"] = html
  35. }else {
  36. c.Data["PageHtml"] = ""
  37. }
  38. b,err := json.Marshal(books)
  39. if err != nil || len(books) <= 0{
  40. c.Data["Result"] = template.JS("[]")
  41. }else{
  42. c.Data["Result"] = template.JS(string(b))
  43. }
  44. }
  45. // Dashboard 项目概要 .
  46. func (c *BookController) Dashboard() {
  47. c.Prepare()
  48. c.TplName = "book/dashboard.tpl"
  49. key := c.Ctx.Input.Param(":key")
  50. if key == ""{
  51. c.Abort("404")
  52. }
  53. book,err := models.NewBookResult().FindByIdentify(key,c.Member.MemberId)
  54. if err != nil {
  55. if err == models.ErrPermissionDenied {
  56. c.Abort("403")
  57. }
  58. c.Abort("500")
  59. }
  60. c.Data["Model"] = *book
  61. }
  62. // Setting 项目设置 .
  63. func (c *BookController) Setting() {
  64. c.Prepare()
  65. c.TplName = "book/setting.tpl"
  66. key := c.Ctx.Input.Param(":key")
  67. if key == ""{
  68. c.Abort("404")
  69. }
  70. book,err := models.NewBookResult().FindByIdentify(key,c.Member.MemberId)
  71. if err != nil {
  72. if err == orm.ErrNoRows {
  73. c.Abort("404")
  74. }
  75. if err == models.ErrPermissionDenied {
  76. c.Abort("403")
  77. }
  78. c.Abort("500")
  79. }
  80. //如果不是创始人也不是管理员则不能操作
  81. if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
  82. c.Abort("403")
  83. }
  84. if book.PrivateToken != "" {
  85. book.PrivateToken = c.BaseUrl() + beego.URLFor("DocumentController.Index",":key",book.Identify,"token",book.PrivateToken)
  86. }
  87. c.Data["Model"] = book
  88. }
  89. //保存项目信息
  90. func (c *BookController) SaveBook() {
  91. bookResult,err := c.IsPermission()
  92. if err != nil {
  93. c.JsonResult(6001,err.Error())
  94. }
  95. book,err := models.NewBook().Find(bookResult.BookId)
  96. if err != nil {
  97. logs.Error("SaveBook => ",err)
  98. c.JsonResult(6002,err.Error())
  99. }
  100. book_name := strings.TrimSpace(c.GetString("book_name"))
  101. description := strings.TrimSpace(c.GetString("description",""))
  102. comment_status := c.GetString("comment_status")
  103. tag := strings.TrimSpace(c.GetString("label"))
  104. if strings.Count(description,"") > 500 {
  105. c.JsonResult(6004,"项目描述不能大于500字")
  106. }
  107. if comment_status != "open" && comment_status != "closed" && comment_status != "group_only" && comment_status != "registered_only" {
  108. comment_status = "closed"
  109. }
  110. if tag != ""{
  111. tags := strings.Split(tag,";")
  112. if len(tags) > 10 {
  113. c.JsonResult(6005,"最多允许添加10个标签")
  114. }
  115. }
  116. book.BookName = book_name
  117. book.Description = description
  118. book.CommentStatus = comment_status
  119. book.Label = tag
  120. if err := book.Update();err != nil {
  121. c.JsonResult(6006,"保存失败")
  122. }
  123. bookResult.BookName = book_name
  124. bookResult.Description = description
  125. bookResult.CommentStatus = comment_status
  126. bookResult.Label = tag
  127. c.JsonResult(0,"ok",bookResult)
  128. }
  129. func (c *BookController) PrivatelyOwned() {
  130. status := c.GetString("status")
  131. if status != "open" && status != "close" {
  132. c.JsonResult(6003,"参数错误")
  133. }
  134. state := 0
  135. if status == "open" {
  136. state = 0
  137. }else{
  138. state = 1
  139. }
  140. bookResult,err := c.IsPermission()
  141. if err != nil {
  142. c.JsonResult(6001,err.Error())
  143. }
  144. //只有创始人才能变更私有状态
  145. if bookResult.RoleId != conf.BookFounder {
  146. c.JsonResult(6002,"权限不足")
  147. }
  148. fmt.Printf("%+v",bookResult)
  149. book,err := models.NewBook().Find(bookResult.BookId)
  150. if err != nil {
  151. c.JsonResult(6005,"项目不存在")
  152. }
  153. book.PrivatelyOwned = state
  154. logs.Info("",state,status)
  155. err = book.Update()
  156. if err != nil {
  157. logs.Error("PrivatelyOwned => ",err)
  158. c.JsonResult(6004,"保存失败")
  159. }
  160. c.JsonResult(0,"ok")
  161. }
  162. // Transfer 转让项目.
  163. func (c *BookController) Transfer() {
  164. c.Prepare()
  165. account := c.GetString("account")
  166. if account == "" {
  167. c.JsonResult(6004,"接受者账号不能为空")
  168. }
  169. member,err := models.NewMember().FindByAccount(account)
  170. if err != nil {
  171. logs.Error("FindByAccount => ",err)
  172. c.JsonResult(6005,"接受用户不存在")
  173. }
  174. if member.Status != 0 {
  175. c.JsonResult(6006,"接受用户已被禁用")
  176. }
  177. if member.MemberId == c.Member.MemberId {
  178. c.JsonResult(6007,"不能转让给自己")
  179. }
  180. bookResult,err := c.IsPermission()
  181. if err != nil {
  182. c.JsonResult(6001,err.Error())
  183. }
  184. err = models.NewRelationship().Transfer(bookResult.BookId,c.Member.MemberId,member.MemberId)
  185. if err != nil {
  186. logs.Error("Transfer => ",err)
  187. c.JsonResult(6008,err.Error())
  188. }
  189. c.JsonResult(0,"ok")
  190. }
  191. //上传项目封面.
  192. func (c *BookController) UploadCover() {
  193. bookResult,err := c.IsPermission()
  194. if err != nil {
  195. c.JsonResult(6001,err.Error())
  196. }
  197. book,err := models.NewBook().Find(bookResult.BookId)
  198. if err != nil {
  199. logs.Error("SaveBook => ",err)
  200. c.JsonResult(6002,err.Error())
  201. }
  202. file,moreFile,err := c.GetFile("image-file")
  203. defer file.Close()
  204. if err != nil {
  205. logs.Error("",err.Error())
  206. c.JsonResult(500,"读取文件异常")
  207. }
  208. ext := filepath.Ext(moreFile.Filename)
  209. if !strings.EqualFold(ext,".png") && !strings.EqualFold(ext,".jpg") && !strings.EqualFold(ext,".gif") && !strings.EqualFold(ext,".jpeg") {
  210. c.JsonResult(500,"不支持的图片格式")
  211. }
  212. x1 ,_ := strconv.ParseFloat(c.GetString("x"),10)
  213. y1 ,_ := strconv.ParseFloat(c.GetString("y"),10)
  214. w1 ,_ := strconv.ParseFloat(c.GetString("width"),10)
  215. h1 ,_ := strconv.ParseFloat(c.GetString("height"),10)
  216. x := int(x1)
  217. y := int(y1)
  218. width := int(w1)
  219. height := int(h1)
  220. fileName := "cover_" + strconv.FormatInt(time.Now().UnixNano(), 16)
  221. filePath := "uploads/" + time.Now().Format("200601") + "/" + fileName + ext
  222. path := filepath.Dir(filePath)
  223. os.MkdirAll(path, os.ModePerm)
  224. err = c.SaveToFile("image-file",filePath)
  225. if err != nil {
  226. logs.Error("",err)
  227. c.JsonResult(500,"图片保存失败")
  228. }
  229. //剪切图片
  230. subImg,err := graphics.ImageCopyFromFile(filePath,x,y,width,height)
  231. if err != nil{
  232. logs.Error("graphics.ImageCopyFromFile => ",err)
  233. c.JsonResult(500,"图片剪切")
  234. }
  235. //生成缩略图并保存到磁盘
  236. err = graphics.ImageResizeSaveFile(subImg,175,230,filePath)
  237. if err != nil {
  238. logs.Error("ImageResizeSaveFile => ",err.Error())
  239. c.JsonResult(500,"保存图片失败")
  240. }
  241. url := "/" + filePath
  242. old_cover := book.Cover
  243. book.Cover = url
  244. if err := book.Update() ; err != nil {
  245. c.JsonResult(6001,"保存图片失败")
  246. }
  247. //如果原封面不是默认封面则删除
  248. if old_cover != conf.GetDefaultCover() {
  249. os.Remove("." + old_cover)
  250. }
  251. c.JsonResult(0,"ok",url)
  252. }
  253. // Users 用户列表.
  254. func (c *BookController) Users() {
  255. c.Prepare()
  256. c.TplName = "book/users.tpl"
  257. key := c.Ctx.Input.Param(":key")
  258. pageIndex,_ := c.GetInt("page",1)
  259. if key == ""{
  260. c.Abort("404")
  261. }
  262. book,err := models.NewBookResult().FindByIdentify(key,c.Member.MemberId)
  263. if err != nil {
  264. if err == models.ErrPermissionDenied {
  265. c.Abort("403")
  266. }
  267. c.Abort("500")
  268. }
  269. c.Data["Model"] = *book
  270. members,totalCount,err := models.NewMemberRelationshipResult().FindForUsersByBookId(book.BookId,pageIndex,15)
  271. if totalCount > 0 {
  272. html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, 10, totalCount)
  273. c.Data["PageHtml"] = html
  274. }else{
  275. c.Data["PageHtml"] = ""
  276. }
  277. b,err := json.Marshal(members)
  278. if err != nil {
  279. c.Data["Result"] = template.JS("[]")
  280. }else{
  281. c.Data["Result"] = template.JS(string(b))
  282. }
  283. }
  284. // AddMember 参加参与用户.
  285. func (c *BookController) AddMember() {
  286. identify := c.GetString("identify")
  287. account := c.GetString("account")
  288. role_id,_ := c.GetInt("role_id",3)
  289. if identify == "" || account == ""{
  290. c.JsonResult(6001,"参数错误")
  291. }
  292. book ,err := c.IsPermission()
  293. if err != nil {
  294. c.JsonResult(6001,err.Error())
  295. }
  296. member := models.NewMember()
  297. if _,err := member.FindByAccount(account) ; err != nil {
  298. c.JsonResult(404,"用户不存在")
  299. }
  300. if member.Status == 1 {
  301. c.JsonResult(6003,"用户已被禁用")
  302. }
  303. if _,err := models.NewRelationship().FindForRoleId(book.BookId,member.MemberId);err == nil {
  304. c.JsonResult(6003,"用户已存在该项目中")
  305. }
  306. relationship := models.NewRelationship()
  307. relationship.BookId = book.BookId
  308. relationship.MemberId = member.MemberId
  309. relationship.RoleId = role_id
  310. if err := relationship.Insert(); err == nil {
  311. memberRelationshipResult := models.NewMemberRelationshipResult().FromMember(member)
  312. memberRelationshipResult.RoleId = role_id
  313. memberRelationshipResult.RelationshipId = relationship.RelationshipId
  314. memberRelationshipResult.BookId = book.BookId
  315. memberRelationshipResult.ResolveRoleName()
  316. c.JsonResult(0,"ok",memberRelationshipResult)
  317. }
  318. c.JsonResult(500,err.Error())
  319. }
  320. // 变更指定用户在指定项目中的权限
  321. func (c *BookController) ChangeRole() {
  322. identify := c.GetString("identify")
  323. member_id,_ := c.GetInt("member_id",0)
  324. role,_ := c.GetInt("role_id",0)
  325. if identify == "" || member_id <=0 {
  326. c.JsonResult(6001,"参数错误")
  327. }
  328. if member_id == c.Member.MemberId {
  329. c.JsonResult(6006,"不能变更自己的权限")
  330. }
  331. book ,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  332. if err != nil {
  333. if err == models.ErrPermissionDenied {
  334. c.JsonResult(403,"权限不足")
  335. }
  336. if err == orm.ErrNoRows {
  337. c.JsonResult(404,"项目不存在")
  338. }
  339. c.JsonResult(6002,err.Error())
  340. }
  341. if book.RoleId != 0 && book.RoleId != 1 {
  342. c.JsonResult(403,"权限不足")
  343. }
  344. member := models.NewMember()
  345. if err := member.Find(member_id); err != nil {
  346. c.JsonResult(6003,"用户不存在")
  347. }
  348. if member.Status == 1 {
  349. c.JsonResult(6004,"用户已被禁用")
  350. }
  351. relationship,err := models.NewRelationship().UpdateRoleId(book.BookId,member_id,role);
  352. if err != nil {
  353. logs.Error("变更用户在项目中的权限 => ",err)
  354. c.JsonResult(6005,err.Error())
  355. }
  356. memberRelationshipResult := models.NewMemberRelationshipResult().FromMember(member)
  357. memberRelationshipResult.RoleId = relationship.RoleId
  358. memberRelationshipResult.RelationshipId = relationship.RelationshipId
  359. memberRelationshipResult.BookId = book.BookId
  360. memberRelationshipResult.ResolveRoleName()
  361. c.JsonResult(0,"ok",memberRelationshipResult)
  362. }
  363. // 删除参与者.
  364. func (c *BookController) RemoveMember() {
  365. identify := c.GetString("identify")
  366. member_id,_ := c.GetInt("member_id",0)
  367. if identify == "" || member_id <=0 {
  368. c.JsonResult(6001,"参数错误")
  369. }
  370. if member_id == c.Member.MemberId {
  371. c.JsonResult(6006,"不能删除自己")
  372. }
  373. book ,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  374. if err != nil {
  375. if err == models.ErrPermissionDenied {
  376. c.JsonResult(403,"权限不足")
  377. }
  378. if err == orm.ErrNoRows {
  379. c.JsonResult(404,"项目不存在")
  380. }
  381. c.JsonResult(6002,err.Error())
  382. }
  383. //如果不是创始人也不是管理员则不能操作
  384. if book.RoleId != conf.BookFounder && book.RoleId != conf.BookAdmin {
  385. c.JsonResult(403,"权限不足")
  386. }
  387. err = models.NewRelationship().DeleteByBookIdAndMemberId(book.BookId,member_id)
  388. if err != nil {
  389. c.JsonResult(6007,err.Error())
  390. }
  391. c.JsonResult(0,"ok")
  392. }
  393. // Create 创建项目.
  394. func (c *BookController) Create() {
  395. if c.Ctx.Input.IsPost() {
  396. book_name := strings.TrimSpace(c.GetString("book_name",""))
  397. identify := strings.TrimSpace(c.GetString("identify",""))
  398. description := strings.TrimSpace(c.GetString("description",""))
  399. privately_owned,_ := strconv.Atoi(c.GetString("privately_owned"))
  400. comment_status := c.GetString("comment_status")
  401. if book_name == "" {
  402. c.JsonResult(6001,"项目名称不能为空")
  403. }
  404. if identify == "" {
  405. c.JsonResult(6002,"项目标识不能为空")
  406. }
  407. if ok,err := regexp.MatchString(`^[a-z]+[a-zA-Z0-9_\-]*$`,identify); !ok || err != nil {
  408. c.JsonResult(6003,"文档标识只能包含小写字母、数字,以及“-”和“_”符号,并且只能小写字母开头")
  409. }
  410. if strings.Count(identify,"") > 50 {
  411. c.JsonResult(6004,"文档标识不能超过50字")
  412. }
  413. if strings.Count(description,"") > 500 {
  414. c.JsonResult(6004,"项目描述不能大于500字")
  415. }
  416. if privately_owned !=0 && privately_owned != 1 {
  417. privately_owned = 1
  418. }
  419. if comment_status != "open" && comment_status != "closed" && comment_status != "group_only" && comment_status != "registered_only" {
  420. comment_status = "closed"
  421. }
  422. book := models.NewBook()
  423. if books,_ := book.FindByField("identify",identify); len(books) > 0 {
  424. c.JsonResult(6006,"项目标识已存在")
  425. }
  426. book.BookName = book_name
  427. book.Description = description
  428. book.CommentCount = 0
  429. book.PrivatelyOwned = privately_owned
  430. book.CommentStatus = comment_status
  431. book.Identify = identify
  432. book.DocCount = 0
  433. book.MemberId = c.Member.MemberId
  434. book.CommentCount = 0
  435. book.Version = time.Now().Unix()
  436. book.Cover = beego.AppConfig.String("cover")
  437. err := book.Insert()
  438. if err != nil {
  439. c.JsonResult(6005,err.Error())
  440. }
  441. bookResult := models.NewBookResult()
  442. bookResult.FindByIdentify(book.Identify,c.Member.MemberId)
  443. c.JsonResult(0,"ok",bookResult)
  444. }
  445. c.JsonResult(6001,"error")
  446. }
  447. // Edit 编辑项目.
  448. func (p *BookController) Edit() {
  449. p.TplName = "book/edit.tpl"
  450. }
  451. // CreateToken 创建访问来令牌.
  452. func (c *BookController) CreateToken() {
  453. action := c.GetString("action")
  454. bookResult ,err := c.IsPermission()
  455. if err != nil {
  456. if err == models.ErrPermissionDenied {
  457. c.JsonResult(403,"权限不足")
  458. }
  459. if err == orm.ErrNoRows {
  460. c.JsonResult(404,"项目不存在")
  461. }
  462. logs.Error("生成阅读令牌失败 =>",err)
  463. c.JsonResult(6002,err.Error())
  464. }
  465. book := models.NewBook()
  466. if _,err := book.Find(bookResult.BookId);err != nil {
  467. c.JsonResult(6001,"项目不存在")
  468. }
  469. if action == "create" {
  470. if bookResult.PrivatelyOwned == 0 {
  471. c.JsonResult(6001, "公开项目不能创建阅读令牌")
  472. }
  473. book.PrivateToken = string(utils.Krand(conf.GetTokenSize(), utils.KC_RAND_KIND_ALL))
  474. if err := book.Update(); err != nil {
  475. logs.Error("生成阅读令牌失败 => ", err)
  476. c.JsonResult(6003, "生成阅读令牌失败")
  477. }
  478. c.JsonResult(0, "ok", c.BaseUrl() + beego.URLFor("DocumentController.Index",":key",book.Identify,"token",book.PrivateToken))
  479. }else{
  480. book.PrivateToken = ""
  481. if err := book.Update();err != nil {
  482. logs.Error("CreateToken => ",err)
  483. c.JsonResult(6004,"删除令牌失败")
  484. }
  485. c.JsonResult(0,"ok","")
  486. }
  487. }
  488. // Delete 删除项目.
  489. func (c *BookController) Delete() {
  490. c.Prepare()
  491. bookResult ,err := c.IsPermission()
  492. if err != nil {
  493. c.JsonResult(6001,err.Error())
  494. }
  495. if bookResult.RoleId != conf.BookFounder {
  496. c.JsonResult(6002,"只有创始人才能删除项目")
  497. }
  498. err = models.NewBook().ThoroughDeleteBook(bookResult.BookId)
  499. if err == orm.ErrNoRows {
  500. c.JsonResult(6002,"项目不存在")
  501. }
  502. if err != nil {
  503. logs.Error("删除项目 => ",err)
  504. c.JsonResult(6003,"删除失败")
  505. }
  506. c.JsonResult(0,"ok")
  507. }
  508. func (c *BookController) IsPermission() (*models.BookResult,error) {
  509. identify := c.GetString("identify")
  510. book ,err := models.NewBookResult().FindByIdentify(identify,c.Member.MemberId)
  511. if err != nil {
  512. if err == models.ErrPermissionDenied {
  513. return book,errors.New("权限不足")
  514. }
  515. if err == orm.ErrNoRows {
  516. return book,errors.New("项目不存在")
  517. }
  518. return book,err
  519. }
  520. if book.RoleId != conf.BookAdmin && book.RoleId != conf.BookFounder {
  521. return book,errors.New("权限不足")
  522. }
  523. return book,nil
  524. }