You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

video.go 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package controllers
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "time"
  8. "github.com/casbin/casbase/object"
  9. "github.com/casbin/casbase/util"
  10. "github.com/casbin/casbase/video"
  11. )
  12. func (c *ApiController) GetGlobalVideos() {
  13. c.Data["json"] = object.GetGlobalVideos()
  14. c.ServeJSON()
  15. }
  16. func (c *ApiController) GetVideos() {
  17. owner := c.Input().Get("owner")
  18. c.Data["json"] = object.GetVideos(owner)
  19. c.ServeJSON()
  20. }
  21. func (c *ApiController) GetVideo() {
  22. id := c.Input().Get("id")
  23. c.Data["json"] = object.GetVideo(id)
  24. c.ServeJSON()
  25. }
  26. func (c *ApiController) UpdateVideo() {
  27. id := c.Input().Get("id")
  28. var video object.Video
  29. err := json.Unmarshal(c.Ctx.Input.RequestBody, &video)
  30. if err != nil {
  31. panic(err)
  32. }
  33. c.Data["json"] = object.UpdateVideo(id, &video)
  34. c.ServeJSON()
  35. }
  36. func (c *ApiController) AddVideo() {
  37. var video object.Video
  38. err := json.Unmarshal(c.Ctx.Input.RequestBody, &video)
  39. if err != nil {
  40. panic(err)
  41. }
  42. c.Data["json"] = object.AddVideo(&video)
  43. c.ServeJSON()
  44. }
  45. func (c *ApiController) DeleteVideo() {
  46. var video object.Video
  47. err := json.Unmarshal(c.Ctx.Input.RequestBody, &video)
  48. if err != nil {
  49. panic(err)
  50. }
  51. c.Data["json"] = object.DeleteVideo(&video)
  52. c.ServeJSON()
  53. }
  54. func startCoverUrlJob(owner string, name string, videoId string) {
  55. go func(owner string, name string, videoId string) {
  56. for i := 0; i < 20; i++ {
  57. coverUrl := video.GetVideoCoverUrl(videoId)
  58. if coverUrl != "" {
  59. video := object.GetVideo(util.GetIdFromOwnerAndName(owner, name))
  60. if video.CoverUrl != "" {
  61. break
  62. }
  63. video.CoverUrl = coverUrl
  64. object.UpdateVideo(util.GetIdFromOwnerAndName(owner, name), video)
  65. break
  66. }
  67. time.Sleep(time.Second * 5)
  68. }
  69. }(owner, name, videoId)
  70. }
  71. func (c *ApiController) UploadVideo() {
  72. owner := c.GetSessionUsername()
  73. file, header, err := c.GetFile("file")
  74. if err != nil {
  75. panic(err)
  76. }
  77. defer file.Close()
  78. filename := header.Filename
  79. fileId := util.RemoveExt(filename)
  80. fileBuffer := bytes.NewBuffer(nil)
  81. if _, err = io.Copy(fileBuffer, file); err != nil {
  82. c.ResponseError(err.Error())
  83. return
  84. }
  85. fileType := "unknown"
  86. contentType := header.Header.Get("Content-Type")
  87. fileType, _ = util.GetOwnerAndNameFromId(contentType)
  88. if fileType != "video" {
  89. c.ResponseError(fmt.Sprintf("contentType: %s is not video", contentType))
  90. return
  91. }
  92. videoId := video.UploadVideo(fileId, filename, fileBuffer)
  93. if videoId != "" {
  94. startCoverUrlJob(owner, fileId, videoId)
  95. video := &object.Video{
  96. Owner: owner,
  97. Name: fileId,
  98. CreatedTime: util.GetCurrentTime(),
  99. DisplayName: fileId,
  100. VideoId: videoId,
  101. Labels: []*object.Label{},
  102. }
  103. object.AddVideo(video)
  104. c.ResponseOk(fileId)
  105. } else {
  106. c.ResponseError("videoId is empty")
  107. }
  108. }

基于Casbin的开源AI领域知识库平台

Contributors (1)